Skip to content

Commit

Permalink
make NodeState clone
Browse files Browse the repository at this point in the history
  • Loading branch information
ljeub-pometry committed Jan 6, 2025
1 parent 9260555 commit 76fdc18
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 16 deletions.
2 changes: 1 addition & 1 deletion raphtory/src/algorithms/components/in_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ pub fn in_component<'graph, G: GraphViewOps<'graph>>(
NodeState::new(
node.graph.clone(),
node.graph.clone(),
distances,
distances.into(),
Some(Index::new(nodes)),
)
}
Expand Down
2 changes: 1 addition & 1 deletion raphtory/src/algorithms/components/out_components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ pub fn out_component<'graph, G: GraphViewOps<'graph>>(
NodeState::new(
node.graph.clone(),
node.graph.clone(),
distances,
distances.into(),
Some(Index::new(nodes)),
)
}
Expand Down
9 changes: 6 additions & 3 deletions raphtory/src/db/api/state/lazy_node_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ use crate::{
prelude::*,
};
use rayon::prelude::*;
use std::fmt::{Debug, Formatter};
use std::{
fmt::{Debug, Formatter},
sync::Arc,
};

#[derive(Clone)]
pub struct LazyNodeState<'graph, Op, G, GH = G> {
Expand Down Expand Up @@ -100,15 +103,15 @@ impl<'graph, Op: NodeOp + 'graph, G: GraphViewOps<'graph>, GH: GraphViewOps<'gra
NodeState::new(
self.nodes.base_graph.clone(),
self.nodes.graph.clone(),
values,
values.into(),
Some(Index::new(keys)),
)
} else {
let values = self.collect_vec();
NodeState::new(
self.nodes.base_graph.clone(),
self.nodes.graph.clone(),
values,
values.into(),
None,
)
}
Expand Down
19 changes: 11 additions & 8 deletions raphtory/src/db/api/state/node_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ impl<K: Copy + Eq + Hash + Into<usize> + From<usize> + Send + Sync> Index<K> {
}
}

#[derive(Clone)]
pub struct NodeState<'graph, V, G, GH = G> {
base_graph: G,
graph: GH,
values: Vec<V>,
values: Arc<[V]>,
keys: Option<Index<VID>>,
_marker: PhantomData<&'graph ()>,
}
Expand Down Expand Up @@ -122,7 +123,7 @@ impl<'graph, V, G: GraphViewOps<'graph>> NodeState<'graph, V, G> {
.map(|vid| values[vid.index()].clone())
.collect(),
};
Self::new(graph.clone(), graph, values, index)
Self::new(graph.clone(), graph, values.into(), index)
}

/// Construct a node state from an eval result, mapping values
Expand All @@ -147,7 +148,7 @@ impl<'graph, V, G: GraphViewOps<'graph>> NodeState<'graph, V, G> {
}

impl<'graph, V, G: GraphViewOps<'graph>, GH: GraphViewOps<'graph>> NodeState<'graph, V, G, GH> {
pub fn new(base_graph: G, graph: GH, values: Vec<V>, keys: Option<Index<VID>>) -> Self {
pub fn new(base_graph: G, graph: GH, values: Arc<[V]>, keys: Option<Index<VID>>) -> Self {
Self {
base_graph,
graph,
Expand All @@ -157,7 +158,7 @@ impl<'graph, V, G: GraphViewOps<'graph>, GH: GraphViewOps<'graph>> NodeState<'gr
}
}

pub fn into_inner(self) -> (Vec<V>, Option<Index<VID>>) {
pub fn into_inner(self) -> (Arc<[V]>, Option<Index<VID>>) {
(self.values, self.keys)
}
}
Expand Down Expand Up @@ -219,11 +220,13 @@ impl<
}

fn into_values(self) -> impl Iterator<Item = Self::OwnedValue> + 'graph {
self.values.into_iter()
(0..self.values.len()).map(move |i| self.values[i].clone())
}

fn into_par_values(self) -> impl ParallelIterator<Item = Self::OwnedValue> + 'graph {
self.values.into_par_iter()
(0..self.values.len())
.into_par_iter()
.map(move |i| self.values[i].clone())
}

fn iter<'a>(
Expand Down Expand Up @@ -350,15 +353,15 @@ mod test {
let float_state = NodeState {
base_graph: g.clone(),
graph: g.clone(),
values: vec![0.0f64],
values: [0.0f64].into(),
keys: None,
_marker: Default::default(),
};

let int_state = NodeState {
base_graph: g.clone(),
graph: g.clone(),
values: vec![1i64],
values: [1i64].into(),
keys: None,
_marker: Default::default(),
};
Expand Down
6 changes: 3 additions & 3 deletions raphtory/src/db/api/state/node_state_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub trait NodeStateOps<'graph>:
NodeState::new(
self.base_graph().clone(),
self.graph().clone(),
values,
values.into(),
Some(Index::new(keys)),
)
}
Expand All @@ -131,7 +131,7 @@ pub trait NodeStateOps<'graph>:
NodeState::new(
self.base_graph().clone(),
self.graph().clone(),
values,
values.into(),
Some(Index::new(keys)),
)
}
Expand Down Expand Up @@ -168,7 +168,7 @@ pub trait NodeStateOps<'graph>:
NodeState::new(
self.base_graph().clone(),
self.graph().clone(),
values,
values.into(),
Some(Index::new(keys)),
)
}
Expand Down

0 comments on commit 76fdc18

Please sign in to comment.