Skip to main content

webgraph_cli/perm/
rand.rs

1/*
2 * SPDX-FileCopyrightText: 2023 Inria
3 * SPDX-FileCopyrightText: 2023 Tommaso Fontana
4 * SPDX-FileCopyrightText: 2026 Sebastiano Vigna
5 *
6 * SPDX-License-Identifier: Apache-2.0 OR MIT
7 */
8
9use crate::{IntSliceFormat, create_parent_dir};
10use anyhow::Result;
11use clap::Parser;
12use rand::prelude::SliceRandom;
13use std::path::PathBuf;
14
15#[derive(Parser, Debug)]
16#[command(name = "rand", about = "Creates a random permutation.", long_about = None, next_line_help = true)]
17pub struct CliArgs {
18    /// The number of elements in the permutation.​
19    pub len: usize,
20    /// The filename of the random permutation.​
21    pub dst: PathBuf,
22
23    #[arg(long, value_enum, default_value_t)]
24    /// The format of the permutation file.​
25    pub fmt: IntSliceFormat,
26}
27
28pub fn main(args: CliArgs) -> Result<()> {
29    create_parent_dir(&args.dst)?;
30
31    let mut rng = rand::rng();
32    let mut perm = (0..args.len).collect::<Vec<_>>();
33    perm.shuffle(&mut rng);
34
35    args.fmt.store(&args.dst, &perm, None)?;
36
37    Ok(())
38}