-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathStringSet.hx
194 lines (159 loc) · 4.88 KB
/
StringSet.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
/*
* 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.AnyAsString;
import hx.strings.collection.StringMap;
import hx.strings.internal.Either2;
/**
* haxe.ds.StringMap backed set implementation.
*
* Each added string is guaranteed to only be present once in the collection.
*/
class StringSet {
private static final EMPTY_MAP = new StringMap<Bool>();
var map:StringMap<Bool> = EMPTY_MAP;
/**
* <pre><code>
* >>> new StringSet().size == 0
* >>> new StringSet(["a", "b", "c", "a"]).size == 3
* </code></pre>
*/
public var size(default, null):Int = 0;
inline
public function new(?initialItems:Either2<StringSet,Array<String>>) {
_initMap();
if (initialItems != null)
addAll(initialItems);
}
function _initMap():Void
map = new StringMap<Bool>();
/**
* <pre><code>
* >>> new StringSet(["", "a", "b"]).add("") == false
* >>> new StringSet(["", "a", "b"]).add("a") == false
* >>> new StringSet(["", "a", "b"]).add("c") == true
* >>> new StringSet(["", "a", "b"]).add(null) throws "[item] must not be null!"
* </code></pre>
* @return true if item was added, false if it was already present
*/
public function add(item:AnyAsString):Bool {
if (item == null)
throw "[item] must not be null!";
if (contains(item))
return false;
map.set(item, true);
size++;
return true;
}
/**
* <pre><code>
* >>> new StringSet(["", "a", "b"]).addAll(["a", "b"]) == 0
* >>> new StringSet(["", "a", "b"]).addAll(["a", "c"]) == 1
* >>> new StringSet(["", "a", "b"]).addAll(["c", "d"]) == 2
* >>> new StringSet(["", "a", "b"]).addAll(null) throws "[items] must not be null!"
* </code></pre>
*
* @return number of added items
*/
public function addAll(items:Either2<StringSet,Array<String>>):Int {
if (items == null)
throw "[items] must not be null!";
var count = 0;
switch(items.value) {
case a(set):
if (set == null)
return 0;
for (str in set) {
if (str != null && add(str))
count++;
}
case b(array):
if (array == null)
return 0;
for (str in array) {
if (str != null && add(str))
count++;
}
}
return count;
}
/**
* Empties the set.
*/
inline
public function clear():Void
map.clear();
/**
* <pre><code>
* >>> new StringSet(["", "a", "b"]).contains("") == true
* >>> new StringSet(["", "a", "b"]).contains("a") == true
* >>> new StringSet(["", "a", "b"]).contains("c") == false
* >>> new StringSet(["", "a", "b"]).contains(null) == false
* </code></pre>
*
* @return true if the item is present
*/
inline
public function contains(item:Null<String>):Bool
return item == null ? false : map.exists(item);
/**
* <pre><code>
* >>> new StringSet( ).isEmpty() == true
* >>> new StringSet([] ).isEmpty() == true
* >>> new StringSet(["a", "b"]).isEmpty() == false
* </code></pre>
*/
inline
public function isEmpty():Bool
return size == 0;
/**
* @return an Iterator over the items. No particular order is guaranteed.
*/
inline
public function iterator():Iterator<String>
return map.keys();
/**
* <pre><code>
* >>> new StringSet(["", "a", "b"]).remove("") == true
* >>> new StringSet(["", "a", "b"]).remove("a") == true
* >>> new StringSet(["", "a", "b"]).remove("c") == false
* >>> new StringSet(["", "a", "b"]).remove(null) == false
* </code></pre>
*
* @return true if the item was removed, false if it was not present
*/
public function remove(item:Null<String>):Bool {
if (item == null)
return false;
if (map.remove(item)) {
size--;
return true;
}
return false;
}
/**
* <pre><code>
* >>> new StringSet([]).toArray() == [ ]
* >>> new StringSet([null]).toArray() == [ ]
* >>> new StringSet([""]).toArray() == [ "" ]
* >>> new StringSet(["a"]).toArray() == [ "a" ]
* </code></pre>
*/
public function toArray():StringArray
return [ for (k in map.keys()) k ];
/**
* <pre><code>
* >>> new StringSet([]).toString() == "[]"
* >>> new StringSet([null]).toString() == "[]"
* >>> new StringSet([""]).toString() == '[ "" ]'
* >>> new StringSet(["b"]).toString() == '[ "b" ]'
* </code></pre>
*/
public function toString():String {
if (size == 0) return "[]";
return '[ "' + toArray().join('", "') + '" ]';
}
}