-
Notifications
You must be signed in to change notification settings - Fork 2
/
sorted_search.js
153 lines (118 loc) · 3.57 KB
/
sorted_search.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
/*global global exports*/
var global, exports;
(function (global) {
'use strict';
// API
global.sortedSearch = sortedSearch;
// --- API implementation
var improveFirst = mfun( improveFirst )
, improveLast = mfun( improveFirst, improveLast )
;
function sortedSearch
(sortedArray, x, /*?fun?*/less, /*?fun?*/equal)
/*
In a sorted array, search for first & last occurences of `x`.
If `x` found,return `[ first_index, last_index ]` (integers).
If `x` not found, return `null`.
*/
{
less || (less = less_dflt);
equal || (equal = equal_dflt);
var first_found = false
, last_found = false
, i = 0
, j = sortedArray.length - 1
, imax = j
, jmin = i
;
return improveFirst(
sortedArray, x, less, equal
, isFirstFound, isLastFound
, first_found, last_found, i, j, imax, jmin
);
}
function less_dflt(a,b) { return a < b; }
function equal_dflt(a,b) { return a == b; }
function isFirstFound( sortedArray, i, equal, x )
{
return equal(x, sortedArray[i]) &&
(i < 1 || !equal(x, sortedArray[i - 1]));
}
function isLastFound( sortedArray, j, equal, x)
{
return equal(x, sortedArray[j]) &&
(j > sortedArray.length - 2
|| !equal(x, sortedArray[j + 1]));
}
// --- Private details
function improveFirst(
sortedArray, x, less, equal, isFirstFound, isLastFound
, first_found, last_found, i, j, imax, jmin
)
{
first_found = first_found ||
isFirstFound(sortedArray, i, equal, x);
// Termination tests
if (!(i <= j))
{
if (i > j)
return null; // Done: `x` not found.
else
null.bug;
}
if (first_found && last_found)
return [i, j]; // Done: `x` found.
// Update
if (!first_found)
{
i++;
imax = Math.min( imax, j );
let ind = i + ((imax - i) >> 1)
, v_ind = sortedArray[ind]
;
if (less(v_ind, x) ||
isFirstFound(sortedArray, ind, equal, x)
)
i = ind;
else
imax = ind;
}
return mret(
improveLast
, sortedArray, x, less, equal
, isFirstFound, isLastFound
, first_found, last_found, i, j, imax, jmin
);
}
function improveLast(
sortedArray, x, less, equal, isFirstFound, isLastFound
, first_found, last_found, i, j, imax, jmin
)
{
last_found = last_found ||
isLastFound(sortedArray, j, equal, x);
// Termination tests already done in `improveFirst`,
// not needed here.
// Update
if (!last_found)
{
j--;
jmin = Math.max( jmin, i );
let ind = j - ((j - jmin) >> 1)
, v_ind = sortedArray[ind]
;
if (less(x, v_ind) ||
isLastFound(sortedArray, ind, equal, x)
)
j = ind;
else
jmin = ind;
}
return mret(
improveFirst
, sortedArray, x, less, equal
, isFirstFound, isLastFound
, first_found, last_found, i, j, imax, jmin
);
}
})(global || exports || this);