1 package org.matsim.api.core.v01;
3 import java.util.Arrays;
4 import java.util.Collection;
5 import java.util.HashSet;
6 import java.util.Iterator;
8 import java.util.NoSuchElementException;
9 import java.util.Objects;
11 import java.util.function.BiConsumer;
24 public IdMap(Class<T> idClass) {
28 public IdMap(Class<T> idClass,
int size) {
30 this.data =
new Object[
size];
40 return this.size == 0;
45 if (key instanceof
Id) {
53 Objects.requireNonNull(value);
54 for (Object v : this.data) {
55 if (v != null && value.equals(v)) {
63 public V
get(Object key) {
64 if (key instanceof
Id) {
65 return get((
Id<T>) key);
71 public V
remove(Object key) {
72 if (key instanceof
Id) {
73 return remove((
Id<T>) key);
81 for (Id<T> k : m.keySet()) {
82 int index = k.index();
83 if (index > maxIndex) {
90 int index = k.index();
91 Object oldValue = this.data[index];
92 this.data[k.index()] = v;
93 if (v != null && oldValue == null) {
96 if (v == null && oldValue != null) {
104 for (
int i = 0; i < m.
data.length; i++) {
105 Object value = m.
data[i];
107 Object oldValue = this.data[i];
108 this.data[i] = value;
109 if (oldValue == null) {
117 int idx = key.
index();
118 return idx < this.data.length && this.data[idx] != null;
122 return index < this.data.length && this.data[index] != null;
126 int idx = key.index();
127 if (idx < this.data.length) {
128 return (V) this.data[idx];
133 public V
get(
int index) {
134 if (index < this.data.length) {
135 return (V) this.data[index];
142 return this.
put(key.
index(), value);
145 V
put(
int index, V value) {
147 Object oldValue = this.data[index];
148 this.data[index] = value;
149 if (value != null && oldValue == null) {
152 if (value == null && oldValue != null) {
159 if (index >= this.data.length) {
160 int newSize = Math.max(index + INCREMENT, (
int)(data.length * INCREMENT_FACTOR));
161 Object[] tmp =
new Object[newSize];
162 System.arraycopy(this.data, 0, tmp, 0, this.data.length);
167 return this.
remove(key.index());
171 if (idx < this.data.length) {
172 Object oldValue = this.data[idx];
173 this.data[idx] = null;
174 if (oldValue != null) {
185 Arrays.fill(this.data, null);
190 return new KeySet<>(
this);
195 for (
int i = 0; i < this.data.length; i++) {
196 Object o = this.data[i];
198 action.accept(
Id.
get(i,
this.idClass), (V) o);
205 return new DataCollection<T, V>(
this);
210 return new EntrySet<T, V>(
this);
215 return new DataIterator<>(
this);
222 if (!(o instanceof Map))
224 if (o instanceof
IdMap) {
226 if (this.size != m.
size)
228 for (
int i = 0; i < this.data.length && i < m.
data.length; i++) {
229 if (this.data[i] != m.
data[i])
234 Map<Id<?>, ?> m = (Map<Id<?>, ?>) o;
237 while (iter.hasNext()) {
238 java.util.Map.Entry<
Id<T>, V> e = iter.next();
239 Id<T> key = e.getKey();
240 V value = e.getValue();
242 if (!(m.get(key) == null && m.containsKey(key)))
245 if (!value.equals(m.get(key)))
249 }
catch (ClassCastException | NullPointerException noIdAsKey) {
259 for (
int i = 0; i < data.length; i++) {
260 h += data[i] == null ? 0 : i ^ data[i].hashCode();
275 return this.map.
size();
295 Object[] array =
new Object[this.map.
size];
297 for (Object v : this.map.
data) {
307 public <T> T[]
toArray(T[] a) {
309 if (values == null) {
310 values = (T[])
new Object[this.map.
size];
311 }
else if (values.length <
this.map.size) {
312 values = Arrays.copyOf(values, this.map.
size);
313 }
else if (values.length >
this.map.size) {
314 Arrays.fill(values, this.map.
size, values.length, null);
318 for (Object v : this.map.
data) {
320 values[index] = (T) v;
330 throw new UnsupportedOperationException();
334 public boolean remove(Object o) {
338 Object[] data = this.map.
data;
339 for (
int i = 0; i < data.length; i++) {
340 if (o.equals(data[i])) {
359 public boolean addAll(Collection<? extends V> c) {
360 throw new UnsupportedOperationException();
365 boolean changed =
false;
367 changed = this.
remove(o) | changed;
374 boolean changed =
false;
375 Set<?>
set =
new HashSet<>(c);
376 Object[] data = this.map.
data;
377 for (
int i = 0; i < data.length; i++) {
380 this.map.
data[i] = null;
398 private int index = 0;
399 private int currentIndex = -1;
400 private int nextIndex = -1;
405 this.data = map.
data;
411 while (this.next == null && this.index < this.data.length) {
412 this.nextIndex = this.index;
413 this.next = this.data[this.index];
420 return this.next != null ;
425 if (this.next == null) {
426 throw new NoSuchElementException();
428 Object tmp = this.next;
429 this.currentIndex = this.nextIndex;
435 public void remove() {
436 this.map.
remove(this.currentIndex);
440 private static class IdIterator<T, D>
implements Iterator<Id<T>> {
444 private int index = 0;
455 while (this.next == null && this.index < this.data.length) {
456 if (this.data[this.index] != null) {
457 this.next =
Id.
get(this.index, this.idClass);
465 return this.next != null ;
470 Id<T> tmp = this.next;
476 private static class KeySet<T, V>
implements Set<Id<T>> {
486 return this.map.
size;
510 public <K> K[]
toArray(K[] a) {
511 Id[] keys = (
Id[]) a;
513 keys =
new Id[this.map.
size];
514 }
else if (keys.length <
this.map.size) {
515 keys = Arrays.copyOf(keys, this.map.
size);
516 }
else if (keys.length >
this.map.size) {
517 Arrays.fill(keys, this.map.
size, keys.length, null);
522 for (
int i = 0; i < values.length; i++) {
523 if (values[i] != null) {
533 throw new UnsupportedOperationException();
537 public boolean remove(Object o) {
538 return this.map.
remove(o) != null;
551 throw new UnsupportedOperationException();
556 Set<Id<?>> ids =
new HashSet<>();
558 if (o instanceof
Id) {
562 boolean changed =
false;
563 for (
int i = 0; i < this.map.
data.length; i++) {
564 if (this.map.
data[i] != null) {
566 if (!ids.contains(t)) {
567 this.map.
data[i] = null;
578 Set<Id<?>> ids =
new HashSet<>();
580 if (o instanceof
Id) {
584 boolean changed =
false;
585 for (
int i = 0; i < this.map.
data.length; i++) {
586 if (this.map.
data[i] != null) {
588 if (ids.contains(t)) {
589 this.map.
data[i] = null;
607 if (!(o instanceof Set))
609 if (o instanceof
KeySet) {
613 for (
int i = 0; i < this.map.
data.length && i < k.
map.data.length; i++) {
614 if ((this.map.
data[i] == null && k.
map.data[i] != null) || (this.map.
data[i] != null && k.
map.data[i] == null))
619 Collection<?> c = (Collection<?>) o;
620 if (c.size() !=
size())
624 }
catch (ClassCastException | NullPointerException unused) {
633 for (
int i = 0; i < this.map.
data.length; i++) {
634 h += this.map.
data[i] == null ? 0 : i;
640 private static class EntrySet<T, V>
implements Set<Map.Entry<Id<T>, V>> {
650 return this.map.
size();
660 if (o instanceof Map.Entry) {
661 Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
662 return e.getValue().equals(this.map.
get(e.getKey()));
678 public <T> T[]
toArray(T[] a) {
680 if (entries == null) {
682 }
else if (entries.length <
this.
map.size) {
683 entries = Arrays.copyOf(entries, this.map.
size);
684 }
else if (entries.length >
this.
map.size) {
685 Arrays.fill(entries, this.map.
size, entries.length, null);
690 for (
int i = 0; i < values.length; i++) {
691 if (values[i] != null) {
696 return (T[]) entries;
701 throw new UnsupportedOperationException();
705 public boolean remove(Object o) {
706 if (o instanceof Map.Entry) {
707 Map.Entry e = (
Entry) o;
708 Object k = e.getKey();
709 if (k instanceof
Id) {
711 int index =
id.index();
712 V value = this.map.
get(index);
713 if (value != null && value.equals(e.getValue())) {
724 throw new UnsupportedOperationException();
728 public boolean addAll(Collection<? extends Map.Entry<
Id<T>, V>> c) {
729 throw new UnsupportedOperationException();
734 throw new UnsupportedOperationException();
739 throw new UnsupportedOperationException();
751 if (!(o instanceof Set))
757 Collection<?> c = (Collection<?>) o;
758 if (c.size() !=
size())
762 }
catch (ClassCastException | NullPointerException unused) {
774 public static class Entry<T, V>
implements Map.
Entry<Id<T>, V> {
783 this.value = this.map.
get(index);
798 return this.map.
put(this.index, value);
803 if (
this == o)
return true;
804 if (!(o instanceof Map.Entry))
806 if (o instanceof
Entry) {
808 return this.index == e.
index && this.value.equals(e.
value);
810 Map.Entry<?, ?> e = (Map.Entry<?, ?>) o;
811 if (e.getKey() == null || e.getValue() == null)
813 return this.getKey().equals(e.getKey()) && this.value.equals(e.getValue());
819 return index ^ (value == null ? 0 : value.hashCode());
823 private static class EntryIterator<T, V>
implements Iterator<Map.Entry<Id<T>, V>> {
827 private int index = 0;
833 this.data = map.
data;
839 while (this.next == null && this.index < this.data.length) {
840 if (this.map.
data[
this.index] != null) {
841 this.next =
new Entry<>(this.
map, this.index);
849 return this.next != null ;
854 this.current = this.next;
860 public void remove() {
boolean containsAll(Collection<?> c)
static< T > Id< T > get(int index, final Class< T > type)
static< T > int getNumberOfIds(final Class< T > type)
void ensureCapacity(int index)
boolean add(Map.Entry< Id< T >, V > entry)
Set< Map.Entry< Id< T >, V > > entrySet()
boolean containsKey(int index)
void forEach(BiConsumer<? super Id< T >, ? super V > action)
boolean contains(Object o)
boolean containsKey(Object key)
boolean containsKey(Id< T > key)
boolean contains(Object o)
boolean removeAll(Collection<?> c)
IdMap(Class< T > idClass)
DataCollection(IdMap map)
boolean containsAll(Collection<?> c)
IdMap(Class< T > idClass, int size)
Entry(IdMap< T, V > map, int index)
void putAll(Map<? extends Id< T >, ? extends V > m)
static final float INCREMENT_FACTOR
boolean contains(Object o)
boolean addAll(Collection<? extends Id< T >> c)
boolean retainAll(Collection<?> c)
static final int INCREMENT
Iterator< Map.Entry< Id< T >, V > > iterator()
void putAll(IdMap< T, ? extends V > m)
boolean containsAll(Collection<?> c)
Iterator< Id< T > > iterator()
V put(Id< T > key, V value)
Map.Entry< Id< T >, V > next()
boolean retainAll(Collection<?> c)
boolean containsValue(Object value)
boolean removeAll(Collection<?> c)
boolean addAll(Collection<? extends V > c)
boolean addAll(Collection<? extends Map.Entry< Id< T >, V >> c)
boolean retainAll(Collection<?> c)
boolean removeAll(Collection<?> c)