Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 48 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,23 +541,7 @@ impl<E: Encoding, const N: usize> OrdPath<E, N> {

/// Returns the `OrdPath` without its final element, if there is one.
pub fn parent(&self) -> Option<&OrdPath<E, N>> {
let src = self.bytes();
if src.len() == 0 {
return None;
}
let mut bits_prev = 0usize;
let mut reader = Reader::new(src, self.encoding());
unsafe {
// SAFETY: Validation of the data happens on creation even for a byte slice.
if let Some((_, stage)) = reader.read().unwrap_unchecked() {
let mut bits = stage.bits() as usize;
while let Some((_, stage)) = reader.read().unwrap_unchecked() {
bits_prev = bits;
bits += stage.bits() as usize;
}
}
}
Some(Self::new(self.path(), bits_prev))
self.ancestors().next()
}

/// Returns `true` if `self` is an ancestor of `other`.
Expand Down Expand Up @@ -708,6 +692,32 @@ impl Bytes {
},
}
}

#[inline]
fn ancestor<E: Encoding>(&self, enc: &E, nth: usize) -> Option<&Self> {
const FORWARD_BUF_LEN: usize = size_of::<usize>();
let mut bytes = self;
for _ in 0..=nth.div_ceil(FORWARD_BUF_LEN) {
if bytes.len_in_bits() == 0 {
return None;
}
let mut idx = 0;
let mut buf = [0u8; FORWARD_BUF_LEN];
let mut bits = 0;
let mut reader = Reader::new(bytes, enc);
while let Some((_, stage)) = reader.read().unwrap() {
bits += stage.bits() as usize;
buf[idx % buf.len()] = stage.bits();
idx = idx.wrapping_add(1);
}
for _ in 0..=buf.len().min(nth % FORWARD_BUF_LEN) {
idx = idx.wrapping_sub(1);
bits -= buf[idx % buf.len()] as usize;
}
bytes = unsafe { Bytes::from_raw_parts(bytes.data.as_ptr(), bits) };
}
Some(bytes)
}
}

impl Read for &Bytes {
Expand Down Expand Up @@ -867,7 +877,15 @@ impl<'a, E: Encoding, const N: usize> Iterator for Ancestors<'a, E, N> {
type Item = &'a OrdPath<E, N>;

fn next(&mut self) -> Option<Self::Item> {
self.path = self.path.and_then(|p| p.parent());
self.nth(0)
}

fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.path = self.path.and_then(|p| {
p.bytes()
.ancestor(p.encoding(), n)
.map(|b| OrdPath::new(p.path(), b.len_in_bits()))
});
self.path
}
}
Expand Down Expand Up @@ -1046,21 +1064,19 @@ mod tests {

#[test]
fn path_parent() {
fn parent(p: Option<OrdPathBuf>) -> Option<OrdPathBuf> {
p.and_then(|p| p.parent().map(|p| p.to_owned()))
}

fn assert(s: &[i64]) {
let mut p = Some(<OrdPathBuf>::from_ordinals(s, DefaultEncoding));
for i in (0..s.len()).rev() {
p = parent(p);
assert_eq!(p, Some(OrdPathBuf::from_ordinals(&s[..i], DefaultEncoding)));
}
assert_eq!(parent(p), None);
let ords = (i8::MIN..i8::MAX).map(|x| x as i64).collect::<Vec<_>>();
for n in 1..ords.len() {
let ords = &ords[..n];
let path = <OrdPathBuf>::from_ordinals(ords, DefaultEncoding);
dbg!(&ords);
dbg!(&path);
assert_eq!(
ords[..(ords.len() - 1)],
path.parent()
.map(|p| p.ordinals().collect::<Vec<_>>())
.unwrap_or_default()
);
}

assert(&[1, 2]);
assert(&[344, 345]);
}

#[cfg(feature = "serde")]
Expand Down
2 changes: 1 addition & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<R: Read, E: Encoding> Reader<R, E> {
if consumed > 0 {
let acc_next = u64::from_be_bytes(buf);
let acc = if self.len > 0 {
acc_next >> self.len | self.acc
(acc_next >> self.len) | self.acc
} else {
acc_next
};
Expand Down
4 changes: 2 additions & 2 deletions src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl<W: Write, E: Encoding> Writer<W, E> {
let value = (value - stage.ordinal_min()) as u64;

let buf = match stage.bits() < 64 {
true => (prefix << (stage.ordinal_bits()) | value) << (64 - stage.bits()),
false => prefix << (64 - stage.prefix_bits()) | (value >> (stage.bits() - 64)),
true => ((prefix << stage.ordinal_bits()) | value) << (64 - stage.bits()),
false => (prefix << (64 - stage.prefix_bits())) | (value >> (stage.bits() - 64)),
};

let len = self.len & 127;
Expand Down