-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySet.java
More file actions
125 lines (105 loc) · 3.54 KB
/
ArraySet.java
File metadata and controls
125 lines (105 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
public class ArraySet<T> extends AbstractSet<T> implements SortedSet<T> {
private final List<T> sortedData;
private final Comparator<? super T> comparator;
private ArraySet(List<T> list, Comparator<? super T> comp) {
sortedData = list;
comparator = comp;
}
public ArraySet() {
this(Collections.emptyList(), null);
}
public ArraySet(Collection<? extends T> collection, Comparator<? super T> comp) {
comparator = comp;
TreeSet<T> set = new TreeSet<>(comp);
set.addAll(Objects.requireNonNull(collection));
sortedData = new ArrayList<>(set);
}
public ArraySet(Collection<? extends T> collection) {
this(collection, null);
}
public ArraySet(ArraySet<T> other) {
sortedData = other.sortedData;
comparator = other.comparator;
}
private static final String setIsEmptyMessage = "ArraySet is empty, cannot get ";
public T first() {
if (!sortedData.isEmpty()) {
return sortedData.get(0);
}
throw new NoSuchElementException(setIsEmptyMessage + "first element");
}
public T last() {
if (!sortedData.isEmpty()) {
return sortedData.get(sortedData.size() - 1);
}
throw new NoSuchElementException(setIsEmptyMessage + "last element");
}
public int size() {
return sortedData.size();
}
@Override
public boolean contains(Object o) {
return Collections.binarySearch(sortedData, (T) o, comparator) >= 0;
}
@Override
public Iterator<T> iterator() {
return Collections.unmodifiableList(sortedData).iterator();
}
private int searchForPosition(T element, int addIfFound, int addIfNotFound) {
int index = Collections.binarySearch(sortedData, element, comparator);
if (index >= 0) {
return index + addIfFound;
}
index = -(index + 1);
return index + addIfNotFound;
}
private SortedSet<T> subSet(T fromElement, T toElement, boolean toInclusive) {
int addIfFound = 0;
int addIfNotFound = 0;
int leftBorder = searchForPosition(fromElement, addIfFound, addIfNotFound);
if(toInclusive){
addIfFound = 0;
} else {
addIfFound = -1;
}
addIfNotFound = -1;
int rightBorder = searchForPosition(toElement, addIfFound, addIfNotFound) + 1;
if (leftBorder - rightBorder >= 0) {
return Collections.emptySortedSet();
}
return new ArraySet<T>(sortedData.subList(leftBorder, rightBorder), comparator);
}
@Override
public SortedSet<T> subSet(T fromElement, T toElement) {
return subSet(fromElement, toElement, false);
}
@Override
public SortedSet<T> tailSet(T fromElement) {
if (sortedData.isEmpty()) {
return Collections.emptyNavigableSet();
}
return subSet(fromElement, last(), true);
}
@Override
public SortedSet<T> headSet(T toElement) {
if (sortedData.isEmpty()) {
return Collections.emptyNavigableSet();
}
return subSet(first(), toElement, false);
}
@Override
public Comparator<? super T> comparator() {
return comparator;
}
}