Skip to main content

webgraph_cli/seq/
int.rs

1/*
2 * SPDX-FileCopyrightText: 2026 Sebastiano Vigna
3 *
4 * SPDX-License-Identifier: Apache-2.0 OR MIT
5 */
6
7use crate::{IntSlice, IntSliceFormat};
8use anyhow::Result;
9use clap::Parser;
10use std::path::PathBuf;
11use value_traits::slices::SliceByValue;
12
13#[derive(Parser, Debug)]
14#[command(name = "int", about = "Converts an integer sequence (e.g., permutations, eccentricities, components) between formats.", long_about = None, next_line_help = true)]
15pub struct CliArgs {
16    /// The source file path.​
17    pub src: PathBuf,
18
19    /// The destination file path.​
20    pub dst: PathBuf,
21
22    #[arg(long, value_enum, default_value_t)]
23    /// The format of the source file.​
24    pub src_fmt: IntSliceFormat,
25
26    #[arg(long, value_enum, default_value_t)]
27    /// The format of the destination file.​
28    pub dst_fmt: IntSliceFormat,
29}
30
31/// Stores a slice with its computed max.
32fn store_slice(data: &[usize], dst: &PathBuf, dst_fmt: IntSliceFormat) -> Result<()> {
33    let max = data.iter().copied().max().unwrap_or(0);
34    dst_fmt.store(dst, data, Some(max))
35}
36
37/// Collects a [`SliceByValue`] into a [`Vec<usize>`] and stores it.
38fn collect_and_store(
39    slice: &impl SliceByValue<Value = usize>,
40    dst: &PathBuf,
41    dst_fmt: IntSliceFormat,
42) -> Result<()> {
43    let data: Vec<usize> = (0..slice.len()).map(|i| slice.index_value(i)).collect();
44    let max = data.iter().copied().max().unwrap_or(0);
45    dst_fmt.store(dst, &data, Some(max))
46}
47
48pub fn main(args: CliArgs) -> Result<()> {
49    let loaded = args.src_fmt.load(&args.src)?;
50    log::info!(
51        "Loaded {} elements from {}",
52        loaded.len(),
53        args.src.display()
54    );
55
56    match &loaded {
57        IntSlice::Owned(v) => store_slice(v, &args.dst, args.dst_fmt)?,
58        #[cfg(target_pointer_width = "64")]
59        IntSlice::Java(j) => collect_and_store(j, &args.dst, args.dst_fmt)?,
60        IntSlice::Epserde(m) => store_slice(m.uncase(), &args.dst, args.dst_fmt)?,
61        IntSlice::BitFieldVec(m) => collect_and_store(m.uncase(), &args.dst, args.dst_fmt)?,
62    }
63
64    log::info!("Stored {} elements to {}", loaded.len(), args.dst.display());
65    Ok(())
66}