total_space/
utilities.rs

1//! Odds and ends.
2
3use std::collections::hash_map::DefaultHasher;
4use std::fmt::Debug;
5use std::fmt::Display;
6use std::hash::Hash;
7use std::hash::Hasher;
8
9/// A trait for data having a short name.
10pub trait Name {
11    fn name(&self) -> String;
12}
13
14/// A trait for data that has a short name (via `AsRef<&'static str>`) and a full display name (via
15/// `Display`).
16pub trait Named = Display + Name;
17
18/// A trait for anything we use as a key in a HashMap.
19pub trait KeyLike = Eq + Hash + Copy + Debug + Sized;
20
21/// A trait for data we pass around in the model.
22pub trait DataLike = KeyLike + Named + Default;
23
24/// A trait for anything we use as a zero-based index.
25pub trait IndexLike: KeyLike + PartialOrd + Ord {
26    /// Convert a `usize` to the index.
27    fn from_usize(value: usize) -> Self;
28
29    /// Convert the index to a `usize`.
30    fn to_usize(&self) -> usize;
31
32    /// The invalid (maximal) value.
33    fn invalid() -> Self;
34
35    /// Decrement the value.
36    fn decr(&mut self) {
37        let value = self.to_usize();
38        assert!(value > 0);
39        *self = Self::from_usize(value - 1);
40    }
41
42    /// Increment the value.
43    fn incr(&mut self) {
44        assert!(self.is_valid());
45        let value = self.to_usize();
46        *self = Self::from_usize(value + 1);
47        assert!(self.is_valid());
48    }
49
50    /// Is a valid value (not the maximal value).
51    fn is_valid(&self) -> bool {
52        *self != Self::invalid()
53    }
54}
55
56pub(crate) const RIGHT_ARROW: &str = "&#8594;";
57
58pub(crate) const RIGHT_DOUBLE_ARROW: &str = "&#8658;";
59
60pub(crate) fn calculate_string_hash(string: &str) -> u64 {
61    let mut hasher = DefaultHasher::new();
62    string.hash(&mut hasher);
63    hasher.finish()
64}
65
66pub(crate) fn calculate_strings_hash(first: &str, second: &str) -> u64 {
67    let mut hasher = DefaultHasher::new();
68    first.hash(&mut hasher);
69    second.hash(&mut hasher);
70    hasher.finish()
71}