diff --git a/Cargo.toml b/Cargo.toml index bf5c65e..a659328 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,4 +3,4 @@ resolver = "2" members = [ "shell", "core" -] \ No newline at end of file +] diff --git a/core/src/error.rs b/core/src/error.rs index 8c9609f..02d04be 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -29,6 +29,7 @@ pub struct TraceItem { #[derive(Debug, Clone)] pub struct Traceback(pub Vec); +#[inline] pub fn start_trace(item: TraceItem) -> Traceback { Traceback::new().with_trace(item) } diff --git a/core/src/hashmap.rs b/core/src/hashmap.rs index b192fa2..f88707b 100644 --- a/core/src/hashmap.rs +++ b/core/src/hashmap.rs @@ -35,14 +35,15 @@ const INDEXED_BUCKETS: u8 = 199; * * Not a priority: minimal collisions * - * Just to make sure this is not misused I keep it private + * Just to make sure this is not misused I keep it private. + * And yes, I am sure a B-Tree would be better. */ #[inline] fn string_hash(input: &String) -> u8 { input .chars() - .map(|c| c.to_digit(10) - .or_else(|| Some(0)) + .map(|c| c.to_digit(36) // each letter and number get a digit + .or_else(|| Some(0)) // all else is 0 .unwrap()) .reduce(|acc, i| (acc + i) % INDEXED_BUCKETS as u32) .or_else(|| Some(0)) @@ -151,9 +152,46 @@ mod tests { use super::*; #[test] - fn add_and_fetch_simple() { + fn add_fetch_and_remove_simple() { let mut q = QuickMap::::new(); + let key = String::from("test"); q.insert(String::from("test"), 1); - assert_eq!(*q.get(&String::from("test")).unwrap(), 1); + assert_eq!(*q.get(&key).unwrap(), 1); + assert!(q.contains_key(&key)); + assert_eq!( + q.remove(&key), + Some(1), + ); + assert_eq!(q.contains_key(&key), false); + assert_eq!(q.get(&key), None); + } + + #[test] + fn iter_test() { + let mut q = QuickMap::::new(); + let k1 = String::from("test1"); + let k2 = String::from("test1@"); // will be in same bucket + let k3 = String::from("test2"); + let k4 = String::from("test2--"); // will be in same bucket + q.insert(k1.clone(), 1); + q.insert(k2.clone(), 2); + q.insert(k3.clone(), 3); + q.insert(k4.clone(), 4); + + let mut i = q.iter(); + let entry1 = i.next().unwrap(); + let entry2 = i.next().unwrap(); + let entry3 = i.next().unwrap(); + let entry4 = i.next().unwrap(); + + assert_eq!(i.next(), None); + assert_eq!(entry1.0, k1); + assert_eq!(entry1.1, 1); + assert_eq!(entry2.0, k2); + assert_eq!(entry2.1, 2); + assert_eq!(entry3.0, k3); + assert_eq!(entry3.1, 3); + assert_eq!(entry4.0, k4); + assert_eq!(entry4.1, 4); } } diff --git a/core/src/lex.rs b/core/src/lex.rs index c8a006b..379e81b 100644 --- a/core/src/lex.rs +++ b/core/src/lex.rs @@ -59,6 +59,7 @@ pub fn lex(document: &String) -> Result, Traceback> { * Returns Ok(Rc) if lexing passes * Returns Err(String) if an error occurs */ +#[inline] fn process(document: &String) -> Result, String> { let doc_len = document.len(); @@ -227,6 +228,7 @@ fn process(document: &String) -> Result, String> { * - equals is also allowed but only for shell command compatibility * else returns false */ +#[inline] fn tok_is_symbol(token: &str) -> Option { for t in token.chars() { if !t.is_alphanumeric() && diff --git a/core/src/segment.rs b/core/src/segment.rs index f4813b7..c20360b 100644 --- a/core/src/segment.rs +++ b/core/src/segment.rs @@ -185,6 +185,7 @@ impl Seg { } } +#[inline] fn seg_to_string(s: &Seg, parens: bool) -> String { let mut string = String::new(); if parens { diff --git a/core/src/stl.rs b/core/src/stl.rs index 835a0e9..60ff67e 100644 --- a/core/src/stl.rs +++ b/core/src/stl.rs @@ -30,6 +30,7 @@ pub const CFG_FILE_VNAME: &str = "FLESH_CFG_FILE"; /// static_stdlib /// inserts all stdlib functions that can be inserted without /// any kind of further configuration data into a symtable +#[inline] pub fn static_stdlib(syms: &mut SymTable, print: fn(&String), read: fn() -> String) { append::add_list_lib(syms); strings::add_string_lib(syms, print, read);