From 03524ba7d033db79d093fb2de40b47b36cad82eb Mon Sep 17 00:00:00 2001 From: NikVolf Date: Sat, 7 Sep 2019 13:52:19 +0300 Subject: [PATCH] entry to the dedicated module --- src/entry.rs | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 47 ++--------------------------------------------- 2 files changed, 50 insertions(+), 45 deletions(-) create mode 100644 src/entry.rs diff --git a/src/entry.rs b/src/entry.rs new file mode 100644 index 0000000..da0b131 --- /dev/null +++ b/src/entry.rs @@ -0,0 +1,48 @@ +use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt, ByteOrder}; + +use crate::{EntryKind, NodeData, Error, EntryLink}; + +#[derive(Debug)] +pub struct Entry { + pub(crate) kind: EntryKind, + pub(crate) data: NodeData, +} + +impl Entry { + pub fn update_siblings(&mut self, left: EntryLink, right: EntryLink) { + self.kind = EntryKind::Node(left, right); + } + + pub fn complete(&self) -> bool { + let leaves = self.leaf_count(); + leaves & (leaves - 1) == 0 + } + + pub fn leaf_count(&self) -> u64 { + self.data.end_height - self.data.start_height + 1 + } + + pub fn is_leaf(&self) -> bool { + if let EntryKind::Leaf = self.kind { true } else { false } + } + + pub fn left(&self) -> Result { + match self.kind { + EntryKind::Leaf => { Err(Error::ExpectedNode) } + EntryKind::Node(left, _) => Ok(left) + } + } + + pub fn right(&self) -> Result { + match self.kind { + EntryKind::Leaf => { Err(Error::ExpectedNode) } + EntryKind::Node(_, right) => Ok(right) + } + } +} + +impl From for Entry { + fn from(s: NodeData) -> Self { + Entry { kind: EntryKind::Leaf, data: s } + } +} diff --git a/src/lib.rs b/src/lib.rs index cac2313..552dee2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,9 +4,11 @@ mod tree; mod node_data; +mod entry; pub use tree::Tree; pub use node_data::NodeData; +pub use entry::Entry; #[derive(Debug, derive_more::Display)] pub enum Error { @@ -38,51 +40,6 @@ pub enum EntryKind { Node(EntryLink, EntryLink), } -#[derive(Debug)] -pub struct Entry { - kind: EntryKind, - data: NodeData, -} - -impl Entry { - pub fn update_siblings(&mut self, left: EntryLink, right: EntryLink) { - self.kind = EntryKind::Node(left, right); - } - - pub fn complete(&self) -> bool { - let leaves = self.leaf_count(); - leaves & (leaves - 1) == 0 - } - - pub fn leaf_count(&self) -> u64 { - self.data.end_height - self.data.start_height + 1 - } - - pub fn is_leaf(&self) -> bool { - if let EntryKind::Leaf = self.kind { true } else { false } - } - - pub fn left(&self) -> Result { - match self.kind { - EntryKind::Leaf => { Err(Error::ExpectedNode) } - EntryKind::Node(left, _) => Ok(left) - } - } - - pub fn right(&self) -> Result { - match self.kind { - EntryKind::Leaf => { Err(Error::ExpectedNode) } - EntryKind::Node(_, right) => Ok(right) - } - } -} - -impl From for Entry { - fn from(s: NodeData) -> Self { - Entry { kind: EntryKind::Leaf, data: s } - } -} - impl Error { pub (crate) fn augment(self, link: EntryLink) -> Self { match self {