-
Notifications
You must be signed in to change notification settings - Fork 18
/
SortedStringMap.hx
161 lines (131 loc) · 3.82 KB
/
SortedStringMap.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
/*
* 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.strings.collection;
import haxe.ds.BalancedTree;
import hx.strings.Strings;
/**
* A map with sorted String keys.
*/
@:forward
abstract SortedStringMap<V>(SortedStringMapImpl<V>) from SortedStringMapImpl<V> {
inline
public function new(?comparator:String -> String -> Int)
this = new SortedStringMapImpl<V>(comparator);
@:to
function __toStringMap():StringMap<V>
return cast this;
@:arrayAccess
@:noCompletion
@:noDoc @:dox(hide)
public inline function __arrayGet(key:String)
return this.get(key);
@:arrayAccess
@:noCompletion
@:noDoc @:dox(hide)
inline
public function __arrayWrite(key:String, value:V):V {
this.set(key, value);
return value;
}
}
/**
* <pre><code>
* >>> ({var m = new SortedStringMap<Int>(); m.set("1", 1); m.clear(); m; }).isEmpty() == true
* </code></pre>
*/
@:noDoc @:dox(hide)
@:noCompletion
class SortedStringMapImpl<V> extends BalancedTree<String, V> implements haxe.Constraints.IMap<String,V> {
final cmp:String -> String -> Int;
/**
* <b>IMPORTANT:</b> There is currently no native support for getting the size of a map,
* therefore this is emulated for now by using an iterator - which impacts performance.
*
* <pre><code>
* >>> new SortedStringMap<Int>().size == 0
* >>> ({var m = new SortedStringMap<Int>(); m.set("1", 1); m.set("2", 1); m; }).size == 2
* </code></pre>
*/
public var size(get, never):Int;
inline
function get_size():Int {
var count = 0;
final it = this.keys();
while (it.hasNext()) {
it.next();
count++;
}
return count;
}
/**
* @param comparator used for sorting the String keys. Default is the UTF8 supporting Strings#compare() method
*/
public function new(?comparator:(String, String) -> Int) {
super();
if (comparator == null)
#if jvm
// TODO workaround for java.lang.IllegalAccessError: no such method: hx.strings.Strings.compare(String,String)int/invokeStatic
this.cmp = (s1, s2) -> Strings.compare(s1,s2);
#else
this.cmp = Strings.compare;
#end
else
this.cmp = comparator;
}
@:arrayAccess
@:noCompletion
@:noDoc @:dox(hide)
inline
public function __arrayWrite(k:String, v:V):V {
this.set(k, v);
return v;
}
/**
* <pre><code>
* >>> new SortedStringMap<Int>().copy() != null
* </code></pre>
*/
override
public function copy():SortedStringMapImpl<V> {
final clone = new SortedStringMapImpl<V>();
for (k => v in this)
clone.set(k, v);
return clone;
}
inline
override
function compare(s1:String, s2:String):Int
return cmp(s1, s2);
/**
* <pre><code>
* >>> ({var m = new SortedStringMap<Int>(); m.set("1", 10); m["1"]; }) == 10
* </code></pre>
*/
@:arrayAccess
override
public function get(key:String):Null<V>
return super.get(key);
/**
* <pre><code>
* >>> new SortedStringMap<Int>().isEmpty() == true
* >>> ({var m = new SortedStringMap<Int>(); m.set("1", 1); m; }).isEmpty() == false
* </code></pre>
*/
inline
public function isEmpty():Bool
return !this.iterator().hasNext();
/**
* Copies all key-value pairs from the source map into this map.
*
* @param replace if true existing key-value pairs are replaced otherwise they will be skipped
* @return the number of copied key-value pairs
*/
inline
public function setAll(source:StringMap<V>, replace:Bool = true):Int {
final m:StringMap<V> = this;
return m.setAll(source, replace);
}
}