darkfi/util/
mod.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19/// Command-line interface utilities
20pub mod cli;
21
22/// Various encoding formats
23pub mod encoding;
24
25/// Filesystem utilities
26pub mod file;
27
28/// Parsing helpers
29pub mod parse;
30
31/// Filesystem path utilities
32pub mod path;
33
34/// Time utilities
35pub mod time;
36
37/// Ring Buffer implementation
38pub mod ringbuffer;
39
40/// Logging utilities
41pub mod logger;
42
43/// Permuted Congruential Generator (PCG)
44/// This is an insecure PRNG used for simulations and tests.
45#[cfg(feature = "rand")]
46pub mod pcg;
47
48/// Return the most frequent element in vec or just any item.
49pub fn most_frequent_or_any<T: Eq + Clone>(items: &[T]) -> Option<T> {
50    if items.is_empty() {
51        return None;
52    }
53
54    let mut max_count = 0;
55    let mut most_freq = &items[0];
56
57    for i in 0..items.len() {
58        let mut count = 0;
59
60        for j in 0..items.len() {
61            if items[i] == items[j] {
62                count += 1;
63            }
64        }
65
66        if count > max_count {
67            max_count = count;
68            most_freq = &items[i];
69        }
70    }
71
72    Some(most_freq.clone())
73}