Skip to content

Commit

Permalink
sorted_simplify workign
Browse files Browse the repository at this point in the history
  • Loading branch information
vigna committed Nov 14, 2024
1 parent 4513d3a commit 7d23314
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/cli/transform/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,14 +273,15 @@ where
.endianness::<E>()
.load()?;

let num_nodes = seq_graph.num_nodes();
// transpose the graph
let sorted =
crate::transform::simplify(&seq_graph, args.batch_size.batch_size).unwrap();
crate::transform::simplify_sorted(seq_graph, args.batch_size.batch_size).unwrap();

BvComp::parallel_endianness(
&args.dst,
&sorted,
seq_graph.num_nodes(),
num_nodes,
args.ca.into(),
&thread_pool,
dir,
Expand Down
5 changes: 5 additions & 0 deletions src/graphs/arc_list_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ pub struct Succ<'succ, L, I: IntoIterator<Item = (usize, usize, L)>> {
node_iter: &'succ mut Iter<L, I>,
}

unsafe impl<'a, L, I: IntoIterator<Item = (usize, usize, L)>> SortedIterator for Succ<'a, L, I> where
I::IntoIter: SortedIterator
{
}

impl<'a, L, I: IntoIterator<Item = (usize, usize, L)>> Iterator for Succ<'a, L, I> {
type Item = (usize, L);
fn next(&mut self) -> Option<Self::Item> {
Expand Down
20 changes: 11 additions & 9 deletions src/transform/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::graphs::{
arc_list_graph, no_selfloops_graph::NoSelfLoopsGraph, union_graph::UnionGraph,
};
use crate::labels::Left;
use crate::traits::{SequentialGraph, SortedIterator, SortedLender, SplitLabeling};
use crate::traits::{LenderIntoIter, SequentialGraph, SortedIterator, SortedLender, SplitLabeling};
use crate::utils::sort_pairs::{BatchIterator, KMergeIters, SortPairs};
use anyhow::{Context, Result};
use dsi_progress_logger::prelude::*;
Expand All @@ -27,18 +27,20 @@ use super::transpose;
/// sorting half the number of arcs of
/// [`simplify`](crate::transform::simplify::simplify).
#[allow(clippy::type_complexity)]
pub fn simplify_sorted<G: SequentialGraph + SortedLender + SortedIterator>(
graph: &G,
pub fn simplify_sorted<G: SequentialGraph>(
graph: G,
batch_size: usize,
) -> Result<
NoSelfLoopsGraph<
UnionGraph<&G, Left<arc_list_graph::ArcListGraph<KMergeIters<BatchIterator<()>, ()>>>>,
UnionGraph<G, Left<arc_list_graph::ArcListGraph<KMergeIters<BatchIterator<()>, ()>>>>,
>,
> {
Ok(NoSelfLoopsGraph(UnionGraph(
graph,
transpose(graph, batch_size).context("Could not transpose the graph")?,
)))
>
where
for<'a> G::Lender<'a>: SortedLender,
for<'a, 'b> LenderIntoIter<'a, G::Lender<'b>>: SortedIterator,
{
let transpose = transpose(&graph, batch_size).context("Could not transpose the graph")?;
Ok(NoSelfLoopsGraph(UnionGraph(graph, transpose)))
}

/// Returns a simplified (i.e., undirected and loopless) version of the provided
Expand Down
9 changes: 8 additions & 1 deletion src/utils/sort_pairs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//! Facilities to sort externally pairs of nodes with an associated label.
use super::{ArcMmapHelper, MmapHelper};
use crate::traits::{BitDeserializer, BitSerializer};
use crate::traits::{BitDeserializer, BitSerializer, SortedIterator};
use anyhow::{anyhow, Context};
use dary_heap::PeekMut;
use dsi_bitstream::prelude::*;
Expand Down Expand Up @@ -387,6 +387,8 @@ impl<D: BitDeserializer<NE, BitReader> + Clone> Clone for BatchIterator<D> {
}
}

unsafe impl<D: BitDeserializer<NE, BitReader>> SortedIterator for BatchIterator<D> {}

impl<D: BitDeserializer<NE, BitReader>> Iterator for BatchIterator<D> {
type Item = (usize, usize, D::DeserType);
fn next(&mut self) -> Option<Self::Item> {
Expand Down Expand Up @@ -495,6 +497,11 @@ impl<T, I: Iterator<Item = (usize, usize, T)>> KMergeIters<I, T> {
}
}

unsafe impl<T, I: Iterator<Item = (usize, usize, T)> + SortedIterator> SortedIterator
for KMergeIters<I, T>
{
}

#[allow(clippy::uninit_assumed_init)]
impl<T, I: Iterator<Item = (usize, usize, T)>> Iterator for KMergeIters<I, T> {
type Item = (usize, usize, T);
Expand Down

0 comments on commit 7d23314

Please sign in to comment.