eta_graph/
vertex_storage.rs

1use std::ops::{Index, IndexMut};
2use std::slice::{Iter, IterMut};
3use crate::handles::types::VHandle;
4use crate::traits::{StoreVertex};
5
6pub struct VertexStorage<VertexType> {
7    data: Vec<VertexType>,
8}
9impl<VertexType> Default for VertexStorage<VertexType> {
10    fn default() -> Self {
11        Self::new()
12    }
13}
14
15impl <VertexType> VertexStorage<VertexType> {
16    pub fn new() -> Self {
17        VertexStorage {
18            data: Vec::new(),
19        }
20    }
21
22    #[inline(always)]
23    pub fn push(&mut self, val: VertexType) {
24        self.data.push(val);
25    }
26}
27
28impl<VertexType> Index<VHandle> for VertexStorage<VertexType> {
29    type Output = VertexType;
30    #[inline(always)]
31    fn index(&self, index: VHandle) -> &Self::Output {
32        return self.data.index(index as usize);
33    }
34}
35
36impl<VertexType> IndexMut<VHandle> for VertexStorage<VertexType> {
37    #[inline(always)]
38    fn index_mut(&mut self, index: VHandle) -> &mut Self::Output {
39        &mut self.data[index as usize]
40    }
41}
42
43impl<VertexType> Clone for VertexStorage<VertexType>
44where VertexType: Clone {
45    #[inline(always)]
46    fn clone(&self) -> Self {
47        VertexStorage {
48            data: self.data.clone(),
49        }
50    }
51}
52impl <VertexType> StoreVertex for VertexStorage<VertexType>
53{
54    type VertexType = VertexType;
55    #[inline(always)]
56    fn is_empty(&self) -> bool {
57        self.data.is_empty()
58    }
59    #[inline(always)]
60    fn len(&self) -> usize {
61        self.data.len()
62    }
63    #[inline(always)]
64    fn push(&mut self, val: VertexType) {
65        self.data.push(val);
66    }
67    #[inline(always)]
68    fn capacity(&self) -> usize {
69        self.data.capacity()
70    }
71    #[inline(always)]
72    fn iter(&self) -> Iter<VertexType>{
73        self.data.iter()
74    }
75    #[inline(always)]
76    fn iter_mut(&mut self) -> IterMut<VertexType> {
77        self.data.iter_mut()
78    }
79
80    #[inline(always)]
81    fn as_slice(&self) -> &[VertexType] {
82        self.data.as_slice()
83    }
84}