1 /* |
|
2 * Copyright (c) 2017, 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 package org.graalvm.util; |
|
24 |
|
25 import java.util.Iterator; |
|
26 import java.util.Map; |
|
27 import java.util.function.BiFunction; |
|
28 |
|
29 import org.graalvm.util.impl.EconomicMapImpl; |
|
30 |
|
31 /** |
|
32 * Memory efficient map data structure. |
|
33 */ |
|
34 public interface EconomicMap<K, V> extends UnmodifiableEconomicMap<K, V> { |
|
35 |
|
36 V put(K key, V value); |
|
37 |
|
38 default void putAll(EconomicMap<K, V> other) { |
|
39 MapCursor<K, V> e = other.getEntries(); |
|
40 while (e.advance()) { |
|
41 put(e.getKey(), e.getValue()); |
|
42 } |
|
43 } |
|
44 |
|
45 void clear(); |
|
46 |
|
47 V removeKey(K key); |
|
48 |
|
49 @Override |
|
50 MapCursor<K, V> getEntries(); |
|
51 |
|
52 void replaceAll(BiFunction<? super K, ? super V, ? extends V> function); |
|
53 |
|
54 default void putAll(UnmodifiableEconomicMap<? extends K, ? extends V> other) { |
|
55 UnmodifiableMapCursor<? extends K, ? extends V> entry = other.getEntries(); |
|
56 while (entry.advance()) { |
|
57 put(entry.getKey(), entry.getValue()); |
|
58 } |
|
59 } |
|
60 |
|
61 /** |
|
62 * Creates a new map that guarantees insertion order on the key set with the default |
|
63 * {@link Equivalence#DEFAULT} comparison strategy for keys. |
|
64 */ |
|
65 static <K, V> EconomicMap<K, V> create() { |
|
66 return EconomicMap.create(Equivalence.DEFAULT); |
|
67 } |
|
68 |
|
69 /** |
|
70 * Creates a new map that guarantees insertion order on the key set with the default |
|
71 * {@link Equivalence#DEFAULT} comparison strategy for keys and initializes with a specified |
|
72 * capacity. |
|
73 */ |
|
74 static <K, V> EconomicMap<K, V> create(int initialCapacity) { |
|
75 return EconomicMap.create(Equivalence.DEFAULT, initialCapacity); |
|
76 } |
|
77 |
|
78 /** |
|
79 * Creates a new map that guarantees insertion order on the key set with the given comparison |
|
80 * strategy for keys. |
|
81 */ |
|
82 static <K, V> EconomicMap<K, V> create(Equivalence strategy) { |
|
83 return EconomicMapImpl.create(strategy); |
|
84 } |
|
85 |
|
86 /** |
|
87 * Creates a new map that guarantees insertion order on the key set with the default |
|
88 * {@link Equivalence#DEFAULT} comparison strategy for keys and copies all elements from the |
|
89 * specified existing map. |
|
90 */ |
|
91 static <K, V> EconomicMap<K, V> create(UnmodifiableEconomicMap<K, V> m) { |
|
92 return EconomicMap.create(Equivalence.DEFAULT, m); |
|
93 } |
|
94 |
|
95 /** |
|
96 * Creates a new map that guarantees insertion order on the key set and copies all elements from |
|
97 * the specified existing map. |
|
98 */ |
|
99 static <K, V> EconomicMap<K, V> create(Equivalence strategy, UnmodifiableEconomicMap<K, V> m) { |
|
100 return EconomicMapImpl.create(strategy, m); |
|
101 } |
|
102 |
|
103 /** |
|
104 * Creates a new map that guarantees insertion order on the key set and initializes with a |
|
105 * specified capacity. |
|
106 */ |
|
107 static <K, V> EconomicMap<K, V> create(Equivalence strategy, int initialCapacity) { |
|
108 return EconomicMapImpl.create(strategy, initialCapacity); |
|
109 } |
|
110 |
|
111 /** |
|
112 * Wraps an existing {@link java.util.Map} as an {@link org.graalvm.util.EconomicMap}. |
|
113 */ |
|
114 static <K, V> EconomicMap<K, V> wrapMap(Map<K, V> map) { |
|
115 return new EconomicMap<K, V>() { |
|
116 |
|
117 @Override |
|
118 public V get(K key) { |
|
119 V result = map.get(key); |
|
120 return result; |
|
121 } |
|
122 |
|
123 @Override |
|
124 public V put(K key, V value) { |
|
125 V result = map.put(key, value); |
|
126 return result; |
|
127 } |
|
128 |
|
129 @Override |
|
130 public int size() { |
|
131 int result = map.size(); |
|
132 return result; |
|
133 } |
|
134 |
|
135 @Override |
|
136 public boolean containsKey(K key) { |
|
137 return map.containsKey(key); |
|
138 } |
|
139 |
|
140 @Override |
|
141 public void clear() { |
|
142 map.clear(); |
|
143 } |
|
144 |
|
145 @Override |
|
146 public V removeKey(K key) { |
|
147 V result = map.remove(key); |
|
148 return result; |
|
149 } |
|
150 |
|
151 @Override |
|
152 public Iterable<V> getValues() { |
|
153 return map.values(); |
|
154 } |
|
155 |
|
156 @Override |
|
157 public Iterable<K> getKeys() { |
|
158 return map.keySet(); |
|
159 } |
|
160 |
|
161 @Override |
|
162 public boolean isEmpty() { |
|
163 return map.isEmpty(); |
|
164 } |
|
165 |
|
166 @Override |
|
167 public MapCursor<K, V> getEntries() { |
|
168 Iterator<java.util.Map.Entry<K, V>> iterator = map.entrySet().iterator(); |
|
169 return new MapCursor<K, V>() { |
|
170 |
|
171 private Map.Entry<K, V> current; |
|
172 |
|
173 @Override |
|
174 public boolean advance() { |
|
175 boolean result = iterator.hasNext(); |
|
176 if (result) { |
|
177 current = iterator.next(); |
|
178 } |
|
179 |
|
180 return result; |
|
181 } |
|
182 |
|
183 @Override |
|
184 public K getKey() { |
|
185 return current.getKey(); |
|
186 } |
|
187 |
|
188 @Override |
|
189 public V getValue() { |
|
190 return current.getValue(); |
|
191 } |
|
192 |
|
193 @Override |
|
194 public void remove() { |
|
195 iterator.remove(); |
|
196 } |
|
197 }; |
|
198 } |
|
199 |
|
200 @Override |
|
201 public void replaceAll(BiFunction<? super K, ? super V, ? extends V> function) { |
|
202 map.replaceAll(function); |
|
203 } |
|
204 }; |
|
205 } |
|
206 } |
|