cnetworks/lib.rs
1/*!
2This is a library of tools for creating and manipulating complex networks.
3
4From [Wikipedia](https://en.wikipedia.org/wiki/Complex_network):
5
6> Complex network is a graph-like structure with non-trivial characteristics.
7> A complex network is a graph (network) with non-trivial topological features—features that do
8> not occur in simple networks such as lattices or random graphs but often occur in networks
9> representing real systems. The study of complex networks is a young and active area of scientific
10> research inspired largely by empirical findings of real-world networks such as computer networks,
11> biological networks, technological networks, brain networks, climate networks and social networks.
12*/
13mod model;
14mod network;
15mod vecmap;
16mod weight;
17
18pub mod bfs;
19pub mod locate;
20pub mod misc;
21pub mod si;
22
23pub use model::Model;
24pub use network::Network;
25pub use weight::Weight;
26
27pub mod cn {
28 //! Library-specific utility module.
29 //!
30 //! This module serves as a namespace for the various utilities with generic names.
31
32 pub use crate::vecmap::VecMap;
33 pub use crate::vecmap::VecSet;
34 use rustc_hash::FxHasher;
35 use std::hash::BuildHasherDefault as Builder;
36
37 /// Variant of [`indexmap::IndexMap`] using [`FxHasher`].
38 pub type IndexMap<K, V> = indexmap::IndexMap<K, V, Builder<FxHasher>>;
39 /// Variant of [`indexmap::IndexSet`] using [`FxHasher`].
40 pub type IndexSet<T> = indexmap::IndexSet<T, Builder<FxHasher>>;
41
42 /// Shorthand trait for **exact size**, **double-ended** iterator.
43 pub trait Iter<I>: DoubleEndedIterator<Item = I> {}
44 impl<T: DoubleEndedIterator + Iterator<Item = I>, I> Iter<I> for T {}
45
46 /// Error enum used for various recoverable errors
47 #[derive(Debug, PartialEq)]
48 pub enum Err {
49 /// Emitted whenever a change involving a non-existent node has been requested.
50 NoSuchNode(usize),
51 /// Emitted when the link weight is not in `(0, 1]`.
52 BadWeight(f64),
53 /// Emitted when an attempt is made to link a node to itself.
54 LinkToSelf(usize),
55 /// Emitted when constant probability is not in `(0, 1]`.
56 BadProbability(f64),
57 /// Emitted when an attempt is made to attach a node which already exists.
58 NodeExists(usize),
59 /// Emitted when an attempt is made to run an algorithm requiring at least 1 target without
60 /// targets.
61 NoTarget,
62 /// Emitted when the network is required to be whole, but is not.
63 NotWhole,
64 }
65
66 /// A variant of [`std::result::Result`] which returns [`cn::Err`](crate::cn::Err).
67 pub type Result<T> = std::result::Result<T, Err>;
68
69 impl std::fmt::Display for Err {
70 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
71 let message = match self {
72 Err::NoSuchNode(i) => format!("No such node {}", i),
73 Err::BadWeight(w) => format!("Link weight cannot be {}", w),
74 Err::LinkToSelf(i) => format!("Attempted linking node {} to itself", i),
75 Err::BadProbability(p) => format!("Probability cannot be {}", p),
76 Err::NodeExists(i) => format!("Node {} already exists", i),
77 Err::NoTarget => "No search targets were specified".to_string(),
78 Err::NotWhole => "The network must be whole".to_string(),
79 };
80 write!(f, "{}", message)
81 }
82 }
83
84 impl std::error::Error for Err {}
85}