generalized_suffix_tree/data/
tree_item.rs

1use crate::suffix_node::node::NodeID;
2use serde::{Serialize, Deserialize};
3use std::{cmp::Ordering, fmt};
4use core::fmt::{Debug, Display};
5
6#[derive(Debug, Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
7pub enum Character<T: PartialEq + Display + Debug + PartialOrd>{
8    Char(T),
9    Terminal
10}
11
12impl<T> Display for Character<T>
13where
14    T: PartialEq + Display + Debug + PartialOrd
15{
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self{
18            Character::Char(t) => write!(f, "{}", t),
19            Character::Terminal => write!(f, ""),
20        }
21    }
22}
23
24impl<T> Character<T>
25where
26    T: PartialEq + Display + Debug + PartialOrd
27{
28    pub fn is_terminal(&self)->bool{
29        matches!(&self, Character::Terminal)
30    }
31
32    pub fn into_inner(&self)->Option<&T>{
33        match self {
34            Character::Char(x) => Some(x),
35            _ => None,
36        }
37    }
38}
39
40impl<T> PartialOrd for Character<T>
41where
42    T: PartialEq + Display + Debug + PartialOrd
43{
44    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
45        match self{
46            Character::Terminal => {
47                match other{
48                    Character::Terminal => Some(Ordering::Equal),
49                    _ => Some(Ordering::Less)
50                }
51            },
52            Character::Char(t) => {
53                match other{
54                    Character::Terminal => Some(Ordering::Greater),
55                    Character::Char(o) => t.partial_cmp(o)
56                }
57            }
58        }
59        // return None;
60    }
61}
62
63
64pub trait TreeItem<T, U>
65where
66    T: PartialEq + Display + Debug + PartialOrd
67{
68    fn new(k: U, v: Vec<T>)->Self;
69    fn get_string(&self) -> &[Character<T>];
70    fn get_id(&self) -> &U;
71    fn get_nodes(&self) -> impl ExactSizeIterator<Item= &NodeID>;
72    fn add_data_to_node(&mut self, node_id: &NodeID);
73}