#[cfg(test)]
mod unit_test;
use std::cmp::Ordering;
use std::collections::BTreeMap;
#[derive(Debug, Clone, Default)]
pub struct BTreeTable<T, U> {
tree: BTreeMap<T, U>,
}
impl<T, U> BTreeTable<T, U> {
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn len(&self) -> usize {
self.tree.len()
}
}
impl<T: Ord, U> BTreeTable<T, U> {
pub fn new() -> Self {
Self {
tree: BTreeMap::<T, U>::new(),
}
}
pub fn init(key: T, value: U) -> Self {
let mut tree = Self::new();
tree.insert(key, value);
tree
}
pub fn contains(&self, key: &T) -> bool {
self.tree.get(key).is_some()
}
pub fn get(&self, key: &T) -> Option<&U> {
self.tree.get(key)
}
pub fn insert(&mut self, key: T, value: U) {
self.tree.insert(key, value);
}
pub fn delete(&mut self, key: &T) -> Option<U> {
self.tree.remove(key)
}
}
impl<T: Ord, U: Ord> BTreeTable<T, U> {
pub fn strict_floor(&self, key: &T) -> Option<&T> {
let res = self.tree.range(..key).max();
if let Some(item) = res {
Some(item.0)
} else {
None
}
}
pub fn ceil(&self, key: &T) -> Option<&T> {
let res = self.tree.range(key..).min();
if let Some(item) = res {
Some(item.0)
} else {
None
}
}
pub fn range_search(&self, low: &T, high: &T) -> Vec<&T> {
self.tree
.range(low..high)
.into_iter()
.map(|item| item.0)
.collect::<Vec<&T>>()
}
pub fn range_count(&self, low: &T, high: &T) -> usize {
self.range_search(low, high).len()
}
}
#[derive(Clone, Debug, PartialEq)]
struct Node<T, U> {
key: T,
value: U,
left: Option<Box<Node<T, U>>>,
right: Option<Box<Node<T, U>>>,
}
impl<T, U> Node<T, U> {
pub fn init(_key: T, _value: U) -> Self {
Self {
key: _key,
value: _value,
left: None,
right: None,
}
}
}
#[derive(Debug, Clone)]
pub struct BSearchTree<T, U> {
root: Option<Box<Node<T, U>>>,
len: usize,
}
impl<T, U> Default for BSearchTree<T, U> {
fn default() -> Self {
Self::new()
}
}
impl<T, U> BSearchTree<T, U> {
pub fn new() -> Self {
Self { root: None, len: 0 }
}
pub fn init(key: T, value: U) -> Self {
Self {
root: Some(Box::new(Node::init(key, value))),
len: 1,
}
}
pub fn len(&self) -> usize {
self.len
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<T: Eq + Ord, U: Eq> BSearchTree<T, U> {
pub fn contains(&self, key: &T) -> bool {
self.get(key).is_some()
}
pub fn get(&self, key: &T) -> Option<&U> {
let mut node = &self.root;
while node.is_some() {
let temp_node = node.as_ref().unwrap();
match key.cmp(&temp_node.key) {
Ordering::Less => node = &temp_node.left,
Ordering::Greater => node = &temp_node.right,
Ordering::Equal => return Some(&temp_node.value),
}
}
None
}
}
impl<T: Ord, U> BSearchTree<T, U> {
fn put(node: &mut Option<Box<Node<T, U>>>, key: T, value: U) -> Option<&U> {
match node {
None => *node = Some(Box::new(Node::init(key, value))),
Some(ref mut nod) => match key.cmp(&nod.key) {
Ordering::Less => {
return Self::put(&mut nod.left, key, value);
}
Ordering::Greater => {
return Self::put(&mut nod.right, key, value);
}
Ordering::Equal => {
nod.value = value;
return Some(&nod.value);
}
},
}
None
}
pub fn insert(&mut self, key: T, value: U) {
if Self::put(&mut self.root, key, value).is_none() {
self.len += 1;
}
}
}
impl<T: Eq + Ord, U: Ord> BSearchTree<T, U> {
pub fn min(&self) -> Option<&T> {
let mut node = &self.root;
let mut result = None;
while node.is_some() {
let temp_node = node.as_ref().unwrap();
result = Some(&temp_node.key);
node = &temp_node.left;
}
result
}
pub fn max(&self) -> Option<&T> {
let mut node = &self.root;
let mut result = None;
while node.is_some() {
let temp_node = node.as_ref().unwrap();
result = Some(&temp_node.key);
node = &temp_node.right;
}
result
}
fn recursive_floor<'a>(
node: &'a Option<Box<Node<T, U>>>,
key: &T,
) -> &'a Option<Box<Node<T, U>>> {
if node.is_none() {
return &None;
}
let current_node = node.as_ref().unwrap();
if key == ¤t_node.key {
return node;
}
if key < ¤t_node.key {
return Self::recursive_floor(¤t_node.left, key);
}
let temp_node = Self::recursive_floor(¤t_node.right, key);
if temp_node.is_some() {
temp_node
} else {
node
}
}
pub fn floor(&self, key: &T) -> Option<&T> {
let node = Self::recursive_floor(&self.root, key);
if node.is_none() {
return None;
}
return Some(&node.as_ref().unwrap().key);
}
}
#[derive(Default, Clone, Debug)]
pub struct OrdVecTable<T, U> {
vec: Vec<Pair<T, Option<U>>>,
}
impl<T, U> OrdVecTable<T, U> {
pub fn new() -> Self {
Self { vec: Vec::new() }
}
pub fn init(key: T, value: U) -> Self {
let mut symbol_table = Self::new();
symbol_table.vec.push(Pair::init(key, Some(value)));
symbol_table
}
pub fn len(&self) -> usize {
self.vec.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn min(&self) -> Option<&T> {
if self.is_empty() {
None
} else {
Some(self.vec[0].first())
}
}
pub fn max(&self) -> Option<&T> {
if self.vec.is_empty() {
None
} else {
Some(self.vec[self.vec.len() - 1].first())
}
}
}
impl<T: Ord + Clone, U: Eq> OrdVecTable<T, U> {
pub fn contains(&self, key: &T) -> bool {
self.get(key).is_some()
}
pub fn get(&self, key: &T) -> Option<&U> {
if let Ok(index) = self.vec.binary_search(&Pair::init(key.clone(), None)) {
return self.vec[index].second().as_ref();
} else {
None
}
}
pub fn floor(&self, key: &T) -> Option<&T> {
if self.is_empty() {
None
} else {
let index = self.vec.binary_search(&Pair::init(key.clone(), None));
match index {
Ok(ind) => Some(self.vec[ind].first()),
Err(ind) => {
if ind > 0 {
Some(self.vec[ind - 1].first())
} else {
None
}
}
}
}
}
pub fn ceil(&self, key: &T) -> Option<&T> {
if self.is_empty() {
None
} else {
let index = self.vec.binary_search(&Pair::init(key.clone(), None));
match index {
Ok(ind) => Some(self.vec[ind].first()),
Err(ind) => {
if ind < self.vec.len() - 1 && ind > 0 {
Some(self.vec[ind + 1].first())
} else if ind == 0 {
Some(self.vec[0].first())
} else {
None
}
}
}
}
}
}
impl<T: Ord + Clone, U: Eq + Clone> OrdVecTable<T, U> {
fn put(&mut self, key: T, value: Option<U>) -> Option<U> {
let candidate = Pair::init(key.clone(), None);
let index = self.vec.binary_search(&candidate);
match index {
Ok(ind) => {
let temp_val = self.vec[ind].second().as_ref().cloned();
let mut_val = self.vec[ind].second_mut();
*mut_val = value;
temp_val
}
Err(ind) => {
if ind < self.vec.len() {
self.vec.insert(ind, Pair::init(key, value));
} else {
self.vec.push(Pair::init(key, value))
}
None
}
}
}
pub fn insert(&mut self, key: T, value: U) {
self.put(key, Some(value));
}
pub fn delete(&mut self, key: &T) -> Option<U> {
self.put(key.clone(), None) }
}
#[derive(Default, Clone, Debug)]
struct Pair<T, U> {
tuple: (T, U),
}
impl<T, U> Pair<T, U> {
pub fn init(key: T, value: U) -> Self {
Self {
tuple: (key, value),
}
}
pub fn first(&self) -> &T {
&self.tuple.0
}
pub fn second(&self) -> &U {
&self.tuple.1
}
pub fn first_mut(&mut self) -> &mut T {
&mut self.tuple.0
}
pub fn second_mut(&mut self) -> &mut U {
&mut self.tuple.1
}
}
impl<T: Ord, U> Ord for Pair<T, U> {
fn cmp(&self, other: &Self) -> Ordering {
self.tuple.0.cmp(&other.tuple.0)
}
}
impl<T: Ord, U> PartialOrd for Pair<T, U> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.tuple.0.cmp(&other.tuple.0))
}
}
impl<T: Ord, U> Eq for Pair<T, U> {}
impl<T: Ord, U> PartialEq for Pair<T, U> {
fn eq(&self, other: &Self) -> bool {
self.tuple.0 == other.tuple.0
}
}
#[derive(Default, Clone, Debug)]
pub struct UnordVecTable<T, U> {
vec: Vec<(T, Option<U>)>,
}
impl<T, U> UnordVecTable<T, U> {
pub fn new() -> Self {
Self { vec: Vec::new() }
}
pub fn init(key: T, value: U) -> Self {
let mut symbol_table = Self::new();
symbol_table.vec.push((key, Some(value)));
symbol_table
}
pub fn len(&self) -> usize {
self.vec.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
}
impl<T: Eq, U: Eq> UnordVecTable<T, U> {
pub fn contains(&self, key: &T) -> bool {
self.get(key).is_some()
}
}
impl<T: Eq, U> UnordVecTable<T, U> {
pub fn get(&self, key: &T) -> Option<&U> {
for k in 0..self.vec.len() {
if &self.vec[k].0 == key {
return self.vec[k].1.as_ref();
}
}
None
}
}
impl<T: Eq, U: Clone> UnordVecTable<T, U> {
fn put(&mut self, key: T, value: Option<U>) -> Option<U> {
let mut k = 0;
let mut val = None;
let length = self.vec.len();
while k < length {
if self.vec[k].0 == key {
val = self.vec[k].1.clone();
self.vec[k].1 = value.clone();
break;
}
k += 1;
}
if self.is_empty() || (k == length && value.is_some()) {
self.vec.push((key, value));
}
val
}
pub fn insert(&mut self, key: T, value: U) {
self.put(key, Some(value));
}
}
impl<T: Eq + Clone, U: Clone> UnordVecTable<T, U> {
pub fn delete(&mut self, key: &T) -> Option<U> {
self.put(key.clone(), None) }
}