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
use crate::collections::sparse_vector::operations::get_element_value::GetVectorElementValue;
use crate::{
    collections::{
        sparse_matrix::operations::GetMatrixElementValueTyped,
        sparse_vector::{SparseVector, VectorElement},
    },
    error::SparseLinearAlgebraError,
    index::ElementIndex,
    value_type::ValueType,
};

use super::GetVectorElementValueTyped;

pub trait GetVectorElement<T: ValueType> {
    fn get_element(
        &self,
        index: ElementIndex,
    ) -> Result<Option<VectorElement<T>>, SparseLinearAlgebraError>;
    fn get_element_or_default(
        &self,
        index: ElementIndex,
    ) -> Result<VectorElement<T>, SparseLinearAlgebraError>;
}

impl<T: ValueType + Default + GetVectorElementValueTyped<T>> GetVectorElement<T>
    for SparseVector<T>
{
    fn get_element(
        &self,
        index: ElementIndex,
    ) -> Result<Option<VectorElement<T>>, SparseLinearAlgebraError> {
        match self.get_element_value(&index)? {
            Some(value) => Ok(Some(VectorElement::new(index, value))),
            None => Ok(None),
        }
    }

    fn get_element_or_default(
        &self,
        index: ElementIndex,
    ) -> Result<VectorElement<T>, SparseLinearAlgebraError> {
        Ok(VectorElement::new(
            index,
            self.get_element_value_or_default(&index)?,
        ))
    }
}