Skip to content

Commit 18d2a12

Browse files
Merge pull request #59 from antiguru/larges
Encode u128/u128 as [u8; 16]
2 parents 5cad202 + 4894c8e commit 18d2a12

File tree

2 files changed

+168
-5
lines changed

2 files changed

+168
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ members = ["columnar_derive"]
1818
[dependencies]
1919
serde = { version = "1.0", optional = true, features = ["derive"] }
2020
smallvec = { version = "1.13.2", features = ["const_generics"] }
21-
bytemuck = "1.20"
21+
bytemuck = { version = "1.20", features = ["min_const_generics"] }
2222
columnar_derive = { path = "columnar_derive", version = "0.10" }
2323

2424
[dev-dependencies]

src/lib.rs

Lines changed: 167 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -967,14 +967,27 @@ pub mod primitive {
967967
bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap()
968968
}
969969
}
970+
impl<'a, const N: usize> crate::AsBytes<'a> for &'a [[$index_type; N]] {
971+
#[inline(always)]
972+
fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> {
973+
std::iter::once((std::mem::align_of::<$index_type>() as u64, bytemuck::cast_slice(&self[..])))
974+
}
975+
}
976+
impl<'a, const N: usize> crate::FromBytes<'a> for &'a [[$index_type; N]] {
977+
#[inline(always)]
978+
fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
979+
// We use `unwrap()` here in order to panic with the `bytemuck` error, which may be informative.
980+
bytemuck::try_cast_slice(bytes.next().expect("Iterator exhausted prematurely")).unwrap()
981+
}
982+
}
970983
)* }
971984
}
972985

973-
implement_columnable!(u8, u16, u32, u64, u128);
974-
implement_columnable!(i8, i16, i32, i64, i128);
986+
implement_columnable!(u8, u16, u32, u64);
987+
implement_columnable!(i8, i16, i32, i64);
975988
implement_columnable!(f32, f64);
976-
implement_columnable!(Wrapping<u8>, Wrapping<u16>, Wrapping<u32>, Wrapping<u64>, Wrapping<u128>);
977-
implement_columnable!(Wrapping<i8>, Wrapping<i16>, Wrapping<i32>, Wrapping<i64>, Wrapping<i128>);
989+
implement_columnable!(Wrapping<u8>, Wrapping<u16>, Wrapping<u32>, Wrapping<u64>);
990+
implement_columnable!(Wrapping<i8>, Wrapping<i16>, Wrapping<i32>, Wrapping<i64>);
978991

979992
pub use sizes::{Usizes, Isizes};
980993
/// Columnar stores for `usize` and `isize`, stored as 64 bits.
@@ -1133,6 +1146,156 @@ pub mod primitive {
11331146
}
11341147
}
11351148

1149+
pub use larges::{U128s, I128s};
1150+
/// Columnar stores for `u128` and `i128`, stored as [u8; 16] bits.
1151+
mod larges {
1152+
1153+
use crate::{Clear, Columnar, Container, Len, Index, IndexAs, Push, HeapSize};
1154+
use crate::common::PushIndexAs;
1155+
1156+
type Encoded = [u8; 16];
1157+
1158+
#[derive(Copy, Clone, Default)]
1159+
pub struct U128s<CV = Vec<Encoded>> { pub values: CV }
1160+
1161+
impl Columnar for u128 {
1162+
fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
1163+
type Container = U128s;
1164+
}
1165+
1166+
impl<CV: PushIndexAs<Encoded>> Container for U128s<CV> {
1167+
type Ref<'a> = u128;
1168+
type Borrowed<'a> = U128s<CV::Borrowed<'a>> where CV: 'a;
1169+
fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
1170+
U128s { values: self.values.borrow() }
1171+
}
1172+
#[inline(always)]
1173+
fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where CV: 'a {
1174+
U128s { values: CV::reborrow(thing.values) }
1175+
}
1176+
#[inline(always)]
1177+
fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
1178+
1179+
#[inline(always)]
1180+
fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) {
1181+
self.values.extend_from_self(other.values, range)
1182+
}
1183+
1184+
fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
1185+
self.values.reserve_for(selves.map(|x| x.values))
1186+
}
1187+
}
1188+
1189+
impl<CV: Len> Len for U128s<CV> { fn len(&self) -> usize { self.values.len() }}
1190+
impl<CV: IndexAs<Encoded>> Index for U128s<CV> {
1191+
type Ref = u128;
1192+
#[inline(always)] fn get(&self, index: usize) -> Self::Ref { u128::from_le_bytes(self.values.index_as(index)) }
1193+
}
1194+
impl<CV: IndexAs<Encoded>> Index for &U128s<CV> {
1195+
type Ref = u128;
1196+
#[inline(always)] fn get(&self, index: usize) -> Self::Ref { u128::from_le_bytes(self.values.index_as(index)) }
1197+
}
1198+
impl<CV: for<'a> Push<&'a Encoded>> Push<u128> for U128s<CV> {
1199+
#[inline]
1200+
fn push(&mut self, item: u128) { self.values.push(&item.to_le_bytes()) }
1201+
}
1202+
impl Push<&u128> for U128s {
1203+
#[inline]
1204+
fn push(&mut self, item: &u128) { self.values.push(item.to_le_bytes()) }
1205+
}
1206+
impl<CV: Clear> Clear for U128s<CV> { fn clear(&mut self) { self.values.clear() }}
1207+
1208+
impl<CV: HeapSize> HeapSize for U128s<CV> {
1209+
fn heap_size(&self) -> (usize, usize) {
1210+
self.values.heap_size()
1211+
}
1212+
}
1213+
1214+
impl<'a, CV: crate::AsBytes<'a>> crate::AsBytes<'a> for U128s<CV> {
1215+
#[inline(always)]
1216+
fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> {
1217+
self.values.as_bytes()
1218+
}
1219+
}
1220+
1221+
impl<'a, CV: crate::FromBytes<'a>> crate::FromBytes<'a> for U128s<CV> {
1222+
#[inline(always)]
1223+
fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
1224+
Self { values: CV::from_bytes(bytes) }
1225+
}
1226+
}
1227+
1228+
#[derive(Copy, Clone, Default)]
1229+
pub struct I128s<CV = Vec<Encoded>> { pub values: CV }
1230+
1231+
impl Columnar for i128 {
1232+
fn into_owned<'a>(other: crate::Ref<'a, Self>) -> Self { other }
1233+
type Container = I128s;
1234+
}
1235+
1236+
impl<CV: PushIndexAs<Encoded>> Container for I128s<CV> {
1237+
type Ref<'a> = i128;
1238+
type Borrowed<'a> = I128s<CV::Borrowed<'a>> where CV: 'a;
1239+
fn borrow<'a>(&'a self) -> Self::Borrowed<'a> {
1240+
I128s { values: self.values.borrow() }
1241+
}
1242+
#[inline(always)]
1243+
fn reborrow<'b, 'a: 'b>(thing: Self::Borrowed<'a>) -> Self::Borrowed<'b> where CV: 'a {
1244+
I128s { values: CV::reborrow(thing.values) }
1245+
}
1246+
#[inline(always)]
1247+
fn reborrow_ref<'b, 'a: 'b>(thing: Self::Ref<'a>) -> Self::Ref<'b> where Self: 'a { thing }
1248+
1249+
#[inline(always)]
1250+
fn extend_from_self(&mut self, other: Self::Borrowed<'_>, range: std::ops::Range<usize>) {
1251+
self.values.extend_from_self(other.values, range)
1252+
}
1253+
1254+
fn reserve_for<'a, I>(&mut self, selves: I) where Self: 'a, I: Iterator<Item = Self::Borrowed<'a>> + Clone {
1255+
self.values.reserve_for(selves.map(|x| x.values))
1256+
}
1257+
}
1258+
1259+
impl<CV: Len> Len for I128s<CV> { fn len(&self) -> usize { self.values.len() }}
1260+
impl<CV: IndexAs<Encoded>> Index for I128s<CV> {
1261+
type Ref = i128;
1262+
#[inline(always)] fn get(&self, index: usize) -> Self::Ref { i128::from_le_bytes(self.values.index_as(index)) }
1263+
}
1264+
impl<CV: IndexAs<Encoded>> Index for &I128s<CV> {
1265+
type Ref = i128;
1266+
#[inline(always)] fn get(&self, index: usize) -> Self::Ref { i128::from_le_bytes(self.values.index_as(index)) }
1267+
}
1268+
impl<CV: for<'a> Push<&'a Encoded>> Push<i128> for I128s<CV> {
1269+
#[inline]
1270+
fn push(&mut self, item: i128) { self.values.push(&item.to_le_bytes()) }
1271+
}
1272+
impl Push<&i128> for I128s {
1273+
#[inline]
1274+
fn push(&mut self, item: &i128) { self.values.push(item.to_le_bytes()) }
1275+
}
1276+
impl<CV: Clear> Clear for I128s<CV> { fn clear(&mut self) { self.values.clear() }}
1277+
1278+
impl<CV: HeapSize> HeapSize for I128s<CV> {
1279+
fn heap_size(&self) -> (usize, usize) {
1280+
self.values.heap_size()
1281+
}
1282+
}
1283+
1284+
impl<'a, CV: crate::AsBytes<'a>> crate::AsBytes<'a> for I128s<CV> {
1285+
#[inline(always)]
1286+
fn as_bytes(&self) -> impl Iterator<Item=(u64, &'a [u8])> {
1287+
self.values.as_bytes()
1288+
}
1289+
}
1290+
1291+
impl<'a, CV: crate::FromBytes<'a>> crate::FromBytes<'a> for I128s<CV> {
1292+
#[inline(always)]
1293+
fn from_bytes(bytes: &mut impl Iterator<Item=&'a [u8]>) -> Self {
1294+
Self { values: CV::from_bytes(bytes) }
1295+
}
1296+
}
1297+
}
1298+
11361299
/// Columnar stores for non-decreasing `u64`, stored in various ways.
11371300
///
11381301
/// The venerable `Vec<u64>` works as a general container for arbitrary offests,

0 commit comments

Comments
 (0)