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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
use crate::error::{
    GraphBlasError, GraphBlasErrorType, LogicError, LogicErrorType, SparseLinearAlgebraError,
};
use crate::index::ElementIndex;
use crate::value_type::ValueType;

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct VectorElement<T: ValueType> {
    index: ElementIndex,
    value: T,
}

impl<T: ValueType> VectorElement<T> {
    pub fn new(index: ElementIndex, value: T) -> Self {
        Self { index, value }
    }
}

impl<T: ValueType + Clone> VectorElement<T> {
    pub fn index(&self) -> ElementIndex {
        self.index.to_owned()
    }
    pub fn value(&self) -> T {
        self.value.to_owned()
    }

    pub fn from_pair(index: ElementIndex, value: T) -> Self {
        Self::new(index, value)
    }
}

impl<T: ValueType> From<(ElementIndex, T)> for VectorElement<T> {
    fn from(element: (ElementIndex, T)) -> Self {
        Self {
            index: element.0,
            value: element.1,
        }
    }
}

// TODO: check for uniqueness
/// Equivalent to Sparse Coordinate List (COO)
#[derive(Debug, Clone, PartialEq)]
pub struct VectorElementList<T: ValueType> {
    // elements: Vec<Element<T>>,
    index: Vec<ElementIndex>,
    value: Vec<T>,
}

impl<T: ValueType + Clone + Copy> VectorElementList<T> {
    pub fn new() -> Self {
        Self {
            index: Vec::new(),
            value: Vec::new(),
        }
    }

    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            index: Vec::with_capacity(capacity),
            value: Vec::with_capacity(capacity),
        }
    }

    pub fn from_vectors(
        index: Vec<ElementIndex>,
        value: Vec<T>,
    ) -> Result<Self, SparseLinearAlgebraError> {
        #[cfg(debug_assertions)]
        if index.len() != value.len() {
            return Err(GraphBlasError::new(
                GraphBlasErrorType::DimensionMismatch,
                format!(
                    "Length of vectors must be equal: index.len() = {}, value.len() = {}",
                    index.len(),
                    value.len()
                ),
            )
            .into());
        }
        Ok(Self { index, value })
    }

    pub fn from_element_vector(elements: Vec<VectorElement<T>>) -> Self {
        let mut element_list: Self = Self::with_capacity(elements.len());
        elements
            .into_iter()
            .for_each(|element| element_list.push_element(element));
        return element_list;
    }

    pub fn push_element(&mut self, element: VectorElement<T>) -> () {
        self.index.push(element.index());
        self.value.push(element.value());
    }

    pub fn append_element_vec(&mut self, elements: Vec<VectorElement<T>>) -> () {
        let mut element_list_to_append = Self::from_element_vector(elements);
        self.index.append(&mut element_list_to_append.index);
        self.value.append(&mut element_list_to_append.value);
    }

    pub fn indices_ref(&self) -> &[ElementIndex] {
        self.index.as_slice()
    }

    pub(crate) fn index(&self, index: ElementIndex) -> Result<&ElementIndex, LogicError> {
        #[cfg(debug_assertions)]
        if index >= self.length() {
            return Err(LogicError::new(
                LogicErrorType::IndexOutOfBounds,
                format!(
                    "index value {} larger than vector length {}",
                    index,
                    self.length()
                ),
                None,
            ));
        }
        Ok(&self.index[index])
    }

    pub fn values_ref(&self) -> &[T] {
        self.value.as_slice()
    }

    // pub fn as_element_vec(&self) -> &Vec<Element<T>> {
    //     &self.elements
    // }

    // pub fn as_element_vec_mut(&mut self) -> &mut Vec<Element<T>> {
    //     &mut self.elements
    // }

    pub fn length(&self) -> usize {
        self.value.len()
    }
}

// impl<T: ValueType> From<(Vec<Index>, Vec<Index>, Vec<T>)> for ElementVector<T> {
//     fn from(elements: (Vec<Index>, Vec<Index>, Vec<T>)) -> Self {
//         ElementVector {
//             row_index: elements.0,
//             column_index: elements.1,
//             value: elements.2,
//         }
//     }
// }

// impl<T: ValueType + Clone> From<(&[Index], &[Index], &[T])> for ElementVector<T> {
//     fn from(elements: (&[Index], &[Index], &[T])) -> Self {
//         ElementVector {
//             row_index: elements.0.to_vec(),
//             column_index: elements.1.to_vec(),
//             value: elements.2.to_vec(),
//         }
//     }
// }