-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathSynchronizedMap.hx
297 lines (225 loc) · 7.89 KB
/
SynchronizedMap.hx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* SPDX-FileCopyrightText: © Vegard IT GmbH (https://vegardit.com) and contributors
* SPDX-FileContributor: Sebastian Thomschke, Vegard IT GmbH
* SPDX-License-Identifier: Apache-2.0
*/
package hx.concurrent.collection;
import haxe.Constraints.IMap;
import haxe.ds.HashMap;
import haxe.ds.IntMap;
import haxe.ds.ObjectMap;
import haxe.ds.StringMap;
import hx.concurrent.lock.RLock;
@:forward
abstract SynchronizedMap<K, V>(SynchronizedMapImpl<K, V>) from SynchronizedMapImpl<K, V> to SynchronizedMapImpl<K, V> {
/**
* <pre><code>
* >>> SynchronizedMap.newHashMap().isEmpty == true
* </code></pre>
*/
inline public static function newHashMap<K:{function hashCode():Int;}, V>() {
return new SynchronizedMap<K, V>(new HashMapDelegate<K, V>());
}
/**
* <pre><code>
* >>> SynchronizedMap.newIntMap().isEmpty == true
* </code></pre>
*/
inline public static function newIntMap<V>() {
return new SynchronizedMap<Int, V>(new IntMap<V>());
}
/**
* <pre><code>
* >>> SynchronizedMap.newObjectMap().isEmpty == true
* </code></pre>
*/
inline public static function newObjectMap<K:{}, V>() {
return new SynchronizedMap<K, V>(new ObjectMap<K, V>());
}
/**
* <pre><code>
* >>> SynchronizedMap.newStringMap().isEmpty == true
* </code></pre>
*/
inline public static function newStringMap<String, V>() {
return new SynchronizedMap<String, V>(new StringMap<V>());
}
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50]) .isEmpty == false
* >>> SynchronizedMap.from(["foo" => "bar"]) .isEmpty == false
* >>> SynchronizedMap.from(new haxe.ds.StringMap()).isEmpty == true
* </code></pre>
*/
inline public static function from<K, V>(initialValues:IMap<K, V>) {
return new SynchronizedMap<K, V>(initialValues);
}
/**
* <pre><code>
* >>> SynchronizedMap.newHashMap().isEmpty == true
* </code></pre>
*/
inline public static function fromHashMap<K:{function hashCode():Int;}, V>(initialValues:HashMap<K, V>) {
return new SynchronizedMap<K, V>(new HashMapDelegate<K, V>(initialValues));
}
private function new<K, V>(initialValues:IMap<K, V>) {
this = new SynchronizedMapImpl<K, V>(initialValues.copy());
}
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50])[100] == 50
* >>> SynchronizedMap.from(["foo" => "bar"])["foo"] == "bar"
* >>> SynchronizedMap.from(new haxe.ds.StringMap()) != null
* </code></pre>
*/
@:arrayAccess
inline function _get(k:K):Null<V>
return this.get(k);
@:arrayAccess
inline function _set(k:K, v:V):V {
this.set(k, v);
return v;
}
}
private final class SynchronizedMapImpl<K, V> implements IMap<K, V> {
final _items:IMap<K, V>;
final _sync = new RLock();
inline //
public function new(items:IMap<K, V>) {
this._items = items;
}
/**
* <pre><code>
* >>> SynchronizedMap.from([1 => 1]) .length == 1
* >>> SynchronizedMap.from([1 => 1, 2 => 2]) .length == 2
* >>> SynchronizedMap.from(new haxe.ds.StringMap()).length == 0
* </code></pre>
*/
public var length(get, never):Int;
function get_length():Int
return _sync.execute(function() {
var len = 0;
for (item in _items)
len++;
return len;
});
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50]) .isEmpty == false
* >>> SynchronizedMap.from(["foo" => "bar"]) .isEmpty == false
* >>> SynchronizedMap.from(new haxe.ds.StringMap()).isEmpty == true
* </code></pre>
*/
public var isEmpty(get, never):Bool;
function get_isEmpty():Bool
return _sync.execute(() -> !_items.iterator().hasNext());
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50]) .get(100) == 50
* >>> SynchronizedMap.from(["foo" => "bar"]) .get("foo") == "bar"
* >>> SynchronizedMap.from(new haxe.ds.StringMap()).get("foo") == null
* </code></pre>
*/
inline public function get(k:K):Null<V>
return _sync.execute(() -> _items.get(k));
inline public function set(k:K, v:V):Void
_sync.execute(() -> return _items.set(k, v));
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50]) .exists(100) == true
* >>> SynchronizedMap.from(["foo" => "bar"]).exists("foo") == true
* >>> SynchronizedMap.newStringMap() .exists("foo") == false
* </code></pre>
*/
inline public function exists(k:K):Bool
return _sync.execute(() -> _items.exists(k));
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50]) .remove(100) == true
* >>> SynchronizedMap.from(["foo" => "bar"]).remove("foo") == true
* >>> SynchronizedMap.newStringMap() .remove("foo") == false
* </code></pre>
*/
inline public function remove(k:K):Bool
return _sync.execute(() -> _items.remove(k));
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50]) .keys().next() == 100
#if !cs
* >>> SynchronizedMap.from(["foo" => "bar"]).keys().next() == "foo"
#end
* >>> SynchronizedMap.newStringMap() .keys().hasNext() == false
* </code></pre>
*/
inline public function keys():Iterator<K>
return _sync.execute(() -> new SynchronizedMapIterator<K>(_sync, _items.keys()));
/**
* <pre><code>
* >>> SynchronizedMap.from([100 => 50]) .iterator().next() == 50
#if !cs
* >>> SynchronizedMap.from(["foo" => "bar"]).iterator().next() == "bar"
#end
* >>> SynchronizedMap.newStringMap() .iterator().hasNext() == false
* </code></pre>
*/
inline public function iterator():Iterator<V>
return _sync.execute(() -> new SynchronizedMapIterator<V>(_sync, _items.iterator()));
inline public function keyValueIterator():KeyValueIterator<K, V>
return _sync.execute(() -> new SynchronizedMapIterator<{key:K, value:V}>(_sync, _items.keyValueIterator()));
inline public function copy():SynchronizedMap<K, V>
return _sync.execute(() -> SynchronizedMap.from(_items.copy()));
inline public function toString():String
return _sync.execute(() -> Std.string(_items));
inline public function clear():Void
_sync.execute(() -> _items.clear());
}
private final class SynchronizedMapIterator<T> {
final _sync:RLock;
final _it:Iterator<T>;
inline public function new(sync:RLock, it:Iterator<T>) {
_sync = sync;
_it = it;
}
inline public function hasNext():Bool
return _sync.execute(() -> _it.hasNext());
inline public function next():T
return _sync.execute(() -> _it.next());
}
/**
* workaround for haxe.ds.HashMap not implementing IMap interface
*/
private final class HashMapDelegate<K:{function hashCode():Int;}, V> implements IMap<K, V> {
final map = new HashMap<K, V>();
inline public function new(?from:HashMap<K, V>) {
if (from != null) {
@:nullSafety(Off)
for (k => v in from) {
map.set(k, v);
}
}
}
#if (php || lua) @:nullSafety(Off) #end
inline public function get(k:K):Null<V>
return map.get(k);
#if neko @:nullSafety(Off) #end
inline public function set(k:K, v:V):Void
map.set(k, v);
#if neko @:nullSafety(Off) #end
inline public function exists(k:K):Bool
return map.exists(k);
#if neko @:nullSafety(Off) #end
inline public function remove(k:K):Bool
return map.remove(k);
inline public function keys():Iterator<K>
return map.keys();
inline public function iterator():Iterator<V>
return map.iterator();
inline public function keyValueIterator():KeyValueIterator<K, V>
return map.keyValueIterator();
inline public function copy():IMap<K, V>
return new HashMapDelegate(map);
inline public function toString():String
return Std.string(map);
inline public function clear():Void
map.clear();
}