index.js
3.08 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
exports.add = add
exports.addFromFront = addFromFront
exports.remove = remove
exports.has = has
exports.eq = eq
exports.lte = lte
exports.lt = lt
exports.gte = gte
exports.gt = gt
exports.nearest = nearest
function defaultCmp (a, b) {
if (a === b) return 0
return a < b ? -1 : 1
}
function add (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var top = list.push(value) - 1
while (top) {
if (cmp(list[top - 1], value) < 0) return
list[top] = list[top - 1]
list[top - 1] = value
top--
}
}
function addFromFront (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var top = list.unshift(value) - 1
for (var i = 0; i < top; i++) {
if (cmp(value, list[i + 1]) < 0) return
list[i] = list[i + 1]
list[i + 1] = value
}
}
function lte (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var i = indexOf(list, value, cmp)
if (i === -1) return -1
for (; i >= 0; i--) {
var c = cmp(list[i], value)
if (c <= 0) return i
}
return -1
}
function lt (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var i = indexOf(list, value, cmp)
if (i === -1) return -1
for (; i >= 0; i--) {
var c = cmp(list[i], value)
if (c < 0) return i
}
return -1
}
function gte (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var i = indexOf(list, value, cmp)
if (i === -1) return -1
for (; i < list.length; i++) {
var c = cmp(list[i], value)
if (c >= 0) return i
}
return -1
}
function gt (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var i = indexOf(list, value, cmp)
if (i === -1) return -1
for (; i < list.length; i++) {
var c = cmp(list[i], value)
if (c > 0) return i
}
return -1
}
function eq (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var i = indexOf(list, value, cmp)
if (i === -1) return -1
return cmp(list[i], value) === 0 ? i : -1
}
function nearest (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var len = list.length
var top = len - 1
var btm = 0
var mid = -1
var trending = 1 // 0 = down, 2 = up
while (top >= btm && btm >= 0 && top < len) {
mid = Math.floor((top + btm) / 2)
var c = cmp(list[mid], value)
if (c === 0) return mid
if (c >= 0) {
if (trending === 1) trending = 0
else if (trending === 2) {
if (Math.abs(list[mid] - value) > Math.abs(list[mid - 1] - value)) return mid - 1
return mid
}
top = mid - 1
} else {
if (trending === 1) trending = 2
else if (trending === 0) return mid
btm = mid + 1
}
}
return mid
}
function indexOf (list, value, cmp) {
if (!cmp) cmp = defaultCmp
var len = list.length
var top = len - 1
var btm = 0
var mid = -1
while (top >= btm && btm >= 0 && top < len) {
mid = Math.floor((top + btm) / 2)
var c = cmp(list[mid], value)
if (c === 0) return mid
if (c >= 0) {
top = mid - 1
} else {
btm = mid + 1
}
}
return mid
}
function has (list, value, cmp) {
return eq(list, value, cmp) > -1
}
function remove (list, value, cmp) {
var i = eq(list, value, cmp)
if (i === -1) return false
list.splice(i, 1)
return true
}