-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSortedSet.js
156 lines (132 loc) · 3.79 KB
/
SortedSet.js
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
/**
* Copyright (c) 2017, Applitopia, Inc.
*
* Modified source code is licensed under the MIT-style license found in the
* LICENSE file in the root directory of this source tree.
*/
/**
* Copyright (c) 2014-present, Facebook, Inc.
*
* Original source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import { SetCollection, KeyedCollection } from './Collection';
import { IS_SORTED_SYMBOL } from './predicates/isSorted';
import { isSortedSet } from './predicates/isSortedSet';
import { Set } from './Set';
import { SortedMap, emptySortedMap } from './SortedMap';
import { mapFactory } from './Operations';
export class SortedSet extends Set {
// @pragma Construction
constructor(value, comparator, options) {
if (!comparator) {
if (this instanceof SortedSet) {
comparator = this._map && this.getComparator();
}
if (!comparator) {
comparator = SortedSet.defaultComparator;
}
}
if (!options) {
if (this instanceof SortedSet) {
options = this._map && this.getOptions();
}
if (!options) {
options = SortedSet.defaultOptions;
}
}
return value === null || value === undefined
? emptySortedSet(comparator, options)
: isSortedSet(value) &&
value.getComparator() === comparator &&
value.getOptions() === options
? value
: emptySortedSet(comparator, options).withMutations(set => {
set.pack(value);
});
}
static of(/*...values*/) {
return this(arguments);
}
static fromKeys(value) {
return this(KeyedCollection(value).keySeq());
}
toString() {
return this.__toString('SortedSet {', '}');
}
// @pragma Access
getComparator() {
return this._map.getComparator();
}
getOptions() {
return this._map.getOptions();
}
// @pragma Modification
pack(value) {
const seq =
value === undefined
? undefined
: SetCollection(value)
.toKeyedSeq()
.mapKeys((k, v) => v);
return updateSortedSet(this, this._map.pack(seq));
}
from(value, backwards) {
return this._map.from(value, backwards).toSetSeq();
}
fromIndex(index, backwards) {
return this._map.fromIndex(index, backwards).toSetSeq();
}
sort(comparator) {
// Late binding
return SortedSet(this, comparator, this.getOptions());
}
sortBy(mapper, comparator) {
// Late binding
return SortedSet(mapFactory(this, mapper), comparator, this.getOptions());
}
__ensureOwner(ownerID) {
if (ownerID === this.__ownerID) {
return this;
}
const newMap = this._map.__ensureOwner(ownerID);
if (!ownerID) {
if (this.size === 0) {
return this.__empty();
}
this.__ownerID = ownerID;
this._map = newMap;
return this;
}
return this.__make(newMap, ownerID);
}
}
SortedSet.isSortedSet = isSortedSet;
SortedSet.defaultComparator = SortedMap.defaultComparator;
SortedSet.defaultOptions = SortedMap.defaultOptions;
const SortedSetPrototype = SortedSet.prototype;
SortedSetPrototype[IS_SORTED_SYMBOL] = true;
SortedSetPrototype.__empty = function() {
return emptySortedSet(this.getComparator(), this.getOptions());
};
SortedSetPrototype.__make = makeSortedSet;
function updateSortedSet(set, newMap) {
if (set.__ownerID) {
set.size = newMap.size;
set._map = newMap;
return set;
}
return newMap === set._map
? set
: newMap.size === 0 ? set.__empty() : set.__make(newMap);
}
function makeSortedSet(map, ownerID) {
const set = Object.create(SortedSetPrototype);
set.size = map ? map.size : 0;
set._map = map;
set.__ownerID = ownerID;
return set;
}
function emptySortedSet(comparator, options) {
return makeSortedSet(emptySortedMap(comparator, options));
}