1use alloc::{vec::Vec, string::String};
3
4use core::num::Wrapping;
5
6macro_rules! implement_columnable {
8 ($($index_type:ty),*) => { $(
9 impl crate::Columnar for $index_type {
10 #[inline(always)]
11 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { *other }
12
13 type Container = Vec<$index_type>;
14 }
15
16 impl<'a> crate::AsBytes<'a> for &'a [$index_type] {
17 const SLICE_COUNT: usize = 1;
18 #[inline]
19 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
20 debug_assert!(index < Self::SLICE_COUNT);
21 (core::mem::align_of::<$index_type>() as u64, bytemuck::cast_slice(&self[..]))
22 }
23 }
24 impl<'a> crate::FromBytes<'a> for &'a [$index_type] {
25 const SLICE_COUNT: usize = 1;
26 #[inline(always)]
27 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
28 bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap()
30 }
31 #[inline(always)]
32 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
33 let (w, tail) = store.get(*offset);
34 *offset += 1;
35 let all: &[$index_type] = bytemuck::cast_slice(w);
36 let trim = ((8 - tail as usize) % 8) / core::mem::size_of::<$index_type>();
37 debug_assert!(trim <= all.len(), "from_store: trim {trim} exceeds slice length {}", all.len());
38 all.get(..all.len().wrapping_sub(trim)).unwrap_or(&[])
39 }
40 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
41 sizes.push(core::mem::size_of::<$index_type>());
42 Ok(())
43 }
44 }
45 impl<'a, const N: usize> crate::AsBytes<'a> for &'a [[$index_type; N]] {
46 const SLICE_COUNT: usize = 1;
47 #[inline]
48 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
49 debug_assert!(index < Self::SLICE_COUNT);
50 (core::mem::align_of::<$index_type>() as u64, bytemuck::cast_slice(&self[..]))
51 }
52 }
53 impl<'a, const N: usize> crate::FromBytes<'a> for &'a [[$index_type; N]] {
54 const SLICE_COUNT: usize = 1;
55 #[inline(always)]
56 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
57 bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap()
59 }
60 #[inline(always)]
61 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
62 let (w, tail) = store.get(*offset);
63 *offset += 1;
64 let all: &[[$index_type; N]] = bytemuck::cast_slice(w);
65 let trim = ((8 - tail as usize) % 8) / (core::mem::size_of::<$index_type>() * N);
66 debug_assert!(trim <= all.len(), "from_store: trim {trim} exceeds slice length {}", all.len());
67 all.get(..all.len().wrapping_sub(trim)).unwrap_or(&[])
68 }
69 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
70 sizes.push(core::mem::size_of::<$index_type>() * N);
71 Ok(())
72 }
73 }
74 )* }
75}
76
77implement_columnable!(u8, u16, u32, u64);
78implement_columnable!(i8, i16, i32, i64);
79implement_columnable!(f32, f64);
80implement_columnable!(Wrapping<u8>, Wrapping<u16>, Wrapping<u32>, Wrapping<u64>);
81implement_columnable!(Wrapping<i8>, Wrapping<i16>, Wrapping<i32>, Wrapping<i64>);
82
83pub use sizes::{Usizes, Isizes};
84mod sizes {
86
87 use crate::*;
88 use crate::common::{BorrowIndexAs, PushIndexAs};
89
90 #[derive(Copy, Clone, Default)]
91 pub struct Usizes<CV = Vec<u64>> { pub values: CV }
92
93 impl Columnar for usize {
94 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
95 type Container = Usizes;
96 }
97
98 impl<CV: BorrowIndexAs<u64> + Len> Borrow for Usizes<CV> {
99 type Ref<'a> = usize;
100 type Borrowed<'a> = Usizes<CV::Borrowed<'a>> where CV: 'a;
101 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
102 Usizes { values: self.values.borrow() }
103 }
104 #[inline(always)]
105 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where CV: 'a {
106 Usizes { values: CV::reborrow(thing.values) }
107 }
108 #[inline(always)]
109 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
110 }
111
112 impl<CV: PushIndexAs<u64>> Container for Usizes<CV> {
113 #[inline(always)]
114 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
115 self.values.extend_from_self(other.values, range)
116 }
117
118 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
119 self.values.reserve_for(selves.map(|x| x.values))
120 }
121 }
122
123 impl<CV: Len> Len for Usizes<CV> { fn len(&self) -> usize { self.values.len() }}
124 impl IndexMut for Usizes {
125 type IndexMut<'a> = &'a mut u64;
126 #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> { &mut self.values[index] }
127 }
128 impl<CV: IndexAs<u64>> Index for Usizes<CV> {
129 type Ref = usize;
130 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { self.values.index_as(index).try_into().expect("Usizes values should fit in `usize`") }
131 }
132 impl<CV: IndexAs<u64>> Index for &Usizes<CV> {
133 type Ref = usize;
134 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { self.values.index_as(index).try_into().expect("Usizes values should fit in `usize`") }
135 }
136 impl<CV: for<'a> Push<&'a u64>> Push<usize> for Usizes<CV> {
137 #[inline]
138 fn push(&mut self, item: usize) { self.values.push(&item.try_into().expect("usize must fit in a u64")) }
139 }
140 impl Push<&usize> for Usizes {
141 #[inline]
142 fn push(&mut self, item: &usize) { self.values.push((*item).try_into().expect("usize must fit in a u64")) }
143 }
144 impl<CV: Clear> Clear for Usizes<CV> { fn clear(&mut self) { self.values.clear() }}
145
146 impl<'a, CV: crate::AsBytes<'a>> crate::AsBytes<'a> for crate::primitive::Usizes<CV> {
147 const SLICE_COUNT: usize = CV::SLICE_COUNT;
148 #[inline]
149 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
150 self.values.get_byte_slice(index)
151 }
152 }
153
154 impl<'a, CV: crate::FromBytes<'a>> crate::FromBytes<'a> for crate::primitive::Usizes<CV> {
155 const SLICE_COUNT: usize = CV::SLICE_COUNT;
156 #[inline(always)]
157 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
158 Self { values: CV::from_bytes(bytes) }
159 }
160 #[inline(always)]
161 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
162 Self { values: CV::from_store(store, offset) }
163 }
164 }
165
166
167 #[derive(Copy, Clone, Default)]
168 pub struct Isizes<CV = Vec<i64>> { pub values: CV }
169
170 impl Columnar for isize {
171 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
172 type Container = Isizes;
173 }
174
175 impl<CV: BorrowIndexAs<i64>> Borrow for Isizes<CV> {
176 type Ref<'a> = isize;
177 type Borrowed<'a> = Isizes<CV::Borrowed<'a>> where CV: 'a;
178 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
179 Isizes { values: self.values.borrow() }
180 }
181 #[inline(always)]
182 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where CV: 'a {
183 Isizes { values: CV::reborrow(thing.values) }
184 }
185 #[inline(always)]
186 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
187 }
188
189 impl<CV: PushIndexAs<i64>> Container for Isizes<CV> {
190 #[inline(always)]
191 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
192 self.values.extend_from_self(other.values, range)
193 }
194
195 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
196 self.values.reserve_for(selves.map(|x| x.values))
197 }
198 }
199
200 impl<CV: Len> Len for Isizes<CV> { fn len(&self) -> usize { self.values.len() }}
201 impl IndexMut for Isizes {
202 type IndexMut<'a> = &'a mut i64;
203 #[inline(always)] fn get_mut(&mut self, index: usize) -> Self::IndexMut<'_> { &mut self.values[index] }
204 }
205 impl<CV: IndexAs<i64>> Index for Isizes<CV> {
206 type Ref = isize;
207 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { self.values.index_as(index).try_into().expect("Isizes values should fit in `isize`") }
208 }
209 impl<CV: IndexAs<i64>> Index for &Isizes<CV> {
210 type Ref = isize;
211 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { self.values.index_as(index).try_into().expect("Isizes values should fit in `isize`") }
212 }
213 impl<CV: for<'a> Push<&'a i64>> Push<isize> for Isizes<CV> {
214 #[inline]
215 fn push(&mut self, item: isize) { self.values.push(&item.try_into().expect("isize must fit in a i64")) }
216 }
217 impl Push<&isize> for Isizes {
218 #[inline]
219 fn push(&mut self, item: &isize) { self.values.push((*item).try_into().expect("isize must fit in a i64")) }
220 }
221 impl<CV: Clear> Clear for Isizes<CV> { fn clear(&mut self) { self.values.clear() }}
222
223 impl<'a, CV: crate::AsBytes<'a>> crate::AsBytes<'a> for crate::primitive::Isizes<CV> {
224 const SLICE_COUNT: usize = CV::SLICE_COUNT;
225 #[inline]
226 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
227 self.values.get_byte_slice(index)
228 }
229 }
230
231 impl<'a, CV: crate::FromBytes<'a>> crate::FromBytes<'a> for crate::primitive::Isizes<CV> {
232 const SLICE_COUNT: usize = CV::SLICE_COUNT;
233 #[inline(always)]
234 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
235 Self { values: CV::from_bytes(bytes) }
236 }
237 #[inline(always)]
238 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
239 Self { values: CV::from_store(store, offset) }
240 }
241 }
242}
243
244pub use chars::{Chars};
245mod chars {
247
248 use crate::*;
249 use crate::common::{BorrowIndexAs, PushIndexAs};
250
251 type Encoded = u32;
252
253 #[derive(Copy, Clone, Default)]
254 pub struct Chars<CV = Vec<Encoded>> { pub values: CV }
255
256 impl Columnar for char {
257 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
258 type Container = Chars;
259 }
260
261 impl<CV: BorrowIndexAs<Encoded>> Borrow for Chars<CV> {
262 type Ref<'a> = char;
263 type Borrowed<'a> = Chars<CV::Borrowed<'a>> where CV: 'a;
264 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
265 Chars { values: self.values.borrow() }
266 }
267 #[inline(always)]
268 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where CV: 'a {
269 Chars { values: CV::reborrow(thing.values) }
270 }
271 #[inline(always)]
272 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
273 }
274
275 impl<CV: PushIndexAs<Encoded>> Container for Chars<CV> {
276 #[inline(always)]
277 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
278 self.values.extend_from_self(other.values, range)
279 }
280
281 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
282 self.values.reserve_for(selves.map(|x| x.values))
283 }
284 }
285
286 impl<CV: Len> Len for Chars<CV> { fn len(&self) -> usize { self.values.len() }}
287 impl<CV: IndexAs<Encoded>> Index for Chars<CV> {
288 type Ref = char;
289 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { char::from_u32(self.values.index_as(index)).unwrap() }
290 }
291 impl<CV: IndexAs<Encoded>> Index for &Chars<CV> {
292 type Ref = char;
293 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { char::from_u32(self.values.index_as(index)).unwrap() }
294 }
295 impl<CV: for<'a> Push<&'a Encoded>> Push<char> for Chars<CV> {
296 #[inline]
297 fn push(&mut self, item: char) { self.values.push(&u32::from(item)) }
298 }
299 impl Push<&char> for Chars {
300 #[inline]
301 fn push(&mut self, item: &char) { self.values.push(u32::from(*item)) }
302 }
303 impl<CV: Clear> Clear for Chars<CV> { fn clear(&mut self) { self.values.clear() }}
304
305 impl<'a, CV: crate::AsBytes<'a>> crate::AsBytes<'a> for Chars<CV> {
306 const SLICE_COUNT: usize = CV::SLICE_COUNT;
307 #[inline]
308 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
309 self.values.get_byte_slice(index)
310 }
311 }
312
313 impl<'a, CV: crate::FromBytes<'a>> crate::FromBytes<'a> for Chars<CV> {
314 const SLICE_COUNT: usize = CV::SLICE_COUNT;
315 #[inline(always)]
316 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
317 Self { values: CV::from_bytes(bytes) }
318 }
319 #[inline(always)]
320 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
321 Self { values: CV::from_store(store, offset) }
322 }
323 }
324}
325
326pub use larges::{U128s, I128s};
327mod larges {
329
330 use crate::*;
331 use crate::common::{BorrowIndexAs, PushIndexAs};
332
333 type Encoded = [u8; 16];
334
335 #[derive(Copy, Clone, Default)]
336 pub struct U128s<CV = Vec<Encoded>> { pub values: CV }
337
338 impl Columnar for u128 {
339 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
340 type Container = U128s;
341 }
342
343 impl<CV: BorrowIndexAs<Encoded>> Borrow for U128s<CV> {
344 type Ref<'a> = u128;
345 type Borrowed<'a> = U128s<CV::Borrowed<'a>> where CV: 'a;
346 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
347 U128s { values: self.values.borrow() }
348 }
349 #[inline(always)]
350 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where CV: 'a {
351 U128s { values: CV::reborrow(thing.values) }
352 }
353 #[inline(always)]
354 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
355 }
356
357 impl<CV: PushIndexAs<Encoded>> Container for U128s<CV> {
358 #[inline(always)]
359 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
360 self.values.extend_from_self(other.values, range)
361 }
362
363 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
364 self.values.reserve_for(selves.map(|x| x.values))
365 }
366 }
367
368 impl<CV: Len> Len for U128s<CV> { fn len(&self) -> usize { self.values.len() }}
369 impl<CV: IndexAs<Encoded>> Index for U128s<CV> {
370 type Ref = u128;
371 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { u128::from_le_bytes(self.values.index_as(index)) }
372 }
373 impl<CV: IndexAs<Encoded>> Index for &U128s<CV> {
374 type Ref = u128;
375 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { u128::from_le_bytes(self.values.index_as(index)) }
376 }
377 impl<CV: for<'a> Push<&'a Encoded>> Push<u128> for U128s<CV> {
378 #[inline]
379 fn push(&mut self, item: u128) { self.values.push(&item.to_le_bytes()) }
380 }
381 impl Push<&u128> for U128s {
382 #[inline]
383 fn push(&mut self, item: &u128) { self.values.push(item.to_le_bytes()) }
384 }
385 impl<CV: Clear> Clear for U128s<CV> { fn clear(&mut self) { self.values.clear() }}
386
387 impl<'a, CV: crate::AsBytes<'a>> crate::AsBytes<'a> for U128s<CV> {
388 const SLICE_COUNT: usize = CV::SLICE_COUNT;
389 #[inline]
390 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
391 self.values.get_byte_slice(index)
392 }
393 }
394
395 impl<'a, CV: crate::FromBytes<'a>> crate::FromBytes<'a> for U128s<CV> {
396 const SLICE_COUNT: usize = CV::SLICE_COUNT;
397 #[inline(always)]
398 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
399 Self { values: CV::from_bytes(bytes) }
400 }
401 #[inline(always)]
402 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
403 Self { values: CV::from_store(store, offset) }
404 }
405 }
406
407 #[derive(Copy, Clone, Default)]
408 pub struct I128s<CV = Vec<Encoded>> { pub values: CV }
409
410 impl Columnar for i128 {
411 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
412 type Container = I128s;
413 }
414
415 impl<CV: BorrowIndexAs<Encoded>> Borrow for I128s<CV> {
416 type Ref<'a> = i128;
417 type Borrowed<'a> = I128s<CV::Borrowed<'a>> where CV: 'a;
418 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
419 I128s { values: self.values.borrow() }
420 }
421 #[inline(always)]
422 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where CV: 'a {
423 I128s { values: CV::reborrow(thing.values) }
424 }
425 #[inline(always)]
426 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
427 }
428
429 impl<CV: PushIndexAs<Encoded>> Container for I128s<CV> {
430 #[inline(always)]
431 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
432 self.values.extend_from_self(other.values, range)
433 }
434
435 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
436 self.values.reserve_for(selves.map(|x| x.values))
437 }
438 }
439
440 impl<CV: Len> Len for I128s<CV> { fn len(&self) -> usize { self.values.len() }}
441 impl<CV: IndexAs<Encoded>> Index for I128s<CV> {
442 type Ref = i128;
443 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { i128::from_le_bytes(self.values.index_as(index)) }
444 }
445 impl<CV: IndexAs<Encoded>> Index for &I128s<CV> {
446 type Ref = i128;
447 #[inline(always)] fn get(&self, index: usize) -> Self::Ref { i128::from_le_bytes(self.values.index_as(index)) }
448 }
449 impl<CV: for<'a> Push<&'a Encoded>> Push<i128> for I128s<CV> {
450 #[inline]
451 fn push(&mut self, item: i128) { self.values.push(&item.to_le_bytes()) }
452 }
453 impl Push<&i128> for I128s {
454 #[inline]
455 fn push(&mut self, item: &i128) { self.values.push(item.to_le_bytes()) }
456 }
457 impl<CV: Clear> Clear for I128s<CV> { fn clear(&mut self) { self.values.clear() }}
458
459 impl<'a, CV: crate::AsBytes<'a>> crate::AsBytes<'a> for I128s<CV> {
460 const SLICE_COUNT: usize = CV::SLICE_COUNT;
461 #[inline]
462 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
463 self.values.get_byte_slice(index)
464 }
465 }
466
467 impl<'a, CV: crate::FromBytes<'a>> crate::FromBytes<'a> for I128s<CV> {
468 const SLICE_COUNT: usize = CV::SLICE_COUNT;
469 #[inline(always)]
470 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
471 Self { values: CV::from_bytes(bytes) }
472 }
473 #[inline(always)]
474 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
475 Self { values: CV::from_store(store, offset) }
476 }
477 }
478}
479
480pub mod offsets {
486
487
488 pub use array::Fixeds;
489 pub use stride::Strides;
490
491 mod array {
498
499 use alloc::{vec::Vec, string::String};
500 use crate::{Container, Borrow, Index, Len, Push};
501 use crate::common::index::CopyAs;
502
503 #[derive(Copy, Clone, Debug, Default)]
505 pub struct Fixeds<const K: u64, CC = u64> { pub count: CC }
506
507 impl<const K: u64> Borrow for Fixeds<K> {
508 type Ref<'a> = u64;
509 type Borrowed<'a> = Fixeds<K, &'a u64>;
510 #[inline(always)]
511 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { Fixeds { count: &self.count } }
512 #[inline(always)]
513 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a {
514 Fixeds { count: thing.count }
515 }
516 #[inline(always)]
517 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
518 }
519
520 impl<const K: u64> Container for Fixeds<K> {
521 #[inline(always)]
522 fn extend_from_self(&mut self, _other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
523 self.count += range.len() as u64;
524 }
525
526 fn reserve_for<'a, I>(&mut self, _selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone { }
527 }
528
529 impl<const K: u64, CC: CopyAs<u64>> Len for Fixeds<K, CC> {
530 #[inline(always)] fn len(&self) -> usize { self.count.copy_as() as usize }
531 }
532
533 impl<const K: u64, CC> Index for Fixeds<K, CC> {
534 type Ref = u64;
535 #[inline(always)]
536 fn get(&self, index: usize) -> Self::Ref { (index as u64 + 1) * K }
537 }
538 impl<'a, const K: u64, CC> Index for &'a Fixeds<K, CC> {
539 type Ref = u64;
540 #[inline(always)]
541 fn get(&self, index: usize) -> Self::Ref { (index as u64 + 1) * K }
542 }
543
544 impl<'a, const K: u64, T> Push<T> for Fixeds<K> {
545 #[inline(always)]
547 fn push(&mut self, _item: T) { self.count += 1; }
548 #[inline(always)]
549 fn extend(&mut self, iter: impl IntoIterator<Item=T>) {
550 self.count += iter.into_iter().count() as u64;
551 }
552 }
553
554 impl<const K: u64> crate::Clear for Fixeds<K> {
555 #[inline(always)]
556 fn clear(&mut self) { self.count = 0; }
557 }
558
559 impl<'a, const K: u64> crate::AsBytes<'a> for Fixeds<K, &'a u64> {
560 const SLICE_COUNT: usize = 1;
561 #[inline]
562 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
563 debug_assert!(index < Self::SLICE_COUNT);
564 (8, bytemuck::cast_slice(core::slice::from_ref(self.count)))
565 }
566 }
567 impl<'a, const K: u64> crate::FromBytes<'a> for Fixeds<K, &'a u64> {
568 const SLICE_COUNT: usize = 1;
569 #[inline(always)]
570 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
571 Self { count: &bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap()[0] }
572 }
573 #[inline(always)]
574 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
575 let (w, _) = store.get(*offset); *offset += 1;
576 debug_assert!(!w.is_empty(), "Fixeds::from_store: empty count slice");
577 Self { count: w.first().unwrap_or(&0) }
578 }
579 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
580 sizes.push(8);
581 Ok(())
582 }
583 fn validate(slices: &[(&[u64], u8)]) -> Result<(), String> {
584 if slices.is_empty() || slices[0].0.is_empty() {
585 return Err("Fixeds: count slice must be non-empty".into());
586 }
587 Ok(())
588 }
589 }
590
591 use super::Strides;
592 impl<const K: u64> core::convert::TryFrom<Strides> for Fixeds<K> {
593 type Error = Strides;
594 fn try_from(item: Strides) -> Result<Self, Self::Error> {
595 if item.strided() == Some(K) { Ok( Self { count: item.head[1] } ) } else { Err(item) }
596 }
597 }
598 impl<'a, const K: u64> core::convert::TryFrom<Strides<&'a [u64], &'a [u64]>> for Fixeds<K, &'a u64> {
599 type Error = Strides<&'a [u64], &'a [u64]>;
600 fn try_from(item: Strides<&'a [u64], &'a [u64]>) -> Result<Self, Self::Error> {
601 if item.strided() == Some(K) { Ok( Self { count: &item.head[1] } ) } else { Err(item) }
602 }
603 }
604 }
605
606 mod stride {
614
615 use alloc::{vec::Vec, string::String};
616 use core::ops::Deref;
617 use crate::{Container, Borrow, Index, IndexAs, Len, Push, Clear, AsBytes, FromBytes};
618
619 #[derive(Copy, Clone, Debug, Default)]
626 pub struct Strides<BC = Vec<u64>, HC = [u64; 2]> {
627 pub head: HC,
628 pub bounds: BC,
629 }
630
631 impl Borrow for Strides {
632 type Ref<'a> = u64;
633 type Borrowed<'a> = Strides<&'a [u64], &'a [u64]>;
634
635 #[inline(always)] fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { Strides { head: &self.head, bounds: &self.bounds[..] } }
636 #[inline(always)] fn reborrow<'b, 'a: 'b>(item: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a {
637 Strides { head: item.head, bounds: item.bounds }
638 }
639 #[inline(always)] fn reborrow_ref<'b, 'a: 'b>(item: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { item }
640 }
641
642 impl Container for Strides {
643 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
644 self.bounds.reserve_for(selves.map(|x| x.bounds))
645 }
646 }
647
648 impl<'a> Push<&'a u64> for Strides { #[inline(always)] fn push(&mut self, item: &'a u64) { self.push(*item) } }
649 impl Push<u64> for Strides { #[inline(always)] fn push(&mut self, item: u64) { self.push(item) } }
650 impl Clear for Strides { #[inline(always)] fn clear(&mut self) { self.clear() } }
651
652 impl<BC: Len, HC: IndexAs<u64>> Len for Strides<BC, HC> {
653 #[inline(always)]
654 fn len(&self) -> usize { self.head.index_as(1) as usize + self.bounds.len() }
655 }
656 impl<BC: IndexAs<u64>, HC: IndexAs<u64>> Index for Strides<BC, HC> {
657 type Ref = u64;
658 #[inline(always)]
659 fn get(&self, index: usize) -> Self::Ref {
660 let index = index as u64;
661 let length = self.head.index_as(1);
662 let stride = self.head.index_as(0);
663 if index < length { (index+1) * stride } else { self.bounds.index_as((index - length) as usize) }
664 }
665 }
666
667 impl<'a, BC: AsBytes<'a>> AsBytes<'a> for Strides<BC, &'a [u64]> {
668 const SLICE_COUNT: usize = 1 + BC::SLICE_COUNT;
669 #[inline]
670 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
671 debug_assert!(index < Self::SLICE_COUNT);
672 if index < 1 {
673 (8u64, bytemuck::cast_slice(self.head))
674 } else {
675 self.bounds.get_byte_slice(index - 1)
676 }
677 }
678 }
679 impl<'a, BC: FromBytes<'a>> FromBytes<'a> for Strides<BC, &'a [u64]> {
680 const SLICE_COUNT: usize = 1 + BC::SLICE_COUNT;
681 #[inline(always)]
682 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
683 let head: &[u64] = bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap();
684 let bounds = BC::from_bytes(bytes);
685 Self { head, bounds }
686 }
687 #[inline(always)]
688 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
689 let (head, _) = store.get(*offset); *offset += 1;
690 debug_assert!(head.len() >= 2, "Strides::from_store: head slice too short (len {})", head.len());
691 let bounds = BC::from_store(store, offset);
692 Self { head, bounds }
693 }
694 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
695 sizes.push(8); BC::element_sizes(sizes)
697 }
698 fn validate(slices: &[(&[u64], u8)]) -> Result<(), String> {
699 if slices.is_empty() || slices[0].0.len() < 2 {
700 return Err("Strides: head slice must have at least 2 elements (stride, length)".into());
701 }
702 BC::validate(&slices[1..])
703 }
704 }
705
706 impl Strides {
707 pub fn new(stride: u64, length: u64) -> Self {
708 Self { head: [stride, length], bounds: Vec::default() }
709 }
710 #[inline(always)]
711 pub fn push(&mut self, item: u64) {
712 if self.head[1] == 0 {
713 self.head[0] = item;
714 self.head[1] = 1;
715 }
716 else if !self.bounds.is_empty() {
717 self.bounds.push(item);
718 }
719 else if item == self.head[0] * (self.head[1] + 1) {
720 self.head[1] += 1;
721 }
722 else {
723 self.bounds.push(item);
724 }
725 }
726 #[inline(always)]
730 pub fn pop(&mut self) {
731 debug_assert!(self.len() > 0);
732 if self.bounds.is_empty() { self.head[1] -= 1; }
733 else { self.bounds.pop(); }
734 }
735 #[inline(always)]
736 pub fn clear(&mut self) {
737 self.head = [0, 0];
738 self.bounds.clear();
739 }
740 }
741
742 impl<BC: Deref<Target=[u64]>, HC: IndexAs<u64>> Strides<BC, HC> {
743 #[inline(always)]
744 pub fn bounds(&self, index: usize) -> (usize, usize) {
745 let stride = self.head.index_as(0);
746 let length = self.head.index_as(1);
747 let index = index as u64;
748 let lower = if index == 0 { 0 } else {
749 let index = index - 1;
750 if index < length { (index+1) * stride } else { self.bounds[(index - length) as usize] }
751 } as usize;
752 let upper = if index < length { (index+1) * stride } else { self.bounds[(index - length) as usize] } as usize;
753 (lower, upper)
754 }
755 }
756 impl<BC: Len, HC: IndexAs<u64>> Strides<BC, HC> {
757 #[inline(always)] pub fn strided(&self) -> Option<u64> {
758 if self.bounds.is_empty() {
759 Some(self.head.index_as(0))
760 }
761 else { None }
762 }
763 }
764 }
765
766 #[cfg(test)]
767 mod test {
768 use alloc::vec::Vec;
769 #[test]
770 fn round_trip() {
771
772 use crate::common::{Index, Push, Len};
773 use crate::{Borrow, Vecs};
774 use crate::primitive::offsets::{Strides, Fixeds};
775
776 let mut cols = Vecs::<Vec::<i32>, Strides>::default();
777 for i in 0 .. 100 {
778 cols.push(&[1i32, 2, i]);
779 }
780
781 let cols = Vecs {
782 bounds: TryInto::<Fixeds<3>>::try_into(cols.bounds).unwrap(),
783 values: cols.values,
784 };
785
786 assert_eq!(cols.borrow().len(), 100);
787 for i in 0 .. 100 {
788 assert_eq!(cols.borrow().get(i).len(), 3);
789 }
790
791 let mut cols = Vecs {
792 bounds: Strides::new(3, cols.bounds.count),
793 values: cols.values
794 };
795
796 cols.push(&[0, 0]);
797 assert!(TryInto::<Fixeds<3>>::try_into(cols.bounds).is_err());
798 }
799 }
800}
801
802pub use empty::Empties;
803mod empty {
805
806 use alloc::{vec::Vec, string::String};
807 use crate::common::index::CopyAs;
808 use crate::{Clear, Columnar, Container, Len, IndexMut, Index, Push, Borrow};
809
810 #[derive(Copy, Clone, Debug, Default)]
811 pub struct Empties<CC = u64> { pub count: CC, pub empty: () }
812
813 impl Columnar for () {
814 #[inline(always)]
815 fn into_owned<'a>(_other: crate::Ref<'a, Self>) -> Self { }
816 type Container = Empties;
817 }
818
819 impl Borrow for Empties {
820 type Ref<'a> = ();
821 type Borrowed<'a> = Empties<&'a u64>;
822 #[inline(always)]
823 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> { Empties { count: &self.count, empty: () } }
824 #[inline(always)]
825 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where Self: 'a {
826 Empties { count: thing.count, empty: () }
827 }
828 #[inline(always)]
829 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
830 }
831
832 impl Container for Empties {
833 #[inline(always)]
834 fn extend_from_self(&mut self, _other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
835 self.count += range.len() as u64;
836 }
837
838 fn reserve_for<'a, I>(&mut self, _selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone { }
839 }
840
841 impl<CC: CopyAs<u64>> Len for Empties<CC> {
842 #[inline(always)] fn len(&self) -> usize { self.count.copy_as() as usize }
843 }
844 impl<CC> IndexMut for Empties<CC> {
845 type IndexMut<'a> = &'a mut () where CC: 'a;
846 #[inline(always)] fn get_mut(&mut self, _index: usize) -> Self::IndexMut<'_> { &mut self.empty }
848 }
849 impl<CC> Index for Empties<CC> {
850 type Ref = ();
851 #[inline(always)]
852 fn get(&self, _index: usize) -> Self::Ref { }
853 }
854 impl<'a, CC> Index for &'a Empties<CC> {
855 type Ref = &'a ();
856 #[inline(always)]
857 fn get(&self, _index: usize) -> Self::Ref { &() }
858 }
859 impl Push<()> for Empties {
860 #[inline(always)]
862 fn push(&mut self, _item: ()) { self.count += 1; }
863 #[inline(always)]
864 fn extend(&mut self, iter: impl IntoIterator<Item=()>) {
865 self.count += iter.into_iter().count() as u64;
866 }
867 }
868 impl<'a> Push<&'a ()> for Empties {
869 #[inline(always)]
871 fn push(&mut self, _item: &()) { self.count += 1; }
872 #[inline(always)]
873 fn extend(&mut self, iter: impl IntoIterator<Item=&'a ()>) {
874 self.count += iter.into_iter().count() as u64;
875 }
876 }
877
878 impl Clear for Empties {
879 #[inline(always)]
880 fn clear(&mut self) { self.count = 0; }
881 }
882
883 impl<'a> crate::AsBytes<'a> for crate::primitive::Empties<&'a u64> {
884 const SLICE_COUNT: usize = 1;
885 #[inline]
886 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
887 debug_assert!(index < Self::SLICE_COUNT);
888 (8, bytemuck::cast_slice(core::slice::from_ref(self.count)))
889 }
890 }
891 impl<'a> crate::FromBytes<'a> for crate::primitive::Empties<&'a u64> {
892 const SLICE_COUNT: usize = 1;
893 #[inline(always)]
894 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
895 Self { count: &bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap()[0], empty: () }
896 }
897 #[inline(always)]
898 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
899 let (w, _) = store.get(*offset); *offset += 1;
900 debug_assert!(!w.is_empty(), "Empties::from_store: empty count slice");
901 Self { count: w.first().unwrap_or(&0), empty: () }
902 }
903 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
904 sizes.push(8);
905 Ok(())
906 }
907 fn validate(slices: &[(&[u64], u8)]) -> Result<(), String> {
908 if slices.is_empty() || slices[0].0.is_empty() {
909 return Err("Empties: count slice must be non-empty".into());
910 }
911 Ok(())
912 }
913 }
914}
915
916pub use boolean::Bools;
917mod boolean {
919
920 use alloc::{vec::Vec, string::String};
921 use crate::{Container, Clear, Len, Index, IndexAs, Push, Borrow};
922
923 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
930 #[derive(Copy, Clone, Debug, Default, PartialEq)]
931 pub struct Bools<VC = Vec<u64>, TC = [u64; 2]> {
932 pub values: VC,
934 pub tail: TC,
936 }
937
938 impl crate::Columnar for bool {
939 #[inline(always)]
940 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
941 type Container = Bools;
942 }
943
944 impl<VC: crate::common::BorrowIndexAs<u64>> Borrow for Bools<VC> {
945 type Ref<'a> = bool;
946 type Borrowed<'a> = Bools<VC::Borrowed<'a>, &'a [u64]> where VC: 'a;
947 #[inline(always)]
948 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
949 Bools {
950 values: self.values.borrow(),
951 tail: &self.tail,
952 }
953 }
954 #[inline(always)]
955 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where VC: 'a {
956 Bools {
957 values: VC::reborrow(thing.values),
958 tail: thing.tail,
959 }
960 }
961 #[inline(always)]
962 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
963 }
964
965 impl<VC: crate::common::PushIndexAs<u64>> Container for Bools<VC> {
966 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
969 self.values.reserve_for(selves.map(|x| x.values))
970 }
971 }
972
973 impl<'a, VC: crate::AsBytes<'a>> crate::AsBytes<'a> for crate::primitive::Bools<VC, &'a [u64]> {
974 const SLICE_COUNT: usize = VC::SLICE_COUNT + 1;
975 #[inline]
976 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
977 debug_assert!(index < Self::SLICE_COUNT);
978 if index < VC::SLICE_COUNT {
979 self.values.get_byte_slice(index)
980 } else {
981 (core::mem::align_of::<u64>() as u64, bytemuck::cast_slice(self.tail))
982 }
983 }
984 }
985
986 impl<'a, VC: crate::FromBytes<'a>> crate::FromBytes<'a> for crate::primitive::Bools<VC, &'a [u64]> {
987 const SLICE_COUNT: usize = VC::SLICE_COUNT + 1;
988 #[inline(always)]
989 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
990 let values = crate::FromBytes::from_bytes(bytes);
991 let tail: &[u64] = bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap();
992 Self { values, tail }
993 }
994 #[inline(always)]
995 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
996 let values = VC::from_store(store, offset);
997 let (tail, _) = store.get(*offset); *offset += 1;
998 debug_assert!(tail.len() >= 2, "Bools::from_store: tail slice too short (len {})", tail.len());
999 Self { values, tail }
1000 }
1001 fn element_sizes(sizes: &mut Vec<usize>) -> Result<(), String> {
1002 VC::element_sizes(sizes)?;
1003 sizes.push(8); Ok(())
1005 }
1006 fn validate(slices: &[(&[u64], u8)]) -> Result<(), String> {
1007 if slices.len() < Self::SLICE_COUNT {
1008 return Err(format!("Bools: expected {} slices but got {}", Self::SLICE_COUNT, slices.len()));
1009 }
1010 VC::validate(slices)?;
1011 let vc = VC::SLICE_COUNT;
1012 if slices[vc].0.len() < 2 {
1013 return Err("Bools: tail slice must have at least 2 elements (last_word, last_bits)".into());
1014 }
1015 Ok(())
1016 }
1017 }
1018
1019 impl<VC: Len, TC: IndexAs<u64>> Len for Bools<VC, TC> {
1020 #[inline(always)] fn len(&self) -> usize { self.values.len() * 64 + (self.tail.index_as(1) as usize) }
1021 }
1022
1023 impl<VC: Len + IndexAs<u64>, TC: IndexAs<u64>> Index for Bools<VC, TC> {
1024 type Ref = bool;
1025 #[inline(always)] fn get(&self, index: usize) -> Self::Ref {
1026 let block = index / 64;
1027 let word = if block == self.values.len() {
1028 self.tail.index_as(0)
1029 } else {
1030 self.values.index_as(block)
1031 };
1032 let bit = index % 64;
1033 (word >> bit) & 1 == 1
1034 }
1035 }
1036
1037 impl<VC: Len + IndexAs<u64>, TC: IndexAs<u64>> Index for &Bools<VC, TC> {
1038 type Ref = bool;
1039 #[inline(always)] fn get(&self, index: usize) -> Self::Ref {
1040 (*self).get(index)
1041 }
1042 }
1043
1044 impl<VC: for<'a> Push<&'a u64>> Push<bool> for Bools<VC> {
1045 #[inline]
1046 fn push(&mut self, bit: bool) {
1047 self.tail[0] |= (bit as u64) << self.tail[1];
1048 self.tail[1] += 1;
1049 if self.tail[1] == 64 {
1051 self.values.push(&self.tail[0]);
1052 self.tail = [0, 0];
1053 }
1054 }
1055 }
1056 impl<'a, VC: for<'b> Push<&'b u64>> Push<&'a bool> for Bools<VC> {
1057 #[inline(always)]
1058 fn push(&mut self, bit: &'a bool) {
1059 self.push(*bit)
1060 }
1061 }
1062
1063
1064 impl<VC: Clear> Clear for Bools<VC> {
1065 #[inline(always)]
1066 fn clear(&mut self) {
1067 self.values.clear();
1068 self.tail = [0, 0];
1069 }
1070 }
1071
1072}
1073
1074pub use duration::Durations;
1075mod duration {
1077
1078 use alloc::vec::Vec;
1079 use core::time::Duration;
1080 use crate::{Container, Len, Index, IndexAs, Push, Clear, Borrow};
1081
1082 #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1084 #[derive(Copy, Clone, Debug, Default, PartialEq)]
1085 pub struct Durations<SC = Vec<u64>, NC = Vec<u32>> {
1086 pub seconds: SC,
1087 pub nanoseconds: NC,
1088 }
1089
1090 impl crate::Columnar for Duration {
1091 #[inline(always)]
1092 fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
1093 type Container = Durations;
1094 }
1095
1096 impl<SC: crate::common::BorrowIndexAs<u64>, NC: crate::common::BorrowIndexAs<u32>> Borrow for Durations<SC, NC> {
1097 type Ref<'a> = Duration;
1098 type Borrowed<'a> = Durations<SC::Borrowed<'a>, NC::Borrowed<'a>> where SC: 'a, NC: 'a;
1099 #[inline(always)]
1100 fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
1101 Durations {
1102 seconds: self.seconds.borrow(),
1103 nanoseconds: self.nanoseconds.borrow(),
1104 }
1105 }
1106 #[inline(always)]
1107 fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where SC: 'a, NC: 'a {
1108 Durations {
1109 seconds: SC::reborrow(thing.seconds),
1110 nanoseconds: NC::reborrow(thing.nanoseconds),
1111 }
1112 }
1113 #[inline(always)]
1114 fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
1115 }
1116
1117 impl<SC: crate::common::PushIndexAs<u64>, NC: crate::common::PushIndexAs<u32>> Container for Durations<SC, NC> {
1118 #[inline(always)]
1119 fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: core::ops::Range<usize>) {
1120 self.seconds.extend_from_self(other.seconds, range.clone());
1121 self.nanoseconds.extend_from_self(other.nanoseconds, range);
1122 }
1123
1124 fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
1125 self.seconds.reserve_for(selves.clone().map(|x| x.seconds));
1126 self.nanoseconds.reserve_for(selves.map(|x| x.nanoseconds));
1127 }
1128 }
1129
1130 impl<'a, SC: crate::AsBytes<'a>, NC: crate::AsBytes<'a>> crate::AsBytes<'a> for crate::primitive::Durations<SC, NC> {
1131 const SLICE_COUNT: usize = SC::SLICE_COUNT + NC::SLICE_COUNT;
1132 #[inline]
1133 fn get_byte_slice(&self, index: usize) -> (u64, &'a [u8]) {
1134 debug_assert!(index < Self::SLICE_COUNT);
1135 if index < SC::SLICE_COUNT {
1136 self.seconds.get_byte_slice(index)
1137 } else {
1138 self.nanoseconds.get_byte_slice(index - SC::SLICE_COUNT)
1139 }
1140 }
1141 }
1142 impl<'a, SC: crate::FromBytes<'a>, NC: crate::FromBytes<'a>> crate::FromBytes<'a> for crate::primitive::Durations<SC, NC> {
1143 const SLICE_COUNT: usize = SC::SLICE_COUNT + NC::SLICE_COUNT;
1144 #[inline(always)]
1145 fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
1146 Self {
1147 seconds: crate::FromBytes::from_bytes(bytes),
1148 nanoseconds: crate::FromBytes::from_bytes(bytes),
1149 }
1150 }
1151 #[inline(always)]
1152 fn from_store(store: &crate::bytes::indexed::DecodedStore<'a>, offset: &mut usize) -> Self {
1153 Self {
1154 seconds: SC::from_store(store, offset),
1155 nanoseconds: NC::from_store(store, offset),
1156 }
1157 }
1158 }
1159
1160 impl<SC: Len, NC> Len for Durations<SC, NC> {
1161 #[inline(always)] fn len(&self) -> usize { self.seconds.len() }
1162 }
1163
1164 impl<SC: IndexAs<u64>, NC: IndexAs<u32>> Index for Durations<SC, NC> {
1165 type Ref = Duration;
1166 #[inline(always)] fn get(&self, index: usize) -> Self::Ref {
1167 Duration::new(self.seconds.index_as(index), self.nanoseconds.index_as(index))
1168 }
1169 }
1170 impl<SC: IndexAs<u64>, NC: IndexAs<u32>> Index for &Durations<SC, NC> {
1171 type Ref = Duration;
1172 #[inline(always)] fn get(&self, index: usize) -> Self::Ref {
1173 Duration::new(self.seconds.index_as(index), self.nanoseconds.index_as(index))
1174 }
1175 }
1176
1177 impl<SC: for<'a> Push<&'a u64>, NC: for<'a> Push<&'a u32>> Push<core::time::Duration> for Durations<SC, NC> {
1178 #[inline]
1179 fn push(&mut self, item: core::time::Duration) {
1180 self.seconds.push(&item.as_secs());
1181 self.nanoseconds.push(&item.subsec_nanos());
1182 }
1183 }
1184 impl<'a, SC: for<'b> Push<&'b u64>, NC: for<'b> Push<&'b u32>> Push<&'a core::time::Duration> for Durations<SC, NC> {
1185 #[inline]
1186 fn push(&mut self, item: &'a core::time::Duration) {
1187 self.push(*item)
1188 }
1189 }
1190 impl<'a, SC: Push<&'a u64>, NC: Push<&'a u32>> Push<(&'a u64, &'a u32)> for Durations<SC, NC> {
1191 #[inline]
1192 fn push(&mut self, item: (&'a u64, &'a u32)) {
1193 self.seconds.push(item.0);
1194 self.nanoseconds.push(item.1);
1195 }
1196 }
1197
1198 impl<SC: Clear, NC: Clear> Clear for Durations<SC, NC> {
1199 #[inline(always)]
1200 fn clear(&mut self) {
1201 self.seconds.clear();
1202 self.nanoseconds.clear();
1203 }
1204 }
1205
1206}
1207