updated comments

This commit is contained in:
Sven Vogel 2023-05-01 15:51:17 +02:00
parent 0a20116690
commit ad2bf148c3
1 changed files with 28 additions and 1 deletions

View File

@ -1,19 +1,39 @@
/**
* _ _ _ _
* __ ___ __(_) |_| |_ ___ _ __ | |__ _ _
* \ \ /\ / / '__| | __| __/ _ \ '_ \ | '_ \| | | |
* \ V V /| | | | |_| || __/ | | | | |_) | |_| |
* \_/\_/ |_| |_|\__|\__\___|_| |_| |_.__/ \__, |
* |___/
* ____ __ __ _
* / ___|_ _____ _ __ \ \ / /__ __ _ ___| |
* \___ \ \ / / _ \ '_ \ \ \ / / _ \ / _` |/ _ \ |
* ___) \ V / __/ | | | \ V / (_) | (_| | __/ |
* |____/ \_/ \___|_| |_| \_/ \___/ \__, |\___|_|
* |___/
* Licensed under the GPLv2 License, Version 2.0 (the "License");
* Copyright (c) Sven Vogel
*/
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::time::Instant; use std::time::Instant;
use bytesize::ByteSize; use bytesize::ByteSize;
use rand::Rng; use rand::Rng;
use jemalloc_ctl::{stats, epoch}; use jemalloc_ctl::{stats, epoch};
// we use a custom allocator for tracking heap allocations
#[global_allocator] #[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
/// Only stores more efficiently when at least 50% of all elements are zeros /// Wrapper struct around a BinaryTreeMap that stores the non zero elements of a vector by using the indices
/// as keys in the tree.
pub struct SparseVec { pub struct SparseVec {
map: BTreeMap<usize, f64> map: BTreeMap<usize, f64>
} }
impl SparseVec { impl SparseVec {
/// Compute the dot product of two vectors
pub fn dot(&self, other: &SparseVec) -> f64 { pub fn dot(&self, other: &SparseVec) -> f64 {
let mut sum = 0.0; let mut sum = 0.0;
@ -24,14 +44,20 @@ impl SparseVec {
sum sum
} }
/// Create a new SparseVec with a theoretical size of `elements`. `non_null`is the ration of non zero elements
/// in the sparse vector. A value of 0.0 means that all elements are zero.
pub fn new(elements: usize, non_null: f64) -> Self { pub fn new(elements: usize, non_null: f64) -> Self {
// calculate the number of non-zero elements
let non_zero_elements = (elements as f64 * non_null) as usize; let non_zero_elements = (elements as f64 * non_null) as usize;
// create the map
let mut map = BTreeMap::new(); let mut map = BTreeMap::new();
let mut rng = rand::thread_rng(); let mut rng = rand::thread_rng();
// generate some random values
for i in 0..non_zero_elements { for i in 0..non_zero_elements {
// generate a random index that continuesly increases
let idx = i as f32 / non_zero_elements as f32 * (elements as f32 - 4.0) + rng.gen_range(0.0..3.0); let idx = i as f32 / non_zero_elements as f32 * (elements as f32 - 4.0) + rng.gen_range(0.0..3.0);
map.insert(idx as usize, 0.5); map.insert(idx as usize, 0.5);
@ -43,6 +69,7 @@ impl SparseVec {
} }
} }
// rudimentary macro for timing a block of code
macro_rules! time { macro_rules! time {
($name:literal, $block:expr) => {{ ($name:literal, $block:expr) => {{
let start = Instant::now(); let start = Instant::now();