/
Set.hx
130 lines (111 loc) · 4.05 KB
/
Set.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
/*
* Copyright (C)2005-2019 Haxe Foundation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
package js.lib;
/**
The `js.Set` object lets you store unique values of any type, whether
primitive values or object references.
Documentation [Set](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
**/
@:native("Set")
extern class Set<T> {
/**
The number of values in the `js.Set` object.
**/
var size(default, null):Int;
/**
If an iterable object is passed, all of its elements will be added to
the new `js.Set`.
**/
@:pure function new(?iterable:Any);
/**
Returns a boolean asserting whether an element is present with the given
value in the `js.Set` object or not.
**/
@:pure function has(value:T):Bool;
/**
Appends a new element with the given value to the `js.Set` object.
Returns the `js.Set` object.
**/
function add(value:T):Set<T>;
/**
Removes the element associated to the value and returns the value that
`has(value)` would have previously returned.
`has(value)` will return `false` afterwards.
**/
function delete(value:T):Bool;
/**
Removes all elements from the `js.Set` object.
**/
function clear():Void;
/**
Calls `callback` once for each key-value pair present in the `js.Set`
object, in insertion order.
If a `thisArg` parameter is provided to forEach, it will be used as the
`this` value for each callback.
**/
function forEach(callback:(value:T, key:T, set:Set<T>) -> Void, ?thisArg:Any):Void;
/**
Returns a new `js.lib.Iterator` object that contains the keys for each element
in the `js.Set` object in insertion order.
**/
function keys():js.lib.Iterator<T>;
/**
Returns a new `js.lib.Iterator` object that contains the values for each
element in the `js.Set` object in insertion order.
**/
function values():js.lib.Iterator<T>;
/**
Returns a new `js.lib.Iterator` object that contains an array of
`[value, value]` for each element in the `js.Set` object, in insertion
order.
This is kept similar to the `js.Map` object, so that each entry has the
same value for its key and value here.
**/
function entries():js.lib.Iterator<KeyValue<T, T>>;
inline function iterator():HaxeIterator<T> {
return new HaxeIterator(this.values());
}
inline function keyValueIterator():SetKeyValueIterator<T> {
return new SetKeyValueIterator(this);
}
}
/**
key => value iterator for js.lib.Set, tracking the entry index for the key to match the behavior of haxe.ds.List
**/
class SetKeyValueIterator<T> {
final set:js.lib.Set<T>;
final values:HaxeIterator<T>;
var index = 0;
public inline function new(set:js.lib.Set<T>) {
this.set = set;
this.values = new HaxeIterator(set.values());
}
public inline function hasNext():Bool {
return values.hasNext();
}
public inline function next():{key:Int, value:T} {
return {
key: index++,
value: values.next(),
};
}
}