Struct afrim_memory::Node
source · pub struct Node {
pub depth: usize,
pub key: char,
/* private fields */
}Expand description
A node in the text buffer.
0 ----------------> The root node
/ \
'g' 's' -------------> Node: Rc<Node>
/ \
"ɣ" = '+' 'h' -----------> Node: Rc<Node>
\
'+' = "ʃ" ---> Node that holds a value
Fields§
§depth: usizeDepth of the node.
key: charCharacter holded by the node.
Implementations§
source§impl Node
impl Node
sourcepub fn new(key: char, depth: usize) -> Self
pub fn new(key: char, depth: usize) -> Self
Initializes a new node in the text buffer.
Can also be used to initialize the text buffer (not recommanded).
Uses Node::default instead.
§Example
use afrim_memory::Node;
let text_buffer = Node::new('\0', 0);
// You cannot assign directly a value to a node.
// But, an alternative is as below.
let node = Node::new('u', 0);
node.insert(vec![], "ʉ̠̀".to_owned());
assert_eq!(node.take(), Some("ʉ̠̀".to_owned()));Note: Early, Node::new was the only way to initialize a text
buffer but it has been replaced by Node::default
which is now more adapted for this use case.
sourcepub fn insert(&self, sequence: Vec<char>, value: String)
pub fn insert(&self, sequence: Vec<char>, value: String)
Inserts a sequence in the text buffer.
§Example
use afrim_memory::Node;
let text_buffer = Node::default();
text_buffer.insert(vec!['.', 't'], "ṫ".to_owned());
let node = text_buffer.goto('.').and_then(|node| node.goto('t'));
assert_eq!(node.unwrap().take(), Some("ṫ".to_owned()));sourcepub fn goto(&self, character: char) -> Option<Rc<Self>>
pub fn goto(&self, character: char) -> Option<Rc<Self>>
Moves from the current node to his child.
Useful to go through a sequence.
§Example
use afrim_memory::Node;
let text_buffer = Node::default();
text_buffer.insert(vec!['o', '/'], "ø".to_owned());
text_buffer.insert(vec!['o', '*'], "ɔ".to_owned());
text_buffer.insert(vec!['o', '1'], "ò".to_owned());
text_buffer.insert(vec!['o', '*', '~'], "ɔ̃".to_owned());
// let sequence = ['o', '*', '~'];
let node = text_buffer.goto('o').unwrap();
assert_eq!(node.take(), None);
let node = node.goto('*').unwrap();
assert_eq!(node.take(), Some("ɔ".to_owned()));
let node = node.goto('~').unwrap();
assert_eq!(node.take(), Some("ɔ̃".to_owned()));sourcepub fn take(&self) -> Option<String>
pub fn take(&self) -> Option<String>
Extracts the value of the node.
A node in the text buffer don’t always holds a value. Hence, his value is optional.
§Example
use afrim_memory::Node;
let text_buffer = Node::default();
text_buffer.insert(vec!['1', 'c'], "c̀".to_string());
let node = text_buffer.goto('1').unwrap();
assert_eq!(node.take(), None);
let node = node.goto('c').unwrap();
assert_eq!(node.take(), Some("c̀".to_owned()));sourcepub fn is_root(&self) -> bool
pub fn is_root(&self) -> bool
Returns true is the node is at the initial depth.
Useful when dealing with the Cursor.
Will permit to know the beginning and the end of a sequence.
§Example
use afrim_memory::{Cursor, Node};
let text_buffer = Node::default();
text_buffer.insert(vec!['e', '2' ], "é".to_owned());
text_buffer.insert(vec!['i', '7' ], "ǐ".to_owned());
assert!(text_buffer.is_root());
let node = text_buffer.goto('e').unwrap();
assert!(!node.is_root());
Trait Implementations§
source§impl Default for Node
impl Default for Node
source§fn default() -> Self
fn default() -> Self
Create a root node.
A root node always holds a null character as key and is recommanded to use to initialize the text buffer. You should always use it to create a text buffer because the internal code can change.
§Example
use afrim_memory::Node;
// It's recommanded to use it, to initialize your text buffer.
let text_buffer = Node::default();
// Not recommanded.
let another_text_buffer = Node::new('\0', 0);
assert!(text_buffer.is_root());
assert!(another_text_buffer.is_root());