Skip to content

Commit

Permalink
Avoid duplication layout application
Browse files Browse the repository at this point in the history
  • Loading branch information
s1ck committed Nov 17, 2023
1 parent b8d0576 commit a28d250
Showing 1 changed file with 16 additions and 23 deletions.
39 changes: 16 additions & 23 deletions crates/builder/src/graph/adj_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,25 +58,28 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
#[inline]
pub(crate) fn insert(&self, source: NI, target: Target<NI, EV>) {
let mut edges = self.edges[source.index()].write().unwrap();

match self.layout {
CsrLayout::Sorted => match edges.binary_search(&target) {
Ok(i) => edges.insert(i, target),
Err(i) => edges.insert(i, target),
},
CsrLayout::Unsorted => edges.push(target),
CsrLayout::Deduplicated => match edges.binary_search(&target) {
Ok(_) => {}
Err(i) => edges.insert(i, target),
},
};
Self::apply_layout(self.layout, &mut edges, target);
}

#[inline]
pub(crate) fn insert_mut(&mut self, source: NI, target: Target<NI, EV>) {
let edges = self.edges[source.index()].get_mut().unwrap();
Self::apply_layout(self.layout, edges, target);
}

match self.layout {
#[inline]
fn check_bounds(&self, node: NI) -> Result<(), crate::Error> {
if node >= self.node_count() {
return Err(crate::Error::MissingNode {
node: format!("{}", node.index()),
});
};
Ok(())
}

#[inline]
fn apply_layout(layout: CsrLayout, edges: &mut Vec<Target<NI, EV>>, target: Target<NI, EV>) {
match layout {
CsrLayout::Sorted => match edges.binary_search(&target) {
Ok(i) => edges.insert(i, target),
Err(i) => edges.insert(i, target),
Expand All @@ -88,16 +91,6 @@ impl<NI: Idx, EV> AdjacencyList<NI, EV> {
},
};
}

#[inline]
fn check_bounds(&self, node: NI) -> Result<(), crate::Error> {
if node >= self.node_count() {
return Err(crate::Error::MissingNode {
node: format!("{}", node.index()),
});
};
Ok(())
}
}

#[derive(Debug)]
Expand Down

0 comments on commit a28d250

Please sign in to comment.