diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-07-05 23:07:26 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2023-07-05 23:07:26 +0200 |
| commit | 4d577650f737daaeb477bbbd5ae2bad4f1121c38 (patch) | |
| tree | ac973541e0a2d7751af4ece5f7f639e739f81fcc /src/vector.rs | |
first sketch of indexer
Diffstat (limited to 'src/vector.rs')
| -rw-r--r-- | src/vector.rs | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/src/vector.rs b/src/vector.rs new file mode 100644 index 0000000..ce1139d --- /dev/null +++ b/src/vector.rs @@ -0,0 +1,59 @@ +use std::collections::HashMap; +use std::ops::{Deref, DerefMut}; + +pub struct FileVector { + data : HashMap<u64, u64> +} + +impl Deref for FileVector { + type Target = HashMap<u64, u64>; + fn deref(&self) -> &Self::Target { + &self.data + } +} + +impl DerefMut for FileVector { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.data + } +} + +impl FileVector { + pub fn new() -> Self { + Self { data : HashMap::new() } + } + + pub fn from_hex(hex : String) -> Self { + let mut data : HashMap<u64, u64> = HashMap::new(); + let data_chunks : Vec<&str> = hex.split(' ').collect(); + + for chunk in data_chunks { + if !chunk.is_empty() { + let n : Vec<&str> = chunk.split(';').collect(); + let i : u64 = u64::from_str_radix(n[0], 16).expect("could not extract index"); + let v : u64 = u64::from_str_radix(n[1], 16).expect("could not extract value"); + data.insert(i, v); + } + } + + Self { data } + } + + pub fn to_hex(&self) -> String { + let mut hex = String::new(); + + for (i, v) in self.data.iter() { + hex += &format!("{:x};{:x} ", *i, *v); + } + + hex.trim().to_string() + } +} + +pub fn scalar_product(a : &FileVector, b : &FileVector) -> u64 { + let mut c = 0; + for (i, x) in a.iter() { + c += x * (b.get(i).unwrap_or_else(|| &0)); + } + c +} |