1extern crate columnar_derive;
11pub use columnar_derive::Columnar;
12
13pub mod adts;
14pub mod boxed;
15pub mod bytes;
16pub mod lookback;
17pub mod primitive;
18pub mod string;
19pub mod sums;
20pub mod vector;
21pub mod tuple;
22mod arc;
23mod rc;
24
25pub use bytemuck;
26
27pub use vector::Vecs;
28pub use string::Strings;
29pub use sums::{rank_select::RankSelect, result::Results, option::Options};
30pub use lookback::{Repeats, Lookbacks};
31
32pub trait Columnar : 'static {
36 fn copy_from<'a>(&mut self, other: Ref<'a, Self>) where Self: Sized {
40 *self = Self::into_owned(other);
41 }
42 fn into_owned<'a>(other: Ref<'a, Self>) -> Self;
44
45 type Container: ContainerBytes + for<'a> Push<&'a Self>;
50
51 fn as_columns<'a, I>(selves: I) -> Self::Container where I: IntoIterator<Item=&'a Self>, Self: 'a {
53 let mut columns: Self::Container = Default::default();
54 for item in selves {
55 columns.push(item);
56 }
57 columns
58 }
59 fn into_columns<I>(selves: I) -> Self::Container where I: IntoIterator<Item = Self>, Self: Sized {
64 let mut columns: Self::Container = Default::default();
65 for item in selves {
66 columns.push(&item);
67 }
68 columns
69 }
70 #[inline(always)] fn reborrow<'b, 'a: 'b>(thing: Ref<'a, Self>) -> Ref<'b, Self> {
80 Self::Container::reborrow_ref(thing)
81 }
82}
83
84pub type ContainerOf<T> = <T as Columnar>::Container;
88
89pub type Ref<'a, T> = <ContainerOf<T> as Borrow>::Ref<'a>;
93
94pub trait Borrow: Len + Clone + 'static {
96 type Ref<'a> : Copy;
100 type Borrowed<'a>: Copy + Len + Index<Ref = Self::Ref<'a>> where Self: 'a;
104 fn borrow<'a>(&'a self) -> Self::Borrowed<'a>;
106 fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a;
108 fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a;
110}
111
112
113pub trait Container : Borrow + Clear + for<'a> Push<Self::Ref<'a>> + Default + Send {
117 fn with_capacity_for<'a, I>(selves: I) -> Self
123 where
124 Self: 'a,
125 I: Iterator<Item = Self::Borrowed<'a>> + Clone
126 {
127 let mut output = Self::default();
128 output.reserve_for(selves);
129 output
130 }
131
132 fn reserve_for<'a, I>(&mut self, selves: I)
134 where
135 Self: 'a,
136 I: Iterator<Item = Self::Borrowed<'a>> + Clone;
137
138
139 #[inline(always)]
145 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) {
146 self.extend(range.map(|i| other.get(i)))
147 }
148}
149
150impl<T: Clone + 'static> Borrow for Vec<T> {
151 type Ref<'a> = &'a T;
152 type Borrowed<'a> = &'a [T];
153 #[inline(always)] fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { &self[..] }
154 #[inline(always)] fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a { item }
155 #[inline(always)] fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { item }
156}
157
158impl<T: Clone + Send + 'static> Container for Vec<T> {
159 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) {
160 self.extend_from_slice(&other[range])
161 }
162 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
163 self.reserve(selves.map(|x| x.len()).sum::<usize>())
164 }
165}
166
167pub trait ContainerBytes : Container + for<'a> Borrow<Borrowed<'a> : AsBytes<'a> + FromBytes<'a>> { }
169impl<C: Container + for<'a> Borrow<Borrowed<'a> : AsBytes<'a> + FromBytes<'a>>> ContainerBytes for C { }
170
171pub use common::{Clear, Len, Push, IndexMut, Index, IndexAs, HeapSize, Slice, AsBytes, FromBytes};
172pub mod common {
174
175 pub trait Len {
177 fn len(&self) -> usize;
179 fn is_empty(&self) -> bool {
181 self.len() == 0
182 }
183 }
184 impl<L: Len + ?Sized> Len for &L {
185 #[inline(always)] fn len(&self) -> usize { L::len(*self) }
186 }
187 impl<L: Len + ?Sized> Len for &mut L {
188 #[inline(always)] fn len(&self) -> usize { L::len(*self) }
189 }
190 impl<T> Len for Vec<T> {
191 #[inline(always)] fn len(&self) -> usize { self.len() }
192 }
193 impl<T> Len for [T] {
194 #[inline(always)] fn len(&self) -> usize { <[T]>::len(self) }
195 }
196
197 pub trait Push<T> {
199 fn push(&mut self, item: T);
201 #[inline(always)] fn extend(&mut self, iter: impl IntoIterator<Item=T>) {
203 for item in iter {
204 self.push(item);
205 }
206 }
207 }
208 impl<T> Push<T> for Vec<T> {
209 #[inline(always)] fn push(&mut self, item: T) { self.push(item) }
210
211 #[inline(always)]
212 fn extend(&mut self, iter: impl IntoIterator<Item=T>) {
213 std::iter::Extend::extend(self, iter)
214 }
215 }
216 impl<'a, T: Clone> Push<&'a T> for Vec<T> {
217 #[inline(always)] fn push(&mut self, item: &'a T) { self.push(item.clone()) }
218
219 #[inline(always)]
220 fn extend(&mut self, iter: impl IntoIterator<Item=&'a T>) {
221 std::iter::Extend::extend(self, iter.into_iter().cloned())
222 }
223 }
224 impl<'a, T: Clone> Push<&'a [T]> for Vec<T> {
225 #[inline(always)] fn push(&mut self, item: &'a [T]) { self.clone_from_slice(item) }
226 }
227
228
229 pub use index::{Index, IndexMut, IndexAs};
230 pub mod index {
236
237 use crate::Len;
238 use crate::common::IterOwn;
239
240 pub trait IndexMut {
242 type IndexMut<'a> where Self: 'a;
244 fn get_mut(& mut self, index: usize) -> Self::IndexMut<'_>;
245 #[inline(always)] fn last_mut(&mut self) -> Option<Self::IndexMut<'_>> where Self: Len {
247 if self.is_empty() { None }
248 else { Some(self.get_mut(self.len()-1)) }
249 }
250 }
251
252 impl<T: IndexMut + ?Sized> IndexMut for &mut T {
253 type IndexMut<'a> = T::IndexMut<'a> where Self: 'a;
254 #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> {
255 T::get_mut(*self, index)
256 }
257 }
258 impl<T> IndexMut for Vec<T> {
259 type IndexMut<'a> = &'a mut T where Self: 'a;
260 #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> { &mut self[index] }
261 }
262 impl<T> IndexMut for [T] {
263 type IndexMut<'a> = &'a mut T where Self: 'a;
264 #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> { &mut self[index] }
265 }
266
267 pub trait Index {
277 type Ref;
281 fn get(&self, index: usize) -> Self::Ref;
282 #[inline(always)] fn last(&self) -> Option<Self::Ref> where Self: Len {
283 if self.is_empty() { None }
284 else { Some(self.get(self.len()-1)) }
285 }
286 #[inline(always)]
290 fn index_iter(&self) -> IterOwn<&Self> {
291 IterOwn {
292 index: 0,
293 slice: self,
294 }
295 }
296 #[inline(always)]
300 fn into_index_iter(self) -> IterOwn<Self> where Self: Sized {
301 IterOwn {
302 index: 0,
303 slice: self,
304 }
305 }
306 }
307
308 impl<'a, T> Index for &'a [T] {
310 type Ref = &'a T;
311 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { &self[index] }
312 }
313 impl<T: Copy> Index for [T] {
314 type Ref = T;
315 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { self[index] }
316 }
317 impl<'a, T> Index for &'a Vec<T> {
318 type Ref = &'a T;
319 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { &self[index] }
320 }
321 impl<T: Copy> Index for Vec<T> {
322 type Ref = T;
323 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { self[index] }
324 }
325
326
327 pub trait CopyAs<T> : Copy {
332 fn copy_as(self) -> T;
333 }
334 impl<T: Copy> CopyAs<T> for &T {
335 #[inline(always)] fn copy_as(self) -> T { *self }
336 }
337 impl<T: Copy> CopyAs<T> for T {
338 #[inline(always)] fn copy_as(self) -> T { self }
339 }
340
341 pub trait IndexAs<T> {
342 fn index_as(&self, index: usize) -> T;
343 #[inline(always)] fn last(&self) -> Option<T> where Self: Len {
344 if self.is_empty() { None }
345 else { Some(self.index_as(self.len()-1)) }
346 }
347 }
348
349 impl<T: Index, S> IndexAs<S> for T where T::Ref: CopyAs<S> {
350 #[inline(always)] fn index_as(&self, index: usize) -> S { self.get(index).copy_as() }
351 }
352
353 }
354
355 use crate::{Borrow, Container};
356 use crate::common::index::CopyAs;
357 pub trait BorrowIndexAs<T> : for<'a> Borrow<Ref<'a>: CopyAs<T>> { }
361 impl<T, C: for<'a> Borrow<Ref<'a>: CopyAs<T>>> BorrowIndexAs<T> for C { }
362 pub trait PushIndexAs<T> : BorrowIndexAs<T> + Container + for<'a> Push<&'a T> { }
366 impl<T, C: BorrowIndexAs<T> + Container + for<'a> Push<&'a T>> PushIndexAs<T> for C { }
367
368 pub trait Clear {
372 fn clear(&mut self);
374 }
375 impl<T> Clear for Vec<T> {
377 #[inline(always)] fn clear(&mut self) { self.clear() }
378 }
379 impl<T> Clear for &[T] {
381 #[inline(always)] fn clear(&mut self) { *self = &[]; }
382 }
383
384 pub trait HeapSize {
385 fn heap_size(&self) -> (usize, usize) { (0, 0) }
388 }
389
390 impl HeapSize for String {
391 fn heap_size(&self) -> (usize, usize) {
392 (self.len(), self.capacity())
393 }
394 }
395 impl<T: HeapSize> HeapSize for [T] {
396 fn heap_size(&self) -> (usize, usize) {
397 let mut l = std::mem::size_of_val(self);
398 let mut c = std::mem::size_of_val(self);
399 for item in self.iter() {
400 let (il, ic) = item.heap_size();
401 l += il;
402 c += ic;
403 }
404 (l, c)
405 }
406 }
407 impl<T: HeapSize> HeapSize for Vec<T> {
408 fn heap_size(&self) -> (usize, usize) {
409 let mut l = std::mem::size_of::<T>() * self.len();
410 let mut c = std::mem::size_of::<T>() * self.capacity();
411 for item in (self[..]).iter() {
412 let (il, ic) = item.heap_size();
413 l += il;
414 c += ic;
415 }
416 (l, c)
417 }
418 }
419
420 #[derive(Copy, Clone, Debug)]
424 pub struct Slice<S> {
425 pub lower: usize,
426 pub upper: usize,
427 pub slice: S,
428 }
429
430 impl<S> std::hash::Hash for Slice<S> where Self: Index<Ref: std::hash::Hash> {
431 fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
432 self.len().hash(state);
433 for i in 0 .. self.len() {
434 self.get(i).hash(state);
435 }
436 }
437 }
438
439 impl<S> Slice<S> {
440 pub fn slice<R: std::ops::RangeBounds<usize>>(self, range: R) -> Self {
441 use std::ops::Bound;
442 let lower = match range.start_bound() {
443 Bound::Included(s) => std::cmp::max(self.lower, *s),
444 Bound::Excluded(s) => std::cmp::max(self.lower, *s+1),
445 Bound::Unbounded => self.lower,
446 };
447 let upper = match range.end_bound() {
448 Bound::Included(s) => std::cmp::min(self.upper, *s+1),
449 Bound::Excluded(s) => std::cmp::min(self.upper, *s),
450 Bound::Unbounded => self.upper,
451 };
452 assert!(lower <= upper);
453 Self { lower, upper, slice: self.slice }
454 }
455 pub fn new(lower: u64, upper: u64, slice: S) -> Self {
456 let lower: usize = lower.try_into().expect("slice bounds must fit in `usize`");
457 let upper: usize = upper.try_into().expect("slice bounds must fit in `usize`");
458 Self { lower, upper, slice }
459 }
460 pub fn len(&self) -> usize { self.upper - self.lower }
461 pub(crate) fn map<T>(self, f: impl Fn(S) -> T) -> Slice<T> {
463 Slice {
464 lower: self.lower,
465 upper: self.upper,
466 slice: f(self.slice),
467 }
468 }
469 }
470
471 impl<S: Index> PartialEq for Slice<S> where S::Ref: PartialEq {
472 fn eq(&self, other: &Self) -> bool {
473 if self.len() != other.len() { return false; }
474 for i in 0 .. self.len() {
475 if self.get(i) != other.get(i) { return false; }
476 }
477 true
478 }
479 }
480 impl<S: Index> PartialEq<[S::Ref]> for Slice<S> where S::Ref: PartialEq {
481 fn eq(&self, other: &[S::Ref]) -> bool {
482 if self.len() != other.len() { return false; }
483 for i in 0 .. self.len() {
484 if self.get(i) != other[i] { return false; }
485 }
486 true
487 }
488 }
489 impl<S: Index> PartialEq<Vec<S::Ref>> for Slice<S> where S::Ref: PartialEq {
490 fn eq(&self, other: &Vec<S::Ref>) -> bool {
491 if self.len() != other.len() { return false; }
492 for i in 0 .. self.len() {
493 if self.get(i) != other[i] { return false; }
494 }
495 true
496 }
497 }
498
499 impl<S: Index> Eq for Slice<S> where S::Ref: Eq { }
500
501 impl<S: Index> PartialOrd for Slice<S> where S::Ref: PartialOrd {
502 fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
503 use std::cmp::Ordering;
504 let len = std::cmp::min(self.len(), other.len());
505
506 for i in 0 .. len {
507 match self.get(i).partial_cmp(&other.get(i)) {
508 Some(Ordering::Equal) => (),
509 not_equal => return not_equal,
510 }
511 }
512
513 self.len().partial_cmp(&other.len())
514 }
515 }
516
517 impl<S: Index> Ord for Slice<S> where S::Ref: Ord + Eq {
518 fn cmp(&self, other: &Self) -> std::cmp::Ordering {
519 use std::cmp::Ordering;
520 let len = std::cmp::min(self.len(), other.len());
521
522 for i in 0 .. len {
523 match self.get(i).cmp(&other.get(i)) {
524 Ordering::Equal => (),
525 not_equal => return not_equal,
526 }
527 }
528
529 self.len().cmp(&other.len())
530 }
531 }
532
533 impl<S> Len for Slice<S> {
534 #[inline(always)] fn len(&self) -> usize { self.len() }
535 }
536
537 impl<S: Index> Index for Slice<S> {
538 type Ref = S::Ref;
539 #[inline(always)] fn get(&self, index: usize) -> Self::Ref {
540 assert!(index < self.upper - self.lower);
541 self.slice.get(self.lower + index)
542 }
543 }
544 impl<'a, S> Index for &'a Slice<S>
545 where
546 &'a S : Index,
547 {
548 type Ref = <&'a S as Index>::Ref;
549 #[inline(always)] fn get(&self, index: usize) -> Self::Ref {
550 assert!(index < self.upper - self.lower);
551 (&self.slice).get(self.lower + index)
552 }
553 }
554
555 impl<S: IndexMut> IndexMut for Slice<S> {
556 type IndexMut<'a> = S::IndexMut<'a> where S: 'a;
557 #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> {
558 assert!(index < self.upper - self.lower);
559 self.slice.get_mut(self.lower + index)
560 }
561 }
562
563 impl<S: Index + Len> Slice<S> {
564 pub fn into_iter(self) -> IterOwn<Slice<S>> {
569 self.into_index_iter()
570 }
571 }
572
573 impl<'a, T> Slice<&'a [T]> {
574 pub fn as_slice(&self) -> &'a [T] {
575 &self.slice[self.lower .. self.upper]
576 }
577 }
578
579 pub struct IterOwn<S> {
580 index: usize,
581 slice: S,
582 }
583
584 impl<S> IterOwn<S> {
585 pub fn new(index: usize, slice: S) -> Self {
586 Self { index, slice }
587 }
588 }
589
590 impl<S: Index + Len> Iterator for IterOwn<S> {
591 type Item = S::Ref;
592 #[inline(always)] fn next(&mut self) -> Option<Self::Item> {
593 if self.index < self.slice.len() {
594 let result = self.slice.get(self.index);
595 self.index += 1;
596 Some(result)
597 } else {
598 None
599 }
600 }
601 #[inline(always)]
602 fn size_hint(&self) -> (usize, Option<usize>) {
603 (self.slice.len() - self.index, Some(self.slice.len() - self.index))
604 }
605 }
606
607 impl<S: Index + Len> ExactSizeIterator for IterOwn<S> { }
608
609 pub trait AsBytes<'a> {
613 fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])>;
615 }
616
617 pub trait FromBytes<'a> {
622 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self;
634 }
635
636}
637
638pub mod roaring {
640
641 use crate::Results;
642
643 pub struct RoaringBits {
651 _inner: Results<[u64; 1024], Vec<u16>>,
652 }
653}
654
655pub use chain_mod::{chain, chain_one};
656
657pub mod chain_mod {
658 #[inline(always)]
668 pub fn chain<A: IntoIterator, B: IntoIterator<Item=A::Item>>(a: A, b: B) -> Chain<A::IntoIter, B::IntoIter> {
669 Chain { a: Some(a.into_iter()), b: Some(b.into_iter()) }
670 }
671
672 pub struct Chain<A, B> {
673 a: Option<A>,
674 b: Option<B>,
675 }
676
677 impl<A, B> Iterator for Chain<A, B>
678 where
679 A: Iterator,
680 B: Iterator<Item=A::Item>,
681 {
682 type Item = A::Item;
683
684 #[inline(always)]
685 fn next(&mut self) -> Option<Self::Item> {
686 if let Some(a) = self.a.as_mut() {
687 let x = a.next();
688 if x.is_none() {
689 self.a = None;
690 } else {
691 return x;
692 }
693 }
694 if let Some(b) = self.b.as_mut() {
695 let x = b.next();
696 if x.is_none() {
697 self.b = None;
698 } else {
699 return x;
700 }
701 }
702 None
703 }
704
705 #[inline]
706 fn fold<Acc, F>(self, mut acc: Acc, mut f: F) -> Acc
707 where
708 F: FnMut(Acc, Self::Item) -> Acc,
709 {
710 if let Some(a) = self.a {
711 acc = a.fold(acc, &mut f);
712 }
713 if let Some(b) = self.b {
714 acc = b.fold(acc, f);
715 }
716 acc
717 }
718 }
719
720 #[inline(always)]
724 pub fn chain_one<A: IntoIterator>(a: A, b: A::Item) -> ChainOne<A::IntoIter> {
725 ChainOne { a: Some(a.into_iter()), b: Some(b) }
726 }
727
728 pub struct ChainOne<A: Iterator> {
729 a: Option<A>,
730 b: Option<A::Item>,
731 }
732
733 impl<A: Iterator> Iterator for ChainOne<A> {
734 type Item = A::Item;
735
736 #[inline(always)]
737 fn next(&mut self) -> Option<Self::Item> {
738 if let Some(a) = self.a.as_mut() {
739 let x = a.next();
740 if x.is_none() {
741 self.a = None;
742 self.b.take()
743 } else {
744 x
745 }
746 } else {
747 None
748 }
749 }
750 }
751
752 #[cfg(test)]
753 mod test {
754 use super::*;
755
756 #[test]
757 fn test_chain() {
758 let a = [1, 2, 3];
759 let b = [4, 5, 6];
760 let mut chain = chain(a, b);
761 assert_eq!(chain.next(), Some(1));
762 assert_eq!(chain.next(), Some(2));
763 assert_eq!(chain.next(), Some(3));
764 assert_eq!(chain.next(), Some(4));
765 assert_eq!(chain.next(), Some(5));
766 assert_eq!(chain.next(), Some(6));
767 assert_eq!(chain.next(), None);
768 }
769
770 #[test]
771 fn test_chain_one() {
772 let a = [1, 2, 3];
773 let b = 4;
774 let mut chain = chain_one(a, b);
775 assert_eq!(chain.next(), Some(1));
776 assert_eq!(chain.next(), Some(2));
777 assert_eq!(chain.next(), Some(3));
778 assert_eq!(chain.next(), Some(4));
779 assert_eq!(chain.next(), None);
780 }
781 }
782}