webgraph_cli/to/
ascii.rs

1/*
2 * SPDX-FileCopyrightText: 2023 Inria
3 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
4 *
5 * SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
6 */
7
8use crate::GlobalArgs;
9use anyhow::Result;
10use clap::Parser;
11use dsi_bitstream::dispatch::factory::CodesReaderFactoryHelper;
12use dsi_bitstream::prelude::*;
13use dsi_progress_logger::prelude::*;
14use lender::*;
15use std::path::PathBuf;
16use webgraph::graphs::bvgraph::get_endianness;
17use webgraph::traits::SequentialLabeling;
18use webgraph::utils::MmapHelper;
19
20#[derive(Parser, Debug)]
21#[command(name = "ascii", about = "Dumps a graph in ASCII format: a line for each node with successors separated by tabs.", long_about = None)]
22pub struct CliArgs {
23    /// The basename of the graph.
24    pub src: PathBuf,
25}
26
27pub fn main(global_args: GlobalArgs, args: CliArgs) -> Result<()> {
28    match get_endianness(&args.src)?.as_str() {
29        #[cfg(feature = "be_bins")]
30        BE::NAME => ascii_convert::<BE>(global_args, args),
31        #[cfg(feature = "le_bins")]
32        LE::NAME => ascii_convert::<LE>(global_args, args),
33        e => panic!("Unknown endianness: {}", e),
34    }
35}
36
37pub fn ascii_convert<E: Endianness + 'static>(global_args: GlobalArgs, args: CliArgs) -> Result<()>
38where
39    MmapHelper<u32>: CodesReaderFactoryHelper<E>,
40{
41    let seq_graph = webgraph::graphs::bvgraph::sequential::BvGraphSeq::with_basename(args.src)
42        .endianness::<E>()
43        .load()?;
44
45    let mut pl = ProgressLogger::default();
46    pl.display_memory(true).item_name("offset");
47
48    if let Some(duration) = global_args.log_interval {
49        pl.log_interval(duration);
50    }
51
52    pl.start("Computing offsets...");
53
54    let mut iter = seq_graph.iter();
55    while let Some((node_id, successors)) = iter.next() {
56        println!(
57            "{}\t{}",
58            node_id,
59            successors
60                .map(|x| x.to_string())
61                .collect::<Vec<_>>()
62                .join("\t")
63        );
64    }
65
66    pl.done();
67
68    Ok(())
69}