1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*!
This is a library of tools for creating and manipulating complex networks.

From [Wikipedia](https://en.wikipedia.org/wiki/Complex_network):

> Complex network is a graph-like structure with non-trivial characteristics.
> A complex network is a graph (network) with non-trivial topological features—features that do
> not occur in simple networks such as lattices or random graphs but often occur in networks
> representing real systems. The study of complex networks is a young and active area of scientific
> research inspired largely by empirical findings of real-world networks such as computer networks,
> biological networks, technological networks, brain networks, climate networks and social networks.
*/
mod model;
mod network;
mod vecmap;
mod weight;

pub mod bfs;
pub mod locate;
pub mod misc;
pub mod si;

pub use model::Model;
pub use network::Network;
pub use weight::Weight;

pub mod cn {
    //! Library-specific utility module.
    //!
    //! This module serves as a namespace for the various utilities with generic names.

    pub use crate::vecmap::VecMap;
    pub use crate::vecmap::VecSet;
    use rustc_hash::FxHasher;
    use std::hash::BuildHasherDefault as Builder;

    /// Variant of [`indexmap::IndexMap`] using [`FxHasher`].
    pub type IndexMap<K, V> = indexmap::IndexMap<K, V, Builder<FxHasher>>;
    /// Variant of [`indexmap::IndexSet`] using [`FxHasher`].
    pub type IndexSet<T> = indexmap::IndexSet<T, Builder<FxHasher>>;

    /// Shorthand trait for **exact size**, **double-ended** iterator.
    pub trait Iter<I>: DoubleEndedIterator<Item = I> {}
    impl<T: DoubleEndedIterator + Iterator<Item = I>, I> Iter<I> for T {}

    /// Error enum used for various recoverable errors
    #[derive(Debug, PartialEq)]
    pub enum Err {
        /// Emitted whenever a change involving a non-existent node has been requested.
        NoSuchNode(usize),
        /// Emitted when the link weight is not in `(0, 1]`.
        BadWeight(f64),
        /// Emitted when an attempt is made to link a node to itself.
        LinkToSelf(usize),
        /// Emitted when constant probability is not in `(0, 1]`.
        BadProbability(f64),
        /// Emitted when an attempt is made to attach a node which already exists.
        NodeExists(usize),
        /// Emitted when an attempt is made to run an algorithm requiring at least 1 target without
        /// targets.
        NoTarget,
        /// Emitted when the network is required to be whole, but is not.
        NotWhole,
    }

    /// A variant of [`std::result::Result`] which returns [`cn::Err`](crate::cn::Err).
    pub type Result<T> = std::result::Result<T, Err>;

    impl std::fmt::Display for Err {
        fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
            let message = match self {
                Err::NoSuchNode(i) => format!("No such node {}", i),
                Err::BadWeight(w) => format!("Link weight cannot be {}", w),
                Err::LinkToSelf(i) => format!("Attempted linking node {} to itself", i),
                Err::BadProbability(p) => format!("Probability cannot be {}", p),
                Err::NodeExists(i) => format!("Node {} already exists", i),
                Err::NoTarget => "No search targets were specified".to_string(),
                Err::NotWhole => "The network must be whole".to_string(),
            };
            write!(f, "{}", message)
        }
    }

    impl std::error::Error for Err {}
}