-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathSortedList.js
More file actions
212 lines (187 loc) · 5.02 KB
/
SortedList.js
File metadata and controls
212 lines (187 loc) · 5.02 KB
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*package.json
{
"volo": {
"dependencies": {
"sortedlist": "github:shinout/SortedList"
}
}
}
*/
(function(root,factory) {
if (typeof define === 'function' && define.amd) define([], factory);
else if (typeof module == 'object' && module.exports) module.exports = factory();
else root.SortedList = factory();
}(this, function () {
/**
* SortedList : constructor
*/
var SortedList = function SortedList() {
var arr = null,
options = {},
args = arguments;
["0","1"].forEach(function(n) {
var val = args[n];
if (Array.isArray(val)) {
arr = val;
}
else if (val && typeof val == "object") {
options = val;
}
});
if (typeof options.filter == 'function') {
this._filter = options.filter;
}
if (typeof options.compare == 'function') {
this._compare = options.compare;
}
else if (typeof options.compare == 'string' && SortedList.compares[options.compare]) {
this._compare = SortedList.compares[options.compare];
}
this._unique = !!options.unique;
if (options.resume && arr) {
arr.forEach(function(v, i) { this.push(v) }, this);
}
else if (arr) this.insert.apply(this, arr);
};
/**
* SortedList.create(val1, val2)
* creates an instance
**/
SortedList.create = function(val1, val2) {
return new SortedList(val1, val2);
};
SortedList.prototype = new Array();
SortedList.prototype.constructor = Array.prototype.constructor;
/**
* sorted.insertOne(val)
* insert one value
* returns false if failed, inserted position if succeed
**/
SortedList.prototype.insertOne = function(val) {
var pos = this.bsearch(val);
if (this._unique && this.key(val, pos) != null) return false;
if (!this._filter(val, pos)) return false;
this.splice(pos+1, 0, val);
return pos+1;
};
/**
* sorted.insert(val1, val2, ...)
* insert multi values
* returns the list of the results of insertOne()
**/
SortedList.prototype.insert = function() {
return Array.prototype.map.call(arguments, function(val) {
return this.insertOne(val);
}, this);
};
/**
* sorted.remove(pos)
* remove the value in the given position
**/
SortedList.prototype.remove = function(pos) {
this.splice(pos, 1);
return this;
}
/**
* sorted.bsearch(val)
* @returns position of the value
**/
SortedList.prototype.bsearch = function(val) {
if (!this.length) return -1;
var mpos,
mval,
spos = 0,
epos = this.length;
while (epos - spos > 1) {
mpos = Math.floor((spos + epos)/2);
mval = this[mpos];
var comp = this._compare(val, mval);
if (comp == 0) return mpos;
if (comp > 0) spos = mpos;
else epos = mpos;
}
return (spos == 0 && this._compare(this[0], val) > 0) ? -1 : spos;
};
/**
* sorted.key(val)
* @returns first index if exists, null if not
**/
SortedList.prototype.key = function(val, bsResult) {
if (bsResult== null) bsResult = this.bsearch(val);
var pos = bsResult;
if (pos == -1 || this._compare(this[pos], val) < 0)
return (pos+1 < this.length && this._compare(this[pos+1], val) == 0) ? pos+1 : null;
while (pos >= 1 && this._compare(this[pos-1], val) == 0) pos--;
return pos;
};
/**
* sorted.key(val)
* @returns indexes if exists, null if not
**/
SortedList.prototype.keys = function(val, bsResult) {
var ret = [];
if (bsResult == null) bsResult = this.bsearch(val);
var pos = bsResult;
while (pos >= 0 && this._compare(this[pos], val) == 0) {
ret.push(pos);
pos--;
}
var len = this.length;
pos = bsResult+1;
while (pos < len && this._compare(this[pos], val) == 0) {
ret.push(pos);
pos++;
}
return ret.length ? ret : null;
};
/**
* sorted.unique()
* @param createNew : create new instance
* @returns first index if exists, null if not
**/
SortedList.prototype.unique = function(createNew) {
if (createNew) return this.filter(function(v, k) {
return k == 0 || this._compare(this[k-1], v) != 0;
}, this);
var total = 0;
this.map(function(v, k) {
if (k == 0 || this._compare(this[k-1], v) != 0) return null;
return k - (total++);
}, this)
.forEach(function(k) {
if (k != null) this.remove(k);
}, this)
return this;
};
/**
* sorted.toArray()
* get raw array
**/
SortedList.prototype.toArray = function() {
return this.slice();
};
/**
* default filtration function
**/
SortedList.prototype._filter = function(val, pos) {
return true;
};
/**
* comparison functions
**/
SortedList.compares = {
"number": function(a, b) {
var c = a - b;
return (c > 0) ? 1 : (c == 0) ? 0 : -1;
},
"string": function(a, b) {
return (a > b) ? 1 : (a == b) ? 0 : -1;
}
};
/**
* sorted.compare(a, b)
* default comparison function
**/
SortedList.prototype._compare = SortedList.compares["string"];
return SortedList;
}));