simple_sds_sbwt/ops.rs
1//! Operations common to various vectors.
2//!
3//! # Integer vectors
4//!
5//! * [`Vector`]: Basic operations.
6//! * [`Resize`]: Resizable vectors.
7//! * [`Pack`]: Space-efficiency by e.g. bit packing.
8//! * [`Access`]: Random access.
9//! * [`Push`], [`Pop`]: Stack operations.
10//! * [`VectorIndex`]: Rank, select, predecessor, and successor queries.
11//!
12//! # Bitvectors
13//!
14//! * [`BitVec`]: Random access and iterators over all bits.
15//! * [`Rank`]: Rank queries.
16//! * [`Select`]: Select queries and iterators over set bits.
17//! * [`SelectZero`]: Select queries on the complement and iterators over unset bits.
18//! * [`PredSucc`]: Predecessor and successor queries.
19
20use std::iter::FusedIterator;
21use std::cmp;
22
23#[cfg(test)]
24mod tests;
25
26//-----------------------------------------------------------------------------
27
28/// A vector that contains items of a fixed type.
29///
30/// The type of the item must implement [`Eq`] and [`Copy`].
31///
32/// # Examples
33///
34/// ```
35/// use simple_sds_sbwt::ops::{Vector, Resize, Pack, Access, AccessIter};
36/// use simple_sds_sbwt::bits;
37///
38/// #[derive(Clone, Debug, PartialEq, Eq)]
39/// struct Example(Vec<u8>);
40///
41/// impl Example {
42/// fn new() -> Example {
43/// Example(Vec::new())
44/// }
45/// }
46///
47/// impl Vector for Example {
48/// type Item = u8;
49///
50/// fn len(&self) -> usize {
51/// self.0.len()
52/// }
53///
54/// fn width(&self) -> usize {
55/// 8
56/// }
57///
58/// fn max_len(&self) -> usize {
59/// usize::MAX
60/// }
61/// }
62///
63/// impl Resize for Example {
64/// fn resize(&mut self, new_len: usize, value: Self::Item) {
65/// self.0.resize(new_len, value);
66/// }
67///
68/// fn clear(&mut self) {
69/// self.0.clear();
70/// }
71///
72/// fn capacity(&self) -> usize {
73/// self.0.capacity()
74/// }
75///
76/// fn reserve(&mut self, additional: usize) {
77/// self.0.reserve(additional);
78/// }
79/// }
80///
81/// // Packing does not make much sense with the running example.
82/// impl Pack for Example {
83/// fn pack(&mut self) {}
84/// }
85///
86/// impl<'a> Access<'a> for Example {
87/// type Iter = AccessIter<'a, Self>;
88///
89/// fn get(&self, index: usize) -> Self::Item {
90/// self.0[index]
91/// }
92///
93/// fn iter(&'a self) -> Self::Iter {
94/// Self::Iter::new(self)
95/// }
96///
97/// fn is_mutable(&self) -> bool {
98/// true
99/// }
100///
101/// fn set(&mut self, index: usize, value: Self::Item) {
102/// self.0[index] = value
103/// }
104/// }
105///
106/// // Vector
107/// let mut v = Example::new();
108/// assert!(v.is_empty());
109/// assert_eq!(v.len(), 0);
110/// assert_eq!(v.width(), bits::bit_len(u8::MAX as u64));
111///
112/// // Resize
113/// v.reserve(4);
114/// assert!(v.capacity() >= 4);
115/// v.resize(4, 0);
116/// assert_eq!(v.len(), 4);
117/// v.clear();
118/// assert!(v.is_empty());
119///
120/// // Pack
121/// let mut v = Example(vec![1, 2, 3]);
122/// v.pack();
123/// assert_eq!(v.len(), 3);
124///
125/// // Access
126/// assert!(v.is_mutable());
127/// for i in 0..v.len() {
128/// assert_eq!(v.get(i), (i + 1) as u8);
129/// v.set(i, i as u8);
130/// assert_eq!(v.get(i), i as u8);
131/// }
132/// let extracted: Vec<u8> = v.iter().collect();
133/// assert_eq!(extracted, vec![0, 1, 2]);
134/// ```
135pub trait Vector {
136 /// The type of the items in the vector.
137 type Item: Eq + Copy;
138
139 /// Returns the number of items in the vector.
140 fn len(&self) -> usize;
141
142 /// Returns `true` if the vector is empty.
143 fn is_empty(&self) -> bool {
144 self.len() == 0
145 }
146
147 /// Returns the width of of an item in bits.
148 fn width(&self) -> usize;
149
150 /// Returns the maximum length of the vector.
151 fn max_len(&self) -> usize;
152}
153
154/// A vector that can be resized.
155///
156/// See [`Vector`] for an example.
157pub trait Resize: Vector {
158 /// Resizes the vector to a specified length.
159 ///
160 /// If `new_len > self.len()`, the new `new_len - self.len()` values will be initialized.
161 /// If `new_len < self.len()`, the vector is truncated.
162 ///
163 /// # Arguments
164 ///
165 /// * `new_len`: New length of the vector.
166 /// * `value`: Initialization value.
167 ///
168 /// # Panics
169 ///
170 /// May panic if the length would exceed the maximum length.
171 fn resize(&mut self, new_len: usize, value: <Self as Vector>::Item);
172
173 /// Clears the vector without freeing the data.
174 fn clear(&mut self);
175
176 /// Returns the number of items that the vector can store without reallocations.
177 fn capacity(&self) -> usize;
178
179 /// Reserves space for storing at least `self.len() + additional` items in the vector.
180 ///
181 /// Does nothing if the capacity is already sufficient.
182 ///
183 /// # Panics
184 ///
185 /// May panic if the capacity would exceed the maximum length.
186 fn reserve(&mut self, additional: usize);
187}
188
189/// Store the vector more space-efficiently.
190///
191/// This may, for example, reduce the width of an item.
192///
193/// See [`Vector`] for an example.
194pub trait Pack: Vector {
195 /// Try to store the items of the vector more space-efficiently.
196 fn pack(&mut self);
197}
198
199//-----------------------------------------------------------------------------
200
201/// A vector that supports random access to its items.
202///
203/// The default implementations of [`Access::is_mutable`] and [`Access::set`] make the vector immutable.
204///
205/// See [`Vector`] for an example and [`AccessIter`] for a possible implementation of the iterator.
206pub trait Access<'a>: Vector {
207 /// Iterator over the items in the vector.
208 type Iter: Iterator<Item = <Self as Vector>::Item> + ExactSizeIterator;
209
210 /// Returns an item from the vector.
211 ///
212 /// # Panics
213 ///
214 /// May panic if `index` is not a valid index in the vector.
215 /// May panic from I/O errors.
216 fn get(&self, index: usize) -> <Self as Vector>::Item;
217
218 /// Returns an item from the vector or the provided value if `index` is invalid.
219 ///
220 /// # Panics
221 ///
222 /// May panic from I/O errors.
223 fn get_or(&self, index: usize, value: <Self as Vector>::Item) -> <Self as Vector>::Item {
224 if index >= self.len() { value } else { self.get(index) }
225 }
226
227 /// Returns an iterator over the items in the vector.
228 ///
229 /// # Panics
230 ///
231 /// May panic from I/O errors.
232 /// The iterator may also panic for the same reason.
233 fn iter(&'a self) -> Self::Iter;
234
235 /// Returns `true` if the underlying data is mutable.
236 ///
237 /// This is relevant, for example, with memory-mapped vectors, where the underlying file may be opened as read-only.
238 #[inline]
239 fn is_mutable(&self) -> bool {
240 false
241 }
242
243 /// Sets an item in the vector.
244 ///
245 /// # Arguments
246 ///
247 /// * `index`: Index in the vector.
248 /// * `value`: New value of the item.
249 ///
250 /// # Panics
251 ///
252 /// May panic if `index` is not a valid index in the vector.
253 /// May panic if the underlying data is not mutable.
254 /// May panic from I/O errors.
255 fn set(&mut self, _: usize, _: <Self as Vector>::Item) {
256 panic!("Access::set(): The default implementation is immutable");
257 }
258}
259
260//-----------------------------------------------------------------------------
261
262/// A read-only iterator over a [`Vector`] that implements [`Access`].
263///
264/// The iterator uses [`Access::get`] and has efficient implementations of [`Iterator::nth`] and [`DoubleEndedIterator::nth_back`].
265///
266/// See [`Vector`] for an example.
267#[derive(Clone, Debug)]
268pub struct AccessIter<'a, VectorType: Access<'a>> {
269 parent: &'a VectorType,
270 // The first index we have not used.
271 next: usize,
272 // The first index we should not use.
273 limit: usize,
274}
275
276impl<'a, VectorType: Access<'a>> AccessIter<'a, VectorType> {
277 // Creates a new iterator over the vector.
278 pub fn new(parent: &'a VectorType) -> Self {
279 AccessIter {
280 parent,
281 next: 0,
282 limit: parent.len(),
283 }
284 }
285}
286
287impl<'a, VectorType: Access<'a>> Iterator for AccessIter<'a, VectorType> {
288 type Item = <VectorType as Vector>::Item;
289
290 fn next(&mut self) -> Option<Self::Item> {
291 if self.next >= self.limit {
292 None
293 } else {
294 let result = Some(self.parent.get(self.next));
295 self.next += 1;
296 result
297 }
298 }
299
300 fn nth(&mut self, n: usize) -> Option<Self::Item> {
301 self.next += cmp::min(n, self.limit - self.next);
302 self.next()
303 }
304
305 #[inline]
306 fn size_hint(&self) -> (usize, Option<usize>) {
307 let remaining = self.limit - self.next;
308 (remaining, Some(remaining))
309 }
310}
311
312impl<'a, VectorType: Access<'a>> DoubleEndedIterator for AccessIter<'a, VectorType> {
313 fn next_back(&mut self) -> Option<Self::Item> {
314 if self.next >= self.limit {
315 None
316 } else {
317 self.limit -= 1;
318 Some(self.parent.get(self.limit))
319 }
320 }
321
322 fn nth_back(&mut self, n: usize) -> Option<Self::Item> {
323 self.limit -= cmp::min(n, self.limit - self.next);
324 self.next_back()
325 }
326}
327
328impl<'a, VectorType: Access<'a>> ExactSizeIterator for AccessIter<'a, VectorType> {}
329
330impl<'a, VectorType: Access<'a>> FusedIterator for AccessIter<'a, VectorType> {}
331
332//-----------------------------------------------------------------------------
333
334/// Append items to a vector.
335///
336/// [`Pop`] is a separate trait, because a file writer may not implement it.
337///
338/// # Examples
339///
340/// ```
341/// use simple_sds_sbwt::ops::{Vector, Push, Pop};
342///
343/// struct Example(Vec<u8>);
344///
345/// impl Example {
346/// fn new() -> Example {
347/// Example(Vec::new())
348/// }
349/// }
350///
351/// impl Vector for Example {
352/// type Item = u8;
353///
354/// fn len(&self) -> usize {
355/// self.0.len()
356/// }
357///
358/// fn width(&self) -> usize {
359/// 8
360/// }
361///
362/// fn max_len(&self) -> usize {
363/// usize::MAX
364/// }
365/// }
366///
367/// impl Push for Example {
368/// fn push(&mut self, value: Self::Item) {
369/// self.0.push(value);
370/// }
371/// }
372///
373/// impl Pop for Example {
374/// fn pop(&mut self) -> Option<Self::Item> {
375/// self.0.pop()
376/// }
377/// }
378///
379/// // Push
380/// let mut v = Example::new();
381/// assert!(v.is_empty());
382/// v.push(1);
383/// v.push(2);
384/// v.push(3);
385/// assert_eq!(v.len(), 3);
386///
387/// // Pop
388/// assert_eq!(v.pop(), Some(3));
389/// assert_eq!(v.pop(), Some(2));
390/// assert_eq!(v.pop(), Some(1));
391/// assert!(v.pop().is_none());
392/// assert!(v.is_empty());
393/// ```
394pub trait Push: Vector {
395 /// Appends an item to the vector.
396 ///
397 /// # Panics
398 ///
399 /// May panic from I/O errors.
400 /// May panic if the vector would exceed the maximum length.
401 fn push(&mut self, value: <Self as Vector>::Item);
402}
403
404/// Remove and return top items from a vector.
405///
406/// [`Push`] is a separate trait, because a file writer may not implement `Pop`.
407///
408/// See [`Push`] for an example.
409pub trait Pop: Vector {
410 /// Removes and returns the last item from the vector.
411 ///
412 /// Returns [`None`] if there are no more items in the vector.
413 fn pop(&mut self) -> Option<<Self as Vector>::Item>;
414}
415
416//-----------------------------------------------------------------------------
417
418/// Rank, select, predecessor, and successor queries on a vector.
419///
420/// Position `index` of the vector is an occurrence of item `value` of rank `rank`, if `self.get(index) == value` and `self.rank(index) == rank`.
421///
422/// This generalizes [`Rank`], [`Select`], [`SelectZero`], and [`PredSucc`] from binary vectors to vectors with arbitrary items.
423///
424/// # Examples
425///
426/// ```
427/// use simple_sds_sbwt::ops::{Vector, Access, AccessIter, VectorIndex};
428/// use std::cmp;
429///
430/// #[derive(Clone, Debug, PartialEq, Eq)]
431/// struct Example(Vec<char>);
432///
433/// impl Vector for Example {
434/// type Item = char;
435///
436/// fn len(&self) -> usize {
437/// self.0.len()
438/// }
439///
440/// fn width(&self) -> usize {
441/// 32
442/// }
443///
444/// fn max_len(&self) -> usize {
445/// usize::MAX
446/// }
447/// }
448///
449/// impl<'a> Access<'a> for Example {
450/// type Iter = AccessIter<'a, Self>;
451///
452/// fn get(&self, index: usize) -> Self::Item {
453/// self.0[index]
454/// }
455///
456/// fn iter(&'a self) -> Self::Iter {
457/// Self::Iter::new(self)
458/// }
459/// }
460///
461/// struct ValueIter<'a> {
462/// parent: &'a Example,
463/// value: char,
464/// rank: usize,
465/// index: usize,
466/// }
467///
468/// impl<'a> Iterator for ValueIter<'a> {
469/// type Item = (usize, usize);
470///
471/// fn next(&mut self) -> Option<Self::Item> {
472/// while self.index < self.parent.len() {
473/// if self.parent.get(self.index) == self.value {
474/// let result = Some((self.rank, self.index));
475/// self.rank += 1; self.index += 1;
476/// return result;
477/// }
478/// self.index += 1;
479/// }
480/// None
481/// }
482/// }
483///
484/// impl<'a> VectorIndex<'a> for Example {
485/// type ValueIter = ValueIter<'a>;
486///
487/// fn rank(&self, index: usize, value: <Self as Vector>::Item) -> usize {
488/// let index = cmp::min(index, self.len());
489/// let mut result = 0;
490/// for i in 0..index {
491/// if self.get(i) == value {
492/// result += 1;
493/// }
494/// }
495/// result
496/// }
497///
498/// fn value_iter(&'a self, value: <Self as Vector>::Item) -> Self::ValueIter {
499/// Self::ValueIter { parent: self, value, rank: 0, index: 0, }
500/// }
501///
502/// fn value_of(iter: &Self::ValueIter) -> <Self as Vector>::Item {
503/// iter.value
504/// }
505///
506/// fn select(&self, rank: usize, value: <Self as Vector>::Item) -> Option<usize> {
507/// let mut found = 0;
508/// for index in 0..self.len() {
509/// if self.get(index) == value {
510/// if found == rank {
511/// return Some(index);
512/// }
513/// found += 1;
514/// }
515/// }
516/// None
517/// }
518///
519/// fn select_iter(&'a self, rank: usize, value: <Self as Vector>::Item) -> Self::ValueIter {
520/// let index = self.select(rank, value).unwrap_or(self.len());
521/// Self::ValueIter { parent: self, value, rank, index, }
522/// }
523/// }
524///
525/// let vec = Example(vec!['a', 'b', 'c', 'b', 'a', 'b', 'c', 'c']);
526///
527/// // Has item
528/// assert!(vec.contains('b'));
529/// assert!(!vec.contains('d'));
530///
531/// // Rank
532/// assert_eq!(vec.rank(5, 'b'), 2);
533/// assert_eq!(vec.rank(6, 'b'), 3);
534///
535/// // Iterator
536/// let a: Vec<(usize, usize)> = vec.value_iter('a').collect();
537/// assert_eq!(a, vec![(0, 0), (1, 4)]);
538/// assert_eq!(Example::value_of(&vec.value_iter('c')), 'c');
539///
540/// // Select
541/// assert_eq!(vec.select(0, 'c'), Some(2));
542/// let mut iter = vec.select_iter(1, 'c');
543/// assert_eq!(iter.next(), Some((1, 6)));
544/// assert_eq!(iter.next(), Some((2, 7)));
545/// assert!(iter.next().is_none());
546///
547/// // Inverse select
548/// let offset = 3;
549/// let inverse = vec.inverse_select(offset).unwrap();
550/// assert_eq!(inverse, (1, 'b'));
551/// assert_eq!(vec.select(inverse.0, inverse.1), Some(offset));
552///
553/// // PredSucc
554/// assert!(vec.predecessor(1, 'c').next().is_none());
555/// assert_eq!(vec.predecessor(2, 'c').next(), Some((0, 2)));
556/// assert_eq!(vec.predecessor(3, 'c').next(), Some((0, 2)));
557/// assert_eq!(vec.successor(3, 'a').next(), Some((1, 4)));
558/// assert_eq!(vec.successor(4, 'a').next(), Some((1, 4)));
559/// assert!(vec.successor(5, 'a').next().is_none());
560/// ```
561pub trait VectorIndex<'a>: Access<'a> {
562 /// Iterator type over the occurrences of a specific item.
563 ///
564 /// The `Item` in the iterator is a (rank, index) pair such that the value at position `index` is the occurrence of rank `rank`.
565 type ValueIter: Iterator<Item = (usize, usize)>;
566
567 /// Returns `true` if the vector contains an item with the given value.
568 ///
569 /// The default implementation uses [`VectorIndex::rank`].
570 ///
571 /// # Panics
572 ///
573 /// May panic from I/O errors.
574 fn contains(&self, value: <Self as Vector>::Item) -> bool {
575 self.rank(self.len(), value) > 0
576 }
577
578 /// Returns the number of indexes `i < index` in vector such that `self.get(i) == value`.
579 ///
580 /// # Panics
581 ///
582 /// May panic from I/O errors.
583 fn rank(&self, index: usize, value: <Self as Vector>::Item) -> usize;
584
585 /// Computes the inverse of [`VectorIndex::select`], or returns [`None`] if `index` is invalid.
586 ///
587 /// Returns `(rank, value)` such that [`VectorIndex::select`]`(rank, value) == index`.
588 /// The default implementation computes [`VectorIndex::rank`]`(index, `[`Access::get`]`(index))`.
589 ///
590 /// # Panics
591 ///
592 /// May panic from I/O errors.
593 fn inverse_select(&self, index: usize) -> Option<(usize, <Self as Vector>::Item)> {
594 if index >= self.len() {
595 return None;
596 }
597 let value = self.get(index);
598 Some((self.rank(index, value), value))
599 }
600
601 /// Returns an iterator over the occurrences of item `value`.
602 ///
603 /// # Panics
604 ///
605 /// May panic from I/O errors.
606 /// The iterator may also panic for the same reason.
607 fn value_iter(&'a self, value: <Self as Vector>::Item) -> Self::ValueIter;
608
609 /// Returns the value of the items iterated over by the iterator.
610 fn value_of(iter: &Self::ValueIter) -> <Self as Vector>::Item;
611
612 /// Returns the index of the vector that contains the occurrence of item `value` of rank `rank`.
613 ///
614 /// # Panics
615 ///
616 /// May panic from I/O errors.
617 fn select(&self, rank: usize, value: <Self as Vector>::Item) -> Option<usize>;
618
619 /// Returns an iterator at the occurrence of item `value` of rank `rank`.
620 ///
621 /// The iterator will return [`None`] if the rank is out of bounds.
622 ///
623 /// # Panics
624 ///
625 /// May panic from I/O errors.
626 /// The iterator may also panic for the same reason.
627 fn select_iter(&'a self, rank: usize, value: <Self as Vector>::Item) -> Self::ValueIter;
628
629 /// Returns an iterator at the last occurrence `i <= index` of item `value`.
630 ///
631 /// The iterator will return [`None`] if no such occurrence exists.
632 /// The default implementation uses [`VectorIndex::rank`] and [`VectorIndex::select_iter`].
633 ///
634 /// # Panics
635 ///
636 /// May panic from I/O errors.
637 /// The iterator may also panic for the same reason.
638 fn predecessor(&'a self, index: usize, value: <Self as Vector>::Item) -> Self::ValueIter {
639 let rank = self.rank(index + 1, value);
640 let rank = if rank > 0 { rank - 1 } else { self.len() };
641 self.select_iter(rank, value)
642 }
643
644 /// Returns an iterator at the first occurrence `i >= index` of item `value`.
645 ///
646 /// The iterator will return [`None`] if no such occurrence exists.
647 /// The default implementation uses [`VectorIndex::rank`] and [`VectorIndex::select_iter`].
648 ///
649 /// # Panics
650 ///
651 /// May panic from I/O errors.
652 /// The iterator may also panic for the same reason.
653 fn successor(&'a self, index: usize, value: <Self as Vector>::Item) -> Self::ValueIter {
654 let rank = self.rank(index, value);
655 self.select_iter(rank, value)
656 }
657}
658
659//-----------------------------------------------------------------------------
660
661/// An immutable structure that can be seen as a bit array or a sorted array of distinct unsigned integers.
662///
663/// Let `A` be the integer array and `B` the bit array.
664/// `B[i] == true` is then equivalent to value `i` being present in array `A`.
665/// Indexes in both arrays are 0-based, and the ones in the integer array are often called ranks.
666/// Because of the dual interpretations, bitvectors should not implement [`IntoIterator`].
667///
668/// Some operations deal with the complement of the bitvector.
669/// In the bit array interpretation, all bits in the complement vector are flipped.
670/// In the integer array interpretation, the complement contains the values in `0..self.len()` missing from the original.
671///
672/// # Examples
673///
674/// ```
675/// use simple_sds_sbwt::ops::{BitVec, Rank, Select, PredSucc};
676/// use simple_sds_sbwt::raw_vector::{RawVector, AccessRaw};
677/// use simple_sds_sbwt::bits;
678/// use std::cmp;
679///
680/// struct NaiveBitVector {
681/// ones: usize,
682/// data: RawVector,
683/// }
684///
685/// struct BitIter<'a> {
686/// parent: &'a NaiveBitVector,
687/// index: usize,
688/// }
689///
690/// impl<'a> Iterator for BitIter<'a> {
691/// type Item = bool;
692///
693/// fn next(&mut self) -> Option<Self::Item> {
694/// if self.index < self.parent.len() {
695/// let value = self.parent.get(self.index);
696/// self.index += 1;
697/// return Some(value);
698/// }
699/// None
700/// }
701///
702/// fn size_hint(&self) -> (usize, Option<usize>) {
703/// let remaining = self.parent.len() - self.index;
704/// (remaining, Some(remaining))
705/// }
706/// }
707///
708/// impl<'a> ExactSizeIterator for BitIter<'a> {}
709///
710/// impl From<RawVector> for NaiveBitVector {
711/// fn from(data: RawVector) -> Self {
712/// let ones = data.count_ones();
713/// NaiveBitVector {
714/// ones: ones,
715/// data: data,
716/// }
717/// }
718/// }
719///
720/// impl<'a> BitVec<'a> for NaiveBitVector {
721/// type Iter = BitIter<'a>;
722///
723/// fn len(&self) -> usize {
724/// self.data.len()
725/// }
726///
727/// fn count_ones(&self) -> usize {
728/// self.ones
729/// }
730///
731/// fn get(&self, index: usize) -> bool {
732/// self.data.bit(index)
733/// }
734///
735/// fn iter(&'a self) -> Self::Iter {
736/// Self::Iter {
737/// parent: self,
738/// index: 0,
739/// }
740/// }
741/// }
742///
743/// impl<'a> Rank<'a> for NaiveBitVector {
744/// fn supports_rank(&self) -> bool {
745/// true
746/// }
747///
748/// fn enable_rank(&mut self) {}
749///
750/// fn rank(&self, index: usize) -> usize {
751/// let mut result: usize = 0;
752/// let index = cmp::min(self.len(), index);
753/// for i in 0..index {
754/// result += self.get(i) as usize;
755/// }
756/// result
757/// }
758/// }
759///
760/// struct SelectIter<'a> {
761/// parent: &'a NaiveBitVector,
762/// rank: usize,
763/// index: usize,
764/// }
765///
766/// impl<'a> Iterator for SelectIter<'a> {
767/// type Item = (usize, usize);
768///
769/// fn next(&mut self) -> Option<Self::Item> {
770/// while self.index < self.parent.len() {
771/// if self.parent.get(self.index) {
772/// let result = Some((self.rank, self.index));
773/// self.rank += 1; self.index += 1;
774/// return result;
775/// }
776/// self.index += 1;
777/// }
778/// None
779/// }
780///
781/// fn size_hint(&self) -> (usize, Option<usize>) {
782/// let remaining = self.parent.count_ones() - self.rank;
783/// (remaining, Some(remaining))
784/// }
785/// }
786///
787/// impl<'a> ExactSizeIterator for SelectIter<'a> {}
788///
789/// impl<'a> Select<'a> for NaiveBitVector {
790/// type OneIter = SelectIter<'a>;
791///
792/// fn supports_select(&self) -> bool {
793/// true
794/// }
795///
796/// fn enable_select(&mut self) {}
797///
798/// fn one_iter(&'a self) -> Self::OneIter {
799/// Self::OneIter {
800/// parent: self,
801/// rank: 0,
802/// index: 0,
803/// }
804/// }
805///
806/// fn select(&'a self, rank: usize) -> Option<usize> {
807/// let mut found: usize = 0;
808/// for index in 0..self.len() {
809/// if self.get(index) {
810/// if found == rank {
811/// return Some(index);
812/// }
813/// found += 1;
814/// }
815/// }
816/// None
817/// }
818///
819/// fn select_iter(&'a self, rank: usize) -> Self::OneIter {
820/// let index = self.select(rank).unwrap_or(self.len());
821/// Self::OneIter { parent: self, rank, index, }
822/// }
823/// }
824///
825/// impl<'a> PredSucc<'a> for NaiveBitVector {
826/// type OneIter = SelectIter<'a>;
827///
828/// fn supports_pred_succ(&self) -> bool {
829/// true
830/// }
831///
832/// fn enable_pred_succ(&mut self) {}
833///
834/// fn predecessor(&'a self, value: usize) -> Self::OneIter {
835/// let mut rank = self.rank(value + 1);
836/// let index = if rank > 0 { rank = rank - 1; self.select(rank).unwrap() } else { self.len() };
837/// Self::OneIter { parent: self, rank, index, }
838/// }
839///
840/// fn successor(&'a self, value: usize) -> Self::OneIter {
841/// let rank = self.rank(value);
842/// let index = self.select(rank).unwrap_or(self.len());
843/// Self::OneIter { parent: self, rank, index, }
844/// }
845/// }
846///
847/// let mut data = RawVector::with_len(137, false);
848/// data.set_bit(1, true); data.set_bit(33, true); data.set_bit(95, true); data.set_bit(123, true);
849/// let mut bv = NaiveBitVector::from(data);
850///
851/// // BitVec
852/// assert_eq!(bv.len(), 137);
853/// assert!(!bv.is_empty());
854/// assert_eq!(bv.count_ones(), 4);
855/// assert_eq!(bv.count_zeros(), 133);
856/// assert!(bv.get(33));
857/// assert!(!bv.get(34));
858/// for (index, value) in bv.iter().enumerate() {
859/// assert_eq!(value, bv.get(index));
860/// }
861///
862/// // Rank
863/// bv.enable_rank();
864/// assert!(bv.supports_rank());
865/// assert_eq!(bv.rank(33), 1);
866/// assert_eq!(bv.rank(34), 2);
867/// assert_eq!(bv.rank_zero(65), 63);
868///
869/// // Select
870/// bv.enable_select();
871/// assert!(bv.supports_select());
872/// assert_eq!(bv.select(1), Some(33));
873/// let mut iter = bv.select_iter(2);
874/// assert_eq!(iter.next(), Some((2, 95)));
875/// assert_eq!(iter.next(), Some((3, 123)));
876/// assert!(iter.next().is_none());
877/// let v: Vec<(usize, usize)> = bv.one_iter().collect();
878/// assert_eq!(v, vec![(0, 1), (1, 33), (2, 95), (3, 123)]);
879///
880/// // PredSucc
881/// bv.enable_pred_succ();
882/// assert!(bv.supports_pred_succ());
883/// assert!(bv.predecessor(0).next().is_none());
884/// assert_eq!(bv.predecessor(1).next(), Some((0, 1)));
885/// assert_eq!(bv.predecessor(2).next(), Some((0, 1)));
886/// assert_eq!(bv.successor(122).next(), Some((3, 123)));
887/// assert_eq!(bv.successor(123).next(), Some((3, 123)));
888/// assert!(bv.successor(124).next().is_none());
889/// ```
890pub trait BitVec<'a> {
891 /// Iterator type over the bit array.
892 type Iter: Iterator<Item = bool> + ExactSizeIterator;
893
894 /// Returns the length of the bit array or the universe size of the integer array.
895 fn len(&self) -> usize;
896
897 /// Returns `true` if the bitvector is empty.
898 fn is_empty(&self) -> bool {
899 self.len() == 0
900 }
901
902 /// Returns the length of the integer array or the number of ones in the bit array.
903 ///
904 /// Because the vector is immutable, the implementation should cache the value during construction.
905 fn count_ones(&self) -> usize;
906
907 /// Returns the number of zeros in the bit array.
908 #[inline]
909 fn count_zeros(&self) -> usize {
910 self.len() - self.count_ones()
911 }
912
913 /// Reads a bit from the bit array.
914 ///
915 /// In the integer array interpretation, returns `true` if value `index` is in the array.
916 ///
917 /// # Panics
918 ///
919 /// May panic if `index` is not a valid index in the bit array.
920 /// May panic from I/O errors.
921 fn get(&self, index: usize) -> bool;
922
923 /// Returns an iterator over the bit array.
924 ///
925 /// See traits [`Select`] and [`SelectZero`] for other iterators.
926 ///
927 /// # Panics
928 ///
929 /// May panic from I/O errors.
930 /// The iterator may also panic for the same reason.
931 fn iter(&'a self) -> Self::Iter;
932
933 // TODO: add `copy_bit_vec`?
934}
935
936//-----------------------------------------------------------------------------
937
938/// Rank queries on a bitvector.
939///
940/// Some bitvector types do not build rank/select support structures by default.
941/// After the vector has been built, rank support can be enabled with `self.enable_rank()`.
942///
943/// See [`BitVec`] for an example.
944pub trait Rank<'a>: BitVec<'a> {
945 /// Returns `true` if rank support has been enabled.
946 fn supports_rank(&self) -> bool;
947
948 /// Enables rank support for the vector.
949 ///
950 /// No effect if rank support has already been enabled.
951 fn enable_rank(&mut self);
952
953 /// Returns the number of indexes `i < index` in the bit array such that `self.get(i) == true`.
954 ///
955 /// In the integer array interpretation, returns the number of values smaller than `index`.
956 /// The semantics of the query are the same as in [SDSL](https://github.com/simongog/sdsl-lite).
957 ///
958 /// # Panics
959 ///
960 /// May panic if rank support has not been enabled.
961 /// May panic from I/O errors.
962 fn rank(&self, index: usize) -> usize;
963
964 /// Returns the number of indexes `i < index` in the bit array such that `self.get(i) == false`.
965 ///
966 /// In the integer array interpretation, returns the number of missing values smaller than `index`.
967 /// The semantics of the query are the same as in [SDSL](https://github.com/simongog/sdsl-lite).
968 ///
969 /// # Panics
970 ///
971 /// May panic if rank support has not been enabled.
972 /// May panic from I/O errors.
973 #[inline]
974 fn rank_zero(&self, index: usize) -> usize {
975 index - self.rank(index)
976 }
977}
978
979//-----------------------------------------------------------------------------
980
981/// Select queries on a bitvector.
982///
983/// Some bitvector types do not build rank/select support structures by default.
984/// After the vector has been built, select support can be enabled with `self.enable_select()`.
985///
986/// See [`BitVec`] for an example.
987pub trait Select<'a>: BitVec<'a> {
988 /// Iterator type over (index, value) pairs in the integer array.
989 ///
990 /// The `Item` in the iterator is an (index, value) pair in the integer array.
991 /// This can be interpreted as `(i, select(i))` or `(rank(j), j)`.
992 type OneIter: Iterator<Item = (usize, usize)> + ExactSizeIterator;
993
994 /// Returns `true` if select support has been enabled.
995 fn supports_select(&self) -> bool;
996
997 /// Enables select support for the vector.
998 ///
999 /// No effect if select support has already been enabled.
1000 fn enable_select(&mut self);
1001
1002 /// Returns an iterator over the integer array.
1003 ///
1004 /// In the bit array interpretation, the iterator will visit all set bits.
1005 /// The iterator must not require enabling select support.
1006 ///
1007 /// # Panics
1008 ///
1009 /// May panic from I/O errors.
1010 /// The iterator may also panic for the same reason.
1011 fn one_iter(&'a self) -> Self::OneIter;
1012
1013 /// Returns the specified value in the integer array or [`None`] if no such value exists.
1014 ///
1015 /// In the bit array interpretation, the return value is an index `i` such that `self.get(i) == true` and `self.rank(i) == rank`.
1016 /// This trait uses 0-based indexing, while the [SDSL](https://github.com/simongog/sdsl-lite) select uses 1-based indexing.
1017 ///
1018 /// # Panics
1019 ///
1020 /// May panic if select support has not been enabled.
1021 /// May panic from I/O errors.
1022 fn select(&'a self, rank: usize) -> Option<usize>;
1023
1024 /// Returns an iterator at the specified rank in the integer array.
1025 ///
1026 /// The iterator will return [`None`] if the rank is out of bounds.
1027 /// In the bit array interpretation, the iterator points to the set bit of the specified rank.
1028 /// This means a bit array index `i` such that `self.get(i) == true` and `self.rank(i) == rank`.
1029 /// This trait uses 0-based indexing, while the [SDSL](https://github.com/simongog/sdsl-lite) select uses 1-based indexing.
1030 ///
1031 /// # Panics
1032 ///
1033 /// May panic if select support has not been enabled.
1034 /// May panic from I/O errors.
1035 /// The iterator may also panic for the same reasons.
1036 fn select_iter(&'a self, rank: usize) -> Self::OneIter;
1037}
1038
1039//-----------------------------------------------------------------------------
1040
1041/// Select successor queries on the complement of a bitvector.
1042///
1043/// Some bitvector types do not build rank/select support structures by default.
1044/// After the vector has been built, select support for the complement can be enabled with `self.enable_complement()`.
1045///
1046/// This trait is analogous to [`Select`].
1047pub trait SelectZero<'a>: BitVec<'a> {
1048 /// Iterator type over (index, value) pairs in the complement of the integer array.
1049 ///
1050 /// The `Item` in the iterator is an (index, value) pair in the complement of the integer array.
1051 /// This can be interpreted as `(i, select_zero(i))` or `(rank_zero(j), j)`.
1052 type ZeroIter: Iterator<Item = (usize, usize)> + ExactSizeIterator;
1053
1054 /// Returns `true` if select support has been enabled for the complement.
1055 fn supports_select_zero(&self) -> bool;
1056
1057 /// Enables select support for the complement vector.
1058 ///
1059 /// No effect if select support has already been enabled for the complement.
1060 fn enable_select_zero(&mut self);
1061
1062 /// Returns an iterator over the integer array of the complement vector.
1063 ///
1064 /// In the bit array interpretation, the iterator will visit all unset bits.
1065 /// The iterator must not require enabling select support for the complement.
1066 ///
1067 /// # Panics
1068 ///
1069 /// May panic from I/O errors.
1070 /// The iterator may also panic for the same reason.
1071 fn zero_iter(&'a self) -> Self::ZeroIter;
1072
1073 /// Returns the specified value in the complement of the integer array or [`None`] if no such value exists.
1074 ///
1075 /// In the bit array interpretation, the return value is an index `i` such that `self.get(i) == false` and `self.rank_zero(i) == rank`.
1076 /// This trait uses 0-based indexing, while the [SDSL](https://github.com/simongog/sdsl-lite) select uses 1-based indexing.
1077 ///
1078 /// # Panics
1079 ///
1080 /// May panic if select support has not been enabled for the complement.
1081 /// May panic from I/O errors.
1082 fn select_zero(&'a self, rank: usize) -> Option<usize>;
1083
1084 /// Returns an iterator at the specified rank in the complement of the integer array.
1085 ///
1086 /// The iterator will return [`None`] if the rank is out of bounds.
1087 /// In the bit array interpretation, the iterator points to an index `i` such that `self.get(i) == false` and `self.rank_zero(i) == rank`.
1088 /// This trait uses 0-based indexing, while the [SDSL](https://github.com/simongog/sdsl-lite) select uses 1-based indexing.
1089 ///
1090 /// # Panics
1091 ///
1092 /// May panic if select support has not been enabled for the complement.
1093 /// May panic from I/O errors.
1094 /// The iterator may also panic for the same reasons.
1095 fn select_zero_iter(&'a self, rank: usize) -> Self::ZeroIter;
1096}
1097
1098//-----------------------------------------------------------------------------
1099
1100/// Predecessor / successor queries on a bitvector.
1101///
1102/// Some bitvector types do not build rank/select support structures by default.
1103/// After the vector has been built, predecessor/successor support can be enabled with `self.enable_pred_succ()`.
1104/// This may also enable other support structures.
1105///
1106/// See [`BitVec`] for an example.
1107pub trait PredSucc<'a>: BitVec<'a> {
1108 /// Iterator type over (index, value) pairs in the integer array.
1109 ///
1110 /// The `Item` in the iterator is an (index, value) pair in the integer array.
1111 /// This can be interpreted as `(i, select(i))` or `(rank(j), j)`.
1112 type OneIter: Iterator<Item = (usize, usize)> + ExactSizeIterator;
1113
1114 /// Returns `true` if predecessor/successor support has been enabled.
1115 fn supports_pred_succ(&self) -> bool;
1116
1117 /// Enables predecessor/successor support for the vector.
1118 ///
1119 /// No effect if predecessor/successor support has already been enabled.
1120 fn enable_pred_succ(&mut self);
1121
1122 /// Returns an iterator at the largest `v <= value` in the integer array.
1123 ///
1124 /// The iterator will return [`None`] if no such value exists.
1125 /// In the bit array interpretation, the iterator points to the largest `i <= value` such that `self.get(i) == true`.
1126 ///
1127 /// # Panics
1128 ///
1129 /// May panic if predecessor/successor support has not been enabled.
1130 /// May panic from I/O errors.
1131 /// The iterator may also panic for the same reasons.
1132 fn predecessor(&'a self, value: usize) -> Self::OneIter;
1133
1134 /// Returns an iterator at the smallest `v >= value` in the integer array.
1135 ///
1136 /// The iterator will return [`None`] if no such value exists.
1137 /// In the bit array interpretation, the iterator points to the smallest `i >= value` such that `self.get(i) == true`.
1138 ///
1139 /// # Panics
1140 ///
1141 /// May panic if predecessor/successor support has not been enabled.
1142 /// May panic from I/O errors.
1143 /// The iterator may also panic for the same reasons.
1144 fn successor(&'a self, value: usize) -> Self::OneIter;
1145}
1146
1147//-----------------------------------------------------------------------------