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 const SLICE_COUNT: usize = BC::SLICE_COUNT + TC::SLICE_COUNT;
124 #[inline]
125 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
126 debug_assert!(index < Self::SLICE_COUNT);
127 if index < BC::SLICE_COUNT {
128 self.bounds.get_byte_slice(index)
129 } else {
130 self.values.get_byte_slice(index - BC::SLICE_COUNT)
131 }
132 }
133}
134impl<'a, TC: crate::FromBytes<'a>, BC: crate::FromBytes<'a>> crate::FromBytes<'a> for Vecs<TC, BC> {
135 const SLICE_COUNT: usize = BC::SLICE_COUNT + TC::SLICE_COUNT;
136 #[inline(always)]
137 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
138 Self {
139 bounds: crate::FromBytes::from_bytes(bytes),
140 values: crate::FromBytes::from_bytes(bytes),
141 }
142 }
143 #[inline(always)]
144 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
145 Self {
146 bounds: BC::from_store(store, offset),
147 values: TC::from_store(store, offset),
148 }
149 }
150 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
151 BC::element_sizes(sizes)?;
152 TC::element_sizes(sizes)?;
153 Ok(())
154 }
155}
156
157impl<TC: Len> Vecs<TC> {
158 #[inline]
159 pub fn push_iter<I>(&mut self, iter: I) where I: IntoIterator, TC: Push<I::Item> {
160 self.values.extend(iter);
161 self.bounds.push(self.values.len() as u64);
162 }
163}
164
165impl<TC, BC: Len> Len for Vecs<TC, BC> {
166 #[inline(always)] fn len(&self) -> usize { self.bounds.len() }
167}
168
169impl<TC: Copy, BC: Len+IndexAs<u64>> Index for Vecs<TC, BC> {
170 type Ref = Slice<TC>;
171 #[inline(always)]
172 fn get(&self, index: usize) -> Self::Ref {
173 let lower = if index == 0 { 0 } else { self.bounds.index_as(index - 1) };
174 let upper = self.bounds.index_as(index);
175 Slice::new(lower, upper, self.values)
176 }
177}
178impl<'a, TC, BC: Len+IndexAs<u64>> Index for &'a Vecs<TC, BC> {
179 type Ref = Slice<&'a TC>;
180 #[inline(always)]
181 fn get(&self, index: usize) -> Self::Ref {
182 let lower = if index == 0 { 0 } else { self.bounds.index_as(index - 1) };
183 let upper = self.bounds.index_as(index);
184 Slice::new(lower, upper, &self.values)
185 }
186}
187impl<TC, BC: Len+IndexAs<u64>> IndexMut for Vecs<TC, BC> {
188 type IndexMut<'a> = Slice<&'a mut TC> where TC: 'a, BC: 'a;
189
190 #[inline(always)]
191 fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> {
192 let lower = if index == 0 { 0 } else { self.bounds.index_as(index - 1) };
193 let upper = self.bounds.index_as(index);
194 Slice::new(lower, upper, &mut self.values)
195 }
196}
197
198impl<'a, TC: Container, BC: for<'b> Push<&'b u64>> Push<Slice<TC::Borrowed<'a>>> for Vecs<TC, BC> {
199 #[inline]
200 fn push(&mut self, item: Slice<TC::Borrowed<'a>>) {
201 self.values.extend_from_self(item.slice, item.lower .. item.upper);
202 self.bounds.push(&(self.values.len() as u64));
203 }
204}
205
206impl<I: IntoIterator, TC: Push<I::Item> + Len, BC: for<'a> Push<&'a u64>> Push<I> for Vecs<TC, BC> {
207 #[inline]
208 fn push(&mut self, item: I) {
209 self.values.extend(item);
210 self.bounds.push(&(self.values.len() as u64));
211 }
212}
213
214impl<TC: Clear, BC: Clear> Clear for Vecs<TC, BC> {
215 #[inline(always)]
216 fn clear(&mut self) {
217 self.bounds.clear();
218 self.values.clear();
219 }
220}
221
222