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