gdsl 0.2.1

GDSL is a graph data-structure library including graph containers, connected node strutures and efficient algorithms on those structures. Nodes are independent of a graph container and can be used as connected smart pointers.
Documentation
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
//! # Node<K, N, E>
//!
//! `Node` is a key value pair smart-pointer, which includes inbound and
//! outbound connections to other nodes. Nodes can be created
//! individually and they don't depend on any graph container. They are
//! essentially smart-pointers that contain connections to other similar
//! smart pointers. For two nodes to be able to connect, they must have the
//! same type signature. Uniqueness is determined by the node's key.
//!
//! A node's type signature is <KeyType, NodeValueType, EdgeValueType>.
//!
//! - The `KeyType` is required and is used to identify the node.
//! - The `NodeValueType` is optional (supply `()` in type signature)
//!   and is used to store data associated with the node.
//! - The `EdgeValueType` is optional (supply `()` in type signature)
//!   and is used to store data associated with the edge.
//!
//! ```
//! use gdsl::ungraph::*;
//!
//! type N<'a> = Node<usize, &'a str, f64>;
//!
//! let n1 = N::new(1, "Naughty Node");
//! ```
//!
//! For an inner value type to be mutable, it must be wrapped in a mutable
//! pointer such as a `Cell`, `RefCell`, or `Mutex`.
//!
//! Node's are wrapped in a reference counted smart pointer. This means
//! that a node can be cloned and shared among multiple owners.
//!
//! This node uses `Rc` for reference counting, thus it is not thread-safe.

mod adjacent;
mod algo;

use crate::error::Error;
use std::{
    cell::RefCell,
    fmt::Display,
    hash::Hash,
    ops::Deref,
    rc::{Rc, Weak},
};

use self::{
    adjacent::*,
    algo::{bfs::*, dfs::*, order::*, pfs::*},
};

/// An edge between nodes is a tuple struct `Edge(u, v, e)` where `u` is the
/// source node, `v` is the target node, and `e` is the edge's value.
#[derive(Clone)]
pub struct Edge<K, N, E>(pub Node<K, N, E>, pub Node<K, N, E>, pub E)
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone;

impl<K, N, E> Edge<K, N, E>
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone,
{
    /// Returns the source node of the edge.
    pub fn source(&self) -> &Node<K, N, E> {
        &self.0
    }

    /// Returns the target node of the edge.
    pub fn target(&self) -> &Node<K, N, E> {
        &self.1
    }

    /// Returns the edge's value.
    pub fn value(&self) -> &E {
        &self.2
    }

    /// Reverse the edge's direction.
    pub fn reverse(&self) -> Edge<K, N, E> {
        Edge(self.1.clone(), self.0.clone(), self.2.clone())
    }
}

impl<K, N, E> PartialEq for Edge<K, N, E>
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone + Ord,
{
    fn eq(&self, other: &Edge<K, N, E>) -> bool {
        self.2 == other.2
    }
}

impl<K, N, E> Eq for Edge<K, N, E>
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone + Ord,
{
}

impl<K, N, E> PartialOrd for Edge<K, N, E>
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone + Ord,
{
    fn partial_cmp(&self, other: &Edge<K, N, E>) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl<K, N, E> Ord for Edge<K, N, E>
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone + Ord,
{
    fn cmp(&self, other: &Edge<K, N, E>) -> std::cmp::Ordering {
        self.2.cmp(&other.2)
    }
}

/// A `Node<K, N, E>` is a key value pair smart-pointer, which includes inbound and
/// outbound connections to other nodes. Nodes can be created individually and they
/// don't depend on a graph container. Generic parameters include `K` for the node's
/// key, `N` for the node's value, and `E` for the edge's value. Two nodes are equal
/// if they have the same key.
///
/// # Example
///
/// ```
/// use gdsl::ungraph::*;
///
/// let a = Node::new(0x1, "A");
/// let b = Node::new(0x2, "B");
/// let c = Node::new(0x4, "C");
///
/// a.connect(&b, 0.42);
/// a.connect(&c, 1.7);
/// b.connect(&c, 0.09);
/// c.connect(&b, 12.9);
///
/// let Edge(u, v, e) = a.iter().next().unwrap();
///
/// assert!(u == a);
/// assert!(v == b);
/// assert!(e == 0.42);
/// ```
#[derive(Clone)]
pub struct Node<K = usize, N = (), E = ()>
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone,
{
    inner: Rc<(K, N, RefCell<Adjacent<K, N, E>>)>,
}

impl<K, N, E> Node<K, N, E>
where
    K: Clone + Hash + PartialEq + Eq + Display,
    N: Clone,
    E: Clone,
{
    /// Creates a new node with a given key and value. The key is used to
    /// identify the node in the graph. Two nodes with the same key are
    /// considered equal. Value is optional, node use's `()` as default
    /// value type.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::ungraph::*;
    ///
    /// let n1 = Node::<i32, char, ()>::new(1, 'A');
    ///
    /// assert!(*n1.key() == 1);
    /// assert!(*n1.value() == 'A');
    /// ```
    pub fn new(key: K, value: N) -> Self {
        Node {
            inner: Rc::new((key, value, Adjacent::new())),
        }
    }

    /// Returns a reference to the node's key.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::ungraph::*;
    ///
    /// let n1 = Node::<i32, (), ()>::new(1, ());
    ///
    /// assert!(*n1.key() == 1);
    /// ```
    pub fn key(&self) -> &K {
        &self.inner.0
    }

    /// Returns a reference to the node's value.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::ungraph::*;
    ///
    /// let n1 = Node::<i32, char, ()>::new(1, 'A');
    ///
    /// assert!(*n1.value() == 'A');
    /// ```
    pub fn value(&self) -> &N {
        &self.inner.1
    }

    /// Returns the degree of the node. The degree is the number of
    /// adjacent edges.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::digraph::*;
    ///
    /// let a = Node::new(0x1, "A");
    /// let b = Node::new(0x2, "B");
    /// let c = Node::new(0x4, "C");
    ///
    /// a.connect(&b, 0.42);
    /// a.connect(&c, 1.7);
    ///
    /// assert!(a.out_degree() == 2);
    /// ```
    pub fn degree(&self) -> usize {
        self.inner.2.borrow().len_outbound() + self.inner.2.borrow().len_inbound()
    }

    /// Connects this node to another node. The connection is created in both
    /// directions. The connection is created with the given edge value and
    /// defaults to `()`. This function allows for creating multiple
    /// connections between the same nodes.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::ungraph::*;
    ///
    /// let n1 = Node::new(1, ());
    /// let n2 = Node::new(2, ());
    ///
    /// n1.connect(&n2, 4.20);
    ///
    /// assert!(n1.is_connected(n2.key()));
    /// ```
    pub fn connect(&self, other: &Self, value: E) {
        self.inner
            .2
            .borrow_mut()
            .push_outbound((other.clone(), value.clone()));
        other
            .inner
            .2
            .borrow_mut()
            .push_inbound((self.clone(), value));
    }

    /// Connects this node to another node. The connection is created in both
    /// directions. The connection is created with the given edge value and
    /// defaults to `()`. This function doesn't allow for creating multiple
    /// connections between the same nodes. Returns Ok(()) if the connection
    /// was created, Err(EdgeValue) if the connection already exists.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::ungraph::*;
    ///
    /// let n1 = Node::new(1, ());
    /// let n2 = Node::new(2, ());
    ///
    /// match n1.try_connect(&n2, ()) {
    ///    Ok(_) => assert!(n1.is_connected(n2.key())),
    ///    Err(_) => panic!("n1 should be connected to n2"),
    /// }
    ///
    /// match n1.try_connect(&n2, ()) {
    ///    Ok(_) => panic!("n1 should be connected to n2"),
    ///    Err(_) => assert!(n1.is_connected(n2.key())),
    /// }
    /// ```
    pub fn try_connect(&self, other: &Node<K, N, E>, value: E) -> Result<(), Error> {
        if self.is_connected(other.key()) {
            Err(Error::EdgeAlreadyExists)
        } else {
            self.connect(other, value);
            Ok(())
        }
    }

    /// Disconnect two nodes from each other. The connection is removed in both
    /// directions. Returns Ok(EdgeValue) if the connection was removed, Err(())
    /// if the connection doesn't exist.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::ungraph::*;
    ///
    /// let n1 = Node::new(1, ());
    /// let n2 = Node::new(2, ());
    ///
    /// n1.connect(&n2, ());
    ///
    /// assert!(n1.is_connected(n2.key()));
    ///
    /// if n1.disconnect(n2.key()).is_err() {
    ///    panic!("n1 should be connected to n2");
    /// }
    ///
    /// assert!(!n1.is_connected(n2.key()));
    /// ```
    pub fn disconnect(&self, other: &K) -> Result<E, Error> {
        self.inner.2.borrow_mut().remove_undirected(other)
    }

    /// Removes all inbound and outbound connections to and from the node.
    ///
    /// # Example
    ///
    /// ```
    /// use gdsl::ungraph::*;
    ///
    /// let n1 = Node::new(1, ());
    /// let n2 = Node::new(2, ());
    /// let n3 = Node::new(3, ());
    /// let n4 = Node::new(4, ());
    ///
    /// n1.connect(&n2, ());
    /// n1.connect(&n3, ());
    /// n1.connect(&n4, ());
    /// n2.connect(&n1, ());
    /// n3.connect(&n1, ());
    /// n4.connect(&n1, ());
    ///
    /// assert!(n1.is_connected(n2.key()));
    /// assert!(n1.is_connected(n3.key()));
    /// assert!(n1.is_connected(n4.key()));
    ///
    /// n1.isolate();
    ///
    /// assert!(n1.is_orphan());
    /// ```
    pub fn isolate(&self) {
        for Edge(_, v, _) in self.iter() {
            if v.inner.2.borrow_mut().remove_inbound(self.key()).is_err() {
                v.inner.2.borrow_mut().remove_outbound(self.key()).unwrap();
            }
        }
        self.inner.2.borrow_mut().clear_outbound();
        self.inner.2.borrow_mut().clear_inbound();
    }

    /// Returns true if the node is an oprhan. Orphan nodes are nodes that have
    /// no connections.
    pub fn is_orphan(&self) -> bool {
        self.inner.2.borrow().len_outbound() == 0 && self.inner.2.borrow().len_inbound() == 0
    }

    /// Returns true if the node is connected to another node with a given key.
    pub fn is_connected(&self, other: &K) -> bool {
        self.find_adjacent(other).is_some()
    }

    /// Get a pointer to an adjacent node with a given key. Returns None if no
    /// node with the given key is found from the node's adjacency list.
    pub fn find_adjacent(&self, other: &K) -> Option<Node<K, N, E>> {
        self.inner
            .2
            .borrow()
            .find_adjacent(other)
            .map(|(n, _)| n.upgrade().unwrap())
    }

    /// Returns an iterator-like object that can be used to map, filter and
    /// collect reachable nodes or edges in different orderings such as
    /// postorder or preorder.
    pub fn order(&self) -> Order<K, N, E> {
        Order::new(self)
    }

    /// Returns an iterator-like object that can be used to map, filter,
    /// search and collect nodes or edges resulting from a depth-first search.
    pub fn dfs(&self) -> Dfs<K, N, E> {
        Dfs::new(self)
    }

    /// Returns an iterator-like object that can be used to map, filter,
    /// search and collect nodes or edges resulting from a breadth-first search.
    pub fn bfs(&self) -> Bfs<K, N, E> {
        Bfs::new(self)
    }

    /// Returns an iterator-like object that can be used to map, filter,
    /// search and collect nodes or edges resulting from a
    /// priotity-first search.
    pub fn pfs(&self) -> Pfs<K, N, E>
    where
        N: Ord,
    {
        Pfs::new(self)
    }

    /// Returns an iterator over the node's adjacent edges.
    pub fn iter(&self) -> NodeIterator<K, N, E> {
        NodeIterator {
            node: self,
            position: 0,
        }
    }

    pub fn sizeof(&self) -> usize {
        std::mem::size_of::<Node<K, N, E>>()
            + std::mem::size_of::<K>()
            + std::mem::size_of::<N>()
            + self.inner.2.borrow().sizeof()
            + std::mem::size_of::<Self>()
    }
}

//==== TRAIT IMPLEMENTATIONS ==================================================

impl<K, N, E> Deref for Node<K, N, E>
where
    K: Clone + Hash + Display + PartialEq + Eq,
    N: Clone,
    E: Clone,
{
    type Target = N;
    fn deref(&self) -> &Self::Target {
        self.value()
    }
}

impl<K, N, E> PartialEq for Node<K, N, E>
where
    K: Clone + Hash + Display + PartialEq + Eq,
    N: Clone,
    E: Clone,
{
    fn eq(&self, other: &Self) -> bool {
        self.key() == other.key()
    }
}

impl<K, N, E> Eq for Node<K, N, E>
where
    K: Clone + Hash + Display + PartialEq + Eq,
    N: Clone,
    E: Clone,
{
}

impl<K, N, E> PartialOrd for Node<K, N, E>
where
    K: Clone + Hash + PartialEq + Display + Eq,
    N: Clone + Ord,
    E: Clone,
{
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.value().cmp(other.value()))
    }
}

impl<K, N, E> Ord for Node<K, N, E>
where
    K: Clone + Hash + PartialEq + Display + Eq,
    N: Clone + Ord,
    E: Clone,
{
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.value().cmp(other.value())
    }
}

pub struct NodeIterator<'a, K, N, E>
where
    K: Clone + Hash + Display + PartialEq + Eq,
    N: Clone,
    E: Clone,
{
    node: &'a Node<K, N, E>,
    position: usize,
}

impl<'a, K, N, E> Iterator for NodeIterator<'a, K, N, E>
where
    K: Clone + Hash + Display + PartialEq + Eq,
    N: Clone,
    E: Clone,
{
    type Item = Edge<K, N, E>;

    fn next(&mut self) -> Option<Self::Item> {
        let adjacent = &self.node.inner.2.borrow();
        match adjacent.get_adjacent(self.position) {
            Some((n, e)) => {
                self.position += 1;
                Some(Edge(self.node.clone(), n.upgrade().unwrap(), e.clone()))
            }
            None => None,
        }
    }
}

impl<'a, K, N, E> IntoIterator for &'a Node<K, N, E>
where
    K: Clone + Hash + Display + PartialEq + Eq,
    N: Clone,
    E: Clone,
{
    type Item = Edge<K, N, E>;
    type IntoIter = NodeIterator<'a, K, N, E>;

    fn into_iter(self) -> Self::IntoIter {
        NodeIterator {
            node: self,
            position: 0,
        }
    }
}