1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
#![deny(missing_docs)]
//! Data structure to make handling of sequential code more convenient.
//!
//! It takes sequential codes and generates a text buffer that will be used to easily get a
//! corresponding character through an input.
//!
//! # Notes
//! - sequence: A sequential code corresponding to a character.
//! Eg. af1 = "ɑ̀"
//! - input: The user input (or a set of sequences).
//! Eg. ngaf7 nkwe2e2 ka7meru7n
//! - text buffer: The memory where our text data will be stored.
//! - node: A node in the text buffer.
//!
//! # Example
//!
//! ```
//! use afrim_memory::{Node, utils};
//!
//! // Builds a TextBuffer.
//! let text_buffer = Node::default();
//! text_buffer.insert(vec!['a', 'f'], "ɑ".to_owned());
//! text_buffer.insert(vec!['a', 'f', '1'], "ɑ̀".to_owned());
//!
//! // Bulk insertion of data in the TextBuffer.
//! let data = vec![vec!["af11", "ɑ̀ɑ̀"], vec!["?.", "ʔ"]];
//! let text_buffer = utils::build_map(data);
//!
//! // Traverses the tree.
//! let node = text_buffer.goto('a').and_then(|node| node.goto('f')).and_then(|node| node.goto('1')).and_then(|node| node.goto('1'));
//! assert_eq!(node.unwrap().take(), Some("ɑ̀ɑ̀".to_owned()));
//! ```
//!
//! # Example: in reading data through a file
//!
//! ```no_run
//! use afrim_memory::utils;
//! use std::fs;
//!
//! // Import data from a file.
//! let data = fs::read_to_string("./data/sample.txt")
//! .expect("Failed to load the data file");
//! let data = utils::load_data(&data);
//! let text_buffer = utils::build_map(data);
//! ```
//!
//! # Example: with the usage of a cursor
//!
//! ```
//! use afrim_memory::{Cursor, Node};
//! use std::rc::Rc;
//!
//! // Build a TextBuffer.
//! let text_buffer = Node::default();
//! text_buffer.insert(vec!['i', '-'], "ɨ".to_owned());
//! text_buffer.insert(vec!['i', '-', '3'], "ɨ̄".to_owned());
//!
//! // Builds the cursor.
//! let memory = Rc::new(text_buffer);
//! let mut cursor = Cursor::new(memory, 16);
//!
//! // Moves the cursor through the input.
//! let input = "i-3";
//! input.chars().for_each(|c| { cursor.hit(c); });
//! // Verify the current state.
//! assert_eq!(cursor.state(), (Some("ɨ̄".to_owned()), 3, '3'));
//!
//! // Undo the last insertion.
//! assert_eq!(cursor.undo(), Some("ɨ̄".to_owned()));
//! // Verify the current state.
//! assert_eq!(cursor.state(), (Some("ɨ".to_owned()), 2, '-'));
//! ```
//!
//! [`TextBuffer`]: https://en.wikipedia.org/wiki/Text_buffer
use std::collections::{HashMap, VecDeque};
use std::{cell::RefCell, fmt, rc::Rc};
pub mod utils;
/// A node in the text buffer.
///
/// ```text
/// 0 ----------------> The root node
/// / \
/// 'g' 's' -------------> Node: Rc<Node>
/// / \
/// "ɣ" = '+' 'h' -----------> Node: Rc<Node>
/// \
/// '+' = "ʃ" ---> Node that holds a value
/// ```
#[derive(Clone, Debug)]
pub struct Node {
children: RefCell<HashMap<char, Rc<Node>>>,
/// Depth of the node.
pub depth: usize,
/// Character holded by the node.
pub key: char,
value: RefCell<Option<String>>,
}
impl Default for Node {
/// 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());
/// ```
fn default() -> Self {
Self::new('\0', 0)
}
}
impl Node {
/// Initializes a new node in the text buffer.
///
/// Can also be used to initialize the text buffer (not recommanded).
/// Uses [`Node::default`](crate::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`](crate::Node::new) was the only way to initialize a text
/// buffer but it has been replaced by [`Node::default`](crate::Node::default)
/// which is now more adapted for this use case.
pub fn new(key: char, depth: usize) -> Self {
Self {
children: HashMap::new().into(),
depth,
key,
value: None.into(),
}
}
/// 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()));
/// ```
pub fn insert(&self, sequence: Vec<char>, value: String) {
if let Some(character) = sequence.clone().first() {
let new_node = Rc::new(Self::new(*character, self.depth + 1));
self.children
.borrow()
.get(character)
.unwrap_or(&new_node)
.insert(sequence.into_iter().skip(1).collect(), value);
self.children
.borrow_mut()
.entry(*character)
.or_insert(new_node);
} else {
*self.value.borrow_mut() = Some(value);
};
}
/// 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()));
/// ```
pub fn goto(&self, character: char) -> Option<Rc<Self>> {
self.children.borrow().get(&character).map(Rc::clone)
}
/// 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()));
/// ```
pub fn take(&self) -> Option<String> {
self.value.borrow().as_ref().map(ToOwned::to_owned)
}
/// Returns true is the node is at the initial depth.
///
/// Useful when dealing with the [`Cursor`](crate::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());
///
/// ```
pub fn is_root(&self) -> bool {
self.depth == 0
}
}
/// The Cursor permits to keep a track of the different positions while moving in
/// the text buffer.
///
/// ```text
/// '\0' - 'k' - '\0' - 'w' - '\0' '\0' '\0' - '\'' - 'n' - 'i' - '7' |--> 0
/// | /| / |
/// | / | / |
/// 'e' / 'e' / |--> 1
/// | / | / |
/// '2' '2' |--> 2
/// |
/// | depth
/// v
/// ```
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// text_buffer.insert(vec!['e', '2'], "é".to_owned());
/// text_buffer.insert(vec!['i', '7'], "ǐ".to_owned());
///
/// // We build our cursor.
/// let memory = Rc::new(text_buffer);
/// let mut cursor = Cursor::new(memory, 16);
/// let input = "nkwe2e2'ni7";
/// input.chars().for_each(|c| { cursor.hit(c); });
///
/// assert_eq!(
/// cursor.to_sequence(),
/// vec![
/// 'k', '\0', 'w', '\0', 'e', '2', '\0', 'e', '2', '\0',
/// '\'', '\0', 'n', '\0', 'i', '7'
/// ]
/// );
/// ```
///
/// Note the partitioning of this input. The cursor can browse through the memory based
/// on an input and save a track of his positions. It's useful when we want handle
/// backspace operations in an input method engine.
#[derive(Clone)]
pub struct Cursor {
buffer: VecDeque<Rc<Node>>,
root: Rc<Node>,
}
impl fmt::Debug for Cursor {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.to_sequence().fmt(f)
}
}
impl Cursor {
/// Initializes the cursor of a text buffer.
///
/// `capacity` is the number of hit that the cursor can track. The cursor follows the FIFO
/// rule. If the capacity is exceeded, the oldest hit will be discarded.
///
/// **Note**: Be careful when you set this capacity. We recommend to select a capacity equal or
/// greater than the maximun sequence length that you want handle.
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// let memory = Rc::new(text_buffer);
///
/// // A cursor of our text buffer.
/// let cursor = Cursor::new(memory, 16);
/// ```
///
/// **Note**: It's recommended to initialize the text buffer with
/// [`Node::default`](crate::Node::default) to evict unexpected behaviors.
pub fn new(root: Rc<Node>, capacity: usize) -> Self {
Self {
buffer: VecDeque::with_capacity(capacity),
root,
}
}
/// Enters a character in the sequence and returns his corresponding out.
///
/// Permits to simulate the user typing in the input method engine.
/// For each character entered, the cursor will move through the text buffer in looking of the
/// corresponding sequence. If the sequence is got (end on a value), his value will be returned.
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// text_buffer.insert(vec!['o', 'e'], "œ".to_owned());
/// let memory = Rc::new(text_buffer);
///
/// let mut cursor = Cursor::new(memory, 16);
/// // let input= "coeur";
/// assert_eq!(cursor.hit('c'), None);
/// assert_eq!(cursor.hit('o'), None);
/// assert_eq!(cursor.hit('e'), Some("œ".to_owned()));
/// assert_eq!(cursor.hit('u'), None);
/// assert_eq!(cursor.hit('r'), None);
///
/// assert_eq!(cursor.to_sequence(), vec!['\0', 'c', '\0', 'o', 'e', '\0', 'u', '\0', 'r']);
/// ```
///
/// **Note**:
/// - The `\0` at the index 0, marks the beginning of a new sequence and the end of a
/// old. It also represents the root node.
/// - A character don't necessary need to be in the text buffer. The cursor will create a
/// tempory node to represent it in his internal memory. All characters not present in the text
/// buffer will be at the same depth that the root node.
pub fn hit(&mut self, character: char) -> Option<String> {
let node = self
.buffer
.iter()
.last()
.unwrap_or(&Rc::new(Node::default()))
.goto(character)
.or_else(|| {
// We end the current sequence
self.insert(Rc::new(Node::default()));
// and start a new one
self.root.goto(character)
})
.unwrap_or(Rc::new(Node::new(character, 0)));
let out = node.take();
self.insert(node);
out
}
fn insert(&mut self, node: Rc<Node>) {
if self.buffer.len() == self.buffer.capacity() {
self.buffer.pop_front();
}
self.buffer.push_back(node);
}
/// Removes the last node and returns his corresponding out.
/// Or simplily, undo the previous hit.
///
/// Useful to simulate a backspace operation.
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// text_buffer.insert(vec!['o', 'e'], "œ".to_owned());
/// let memory = Rc::new(text_buffer);
///
/// let mut cursor = Cursor::new(memory, 16);
/// // let input = "coeur";
/// assert_eq!(cursor.hit('c'), None);
/// assert_eq!(cursor.hit('o'), None);
/// assert_eq!(cursor.hit('e'), Some("œ".to_owned()));
/// assert_eq!(cursor.hit('u'), None);
/// assert_eq!(cursor.hit('r'), None);
///
/// // Undo
/// assert_eq!(cursor.undo(), None);
/// assert_eq!(cursor.undo(), None);
/// assert_eq!(cursor.undo(), Some("œ".to_owned()));
/// assert_eq!(cursor.undo(), None);
/// assert_eq!(cursor.undo(), None);
///
/// assert_eq!(cursor.to_sequence(), vec!['\0']);
/// ```
///
/// **Note**: Look at the `\0` at the end. It represents the root node, and the start of a
/// new sequence. Even if you remove it until obtain an empty buffer, the cursor will add it
/// before each new sequence. You can considere it as a delimiter between two sequences. But if
/// you want clear or verify if the buffer is empty, you can use [Cursor::clear](crate::Cursor::clear) or [Cursor::is_empty](crate::Cursor::is_empty).
pub fn undo(&mut self) -> Option<String> {
let node = self.buffer.pop_back();
node.and_then(|node| {
if node.key == '\0' {
self.undo()
} else {
node.take()
}
})
}
/// Returns the current state of the cursor.
///
/// Permits to know the current position in the memory and also the last hit.
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// text_buffer.insert(vec!['o', '/'], "ø".to_owned());
/// let memory = Rc::new(text_buffer);
///
/// let mut cursor = Cursor::new(memory, 8);
/// // The cursor starts always at the root node.
/// assert_eq!(cursor.state(), (None, 0, '\0'));
/// cursor.hit('o');
/// assert_eq!(cursor.state(), (None, 1, 'o'));
/// ```
pub fn state(&self) -> (Option<String>, usize, char) {
self.buffer
.iter()
.last()
.map(|n| (n.take(), n.depth, n.key))
.unwrap_or_default()
}
/// Returns the current sequence in the cursor.
///
/// It's always useful to know what is inside the memory of the cursor for debugging / logging.
/// The
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// text_buffer.insert(vec!['.', '.', 'z'], "z̈".to_owned());
/// let memory = Rc::new(text_buffer);
///
/// let mut cursor = Cursor::new(memory, 8);
/// "z..z".chars().for_each(|c| { cursor.hit(c); });
///
/// assert_eq!(cursor.to_sequence(), vec!['\0', 'z', '\0', '.', '.', 'z']);
/// ```
pub fn to_sequence(&self) -> Vec<char> {
self.buffer.iter().map(|node| node.key).collect()
}
/// Clear the memory of the cursor.
///
/// In clearing the internal buffer, all the tracking information will be lost.
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// let memory = Rc::new(text_buffer);
/// let mut cursor = Cursor::new(memory, 8);
///
/// "hello".chars().for_each(|c| { cursor.hit(c); });
/// assert!(!cursor.is_empty());
///
/// cursor.clear();
/// assert!(cursor.is_empty());
/// ```
pub fn clear(&mut self) {
self.buffer.clear();
}
/// Verify if the cursor is empty.
///
/// # Example
///
/// ```
/// use afrim_memory::{Cursor, Node};
/// use std::rc::Rc;
///
/// let text_buffer = Node::default();
/// let memory = Rc::new(text_buffer);
///
/// let mut cursor = Cursor::new(memory, 8);
/// assert!(cursor.is_empty());
///
/// cursor.hit('a');
/// assert!(!cursor.is_empty());
/// ```
pub fn is_empty(&self) -> bool {
return self.buffer.iter().filter(|c| c.key != '\0').count() == 0;
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_node() {
use crate::Node;
let root = Node::default();
assert!(root.is_root());
root.insert(vec!['a', 'f'], "ɑ".to_owned());
root.insert(vec!['a', 'f', '1'], "ɑ̀".to_owned());
assert!(root.goto('a').is_some());
assert!(!root.goto('a').unwrap().is_root());
assert!(root.goto('b').is_none());
let node = root.goto('a').and_then(|e| e.goto('f'));
assert_eq!(node.as_ref().unwrap().take(), Some("ɑ".to_owned()));
let node = node.and_then(|e| e.goto('1'));
assert_eq!(node.as_ref().unwrap().take(), Some("ɑ̀".to_owned()));
}
#[test]
fn test_cursor() {
use crate::{utils, Cursor};
use std::{fs, rc::Rc};
macro_rules! hit {
( $cursor:ident $( $c:expr ),* ) => (
$( $cursor.hit($c); )*
);
}
macro_rules! undo {
( $cursor:ident $occ:expr ) => {
(0..$occ).into_iter().for_each(|_| {
$cursor.undo();
});
};
}
let data = fs::read_to_string("./data/sample.txt").unwrap();
let root = utils::build_map(utils::load_data(&data));
let mut cursor = Cursor::new(Rc::new(root), 8);
assert_eq!(cursor.state(), (None, 0, '\0'));
hit!(cursor '2', 'i', 'a', 'f');
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'i', 'a', 'f']);
assert_eq!(cursor.state(), (Some("íɑ́".to_owned()), 4, 'f'));
undo!(cursor 1);
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'i', 'a']);
undo!(cursor 1);
cursor.hit('e');
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'i', 'e']);
undo!(cursor 2);
hit!(cursor 'o', 'o');
assert_eq!(cursor.to_sequence(), vec!['\0', '2', 'o', 'o']);
undo!(cursor 3);
assert_eq!(cursor.to_sequence(), vec!['\0']);
hit!(cursor '2', '2', 'u', 'a');
assert_eq!(
cursor.to_sequence(),
vec!['\0', '\0', '2', '\0', '2', 'u', 'a']
);
undo!(cursor 4);
assert_eq!(cursor.to_sequence(), vec!['\0', '\0']);
assert!(cursor.is_empty());
undo!(cursor 1);
assert_eq!(cursor.to_sequence(), vec![]);
hit!(
cursor
'a', 'a', '2', 'a', 'e', 'a', '2', 'f', 'a',
'2', '2', 'x', 'x', '2', 'i', 'a', '2', '2', '_', 'f',
'2', 'a', '2', 'a', '_'
);
assert_eq!(
cursor.to_sequence(),
vec!['f', '\0', '2', 'a', '\0', '2', 'a', '_']
);
assert_eq!(
format!("{:?}", cursor),
format!("{:?}", cursor.to_sequence())
);
cursor.clear();
assert_eq!(cursor.to_sequence(), vec![]);
}
}