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;
41 public class ArrayMap<K, V>
implements Map<K, V> {
43 private final static Object[]
EMPTY =
new Object[0];
51 this.data =
new Object[map.size() * 2];
53 for (Map.Entry<K, V> e : map.entrySet()) {
54 this.data[i] = e.getKey();
55 this.data[i + 1] = e.getValue();
62 return this.data.length / 2;
67 return this.data.length == 0;
72 for (
int i = 0, n = this.data.length; i < n; i += 2) {
73 Object k = this.data[i];
74 if (Objects.equals(k, key)) {
83 for (
int i = 1, n = this.data.length; i < n; i += 2) {
84 Object v = this.data[i];
85 if (Objects.equals(v, value)) {
93 public V
get(Object key) {
94 for (
int i = 0, n = this.data.length; i < n; i += 2) {
95 Object k = this.data[i];
96 if (Objects.equals(k, key)) {
97 return (V) this.data[i + 1];
103 public V
put(K key, V value) {
104 for (
int i = 0, n = this.data.length; i < n; i += 2) {
105 Object k = this.data[i];
106 if (Objects.equals(k, key)) {
107 V oldValue = (V) this.data[i + 1];
108 this.data[i + 1] = value;
112 int oldLength = this.data.length;
113 this.data = Arrays.copyOf(this.data, oldLength + 2);
114 this.data[oldLength] = key;
115 this.data[oldLength + 1] = value;
121 for (
int i = 0, n = this.data.length; i < n; i += 2) {
122 Object k = this.data[i];
123 if (Objects.equals(k, key)) {
124 V oldValue = (V) this.data[i + 1];
125 this.data[i + 1] = value;
133 public V
remove(
final Object key) {
134 for (
int i = 0, n = this.data.length; i < n; i += 2) {
135 Object k = this.data[i];
136 if (Objects.equals(k, key)) {
137 V oldValue = (V) this.data[i + 1];
146 public boolean remove(Object key, Object value) {
147 for (
int i = 0, n = this.data.length; i < n; i += 2) {
148 Object k = this.data[i];
149 if (Objects.equals(k, key)) {
150 V v = (V) this.data[i + 1];
151 if (Objects.equals(v, value)) {
161 for (
int i = 0, n = this.data.length; i < n; i += 2) {
162 Object k = this.data[i];
163 if (Objects.equals(k, key)) {
172 for (
int i = 0, n = this.data.length; i < n; i += 2) {
173 Object v = this.data[i + 1];
174 if (Objects.equals(v, value)) {
183 Object[] tmp =
new Object[this.data.length - 2];
185 System.arraycopy(this.data, 0, tmp, 0, i);
187 if (i + 2 < this.data.length) {
188 System.arraycopy(this.data, i + 2, tmp, i, this.data.length - 2 - i);
194 public void putAll(
final Map<? extends K, ? extends V> m) {
195 m.forEach(this::put);
218 private static class Entry<K, V>
implements Map.
Entry<K, V> {
240 throw new UnsupportedOperationException();
245 if (
this == o)
return true;
246 if (o == null || getClass() != o.getClass())
return false;
248 return Objects.
equals(this.k, entry.
k) &&
249 Objects.equals(this.v, entry.
v);
254 return Objects.hash(this.k, this.v);
268 return this.map.
size();
288 Object[] data = this.map.
data;
289 Object[] result =
new Object[data.length / 2];
290 for (
int i = 0; i < result.length; i++) {
291 result[i] = data[i * 2];
297 public <T> T[] toArray(T[] a) {
298 Object[] data = this.map.
data;
299 int resultLength = data.length / 2;
301 if (result == null) {
302 result =
new Object[resultLength];
303 }
else if (result.length != resultLength) {
304 result = Arrays.copyOf(a, resultLength);
306 for (
int i = 0; i < result.length; i++) {
307 result[i] = data[i * 2];
314 throw new UnsupportedOperationException();
318 public boolean remove(Object o) {
333 public boolean addAll(Collection<? extends K> c) {
334 throw new UnsupportedOperationException();
339 boolean modified =
false;
340 Object[] data = this.map.
data;
341 for (
int i = 0, n = data.length; i < n; i += 2) {
342 Object key = data[i];
343 if (!c.contains(key)) {
353 boolean modified =
false;
378 return this.map.
data.length > this.nextIndex;
384 K key = (K) this.map.
data[
this.nextIndex];
388 throw new NoSuchElementException();
392 public void remove() {
398 private static class ValuesView<K, V>
implements Collection<V> {
408 return this.map.
size();
428 Object[] data = this.map.
data;
429 Object[] result =
new Object[data.length / 2];
430 for (
int i = 0; i < result.length; i++) {
431 result[i] = data[i * 2 + 1];
437 public <T> T[] toArray(T[] a) {
438 Object[] data = this.map.
data;
439 int resultLength = data.length / 2;
441 if (result == null) {
442 result =
new Object[resultLength];
443 }
else if (result.length != resultLength) {
444 result = Arrays.copyOf(a, resultLength);
446 for (
int i = 0; i < result.length; i++) {
447 result[i] = data[i * 2 + 1];
454 throw new UnsupportedOperationException();
458 public boolean remove(Object o) {
473 public boolean addAll(Collection<? extends V> c) {
474 throw new UnsupportedOperationException();
479 boolean modified =
false;
490 boolean modified =
false;
491 Object[] data = this.map.
data;
492 for (
int i = 0, n = data.length; i < n; i += 2) {
493 Object value = data[i + 1];
494 if (!c.contains(value)) {
519 return this.map.
data.length > this.nextIndex;
525 V value = (V) this.map.
data[
this.nextIndex + 1];
529 throw new NoSuchElementException();
533 public void remove() {
539 private static class EntrySetView<K, V>
implements Set<java.util.Map.Entry<K, V>> {
549 return this.map.
size();
559 if (o instanceof
Entry) {
561 return Objects.equals(e.
v,
this.map.get(e.
k));
573 Object[] data = this.map.
data;
574 Object[] result =
new Object[data.length / 2];
575 for (
int i = 0; i < result.length; i++) {
576 result[i] =
new Entry<>(data[i * 2], data[i * 2 + 1]);
582 public <T> T[] toArray(T[] a) {
583 Object[] data = this.map.
data;
584 int resultLength = data.length / 2;
586 if (result == null) {
587 result =
new Object[resultLength];
588 }
else if (result.length != resultLength) {
589 result = Arrays.copyOf(a, resultLength);
591 for (
int i = 0; i < result.length; i++) {
592 result[i] =
new Entry<>(data[i * 2], data[i * 2 + 1]);
598 public boolean add(Map.Entry<K, V> kvEntry) {
599 throw new UnsupportedOperationException();
603 public boolean remove(Object o) {
604 if (o instanceof
Entry) {
614 if (!this.contains(o)) {
622 public boolean addAll(Collection<? extends Map.Entry<K, V>> c) {
623 throw new UnsupportedOperationException();
628 boolean modified =
false;
629 Object[] data = this.map.
data;
630 for (
int i = 0, n = data.length; i < n; i += 2) {
631 Object key = data[i];
632 Object value = data[i + 1];
633 if (!c.contains(
new Entry<>(key, value))) {
634 this.map.
remove(key, value);
643 boolean modified =
false;
645 if (o instanceof
Entry) {
661 private static class EntryIterator<K, V>
implements Iterator<java.util.Map.Entry<K, V>> {
672 return this.map.
data.length > this.nextIndex;
676 public java.util.Map.Entry<K, V>
next() {
677 K key = (K) this.map.
data[
this.nextIndex];
678 V value = (V) this.map.
data[this.nextIndex + 1];
680 return new Entry<>(key, value);
final ArrayMap< K, V > map
boolean removeKey(final Object key)
void putAll(final Map<? extends K, ? extends V > m)
boolean containsAll(Collection<?> c)
Iterator< Map.Entry< K, V > > iterator()
boolean contains(Object o)
V setValue(final V value)
boolean contains(Object o)
boolean removeAll(Collection<?> c)
V remove(final Object key)
boolean addAll(Collection<? extends Map.Entry< K, V >> c)
java.util.Map.Entry< K, V > next()
boolean retainAll(Collection<?> c)
boolean containsAll(Collection<?> c)
boolean retainAll(Collection<?> c)
boolean containsKey(Object key)
boolean add(Map.Entry< K, V > kvEntry)
boolean removeAll(Collection<?> c)
final ArrayMap< K, V > map
boolean removeAll(Collection<?> c)
boolean retainAll(Collection<?> c)
boolean removeValue(final Object value)
V replace(K key, V value)
ArrayMap(Map< K, V > map)
boolean containsAll(Collection<?> c)
final ArrayMap< K, V > map
final ArrayMap< K, V > map
ValuesView(ArrayMap< K, V > map)
boolean containsValue(Object value)
final ArrayMap< K, V > map
Set< java.util.Map.Entry< K, V > > entrySet()
boolean contains(Object o)
final ArrayMap< K, V > map
static final Object [] EMPTY
boolean addAll(Collection<? extends V > c)
boolean addAll(Collection<? extends K > c)