MATSIM
IdSet.java
Go to the documentation of this file.
1 package org.matsim.api.core.v01;
2 
3 import java.util.*;
4 
8 public class IdSet<T> implements Set<Id<T>> {
9 
10  private final Class<T> idClass;
11  private int size = 0;
12  private final BitSet data;
13 
14  public IdSet(Class<T> idClass) {
15  this(idClass, Math.max(Id.getNumberOfIds(idClass), 100));
16  }
17 
18  public IdSet(Class<T> idClass, int size) {
19  this.idClass = idClass;
20  this.data = new BitSet(size);
21  }
22 
23  @Override
24  public int size() {
25  return this.size;
26  }
27 
28  @Override
29  public boolean isEmpty() {
30  return this.size == 0;
31  }
32 
33  @Override
34  public boolean contains(Object key) {
35  if (key instanceof Id) {
36  return contains((Id<T>) key);
37  }
38  return false;
39  }
40 
41  public boolean contains(Id<T> id) {
42  return this.data.get(id.index());
43  }
44 
45  public boolean contains(int index) {
46  return this.data.get(index);
47  }
48 
49  @Override
50  public Iterator<Id<T>> iterator() {
51  return new IdSetIterator<>(this);
52  }
53 
54  @Override
55  public Id<T>[] toArray() {
56  int index = 0;
57  int count = 0;
58  Id[] array = new Id[this.size];
59  while (true) {
60  index = this.data.nextSetBit(index);
61  if (index < 0) {
62  break;
63  }
64  array[count] = Id.get(index, this.idClass);
65  count++;
66  index++;
67  }
68  return (Id<T>[]) array;
69  }
70 
71  @Override
72  public <T> T[] toArray(T[] a) {
73  Object[] array = a;
74  if (array == null) {
75  array = new Id[this.size];
76  } else if (array.length < this.size) {
77  array = Arrays.copyOf(a, this.size);
78  } else if (array.length > this.size) {
79  Arrays.fill(a, this.size, a.length, null);
80  }
81 
82  int index = 0;
83  int count = 0;
84  while (true) {
85  index = this.data.nextSetBit(index);
86  if (index < 0) {
87  break;
88  }
89  array[count] = Id.get(index, this.idClass);
90  count++;
91  index++;
92  }
93  return (T[]) array;
94  }
95 
96  @Override
97  public boolean remove(Object key) {
98  if (key instanceof Id) {
99  return remove((Id<T>) key);
100  }
101  return false;
102  }
103 
104  public boolean remove(Id<T> key) {
105  return this.remove(key.index());
106  }
107 
108  private boolean remove(int idx) {
109  if (this.data.get(idx)) {
110  this.data.clear(idx);
111  this.size--;
112  return true;
113  }
114  return false;
115  }
116 
117  @Override
118  public boolean containsAll(Collection<?> c) {
119  for (Object o : c) {
120  if (o instanceof Id) {
121  if (!this.data.get(((Id) o).index())) {
122  return false;
123  }
124  } else {
125  return false;
126  }
127  }
128  return true;
129  }
130 
131  @Override
132  public boolean addAll(Collection<? extends Id<T>> c) {
133  boolean changed = false;
134  for (Id<T> k : c) {
135  int index = k.index();
136  if (!this.data.get(index)) {
137  this.data.set(index);
138  this.size++;
139  changed = true;
140  }
141  }
142  return changed;
143  }
144 
145  public boolean addAll(IdSet<T> m) {
146  boolean changed = false;
147  int index = 0;
148  while (true) {
149  index = m.data.nextSetBit(index);
150  if (index >= 0) {
151  if (!this.data.get(index)) {
152  this.data.set(index);
153  this.size++;
154  changed = true;
155  }
156  index++;
157  } else {
158  break;
159  }
160  }
161  return changed;
162  }
163 
164  @Override
165  public boolean retainAll(Collection<?> c) {
166  boolean changed = false;
167  int index = 0;
168  while (true) {
169  index = this.data.nextSetBit(index);
170  if (index >= 0) {
171  Id<T> id = Id.get(index, this.idClass);
172  if (!c.contains(id)) {
173  this.data.clear(index);
174  this.size--;
175  changed = true;
176  }
177  index++;
178  } else {
179  break;
180  }
181  }
182  return changed;
183  }
184 
185  @Override
186  public boolean removeAll(Collection<?> c) {
187  boolean changed = false;
188  for (Object o : c) {
189  if (o instanceof Id) {
190  int index = ((Id) o).index();
191  if (this.data.get(index)) {
192  this.data.clear(index);
193  this.size--;
194  changed = true;
195  }
196  }
197  }
198  return changed;
199  }
200 
201  @Override
202  public boolean add(Id<T> value) {
203  return this.add(value.index());
204  }
205 
206  private boolean add(int index) {
207  boolean hadValue = this.data.get(index);
208  this.data.set(index);
209  if (!hadValue) {
210  this.size++;
211  }
212  return !hadValue;
213  }
214 
215  @Override
216  public void clear() {
217  this.size = 0;
218  this.data.clear();
219  }
220 
221  @Override
222  public boolean equals(Object o) {
223  if (o == this)
224  return true;
225  if (!(o instanceof Set))
226  return false;
227  if (o instanceof IdSet) {
228  IdSet<?> m = (IdSet<?>) o;
229  return this.idClass.equals(m.idClass) && this.size == m.size && this.data.equals(m.data);
230  } else {
231  Collection<?> c = (Collection<?>) o;
232  if (c.size() != size())
233  return false;
234  try {
235  return containsAll(c);
236  } catch (ClassCastException | NullPointerException unused) {
237  return false;
238  }
239  }
240  }
241 
242  @Override
243  public int hashCode() {
244  if (isEmpty())
245  return -1;
246  int h = 0;
247  for (int i = 0; i < this.data.length(); i++) {
248  h += this.data.get(i) ? i : 0;
249  }
250  return h;
251  }
252 
253  private static class IdSetIterator<T> implements Iterator<Id<T>> {
254 
255  private final IdSet<T> set;
256  private int currentIndex = -1;
257 
258  IdSetIterator(IdSet<T> set) {
259  this.set = set;
260  }
261 
262  @Override
263  public boolean hasNext() {
264  return this.set.data.nextSetBit(this.currentIndex + 1) >= 0;
265  }
266 
267  @Override
268  public Id<T> next() {
269  int index = this.set.data.nextSetBit(this.currentIndex + 1);
270  if (index >= 0) {
271  this.currentIndex = index;
272  return Id.get(index, this.set.idClass);
273  }
274  throw new NoSuchElementException();
275  }
276 
277  @Override
278  public void remove() {
279  this.set.data.clear(this.currentIndex);
280  }
281  }
282 
283 }
static< T > Id< T > get(int index, final Class< T > type)
Definition: Id.java:112
static< T > int getNumberOfIds(final Class< T > type)
Definition: Id.java:122
Iterator< Id< T > > iterator()
Definition: IdSet.java:50
boolean contains(Object key)
Definition: IdSet.java:34
boolean addAll(IdSet< T > m)
Definition: IdSet.java:145
boolean contains(Id< T > id)
Definition: IdSet.java:41
boolean add(int index)
Definition: IdSet.java:206
boolean containsAll(Collection<?> c)
Definition: IdSet.java:118
boolean removeAll(Collection<?> c)
Definition: IdSet.java:186
IdSet(Class< T > idClass)
Definition: IdSet.java:14
IdSet(Class< T > idClass, int size)
Definition: IdSet.java:18
boolean addAll(Collection<? extends Id< T >> c)
Definition: IdSet.java:132
final Class< T > idClass
Definition: IdSet.java:10
boolean add(Id< T > value)
Definition: IdSet.java:202
boolean equals(Object o)
Definition: IdSet.java:222
boolean retainAll(Collection<?> c)
Definition: IdSet.java:165
boolean contains(int index)
Definition: IdSet.java:45