added hashmap branch
This commit is contained in:
parent
8b685b2128
commit
0bbd3ddc4a
|
@ -1,3 +1,4 @@
|
|||
use std::collections::HashMap;
|
||||
use std::ops::{Add, Mul, Sub};
|
||||
use std::thread;
|
||||
use std::time::Instant;
|
||||
|
@ -12,8 +13,7 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
|
|||
|
||||
/// Only stores more efficiently when at least 50% of all elements are zeros
|
||||
pub struct SparseVec {
|
||||
values: Vec<f64>,
|
||||
indices: Vec<usize>,
|
||||
data: HashMap<usize, f64>
|
||||
}
|
||||
|
||||
impl SparseVec {
|
||||
|
@ -21,9 +21,8 @@ impl SparseVec {
|
|||
pub fn dot(&self, other: &SparseVec) -> f64 {
|
||||
let mut sum = 0.0;
|
||||
|
||||
for index in 0..other.indices.len() {
|
||||
// exponential search for an element in the second vector to have the same index
|
||||
sum += binary_search(self.indices[index], &other.indices, &other.values) * self.values[index];
|
||||
for (k, v) in self.data.iter() {
|
||||
sum += v * other.data.get(k).unwrap_or(&0.0);
|
||||
}
|
||||
|
||||
sum
|
||||
|
@ -32,49 +31,21 @@ impl SparseVec {
|
|||
pub fn new(elements: usize, non_null: f64) -> Self {
|
||||
let non_zero_elements = (elements as f64 * non_null) as usize;
|
||||
|
||||
let mut values = Vec::with_capacity(non_zero_elements);
|
||||
let mut indices = Vec::with_capacity(non_zero_elements);
|
||||
let mut data = HashMap::with_capacity(non_zero_elements);
|
||||
|
||||
let mut rng = rand::thread_rng();
|
||||
|
||||
for i in 0..non_zero_elements {
|
||||
values.push(0.5);
|
||||
|
||||
let idx = i as f32 / non_zero_elements as f32 * (elements as f32 - 4.0) + rng.gen_range(0.0..3.0);
|
||||
indices.push(idx as usize);
|
||||
data.insert(idx as usize, 0.5);
|
||||
}
|
||||
|
||||
Self {
|
||||
values,
|
||||
indices
|
||||
data
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn binary_search(target: usize, indices: &[usize], values: &[f64]) -> f64 {
|
||||
let mut range = 0..indices.len();
|
||||
loop {
|
||||
let mut median = (range.end - range.start) >> 1;
|
||||
if median == 0 {
|
||||
break;
|
||||
}
|
||||
median += range.start;
|
||||
|
||||
if indices[median] == target {
|
||||
return values[median];
|
||||
}
|
||||
|
||||
if indices[median] > target {
|
||||
range.end = median;
|
||||
} else {
|
||||
range.start = median;
|
||||
}
|
||||
}
|
||||
|
||||
0.0
|
||||
}
|
||||
|
||||
macro_rules! time {
|
||||
($name:literal, $block:expr) => {{
|
||||
let start = Instant::now();
|
||||
|
|
Loading…
Reference in New Issue