entry to the dedicated module

This commit is contained in:
NikVolf
2019-09-07 13:52:19 +03:00
parent 265c6e5303
commit 03524ba7d0
2 changed files with 50 additions and 45 deletions

48
src/entry.rs Normal file
View File

@@ -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<EntryLink, Error> {
match self.kind {
EntryKind::Leaf => { Err(Error::ExpectedNode) }
EntryKind::Node(left, _) => Ok(left)
}
}
pub fn right(&self) -> Result<EntryLink, Error> {
match self.kind {
EntryKind::Leaf => { Err(Error::ExpectedNode) }
EntryKind::Node(_, right) => Ok(right)
}
}
}
impl From<NodeData> for Entry {
fn from(s: NodeData) -> Self {
Entry { kind: EntryKind::Leaf, data: s }
}
}

View File

@@ -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<EntryLink, Error> {
match self.kind {
EntryKind::Leaf => { Err(Error::ExpectedNode) }
EntryKind::Node(left, _) => Ok(left)
}
}
pub fn right(&self) -> Result<EntryLink, Error> {
match self.kind {
EntryKind::Leaf => { Err(Error::ExpectedNode) }
EntryKind::Node(_, right) => Ok(right)
}
}
}
impl From<NodeData> 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 {