-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.js
217 lines (168 loc) · 5.13 KB
/
index.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
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
213
214
215
216
217
module.exports = function(arr,search,comparitor) {
if(!arr) return -1;
// as long as it has a length i will try and itterate over it.
if(arr.length === undefined) return -1;
if(!comparitor) comparitor = module.exports._defaultComparitor();
return bs(arr,search,comparitor);
}
module.exports.first = function(arr,search,comparitor) {
return module.exports.closest(arr,search,{exists:true},comparitor);
}
module.exports.last = function(arr,search,comparitor) {
return module.exports.closest(arr,search,{exists:true,end:true},comparitor);
}
module.exports.closest = function(arr,search,opts,comparitor) {
if(typeof opts === 'function') {
comparitor = opts;
opts = {};
}
if(arr.length === 0) return -1;
if(arr.length === 1) return 0;
opts = opts||{};
if(!comparitor) comparitor = this._defaultComparitor();
var closest = bsclosest(arr, search, comparitor, opts.end, opts.exists?false:true);
if(closest > arr.length-1) closest = arr.length-1;
else if(closest < 0) closest = 0;
return closest;
}
// inserts element into the correct sorted spot into the array
module.exports.insert = function(arr,search,opts,comparitor){
if(typeof opts === 'function') {
comparitor = opts;
opts = {};
}
opts = opts||{};
if(!comparitor) comparitor = module.exports._defaultComparitor();
if(!arr.length) {
arr[0] = search;
return 0;
}
var closest = module.exports.closest(arr,search,comparitor);
var cmp = comparitor(arr[closest],search);
if(cmp < 0) {//less
arr.splice(++closest,0,search);
} else if(cmp > 0){
arr.splice(closest,0,search);
} else {
if(opts.unique){
arr[closest] = search;
} else {
// im equal. this value should be appended to the list of existing same sorted values.
while(comparitor(arr[closest],search) === 0){
if(closest >= arr.length-1) break;
closest++;
}
arr.splice(closest,0,search);
}
}
return closest;
}
// this method returns the start and end indicies of a range. [start,end]
module.exports.range = function(arr,from,to,comparitor) {
if(!comparitor) comparitor = module.exports._defaultComparitor();
var fromi = module.exports.closest(arr,from,comparitor);
var toi = module.exports.closest(arr,to,{end:true},comparitor);
// this is a hack.
// i should be able to fix the algorithm and generate a correct range.
while(fromi <= toi){
if(comparitor(arr[fromi],from) > -1) break;
fromi++
}
while(toi >= fromi){
if(comparitor(arr[toi],to) < 1) break;
toi--;
}
return [fromi,toi];
}
// this method returns the values of a range;
module.exports.rangeValue = function(arr,from,to,comparitor){
var range = module.exports.range(arr,from,to,comparitor);
return arr.slice(range[0],range[1]+1);
}
//
module.exports.indexObject = function(o,extractor) {
var index = [];
Object.keys(o).forEach(function(k){
index.push({k:k,v:extractor(o[k])});
});
return index.sort(function(o1,o2){
return o1.v - o2.v;
});
}
module.exports.cmp = function(v1,v2){
return v1 - v2;
}
module.exports._defaultComparitor = function() {
var indexMode,indexModeSearch;
var stringMode;
return function(v,search){
// support the object format of generated indexes
if(indexMode === undefined){
if(typeof v === 'object' && v.hasOwnProperty('v')) indexMode = true;
if(typeof search === 'object' && search.hasOwnProperty('v')) indexModeSearch = true
}
if(indexMode) v = v.v;
if(indexModeSearch) search = search.v;
if(stringMode === undefined){
stringMode = false
if(typeof search === 'string' || typeof v === "string"){
stringMode = true
}
}
if(stringMode) v = v+''
return v > search ? 1 : v < search ? -1 : 0
};
};
module.exports._binarySearch = bs;
module.exports._binarySearchClosest = bsclosest;
function bs(arr, search, comparitor) {
var max = arr.length-1,min = 0,middle,cmp;
// continue searching while key may exist
while (max >= min) {
middle = mid(min, max);
cmp = comparitor(arr[middle],search,middle);
if (cmp < 0) {
min = middle + 1;
} else if (cmp > 0) {
max = middle - 1;
} else {
return middle;
}
}
// key not found
return -1;
}
function bsclosest(arr, search, comparitor, invert, closest) {
var mids = {}
, min = 0,max = arr.length-1,middle,cmp
, sanity = arr.length;
while (min < max) {
middle = midCareful(min, max,mids);
cmp = comparitor(arr[middle],search,middle);
if(invert){
if (cmp > 0)max = middle - 1;
else min = middle;
} else {
if (cmp < 0)min = middle + 1;
else max = middle;
}
if(!--sanity) break;
}
if (max == min && comparitor(arr[min],search) === 0) return min;
if(closest) {
var match = comparitor(arr[min],search);
if(min == arr.length-1 && match < 0) return min;
if(min == 0 && match > 0) return 0;
return closest?(invert?min+1:min-1):-1;
}
return -1;
}
function mid(v1,v2){
return v1+Math.floor((v2-v1)/2);
}
function midCareful(v1,v2,mids){
var mid = v1+Math.floor((v2-v1)/2);
if(mids[mid]) mid = v1+Math.ceil((v2-v1)/2);
mids[mid] = 1;
return mid;
}