inline a few things and test hashmap better

Signed-off-by: Ava Affine <ava@sunnypup.io>
This commit is contained in:
Ava Apples Affine 2024-07-29 11:07:05 -07:00
parent 7800b483da
commit a93fb512aa
6 changed files with 49 additions and 6 deletions

View file

@ -3,4 +3,4 @@ resolver = "2"
members = [ members = [
"shell", "shell",
"core" "core"
] ]

View file

@ -29,6 +29,7 @@ pub struct TraceItem {
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Traceback(pub Vec<TraceItem>); pub struct Traceback(pub Vec<TraceItem>);
#[inline]
pub fn start_trace(item: TraceItem) -> Traceback { pub fn start_trace(item: TraceItem) -> Traceback {
Traceback::new().with_trace(item) Traceback::new().with_trace(item)
} }

View file

@ -35,14 +35,15 @@ const INDEXED_BUCKETS: u8 = 199;
* *
* Not a priority: minimal collisions * 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] #[inline]
fn string_hash(input: &String) -> u8 { fn string_hash(input: &String) -> u8 {
input input
.chars() .chars()
.map(|c| c.to_digit(10) .map(|c| c.to_digit(36) // each letter and number get a digit
.or_else(|| Some(0)) .or_else(|| Some(0)) // all else is 0
.unwrap()) .unwrap())
.reduce(|acc, i| (acc + i) % INDEXED_BUCKETS as u32) .reduce(|acc, i| (acc + i) % INDEXED_BUCKETS as u32)
.or_else(|| Some(0)) .or_else(|| Some(0))
@ -151,9 +152,46 @@ mod tests {
use super::*; use super::*;
#[test] #[test]
fn add_and_fetch_simple() { fn add_fetch_and_remove_simple() {
let mut q = QuickMap::<u8>::new(); let mut q = QuickMap::<u8>::new();
let key = String::from("test");
q.insert(String::from("test"), 1); 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::<u8>::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);
} }
} }

View file

@ -59,6 +59,7 @@ pub fn lex(document: &String) -> Result<Box<Seg>, Traceback> {
* Returns Ok(Rc<Seg>) if lexing passes * Returns Ok(Rc<Seg>) if lexing passes
* Returns Err(String) if an error occurs * Returns Err(String) if an error occurs
*/ */
#[inline]
fn process(document: &String) -> Result<Box<Seg>, String> { fn process(document: &String) -> Result<Box<Seg>, String> {
let doc_len = document.len(); let doc_len = document.len();
@ -227,6 +228,7 @@ fn process(document: &String) -> Result<Box<Seg>, String> {
* - equals is also allowed but only for shell command compatibility * - equals is also allowed but only for shell command compatibility
* else returns false * else returns false
*/ */
#[inline]
fn tok_is_symbol(token: &str) -> Option<String> { fn tok_is_symbol(token: &str) -> Option<String> {
for t in token.chars() { for t in token.chars() {
if !t.is_alphanumeric() && if !t.is_alphanumeric() &&

View file

@ -185,6 +185,7 @@ impl Seg {
} }
} }
#[inline]
fn seg_to_string(s: &Seg, parens: bool) -> String { fn seg_to_string(s: &Seg, parens: bool) -> String {
let mut string = String::new(); let mut string = String::new();
if parens { if parens {

View file

@ -30,6 +30,7 @@ pub const CFG_FILE_VNAME: &str = "FLESH_CFG_FILE";
/// static_stdlib /// static_stdlib
/// inserts all stdlib functions that can be inserted without /// inserts all stdlib functions that can be inserted without
/// any kind of further configuration data into a symtable /// any kind of further configuration data into a symtable
#[inline]
pub fn static_stdlib(syms: &mut SymTable, print: fn(&String), read: fn() -> String) { pub fn static_stdlib(syms: &mut SymTable, print: fn(&String), read: fn() -> String) {
append::add_list_lib(syms); append::add_list_lib(syms);
strings::add_string_lib(syms, print, read); strings::add_string_lib(syms, print, read);