-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCopyOnWriteArray.hx
402 lines (336 loc) · 10.3 KB
/
CopyOnWriteArray.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
* 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 hx.concurrent.internal.Either3;
import hx.concurrent.lock.RLock;
@:forward
abstract CopyOnWriteArray<T>(CopyOnWriteArrayImpl<T>) from CopyOnWriteArrayImpl<T> to CopyOnWriteArrayImpl<T> {
/**
* @param initialValues either a hx.concurrent.collection.Collection<T>, an Array<T> or a List<T>.
*/
public function new(?initialValues:Either3<Collection<T>, Array<T>, List<T>>) {
this = new CopyOnWriteArrayImpl();
if (initialValues != null)
this.addAll(initialValues);
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2])[0] == 1
* </code></pre>
*/
@:arrayAccess
inline function _get(idx:Int):Null<T>
return this.get(idx);
/**
* >>> ({var arr=new CopyOnWriteArray([1,2]); arr[2]=3; arr; }).toArray() == [1, 2, 3]
*/
@:arrayAccess
inline function _set(idx:Int, x:T):T {
this._set(idx, x);
return x;
}
}
private class CopyOnWriteArrayImpl<T> implements OrderedCollection<T> {
var _items = new Array<T>();
final _sync = new RLock();
@:allow(hx.concurrent.collection.CopyOnWriteArray)
function _set(idx:Int, x:T):Void {
_sync.execute(function() {
final items = _items.copy();
items[idx] = x;
_items = items;
});
}
/**
* <pre><code>
* >>> new CopyOnWriteArray().first == null
* >>> new CopyOnWriteArray([1,2]).first == 1
* </code></pre>
*/
public var first(get, never):Null<T>;
inline function get_first():Null<T> {
final items = _items;
return items.length == 0 ? null : items[0];
}
/**
* <pre><code>
* >>> new CopyOnWriteArray().last == null
* >>> new CopyOnWriteArray([1,2]).last == 2
* </code></pre>
*/
public var last(get, never):Null<T>;
inline function get_last():Null<T> {
final items = _items;
return items.length == 0 ? null : items[items.length - 1];
}
/**
* <pre><code>
* >>> new CopyOnWriteArray().length == 0
* >>> new CopyOnWriteArray([1,2]).length == 2
* </code></pre>
*/
public var length(get, never):Int;
inline function get_length():Int
return _items.length;
inline //
public function new() {
}
public function add(x:T):Void {
_sync.execute(function() {
final items = _items.copy();
items.push(x);
_items = items;
});
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1]).addIfAbsent(2) == true
* >>> new CopyOnWriteArray([1,2]).addIfAbsent(1) == false
* </code></pre>
*/
public function addIfAbsent(x:T):Bool {
return _sync.execute(function() {
if (_items.indexOf(x) > -1)
return false;
final items = _items.copy();
items.push(x);
_items = items;
return true;
});
}
public function addAll(coll:Either3<Collection<T>, Array<T>, List<T>>):Void {
_sync.execute(function() {
var items:Null<Array<T>> = null;
switch (coll.value) {
case a(coll):
items = _items.copy();
for (i in coll.iterator())
items.push(i);
case b(arr):
items = _items.concat(arr);
case c(list):
items = _items.copy();
for (i in list)
items.push(i);
}
_items = items;
});
}
inline //
public function clear():Void
_items = [];
public function insertAt(idx:Int, x:T):Void {
_items = _sync.execute(function() {
final items = _items.copy();
items.insert(idx, x);
return items;
});
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).remove(1) == true
* >>> new CopyOnWriteArray([2]).remove(1) == false
* >>> new CopyOnWriteArray().remove(1) == false
* </code></pre>
*/
public function remove(x:T):Bool {
return _sync.execute(function() {
if (_items.indexOf(x) == -1)
return false;
final items = _items.copy();
final removed = items.remove(x);
_items = items;
return removed;
});
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).removeAt(1) == 2
* >>> new CopyOnWriteArray([1]).removeAt(1) == null
* >>> new CopyOnWriteArray([1]).removeAt(1, true) throws ~/Index out of range/
* </code></pre>
*/
public function removeAt(idx:Int, throwIfOutOfRange:Bool = false):Null<T> {
return _sync.execute(function() {
if (idx < 0 || idx >= _items.length) {
if (throwIfOutOfRange)
throw "Index out of range.";
return null;
}
final items = _items.copy();
final removed = items.splice(idx, 1);
_items = items;
return removed.length == 0 ? null : removed[0];
});
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).removeFirst() == 1
* >>> new CopyOnWriteArray().removeFirst() == null
* >>> new CopyOnWriteArray().removeFirst(true) throws ~/This collection is empty/
* </code></pre>
*/
public function removeFirst(throwIfEmpty:Bool = false):Null<T> {
return _sync.execute(function() {
if (_items.length == 0) {
if (throwIfEmpty)
throw "This collection is empty.";
return null;
}
final items = _items.copy();
final removed = items.shift();
_items = items;
return removed;
});
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).removeLast() == 2
* >>> new CopyOnWriteArray().removeLast() == null
* >>> new CopyOnWriteArray().removeLast(true) throws ~/This collection is empty/
* </code></pre>
*/
public function removeLast(throwIfEmpty:Bool = false):Null<T> {
return _sync.execute(function() {
if (_items.length == 0) {
if (throwIfEmpty)
throw "This collection is empty.";
return null;
}
final items = _items.copy();
final removed = items.pop();
_items = items;
return removed;
});
}
inline //
public function copy():CopyOnWriteArray<T>
return new CopyOnWriteArray(_items.copy());
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).contains(1) == true
* >>> new CopyOnWriteArray([2]).contains(1) == false
* >>> new CopyOnWriteArray().contains(1) == false
* </code></pre>
*/
inline //
public function contains(x:T):Bool
return indexOf(x) > -1;
/**
* <pre><code>
* >>> new CopyOnWriteArray([2]).isEmpty() == false
* >>> new CopyOnWriteArray().isEmpty() == true
* </code></pre>
*/
inline //
public function isEmpty():Bool
return _items.length == 0;
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).get(1) == 2
* >>> new CopyOnWriteArray([2]).get(1) == null
* >>> new CopyOnWriteArray().get(1, true) throws ~/Index out of range/
* </code></pre>
*/
public function get(idx:Int, throwIfOutOfRange:Bool = false):Null<T> {
var items = _items;
if (idx < 0 || idx >= items.length) {
if (throwIfOutOfRange)
throw "Index out of range.";
return null;
}
return items[idx];
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2,1]).indexOf(1) == 0
* >>> new CopyOnWriteArray([1,2,1]).indexOf(1, 1) == 2
* >>> new CopyOnWriteArray([2]).indexOf(1) == -1
* >>> new CopyOnWriteArray().indexOf(1) == -1
* </code></pre>
*/
inline //
public function indexOf(x:T, startAt:Int = 0):Int
return _items.indexOf(x, startAt);
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2,1]).lastIndexOf(1) == 2
* >>> new CopyOnWriteArray([1,2,1]).lastIndexOf(1, 1) == 0
* >>> new CopyOnWriteArray([2]).lastIndexOf(1) == -1
* >>> new CopyOnWriteArray().lastIndexOf(1) == -1
* </code></pre>
*/
inline //
public function lastIndexOf(x:T, ?startAt:Int):Int {
#if (flash || js)
final items = _items;
return items.lastIndexOf(x, startAt == null ? items.length - 1 : startAt);
#else
return _items.lastIndexOf(x, startAt);
#end
}
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2,1]).filter((x) -> x == 1).toArray() == [1, 1]
* </code></pre>
*/
inline //
public function filter(fn:T->Bool):CopyOnWriteArray<T>
return new CopyOnWriteArray(_items.filter(fn));
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2,1]).map((x) -> Std.string(x)).toArray() == ["1", "2", "1"]
* </code></pre>
*/
inline //
public function map<X>(fn:T->X):CopyOnWriteArray<X>
return new CopyOnWriteArray(_items.map(fn));
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).join("_") == "1_2"
* >>> new CopyOnWriteArray([1]).join("_") == "1"
* >>> new CopyOnWriteArray().join("_") == ""
* </code></pre>
*/
inline //
public function join(sep:String):String
return _items.join(sep);
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).iterator().hasNext() == true
* >>> new CopyOnWriteArray([1,2]).iterator().next() == 1
* >>> new CopyOnWriteArray().iterator().hasNext() == false
* </code></pre>
*/
inline //
public function iterator():Iterator<T>
return _items.iterator();
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).toArray() == [1,2]
* >>> new CopyOnWriteArray([1]).toArray() == [1]
* >>> new CopyOnWriteArray().toArray() == []
* </code></pre>
*/
inline //
public function toArray():Array<T>
return _items.copy();
/**
* <pre><code>
* >>> new CopyOnWriteArray([1,2]).toString() == "[1,2]"
* >>> new CopyOnWriteArray([1]).toString() == "[1]"
* >>> new CopyOnWriteArray().toString() == "[]"
* </code></pre>
*/
inline //
public function toString():String {
#if (flash || js)
return "[" + _items.toString() + "]";
#else
return _items.toString();
#end
}
}