raphtory_api/core/entities/
layers.rs

1use crate::core::storage::arc_str::ArcStr;
2use iter_enum::{
3    DoubleEndedIterator, ExactSizeIterator, FusedIterator, IndexedParallelIterator, Iterator,
4    ParallelExtend, ParallelIterator,
5};
6use rayon::prelude::*;
7use std::{iter::Copied, sync::Arc};
8
9#[derive(Debug, Clone)]
10pub enum Layer {
11    All,
12    None,
13    Default,
14    One(ArcStr),
15    Multiple(Arc<[ArcStr]>),
16}
17
18impl Layer {
19    pub fn contains(&self, name: &str) -> bool {
20        match self {
21            Layer::All => true,
22            Layer::None => false,
23            Layer::Default => name == "_default",
24            Layer::One(layer) => layer == name,
25            Layer::Multiple(layers) => layers.iter().any(|l| l == name),
26        }
27    }
28}
29
30pub trait SingleLayer {
31    fn name(self) -> ArcStr;
32}
33
34impl<T: SingleLayer> From<T> for Layer {
35    fn from(value: T) -> Self {
36        Layer::One(value.name())
37    }
38}
39
40impl SingleLayer for ArcStr {
41    fn name(self) -> ArcStr {
42        self
43    }
44}
45
46impl SingleLayer for String {
47    fn name(self) -> ArcStr {
48        self.into()
49    }
50}
51impl SingleLayer for &str {
52    fn name(self) -> ArcStr {
53        self.into()
54    }
55}
56
57impl SingleLayer for &String {
58    fn name(self) -> ArcStr {
59        self.as_str().into()
60    }
61}
62
63impl SingleLayer for &ArcStr {
64    fn name(self) -> ArcStr {
65        self.clone()
66    }
67}
68
69impl<T: SingleLayer> SingleLayer for Option<T> {
70    fn name(self) -> ArcStr {
71        match self {
72            None => ArcStr::from("_default"),
73            Some(s) => s.name(),
74        }
75    }
76}
77
78impl<T: SingleLayer> From<Vec<T>> for Layer {
79    fn from(names: Vec<T>) -> Self {
80        match names.len() {
81            0 => Layer::None,
82            1 => Layer::One(names.into_iter().next().unwrap().name()),
83            _ => Layer::Multiple(
84                names
85                    .into_iter()
86                    .map(|s| s.name())
87                    .collect::<Vec<_>>()
88                    .into(),
89            ),
90        }
91    }
92}
93
94impl<T: SingleLayer, const N: usize> From<[T; N]> for Layer {
95    fn from(names: [T; N]) -> Self {
96        match N {
97            0 => Layer::None,
98            1 => Layer::One(names.into_iter().next().unwrap().name()),
99            _ => Layer::Multiple(
100                names
101                    .into_iter()
102                    .map(|s| s.name())
103                    .collect::<Vec<_>>()
104                    .into(),
105            ),
106        }
107    }
108}
109
110#[derive(Clone, Debug)]
111pub enum LayerIds {
112    None,
113    All,
114    One(usize),
115    Multiple(Multiple),
116}
117
118#[derive(
119    Iterator,
120    DoubleEndedIterator,
121    ExactSizeIterator,
122    FusedIterator,
123    ParallelIterator,
124    ParallelExtend,
125    IndexedParallelIterator,
126)]
127pub enum LayerVariants<None, All, One, Multiple> {
128    None(None),
129    All(All),
130    One(One),
131    Multiple(Multiple),
132}
133
134#[derive(Clone, Debug, Default)]
135pub struct Multiple(pub Arc<[usize]>);
136
137impl<'a> IntoIterator for &'a Multiple {
138    type Item = usize;
139    type IntoIter = Copied<core::slice::Iter<'a, usize>>;
140
141    fn into_iter(self) -> Self::IntoIter {
142        self.0.iter().copied()
143    }
144}
145
146impl Multiple {
147    #[inline]
148    pub fn contains(&self, id: usize) -> bool {
149        self.0.binary_search(&id).is_ok()
150    }
151
152    #[inline]
153    pub fn into_iter(&self) -> impl Iterator<Item = usize> {
154        let ids = self.0.clone();
155        (0..ids.len()).map(move |i| ids[i])
156    }
157
158    #[inline]
159    pub fn iter(&self) -> impl Iterator<Item = usize> + '_ {
160        self.0.iter().copied()
161    }
162
163    #[inline]
164    pub fn get_id_by_index(&self, index: usize) -> Option<usize> {
165        self.0.get(index).copied()
166    }
167
168    #[inline]
169    pub fn get_index_by_id(&self, id: usize) -> Option<usize> {
170        self.0.binary_search(&id).ok()
171    }
172
173    #[inline]
174    pub fn par_iter(&self) -> impl rayon::iter::ParallelIterator<Item = usize> {
175        let bit_vec = self.0.clone();
176        (0..bit_vec.len()).into_par_iter().map(move |i| bit_vec[i])
177    }
178
179    #[inline]
180    pub fn len(&self) -> usize {
181        self.0.len()
182    }
183
184    #[inline]
185    pub fn is_empty(&self) -> bool {
186        self.0.is_empty()
187    }
188}
189
190impl FromIterator<usize> for Multiple {
191    fn from_iter<I: IntoIterator<Item = usize>>(iter: I) -> Self {
192        let mut inner: Vec<_> = iter.into_iter().collect();
193        inner.sort();
194        inner.dedup();
195        Multiple(inner.into())
196    }
197}
198
199impl From<Vec<usize>> for Multiple {
200    fn from(mut v: Vec<usize>) -> Self {
201        v.sort();
202        v.dedup();
203        Multiple(v.into())
204    }
205}