-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathOrderedStringMap.hx
187 lines (140 loc) · 3.79 KB
/
OrderedStringMap.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
/*
* 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 hx.strings.StringBuilder;
/**
* A map with String keys ordered by insertion.
*/
@:forward
abstract OrderedStringMap<V>(OrderedStringMapImpl<V>) from OrderedStringMapImpl<V> {
inline
public function new()
this = new OrderedStringMapImpl<V>();
@:to
function __toStringMap():StringMap<V>
return cast this;
@:arrayAccess
@:noCompletion
@:noDoc @:dox(hide)
inline
public function __arrayGet(key:String):Null<V>
return this.get(key);
@:arrayAccess
@:noCompletion
@:noDoc @:dox(hide)
inline
public function __arrayWrite(key:String, value:V):V {
this.set(key, value);
return value;
}
}
@:noDoc @:dox(hide)
@:noCompletion
class OrderedStringMapImpl<V> implements haxe.Constraints.IMap<String,V> {
@:allow(hx.strings.collection.ValueIterator)
var __keys:Array<String>;
final __map = new StringMap<V>();
public var size(get, never):Int;
inline
private function get_size():Int
return __keys.length;
inline
public function new()
clear();
/**
* <pre><code>
* >>> ({var m = new OrderedStringMap<Int>(); m.set("1", 1); m.clear(); m; }).isEmpty() == true
* </code></pre>
*/
inline
public function clear():Void {
__keys = new Array<String>();
__map.clear();
}
/**
* <pre><code>
* >>> new OrderedStringMap<Int>().copy() != null
* </code></pre>
*/
inline
public function copy():OrderedStringMapImpl<V> {
final clone = new OrderedStringMapImpl<V>();
for (k => v in this)
clone.set(k, v);
return clone;
}
inline
public function exists(key:String):Bool
return __map.exists(key);
/**
* <pre><code>
* >>> ({var m = new OrderedStringMap<Int>(); m.set("1", 10); m["1"]; }) == 10
* </code></pre>
*/
inline
public function get(key:String):Null<V>
return __map.get(key);
/**
* <pre><code>
* >>> new OrderedStringMap<Int>().isEmpty() == true
* >>> ({var m = new OrderedStringMap<Int>(); m.set("1", 1); m; }).isEmpty() == false
* </code></pre>
*/
inline
public function isEmpty():Bool
return !this.iterator().hasNext();
inline
public function iterator():Iterator<V>
return new ValueIterator<V>(this);
inline
public function keys():Iterator<String>
return __keys.iterator();
inline
public function keyValueIterator():KeyValueIterator<String, V>
return new haxe.iterators.MapKeyValueIterator(this);
public function remove(key:String):Bool {
if (__map.remove(key)) {
__keys.remove(key);
return true;
}
return false;
}
/**
* Sets the value for the given key. Does not change the position of the key in case it existed already.
*/
public function set(key:String, value:V):Void {
final isNew = !__map.exists(key);
__map.set(key, value);
if (isNew)
__keys.push(key);
}
public function toString() : String {
final sb = new StringBuilder("{");
var isFirst = true;
for(key => v in this) {
if(isFirst)
isFirst = false;
else
sb.add(", ");
sb.add(key).add(" => ").add(v);
}
sb.add("}");
return sb.toString();
}
}
private class ValueIterator<V> {
final map:OrderedStringMap<V>;
var pos = -1;
inline
public function new(map:OrderedStringMap<V>)
this.map = map;
inline
public function hasNext():Bool
return pos + 1 < map.__keys.length;
inline
public function next():V
return cast map.get(map.__keys[++pos]);
}