20 package org.matsim.core.utils.collections;
22 import java.util.Arrays;
23 import java.util.Collection;
24 import java.util.Iterator;
26 import java.util.NoSuchElementException;
27 import java.util.Objects;
49 this.keys =
new int[capacity];
50 this.values =
new Object[capacity];
60 return this.length == 0;
69 if (key instanceof Integer) {
76 for (
int i = 0, n = this.length; i < n; i++) {
77 if (this.keys[i] == key) {
86 for (
int i = 0, n = this.length; i < n; i++) {
87 Object v = this.values[i];
88 if (Objects.equals(v, value)) {
100 public V
get(Object key) {
101 if (key instanceof Integer) {
102 return this.
get(((Integer) key).intValue());
107 public V
get(
int key) {
108 for (
int i = 0, n = this.length; i < n; i++) {
109 if (this.keys[i] == key) {
110 return (V) this.values[i];
121 public V
put(Integer key, V value) {
122 return this.
put(key.intValue(), value);
125 public V
put(
int key, V value) {
126 for (
int i = 0, n = this.length; i < n; i++) {
127 if (this.keys[i] == key) {
128 V oldValue = (V) this.values[i];
129 this.values[i] = value;
135 this.keys[this.
length] = key;
136 this.values[this.
length] = value;
142 if (this.keys.length < capacity) {
143 int newLength = (this.keys.length + 1) * 2;
144 while (newLength < capacity) {
145 newLength = (newLength + 1) * 2;
147 this.keys = Arrays.copyOf(this.keys, newLength);
148 this.values = Arrays.copyOf(this.values, newLength);
158 return replace(key.intValue(), value);
162 for (
int i = 0, n = this.length; i < n; i++) {
163 if (this.keys[i] == key) {
164 V oldValue = (V) this.values[i];
165 this.values[i] = value;
177 public V
remove(
final Object key) {
178 if (key instanceof Integer) {
179 return remove(((Integer) key).intValue());
184 public V
remove(
int key) {
185 for (
int i = 0, n = this.length; i < n; i++) {
186 if (this.keys[i] == key) {
187 V oldValue = (V) this.values[i];
200 public boolean remove(Object key, Object value) {
201 if (key instanceof Integer) {
202 return remove(((Integer) key).intValue(), value);
207 public boolean remove(
int key, Object value) {
208 for (
int i = 0, n = this.length; i < n; i++) {
209 if (this.keys[i] == key) {
210 V v = (V) this.values[i];
211 if (Objects.equals(v, value)) {
221 for (
int i = 0, n = this.length; i < n; i++) {
222 if (this.keys[i] == key) {
231 for (
int i = 0, n = this.length; i < n; i++) {
232 Object v = this.values[i];
233 if (Objects.equals(v, value)) {
242 int lastIndex = this.length - 1;
243 this.keys[i] = this.keys[lastIndex];
244 this.values[i] = this.values[lastIndex];
245 this.keys[lastIndex] = 0;
246 this.values[lastIndex] = null;
251 public void putAll(
final Map<? extends Integer, ? extends V> m) {
252 m.forEach(this::put);
257 Arrays.fill(this.keys, 0);
258 Arrays.fill(this.values, null);
277 private static class Entry<V>
implements Map.
Entry<Integer, V> {
299 throw new UnsupportedOperationException();
304 if (
this == o)
return true;
305 if (o == null || getClass() != o.getClass())
return false;
307 return Objects.
equals(this.k, entry.
k) &&
308 Objects.equals(this.v, entry.
v);
313 return Objects.hash(this.k, this.v);
327 return this.map.
size();
337 if (o instanceof Integer) {
338 return this.map.
containsKey(((Integer) o).intValue());
350 Integer[] array =
new Integer[this.map.
length];
351 for (
int i = 0; i < this.map.
length; i++) {
352 array[i] = this.map.
keys[i];
358 public <T> T[] toArray(T[] a) {
359 int resultLength = this.map.
length;
361 if (result == null) {
362 result =
new Object[resultLength];
363 }
else if (result.length != resultLength) {
364 result = Arrays.copyOf(a, resultLength);
366 System.arraycopy(this.map.
values, 0, result, 0, result.length);
371 public boolean add(Integer
k) {
372 throw new UnsupportedOperationException();
376 public boolean remove(Object o) {
377 if (o instanceof Integer) {
378 return this.map.
removeKey(((Integer)o).intValue());
386 if (o instanceof Integer) {
387 if (!this.map.
containsKey(((Integer) o).intValue())) {
398 public boolean addAll(Collection<? extends Integer> c) {
399 throw new UnsupportedOperationException();
404 boolean modified =
false;
405 for (
int i = 0, n = this.map.
length; i < n; i++) {
406 int key = this.map.
keys[i];
407 if (!c.contains(key)) {
417 boolean modified =
false;
419 if (o instanceof Integer) {
420 if (this.map.
removeKey(((Integer) o).intValue())) {
434 private static class KeyIterator<V>
implements Iterator<Integer> {
444 return this.map.
length > this.nextIndex;
450 int key = this.map.
keys[this.nextIndex];
454 throw new NoSuchElementException();
458 public void remove() {
464 private static class ValuesView<V>
implements Collection<V> {
474 return this.map.
size();
494 Object[] data = this.map.
values;
495 Object[] result =
new Object[this.map.
length];
496 if (result.length >= 0) {
497 System.arraycopy(data, 0, result, 0, result.length);
503 public <T> T[] toArray(T[] a) {
504 Object[] data = this.map.
values;
505 int resultLength = this.map.
length;
507 if (result == null) {
508 result =
new Object[resultLength];
509 }
else if (result.length != resultLength) {
510 result = Arrays.copyOf(a, resultLength);
512 System.arraycopy(data, 0, result, 0, result.length);
518 throw new UnsupportedOperationException();
522 public boolean remove(Object o) {
537 public boolean addAll(Collection<? extends V> c) {
538 throw new UnsupportedOperationException();
543 boolean modified =
false;
554 boolean modified =
false;
555 Object[] data = this.map.
values;
556 for (
int i = 0, n = this.map.
length; i < n; i++) {
557 Object value = data[i + 1];
558 if (!c.contains(value)) {
583 return this.map.
length > this.nextIndex;
589 V value = (V) this.map.
values[
this.nextIndex];
593 throw new NoSuchElementException();
597 public void remove() {
603 private static class EntrySetView<V>
implements Set<Map.Entry<Integer, V>> {
613 return this.map.
size();
623 if (o instanceof
Entry) {
625 return Objects.
equals(e.
v,
this.map.get(e.
k));
631 public Iterator<Map.Entry<Integer, V>>
iterator() {
637 Object[] result =
new Object[this.map.
length];
638 for (
int i = 0; i < result.length; i++) {
645 public <T> T[] toArray(T[] a) {
646 int resultLength = this.map.
length;
648 if (result == null) {
649 result =
new Object[resultLength];
650 }
else if (result.length != resultLength) {
651 result = Arrays.copyOf(a, resultLength);
653 for (
int i = 0; i < result.length; i++) {
660 public boolean add(Map.Entry<Integer, V> kvEntry) {
661 throw new UnsupportedOperationException();
665 public boolean remove(Object o) {
666 if (o instanceof
Entry) {
676 if (!this.contains(o)) {
684 public boolean addAll(Collection<? extends Map.Entry<Integer, V>> c) {
685 throw new UnsupportedOperationException();
690 boolean modified =
false;
691 for (
int i = 0, n = this.map.
length; i < n; i++) {
692 int key = this.map.
keys[i];
693 Object value = this.map.
values[i];
694 if (!c.contains(
new Entry<>(key, value))) {
695 this.map.
remove(key, value);
704 boolean modified =
false;
706 if (o instanceof
Entry) {
722 private static class EntryIterator<V>
implements Iterator<Map.Entry<Integer, V>> {
732 return this.map.
length > this.nextIndex;
736 public Map.Entry<Integer, V>
next() {
737 int key = this.map.
keys[this.nextIndex];
738 V value = (V) this.map.
values[
this.nextIndex];
740 return new Entry<>(key, value);
boolean removeValue(final Object value)
final IntArrayMap< V > map
V setValue(final V value)
boolean containsValue(Object value)
final IntArrayMap< V > map
boolean removeAll(Collection<?> c)
final IntArrayMap< V > map
boolean addAll(Collection<? extends Integer > c)
Map.Entry< Integer, V > next()
boolean addAll(Collection<? extends V > c)
V replace(Integer key, V value)
boolean contains(Object o)
boolean contains(Object o)
void putAll(final Map<? extends Integer, ? extends V > m)
boolean containsKey(int key)
boolean contains(Object o)
V remove(final Object key)
ValuesView(IntArrayMap< V > map)
boolean retainAll(Collection<?> c)
final IntArrayMap< V > map
boolean containsAll(Collection<?> c)
Set< Map.Entry< Integer, V > > entrySet()
IntArrayMap(int capacity)
final IntArrayMap< V > map
boolean removeAll(Collection<?> c)
boolean containsKey(Object key)
Iterator< Map.Entry< Integer, V > > iterator()
V put(Integer key, V value)
boolean containsAll(Collection<?> c)
boolean removeAll(Collection<?> c)
boolean removeKey(int key)
boolean retainAll(Collection<?> c)
final IntArrayMap< V > map
boolean retainAll(Collection<?> c)
boolean add(Map.Entry< Integer, V > kvEntry)
Iterator< Integer > iterator()
boolean containsAll(Collection<?> c)
V replace(int key, V value)
boolean addAll(Collection<? extends Map.Entry< Integer, V >> c)
void ensureCapacity(int capacity)