forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathincremental-map.js
168 lines (143 loc) · 3.52 KB
/
incremental-map.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
157
158
159
160
161
162
163
164
165
166
167
168
/**
* Mnemonist Incremental Map
* ==========================
*
* Simple utility class mapping a set of keys unknown beforehand to a range
* of increasing integers. Very useful to map a set of hashable keys to
* and array's indices, for instance.
*/
var iterateOver = require('./utils/iterate.js');
/**
* IncrementalMap.
*
* @constructor
* @param {object} range - Range options:
* @param {number} step - Step of the counter. Defaults to 1.
* @param {number} offset - Starting offset. Defaults to 0.
*/
function IncrementalMap(range) {
if (typeof range === 'object') {
this.step = range.step || 1;
this.offset = range.offset || 0;
}
else {
this.step = 1;
this.offset = 0;
}
this.map = new Map();
this.clear();
}
/**
* Method used to clear the structure.
*
* @return {undefined}
*/
IncrementalMap.prototype.clear = function() {
// Properties
this.size = 0;
this.counter = this.offset;
this.map.clear();
};
/**
* Method used to add a key to the structure without checking its existence
* beforehand.
*
* @param {any} key - Key to add.
* @return {IncrementalMap}
*/
IncrementalMap.prototype.unsafeAdd = function(key) {
this.map.set(key, this.counter);
this.counter += this.step;
this.size++;
};
/**
* Method used to add a key to the structure.
*
* @param {any} key - Key to add.
* @return {IncrementalMap}
*/
IncrementalMap.prototype.add = function(key) {
if (!this.map.has(key)) {
this.map.set(key, this.counter);
this.counter += this.step;
this.size++;
}
return this;
};
/**
* Bootstrapping methods.
*/
IncrementalMap.prototype.get = function(key) {
return this.map.get(key);
};
IncrementalMap.prototype.has = function(key) {
return this.map.has(key);
};
IncrementalMap.prototype.keys = function() {
return this.map.keys();
};
IncrementalMap.prototype.values = function() {
return this.map.values();
};
IncrementalMap.prototype.entries = function() {
return this.map.entries();
};
IncrementalMap.prototype.forEach = function() {
return this.map.forEach.apply(this.map, arguments);
};
/**
* Attaching the #.entries method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined')
IncrementalMap.prototype[Symbol.iterator] = IncrementalMap.prototype.entries;
/**
* Convenience known method.
*/
IncrementalMap.prototype.inspect = function() {
var proxy = [];
proxy.step = this.step;
proxy.offset = this.offset;
var iterator = this.map.entries(),
step;
while ((step = iterator.next(), !step.done))
proxy.push(step.value);
Object.defineProperty(proxy, 'constructor', {
value: IncrementalMap,
enumerable: false
});
return proxy;
};
/**
* Static @.from function taking an abitrary iterable & converting it into
* a structure.
*
* @param {Iterable} iterable - Target iterable.
* @param {object} range - Range options.
* @return {IncrementalMap}
*/
IncrementalMap.from = function(iterable, range) {
var map = new IncrementalMap(range);
iterateOver(function(item) {
map.add(item);
});
return map;
};
/**
* Static @.from function taking an abitrary iterable containing distinct values
* & converting it into a structure.
*
* @param {Iterable} iterable - Target iterable.
* @param {object} range - Range options.
* @return {IncrementalMap}
*/
IncrementalMap.fromDistinct = function(iterable, range) {
var map = new IncrementalMap(range);
iterateOver(function(item) {
map.unsafeAdd(item);
});
return map;
};
/**
* Exporting.
*/
module.exports = IncrementalMap;