use super::branch::*;
use std::mem;
use std::sync::*;
#[derive(Clone, Copy, PartialEq, Debug)]
pub struct RopeNodeIndex(pub usize);
impl RopeNodeIndex {
pub fn idx(self) -> usize {
self.into()
}
}
impl Into<usize> for RopeNodeIndex {
fn into(self) -> usize {
let RopeNodeIndex(idx) = self;
idx
}
}
#[derive(Clone, PartialEq, Debug)]
pub enum RopeNode<Cell, Attribute> {
Empty,
Leaf(Option<RopeNodeIndex>, Vec<Cell>, Arc<Attribute>),
Branch(RopeBranch)
}
impl<Cell, Attribute> RopeNode<Cell, Attribute> {
pub fn take(&mut self) -> RopeNode<Cell, Attribute> {
let mut result = RopeNode::Empty;
mem::swap(self, &mut result);
result
}
pub fn len(&self) -> usize {
match self {
RopeNode::Empty => 0,
RopeNode::Leaf(_, cells, _) => cells.len(),
RopeNode::Branch(branch) => branch.length
}
}
pub fn parent(&self) -> Option<RopeNodeIndex> {
match self {
RopeNode::Empty => None,
RopeNode::Leaf(parent, _, _) => *parent,
RopeNode::Branch(branch) => branch.parent
}
}
}