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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
//! Binary search on sorted lists
//!
//! Most operations in this module are unsafe, because they assume (without checking) that inputs are sorted.
use Ordering;
/// Find a value `p` in `{ min, .., max }` that meets a given criterion, or return `None`.
///
/// The returned value is **not** gauranteed to be the first one that satisfies the criterion.
///
/// We implicitly assume that there exists an ambient sequence of form
///
/// `v = [ false, false, .., true, true, ..., false, false, ..]`
///
/// where at least one `true` value exists, and all `true` values are contiguous
/// (however the `true` values may lie outside the interval `{ min, .., max }`).
/// We assume that `search_direction(p)` is equal to `Ordering::Equal` if `v[p]==true`,
/// is equal to `Ordering::Less` if the first true value occurs below `p`, and
/// is equal to `Ordering::Greater` if the first true value occurs above `p`
///
///
/// # Examples
/// ```
/// use oat_rust::utilities::binary_search::find_sorted_binary_oracle;
///
/// let v = vec![ (3,1.0), (5,1.0) ];
///
/// assert_eq!( find_sorted_binary_oracle(0,1, |p| 1.cmp( & v[p as usize].0 ) ), None ); // search for an entry with index 1
/// assert_eq!( find_sorted_binary_oracle(0,1, |p| 3.cmp( & v[p as usize].0 ) ), Some(0) ); // search for an entry with index 3
/// assert_eq!( find_sorted_binary_oracle(0,1, |p| 5.cmp( & v[p as usize].0 ) ), Some(1) ); // search for an entry with index 5
/// assert_eq!( find_sorted_binary_oracle(0,1, |p| 7.cmp( & v[p as usize].0 ) ), None ); // search for an entry with index 7
/// ```
///
/// # Notes
/// This code is unit-tested on all 0-1 sparse vectors of length < 8; see source code for details.
/// Find an entry with index `n` in a *strictly sorted* sequence of index-value pairs.
///
/// The input, `sparsevec`, is a Rust vector of form `[ (i0,v0), (i1,v1), .. ]` where
/// `i0 ≤ i1 ≤ ..`.
///
/// The output is either
/// - an index `a` such that `sparsevec[a][0] = `n`, or
/// - `None`, if no such index exists
///
/// The value `a` is **not** guaranteed to be the first index where `n` appears.
///
/// # Examples
/// ```
/// use oat_rust::utilities::binary_search::find_sorted_binary_tuple;
///
/// let sparsevec = vec![ (3,1.0), (5,1.0) ];
///
/// assert_eq!( find_sorted_binary_tuple(&sparsevec, 1), None );
/// assert_eq!( find_sorted_binary_tuple(&sparsevec, 3), Some( 0 ) );
/// assert_eq!( find_sorted_binary_tuple(&sparsevec, 5), Some( 1 ) );
/// assert_eq!( find_sorted_binary_tuple(&sparsevec, 7), None );
/// ```
///
/// # Notes
/// This code is unit-tested on all 0-1 sparse vectors of length < 8; see source code for details.
/// Find an entry with value `n` in a *(not necessarily strictly) sorted* `sequence`.
///
/// Look-up is performed by a binary search.
///
/// The output is either
/// - an index `a` such that `sparsevec[a] = `n`, or
/// - `None`, if no such index exists
///
/// The index `a` is **not** guaranteed to be the first index where `n` appears.
///
/// # Examples
/// ```
/// use oat_rust::utilities::binary_search::find_in_sorted_sequence;
///
/// let sequence = vec![ 3, 5 ];
///
/// assert_eq!( find_in_sorted_sequence(&sequence, & 1), None );
/// assert_eq!( find_in_sorted_sequence(&sequence, & 3), Some( 0 ) );
/// assert_eq!( find_in_sorted_sequence(&sequence, & 5), Some( 1 ) );
/// assert_eq!( find_in_sorted_sequence(&sequence, & 7), None );
/// ```
///
/// # Notes
/// This code is unit-tested on all 0-1 sparse vectors of length < 8; see source code for details.
/// Find an entry with value `n` in a user-specified subsequence of a *(not necessarily strictly) sorted* `sequence`.
///
/// Look-up is performed by a binary search.
///
/// The output is either
/// - an index `a` such that `lower ≤ a < upper` and `sparsevec[a] = `n`, or
/// - `None`, if no such index exists
///
/// The `lower` bound defaults to 0 and the `upper` bound defaults to `sequence.len()`.
///
/// The index `a` is **not** guaranteed to be the first index where `n` appears.
///
///
/// # Examples
/// ```
/// use oat_rust::utilities::binary_search::find_in_sorted_sequence_within_bounds;
///
/// let sequence = vec![ 3, 5 ];
/// let lower = Some(1);
/// let upper = Some(1);
///
/// assert_eq!( find_in_sorted_sequence_within_bounds(&sequence, & 3, lower, None ), None );
/// assert_eq!( find_in_sorted_sequence_within_bounds(&sequence, & 5, lower, None ), Some( 1 ) );
/// assert_eq!( find_in_sorted_sequence_within_bounds(&sequence, & 3, None, upper ), Some( 0 ) );
/// assert_eq!( find_in_sorted_sequence_within_bounds(&sequence, & 5, None, upper ), None );
/// ```
///
/// # Notes
/// This code is unit-tested on all 0-1 sparse vectors of length < 8; see source code for details.
/// Determine set containment.
///
/// *Both input vectors must be sored in (not necessarily strictly) ascending oder*.
///
/// ```
/// use oat_rust::utilities::binary_search::contains_subset;
///
/// let a: Vec<usize> = vec![];
/// let b: Vec<usize> = vec![0,1];
/// let c: Vec<usize> = vec![1,2];
/// let d: Vec<usize> = vec![0,1,2];
///
/// assert!( contains_subset( &b, &a ) );
/// assert!( contains_subset( &d, &b ) );
/// assert!( contains_subset( &d, &d ) );
/// assert!( ! contains_subset( &a, &d ) );
/// assert!( ! contains_subset( &b, &c ) );
/// ```
/// Given an increasing sequence `left_limits` and an integer `pigeon`, find
/// an index `p` such that `left_limits[p] <= pigoen < left_limits[p+1]`.
///
/// # Examples
/// ```
/// use oat_rust::utilities::binary_search::find_window;
///
/// let left_limits = vec![0,2,2,3];
///
/// let u: Vec< Option<usize> > = (0..5).map( |x| find_window( &left_limits, x ) ).collect();
/// let v = vec![Some(0),Some(0),Some(2),None,None];
///
/// assert_eq!( u, v );
/// ```