1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use matrix::BasicReadableMatrix;
use matrix::BasicWriteableMatrix;
use matrix::SparseMatrix;

use std::collections::BTreeMap;

pub struct SparseDOK {
    val: BTreeMap<(usize, usize), f32>,
    rows: usize,
    columns: usize
}

impl SparseDOK {
    
	/// creates a new matrix
	pub fn new(rows: usize, columns: usize) -> SparseDOK {
		let m = SparseDOK{val: BTreeMap::new(),  rows: rows, columns: columns};
		return m;
	}

	/// resizes the vector
	pub fn resize(&mut self, rows: usize, columns: usize) {
        self.val = BTreeMap::new();
        self.rows = rows;
        self.columns = columns;
	}
}

impl BasicReadableMatrix for SparseDOK {

	/// returns the number of rows in the matrix
	fn get_rows(&self) -> usize {
		return self.rows;
	}

	/// returns the number of columns in the matrix
	fn get_columns(&self) -> usize {
		return self.columns;
	}

	/// returns the (i,j)-th element of the matrix
	fn get_element(&self, i: usize, j: usize)-> f32 {

		if i >= self.get_rows() || j >= self.get_columns() {
			panic!("access out of bounds");
        }

        let key = (i,j);
        
        match self.val.get(&key) {
            None => return 0.0,
            Some(value) => return *value,
        }
	}
}



impl BasicWriteableMatrix for SparseDOK {

	/// sets the (i,j)-th element of the sparse matrix 
	fn set_element(&mut self, i: usize, j: usize, new_value: f32) {

		if i >= self.get_rows() || j >= self.get_columns() {
			panic!("assignment out of bounds");
        }
        
        let key = (i,j);
        
        self.val.insert(key, new_value);
        
	}

    /// sets all elements to zero
    fn set_zero(&mut self) {

		self.val.clear()
    }

}


impl SparseMatrix for SparseDOK {

    /// returns the number of non-null matrix elements
    fn nnz(&self) -> usize {
        return self.val.len();
    }

}