From ef98b68683157987ee196e22e275bab9d70c76fc Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Sun, 6 Feb 2022 14:26:08 +1030 Subject: [PATCH] Experimenting with no-hash container --- src/main.rs | 64 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index bd0c5eb..adc6d8b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,7 @@ #![allow(dead_code)] #![allow(unused_imports)] +use core::ops::Index; +use std::io::{self, Write}; use std::fs; use std::collections::{HashMap, BTreeMap}; use bitintr::{Lzcnt, Tzcnt}; @@ -32,10 +34,58 @@ struct Word { //letters: [Achar; WORD_LENGTH] } +type TKeys = Charmask; // I give up on making this generic for now +struct ThinArray { + // keys: [TKeys; N_KEYS], + keys: Vec, + items: [T; CAPACITY], + items_used: usize, +} + +impl ThinArray { + fn default() -> Self { + // println!("Initializing ThinArray"); + Self{ + // keys: [0; N_KEYS], + items: array_init::array_init(|_| T::default()), + keys: (0..N_KEYS).map(|_| 0).collect(), + items_used: 0, + } + } + + fn insert(&mut self, key: TKeys, value: T) { + // println!("Insert requested for key {}", key); + debug_assert!(self.items_used < CAPACITY); + self.items_used += 1; + self.items[self.items_used as usize] = value; + // self.items.push(value); + self.keys[key as usize] = self.items_used as TKeys; + } + + fn get(&self, key: TKeys) -> &T { + &self.items[self.keys[key as usize] as usize] + } + + fn contains_key(&self, _key: &TKeys) -> bool { + true + // key < N_KEYS + } +} +impl Index<&TKeys> for ThinArray { + type Output = T; + + fn index(&self, key: &TKeys) -> &T { + // println!("Key requested: {}", key); + &self.items[self.keys[*key as usize] as usize] + } +} + + // type WordCache = HashMap, RandomState>; // ahash // type WordCache = HashMap, BuildHasherDefault>; -type WordCache = BTreeMap>; -// type WordCache = HashMap>; // Default hash is slower than BTree +// type WordCache = BTreeMap>; +type WordCache = ThinArray, CACHE_SIZE, 7000>; +// type WordCache = HashMap>; // Default hash is slower than BTree on M1 // type WordCacheArr = [&Vec; CACHE_SIZE]; fn default_wordcache() -> WordCache { @@ -216,12 +266,16 @@ fn find_word_id_from_str(s: &str, words: &Vec) -> usize { } fn main() { + eprint!("Hello, world!\n"); + // io::stdout().flush().unwrap(); fs::write("test.txt", ["test1", "test2", "test3"].join("\n")).expect("Failed to write output"); let words = load_dictionary("words-kura"); let totalwords = words.len(); - println!("Hello, world! {} words in dict", totalwords); + println!("Loaded dict - {} words in dict", totalwords); let wordcache = generate_wordcache(words); let all_words = &wordcache[&IDX_ALL_WORDS]; + // println!("Cache contains {} keys", wordcache.keys().len()); // 6756 on words-kura + //let sr = simulate(&wordcache[""][0], &wordcache[""][5000], &wordcache); //println!("{:?}", sr); @@ -250,8 +304,4 @@ fn main() { results.sort_by_key(|r| r.1); let results_strs: Vec = results.iter().map(|r| r.0.clone()).collect(); fs::write("results.txt", results_strs.join("\n")).expect("Failed to write output"); - - // let mut cachekeys: Vec = wordcache.keys().map(|k| charmask2str(*k)).collect(); - // cachekeys.sort(); - // println!("{:?}", cachekeys); }