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
use std::sync::{
    Arc,
    Mutex,
    MutexGuard,
};

use freya_native_core::NodeId;
use rustc_hash::FxHashMap;

#[derive(Default, Clone)]
pub struct Layers {
    pub layers: Arc<Mutex<FxHashMap<i16, Vec<NodeId>>>>,
}

impl Layers {
    pub fn insert_node_in_layer(&self, node_id: NodeId, layer_n: i16) {
        let mut layers = self.layers.lock().unwrap();
        let layer = layers.entry(layer_n).or_default();
        layer.push(node_id);
    }

    pub fn remove_node_from_layer(&self, node_id: NodeId, layer_n: i16) {
        let mut layers = self.layers.lock().unwrap();
        let layer = layers.get_mut(&layer_n).unwrap();
        layer.retain(|id| *id != node_id);

        if layer.is_empty() {
            layers.remove(&layer_n);
        }
    }

    pub fn layers(&self) -> MutexGuard<FxHashMap<i16, Vec<NodeId>>> {
        self.layers.lock().unwrap()
    }

    pub fn len_layers(&self) -> usize {
        self.layers.lock().unwrap().len()
    }
}