1use super::{Clear, Columnar, Container, Len, IndexMut, Index, IndexAs, Push, HeapSize, Slice, Borrow};
2
3#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5#[derive(Debug, Default, Copy, Clone, PartialEq)]
6pub struct Vecs<TC, BC = Vec<u64>> {
7 pub bounds: BC,
8 pub values: TC,
9}
10
11impl<T: Columnar> Columnar for Vec<T> {
12 #[inline(always)]
13 fn copy_from<'a>(&mut self, other: crate::Ref<'a, Self>) {
14 self.truncate(other.len());
15 let mut other_iter = other.into_iter();
16 for (s, o) in self.iter_mut().zip(&mut other_iter) {
17 T::copy_from(s, o);
18 }
19 for o in other_iter {
20 self.push(T::into_owned(o));
21 }
22 }
23 #[inline(always)]
24 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self {
25 other.into_iter().map(|x| T::into_owned(x)).collect()
26 }
27 type Container = Vecs<T::Container>;
28}
29
30impl<T: Columnar, const N: usize> Columnar for [T; N] {
31 #[inline(always)]
32 fn copy_from<'a>(&mut self, other: crate::Ref<'a, Self>) {
33 for (s, o) in self.iter_mut().zip(other.into_iter()) {
34 T::copy_from(s, o);
35 }
36 }
37 #[inline(always)]
38 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self {
39 let vec: Vec<_> = other.into_iter().map(|x| T::into_owned(x)).collect();
40 match vec.try_into() {
41 Ok(array) => array,
42 Err(_) => panic!("wrong length"),
43 }
44 }
45 type Container = Vecs<T::Container>;
46}
47
48impl<T: Columnar, const N: usize> Columnar for smallvec::SmallVec<[T; N]> {
49 #[inline(always)]
50 fn copy_from<'a>(&mut self, other: crate::Ref<'a, Self>) {
51 self.truncate(other.len());
52 let mut other_iter = other.into_iter();
53 for (s, o) in self.iter_mut().zip(&mut other_iter) {
54 T::copy_from(s, o);
55 }
56 for o in other_iter {
57 self.push(T::into_owned(o));
58 }
59 }
60 #[inline(always)]
61 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self {
62 other.into_iter().map(|x| T::into_owned(x)).collect()
63 }
64 type Container = Vecs<T::Container>;
65}
66
67impl<BC: crate::common::BorrowIndexAs<u64>, TC: Container> Borrow for Vecs<TC, BC> {
68 type Ref<'a> = Slice<TC::Borrowed<'a>> where TC: 'a;
69 type Borrowed<'a> = Vecs<TC::Borrowed<'a>, BC::Borrowed<'a>> where BC: 'a, TC: 'a;
70 #[inline(always)]
71 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
72 Vecs {
73 bounds: self.bounds.borrow(),
74 values: self.values.borrow(),
75 }
76 }
77 #[inline(always)]
78 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where BC: 'a, TC: 'a {
79 Vecs {
80 bounds: BC::reborrow(thing.bounds),
81 values: TC::reborrow(thing.values),
82 }
83 }
84 #[inline(always)]
85 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a {
86 thing.map(|x| TC::reborrow(x))
87 }
88}
89
90impl<BC: crate::common::PushIndexAs<u64>, TC: Container> Container for Vecs<TC, BC> {
91 #[inline(always)]
92 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) {
93 if !range.is_empty() {
94 let values_len = self.values.len() as u64;
96
97 let other_lower = if range.start == 0 { 0 } else { other.bounds.index_as(range.start-1) };
99 let other_upper = other.bounds.index_as(range.end-1);
100 self.values.extend_from_self(other.values, other_lower as usize .. other_upper as usize);
101
102 if values_len == other_lower {
104 self.bounds.extend_from_self(other.bounds, range);
105 }
106 else {
107 for index in range {
108 let shifted = other.bounds.index_as(index) - other_lower + values_len;
109 self.bounds.push(&shifted)
110 }
111 }
112 }
113 }
114
115 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
116 self.bounds.reserve_for(selves.clone().map(|x| x.bounds));
117 self.values.reserve_for(selves.map(|x| x.values));
118 }
119}
120
121impl<'a, TC: crate::AsBytes<'a>, BC: crate::AsBytes<'a>> crate::AsBytes<'a> for Vecs<TC, BC> {
122 #[inline(always)]
123 fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> {
124 crate::chain(self.bounds.as_bytes(), self.values.as_bytes())
125 }
126}
127impl<'a, TC: crate::FromBytes<'a>, BC: crate::FromBytes<'a>> crate::FromBytes<'a> for Vecs<TC, BC> {
128 #[inline(always)]
129 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
130 Self {
131 bounds: crate::FromBytes::from_bytes(bytes),
132 values: crate::FromBytes::from_bytes(bytes),
133 }
134 }
135}
136
137impl<TC: Len> Vecs<TC> {
138 #[inline]
139 pub fn push_iter<I>(&mut self, iter: I) where I: IntoIterator, TC: Push<I::Item> {
140 self.values.extend(iter);
141 self.bounds.push(self.values.len() as u64);
142 }
143}
144
145impl<TC, BC: Len> Len for Vecs<TC, BC> {
146 #[inline(always)] fn len(&self) -> usize { self.bounds.len() }
147}
148
149impl<TC: Copy, BC: Len+IndexAs<u64>> Index for Vecs<TC, BC> {
150 type Ref = Slice<TC>;
151 #[inline(always)]
152 fn get(&self, index: usize) -> Self::Ref {
153 let lower = if index == 0 { 0 } else { self.bounds.index_as(index - 1) };
154 let upper = self.bounds.index_as(index);
155 Slice::new(lower, upper, self.values)
156 }
157}
158impl<'a, TC, BC: Len+IndexAs<u64>> Index for &'a Vecs<TC, BC> {
159 type Ref = Slice<&'a TC>;
160 #[inline(always)]
161 fn get(&self, index: usize) -> Self::Ref {
162 let lower = if index == 0 { 0 } else { self.bounds.index_as(index - 1) };
163 let upper = self.bounds.index_as(index);
164 Slice::new(lower, upper, &self.values)
165 }
166}
167impl<TC, BC: Len+IndexAs<u64>> IndexMut for Vecs<TC, BC> {
168 type IndexMut<'a> = Slice<&'a mut TC> where TC: 'a, BC: 'a;
169
170 #[inline(always)]
171 fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> {
172 let lower = if index == 0 { 0 } else { self.bounds.index_as(index - 1) };
173 let upper = self.bounds.index_as(index);
174 Slice::new(lower, upper, &mut self.values)
175 }
176}
177
178impl<'a, TC: Container, BC: for<'b> Push<&'b u64>> Push<Slice<TC::Borrowed<'a>>> for Vecs<TC, BC> {
179 #[inline]
180 fn push(&mut self, item: Slice<TC::Borrowed<'a>>) {
181 self.values.extend_from_self(item.slice, item.lower .. item.upper);
182 self.bounds.push(&(self.values.len() as u64));
183 }
184}
185
186impl<I: IntoIterator, TC: Push<I::Item> + Len, BC: for<'a> Push<&'a u64>> Push<I> for Vecs<TC, BC> {
187 #[inline]
188 fn push(&mut self, item: I) {
189 self.values.extend(item);
190 self.bounds.push(&(self.values.len() as u64));
191 }
192}
193
194impl<TC: Clear, BC: Clear> Clear for Vecs<TC, BC> {
195 #[inline(always)]
196 fn clear(&mut self) {
197 self.bounds.clear();
198 self.values.clear();
199 }
200}
201
202impl<TC: HeapSize, BC: HeapSize> HeapSize for Vecs<TC, BC> {
203 fn heap_size(&self) -> (usize, usize) {
204 let (l0, c0) = self.bounds.heap_size();
205 let (l1, c1) = self.values.heap_size();
206 (l0 + l1, c0 + c1)
207 }
208}