rust_bounded_graph 0.2.1

[DEPRECATED] Use `bounded_graph` instead. A thin newtype wrapper for `petgraph` to assist in the creation of graphs with restrictions on their edges
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
//! # DEPRECATED
//!
//! **This crate has been renamed to [`bounded_graph`](https://crates.io/crates/bounded_graph).**
//!
//! Please update your dependencies to use `bounded_graph` instead.
//! This crate will no longer receive updates.
//!
//! ## Migration
//!
//! Simply replace `rust_bounded_graph` with `bounded_graph` in your `Cargo.toml`:
//!
//! ```toml
//! # Old (deprecated):
//! # rust_bounded_graph = "0.2.1"
//!
//! # New:
//! bounded_graph = "0.2"
//! ```
//!
//! Then update your imports from:
//! ```rust,ignore
//! use rust_bounded_graph::*;
//! ```
//!
//! To:
//! ```rust,ignore
//! use bounded_graph::*;
//! ```

use std::{
    cmp,
    ops::{Index, IndexMut},
};

use fixedbitset::FixedBitSet;
use petgraph::{
    csr::IndexType,
    data::{Build, Create, DataMap, DataMapMut},
    graph::{
        DefaultIx, Edge, EdgeIndex, EdgeIndices, EdgeReference, EdgeReferences, EdgeWeightsMut,
        Edges, EdgesConnecting, Externals, Frozen, GraphIndex, Neighbors, Node, NodeIndex,
        NodeIndices, NodeReferences, NodeWeightsMut,
    },
    graph6::ToGraph6,
    visit::{
        Data, EdgeCount, EdgeIndexable, GraphBase, GraphProp, IntoEdgeReferences, IntoEdges,
        IntoEdgesDirected, IntoNeighbors, IntoNeighborsDirected, IntoNodeIdentifiers,
        IntoNodeReferences, NodeCompactIndexable, NodeCount, NodeIndexable, Visitable,
    },
    Directed, Direction, EdgeType, Graph, IntoWeightedEdge, Undirected,
};

#[cfg(feature = "serde-1")]
use serde::{Deserialize, Serialize};

pub trait BoundedNode<Ix: IndexType = DefaultIx> {
    fn can_add_edge(&self, dir: Direction, existing_edge_count: usize, other_node: &Self) -> bool;
}

pub trait EdgeNumberBoundedNode<Ix: IndexType = DefaultIx> {
    fn max_incoming_edges(&self) -> Ix;
    fn max_outgoing_edges(&self) -> Ix;

    fn has_edge_space(&self, dir: Direction, existing_edge_count: usize) -> bool {
        match dir {
            Direction::Incoming => Ix::new(existing_edge_count) < self.max_incoming_edges(),
            Direction::Outgoing => Ix::new(existing_edge_count) < self.max_outgoing_edges(),
        }
    }
}

pub trait SimpleEdgeNumberBoundedNode {}

impl<Ix: IndexType, T: SimpleEdgeNumberBoundedNode + EdgeNumberBoundedNode> BoundedNode<Ix> for T {
    fn can_add_edge(&self, dir: Direction, existing_edge_count: usize, _other_node: &Self) -> bool {
        self.has_edge_space(dir, existing_edge_count)
    }
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde-1", derive(Serialize, Deserialize))]
pub struct BoundedGraph<N: BoundedNode<Ix>, E, Ty: EdgeType = Directed, Ix: IndexType = DefaultIx> {
    graph: Graph<N, E, Ty, Ix>,
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> BoundedGraph<N, E, Ty, Ix> {
    pub fn new() -> Self {
        Self {
            graph: Graph::<N, E, Ty, Ix>::default(),
        }
    }

    pub fn can_add_edge(&self, node: NodeIndex<Ix>, other: NodeIndex<Ix>) -> bool {
        let outgoing = self.graph.edges_directed(node, Direction::Outgoing);
        let o_weight = self.graph.node_weight(node);

        let incoming = self.graph.edges_directed(other, Direction::Incoming);
        let i_weight = self.graph.node_weight(other);
        match (o_weight, i_weight) {
            (Some(o_weight), Some(i_weight)) => {
                o_weight.can_add_edge(Direction::Outgoing, outgoing.count(), i_weight)
                    && i_weight.can_add_edge(Direction::Incoming, incoming.count(), o_weight)
            }
            _ => false,
        }
    }

    pub fn as_graph(&self) -> &Graph<N, E, Ty, Ix> {
        &self.graph
    }

    pub fn node_weight(&self, node: NodeIndex<Ix>) -> Option<&N> {
        self.graph.node_weight(node)
    }

    pub fn edge_weight(&self, edge: EdgeIndex<Ix>) -> Option<&E> {
        self.graph.edge_weight(edge)
    }

    pub fn node_weight_mut(&mut self, node: NodeIndex<Ix>) -> Option<&mut N> {
        self.graph.node_weight_mut(node)
    }

    pub fn edge_weight_mut(&mut self, edge: EdgeIndex<Ix>) -> Option<&mut E> {
        self.graph.edge_weight_mut(edge)
    }

    pub fn node_count(&self) -> usize {
        self.graph.node_count()
    }

    pub fn edge_count(&self) -> usize {
        self.graph.edge_count()
    }

    pub fn is_directed(&self) -> bool {
        self.graph.is_directed()
    }

    pub fn add_node(&mut self, node: N) -> NodeIndex<Ix> {
        self.graph.add_node(node)
    }

    pub fn add_edge(
        &mut self,
        source: NodeIndex<Ix>,
        target: NodeIndex<Ix>,
        edge: E,
    ) -> Result<EdgeIndex<Ix>, &'static str> {
        if !self.can_add_edge(source, target) {
            return Err("Cannot add edge from source node to target node");
        }

        Ok(self.graph.add_edge(source, target, edge))
    }

    pub fn update_edge(
        &mut self,
        a: NodeIndex<Ix>,
        b: NodeIndex<Ix>,
        weight: E,
    ) -> Result<EdgeIndex<Ix>, &'static str> {
        match self.graph.find_edge(a, b) {
            Some(edge) => {
                self.graph[edge] = weight;
                Ok(edge)
            }
            None => self.add_edge(a, b, weight),
        }
    }

    pub fn edge_endpoints(&self, edge: EdgeIndex<Ix>) -> Option<(NodeIndex<Ix>, NodeIndex<Ix>)> {
        self.graph.edge_endpoints(edge)
    }

    pub fn remove_node(&mut self, node: NodeIndex<Ix>) -> Option<N> {
        self.graph.remove_node(node)
    }

    pub fn remove_edge(&mut self, edge: EdgeIndex<Ix>) -> Option<E> {
        self.graph.remove_edge(edge)
    }

    pub fn neighbors(&self, node: NodeIndex<Ix>) -> Neighbors<'_, E, Ix> {
        self.graph.neighbors(node)
    }

    pub fn neighbors_directed(&self, node: NodeIndex<Ix>, dir: Direction) -> Neighbors<'_, E, Ix> {
        self.graph.neighbors_directed(node, dir)
    }

    pub fn neighbors_undirected(&self, node: NodeIndex<Ix>) -> Neighbors<'_, E, Ix> {
        self.graph.neighbors_undirected(node)
    }

    pub fn edges(&self, node: NodeIndex<Ix>) -> Edges<'_, E, Ty, Ix> {
        self.graph.edges(node)
    }

    pub fn edges_directed(&self, node: NodeIndex<Ix>, dir: Direction) -> Edges<'_, E, Ty, Ix> {
        self.graph.edges_directed(node, dir)
    }

    pub fn edges_connecting(
        &self,
        a: NodeIndex<Ix>,
        b: NodeIndex<Ix>,
    ) -> EdgesConnecting<E, Ty, Ix> {
        self.graph.edges_connecting(a, b)
    }

    pub fn contains_edge(&self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> bool {
        self.graph.contains_edge(a, b)
    }

    pub fn find_edge(&self, a: NodeIndex<Ix>, b: NodeIndex<Ix>) -> Option<EdgeIndex<Ix>> {
        self.graph.find_edge(a, b)
    }

    pub fn find_edge_undirected(
        &self,
        a: NodeIndex<Ix>,
        b: NodeIndex<Ix>,
    ) -> Option<(EdgeIndex<Ix>, Direction)> {
        self.graph.find_edge_undirected(a, b)
    }

    pub fn externals(&self, dir: Direction) -> Externals<'_, N, Ty, Ix> {
        self.graph.externals(dir)
    }

    pub fn node_indices(&self) -> NodeIndices<Ix> {
        self.graph.node_indices()
    }

    pub fn node_weights_mut(&mut self) -> NodeWeightsMut<'_, N, Ix> {
        self.graph.node_weights_mut()
    }

    pub fn node_weights(&self) -> impl Iterator<Item = &N> + '_ {
        self.graph.node_weights()
    }

    pub fn edge_indices(&self) -> EdgeIndices<Ix> {
        self.graph.edge_indices()
    }

    pub fn edge_references(&self) -> EdgeReferences<'_, E, Ix> {
        self.graph.edge_references()
    }

    pub fn edge_weights(&self) -> impl Iterator<Item = &E> + '_ {
        self.graph.edge_weights()
    }

    pub fn edge_weights_mut(&mut self) -> EdgeWeightsMut<E, Ix> {
        self.graph.edge_weights_mut()
    }

    pub fn into_nodes_edges(self) -> (Vec<Node<N, Ix>>, Vec<Edge<E, Ix>>) {
        self.graph.into_nodes_edges()
    }

    pub fn index_twice_mut<T, U>(
        &mut self,
        i: T,
        j: U,
    ) -> (
        &mut <Graph<N, E, Ty, Ix> as Index<T>>::Output,
        &mut <Graph<N, E, Ty, Ix> as Index<U>>::Output,
    )
    where
        Graph<N, E, Ty, Ix>: Index<T> + Index<U>,
        Graph<N, E, Ty, Ix>: IndexMut<T> + IndexMut<U>,
        T: GraphIndex,
        U: GraphIndex,
    {
        self.graph.index_twice_mut(i, j)
    }

    // TODO: this one is more complex. Leave out for v0.1
    // pub fn reverse(&mut self) {
    //     self.graph.reverse();
    // }

    pub fn clear(&mut self) {
        self.graph.clear();
    }

    pub fn clear_edges(&mut self) {
        self.graph.clear_edges();
    }

    pub fn capacity(&self) -> (usize, usize) {
        self.graph.capacity()
    }

    pub fn reserve_nodes(&mut self, additional: usize) {
        self.graph.reserve_nodes(additional)
    }

    pub fn reserve_edges(&mut self, additional: usize) {
        self.graph.reserve_edges(additional)
    }

    pub fn reserve_exact_nodes(&mut self, additional: usize) {
        self.graph.reserve_exact_nodes(additional)
    }

    pub fn reserve_exact_edges(&mut self, additional: usize) {
        self.graph.reserve_exact_edges(additional)
    }

    pub fn shrink_to_fit_nodes(&mut self) {
        self.graph.shrink_to_fit_nodes()
    }

    pub fn shrink_to_fit_edges(&mut self) {
        self.graph.shrink_to_fit_edges()
    }

    pub fn shrink_to_fit(&mut self) {
        self.graph.shrink_to_fit()
    }

    pub fn retain_nodes<F>(&mut self, visit: F)
    where
        F: FnMut(Frozen<Graph<N, E, Ty, Ix>>, NodeIndex<Ix>) -> bool,
    {
        self.graph.retain_nodes(visit)
    }

    pub fn retain_edges<F>(&mut self, visit: F)
    where
        F: FnMut(Frozen<Graph<N, E, Ty, Ix>>, EdgeIndex<Ix>) -> bool,
    {
        self.graph.retain_edges(visit)
    }

    // This one needs to be copy pasted in in order to invoke the BoundedGraph add_edge impl
    pub fn extend_with_edges<I>(&mut self, iterable: I)
    where
        I: IntoIterator,
        I::Item: IntoWeightedEdge<E>,
        <I::Item as IntoWeightedEdge<E>>::NodeId: Into<NodeIndex<Ix>>,
        N: Default,
    {
        let iter = iterable.into_iter();
        let (low, _) = iter.size_hint();
        self.graph.reserve_edges(low);

        for elt in iter {
            let (source, target, weight) = elt.into_weighted_edge();
            let (source, target) = (source.into(), target.into());
            let nx = cmp::max(source, target);
            while nx.index() >= self.node_count() {
                self.add_node(N::default());
            }
            let _ = self.add_edge(source, target, weight); // Continue on failures
        }
    }

    // Same as above
    pub fn from_edges<I>(iterable: I) -> Self
    where
        I: IntoIterator,
        I::Item: IntoWeightedEdge<E>,
        <I::Item as IntoWeightedEdge<E>>::NodeId: Into<NodeIndex<Ix>>,
        N: Default,
    {
        let mut g = Self::with_capacity(0, 0);
        g.extend_with_edges(iterable);
        g
    }

    pub fn map<'a, F, G, N2, E2>(&'a self, node_map: F, edge_map: G) -> BoundedGraph<N2, E2, Ty, Ix>
    where
        F: FnMut(NodeIndex<Ix>, &'a N) -> N2,
        G: FnMut(EdgeIndex<Ix>, &'a E) -> E2,
        N2: BoundedNode<Ix>,
    {
        BoundedGraph::<N2, E2, Ty, Ix> {
            graph: self.graph.map(node_map, edge_map),
        }
    }

    pub fn filter_map<'a, F, G, N2, E2>(
        &'a self,
        node_map: F,
        edge_map: G,
    ) -> BoundedGraph<N2, E2, Ty, Ix>
    where
        F: FnMut(NodeIndex<Ix>, &'a N) -> Option<N2>,
        G: FnMut(EdgeIndex<Ix>, &'a E) -> Option<E2>,
        N2: BoundedNode<Ix>,
    {
        BoundedGraph::<N2, E2, Ty, Ix> {
            graph: self.graph.filter_map(node_map, edge_map),
        }
    }

    pub fn into_edge_type<NewTy: EdgeType>(self) -> BoundedGraph<N, E, NewTy, Ix> {
        BoundedGraph::<N, E, NewTy, Ix> {
            graph: self.graph.into_edge_type(),
        }
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> GraphBase for BoundedGraph<N, E, Ty, Ix> {
    type NodeId = NodeIndex<Ix>;
    type EdgeId = EdgeIndex<Ix>;
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> Data for BoundedGraph<N, E, Ty, Ix> {
    type NodeWeight = N;
    type EdgeWeight = E;
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> GraphProp for BoundedGraph<N, E, Ty, Ix> {
    fn is_directed(&self) -> bool {
        self.graph.is_directed()
    }

    type EdgeType = Ty;
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> DataMap for BoundedGraph<N, E, Ty, Ix> {
    fn node_weight(&self, node: NodeIndex<Ix>) -> Option<&N> {
        self.graph.node_weight(node)
    }

    fn edge_weight(&self, edge: EdgeIndex<Ix>) -> Option<&E> {
        self.graph.edge_weight(edge)
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> DataMapMut for BoundedGraph<N, E, Ty, Ix> {
    fn node_weight_mut(&mut self, node: NodeIndex<Ix>) -> Option<&mut N> {
        self.graph.node_weight_mut(node)
    }

    fn edge_weight_mut(&mut self, edge: EdgeIndex<Ix>) -> Option<&mut E> {
        self.graph.edge_weight_mut(edge)
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> EdgeCount for BoundedGraph<N, E, Ty, Ix> {
    fn edge_count(&self) -> usize {
        self.graph.edge_count()
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> EdgeIndexable
    for BoundedGraph<N, E, Ty, Ix>
{
    fn edge_bound(self: &Self) -> usize {
        self.graph.edge_bound()
    }

    fn to_index(self: &Self, a: Self::EdgeId) -> usize {
        <Graph<N, E, Ty, Ix> as EdgeIndexable>::to_index(&self.graph, a)
    }

    fn from_index(self: &Self, i: usize) -> Self::EdgeId {
        <Graph<N, E, Ty, Ix> as EdgeIndexable>::from_index(&self.graph, i)
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> NodeIndexable
    for BoundedGraph<N, E, Ty, Ix>
{
    fn node_bound(self: &Self) -> usize {
        self.graph.node_bound()
    }

    fn to_index(self: &Self, a: Self::NodeId) -> usize {
        <Graph<N, E, Ty, Ix> as NodeIndexable>::to_index(&self.graph, a)
    }

    fn from_index(self: &Self, i: usize) -> Self::NodeId {
        <Graph<N, E, Ty, Ix> as NodeIndexable>::from_index(&self.graph, i)
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> Index<EdgeIndex<Ix>>
    for BoundedGraph<N, E, Ty, Ix>
{
    type Output = <Graph<N, E, Ty, Ix> as Index<EdgeIndex<Ix>>>::Output;

    fn index(&self, index: EdgeIndex<Ix>) -> &Self::Output {
        &self.graph[index]
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> Index<NodeIndex<Ix>>
    for BoundedGraph<N, E, Ty, Ix>
{
    type Output = <Graph<N, E, Ty, Ix> as Index<NodeIndex<Ix>>>::Output;

    fn index(&self, index: NodeIndex<Ix>) -> &Self::Output {
        &self.graph[index]
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> IndexMut<EdgeIndex<Ix>>
    for BoundedGraph<N, E, Ty, Ix>
{
    fn index_mut(&mut self, index: EdgeIndex<Ix>) -> &mut Self::Output {
        &mut self.graph[index]
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> IndexMut<NodeIndex<Ix>>
    for BoundedGraph<N, E, Ty, Ix>
{
    fn index_mut(&mut self, index: NodeIndex<Ix>) -> &mut Self::Output {
        &mut self.graph[index]
    }
}

impl<'a, N: 'a + BoundedNode<Ix>, E: 'a, Ty: EdgeType, Ix: IndexType> IntoEdgeReferences
    for &'a BoundedGraph<N, E, Ty, Ix>
{
    type EdgeRef = EdgeReference<'a, E, Ix>;

    type EdgeReferences = EdgeReferences<'a, E, Ix>;

    fn edge_references(self) -> Self::EdgeReferences {
        self.graph.edge_references()
    }
}

impl<'a, N: 'a + BoundedNode<Ix>, E: 'a, Ty: EdgeType, Ix: IndexType> IntoEdges
    for &'a BoundedGraph<N, E, Ty, Ix>
{
    type Edges = Edges<'a, E, Ty, Ix>;

    fn edges(self, a: Self::NodeId) -> Self::Edges {
        self.graph.edges(a)
    }
}

impl<'a, N: 'a + BoundedNode<Ix>, E: 'a, Ty: EdgeType, Ix: IndexType> IntoEdgesDirected
    for &'a BoundedGraph<N, E, Ty, Ix>
{
    type EdgesDirected = Edges<'a, E, Ty, Ix>;

    fn edges_directed(self, a: Self::NodeId, dir: Direction) -> Self::EdgesDirected {
        self.graph.edges_directed(a, dir)
    }
}

impl<'a, N: 'a + BoundedNode<Ix>, E: 'a, Ty: EdgeType, Ix: IndexType> IntoNeighbors
    for &'a BoundedGraph<N, E, Ty, Ix>
{
    type Neighbors = Neighbors<'a, E, Ix>;

    fn neighbors(self, a: Self::NodeId) -> Self::Neighbors {
        self.graph.neighbors(a)
    }
}

impl<'a, N: 'a + BoundedNode<Ix>, E: 'a, Ty: EdgeType, Ix: IndexType> IntoNeighborsDirected
    for &'a BoundedGraph<N, E, Ty, Ix>
{
    type NeighborsDirected = Neighbors<'a, E, Ix>;

    fn neighbors_directed(self, a: Self::NodeId, dir: Direction) -> Self::Neighbors {
        self.graph.neighbors_directed(a, dir)
    }
}

impl<'a, N: 'a + BoundedNode<Ix>, E: 'a, Ty: EdgeType, Ix: IndexType> IntoNodeReferences
    for &'a BoundedGraph<N, E, Ty, Ix>
{
    type NodeRef = (NodeIndex<Ix>, &'a N);

    type NodeReferences = NodeReferences<'a, N, Ix>;

    fn node_references(self) -> Self::NodeReferences {
        self.graph.node_references()
    }
}

impl<'a, N: 'a + BoundedNode<Ix>, E: 'a, Ty: EdgeType, Ix: IndexType> IntoNodeIdentifiers
    for &'a BoundedGraph<N, E, Ty, Ix>
{
    type NodeIdentifiers = NodeIndices<Ix>;

    fn node_identifiers(self) -> Self::NodeIdentifiers {
        self.graph.node_identifiers()
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> NodeCount for BoundedGraph<N, E, Ty, Ix> {
    fn node_count(&self) -> usize {
        self.graph.node_count()
    }
}

impl<N: BoundedNode<Ix>, E, Ix: IndexType> ToGraph6 for BoundedGraph<N, E, Undirected, Ix> {
    fn graph6_string(&self) -> String {
        self.graph.graph6_string()
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> Visitable for BoundedGraph<N, E, Ty, Ix> {
    type Map = FixedBitSet;

    fn visit_map(self: &Self) -> Self::Map {
        self.graph.visit_map()
    }

    fn reset_map(self: &Self, map: &mut Self::Map) {
        self.graph.reset_map(map)
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> NodeCompactIndexable
    for BoundedGraph<N, E, Ty, Ix>
{
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> Create for BoundedGraph<N, E, Ty, Ix> {
    fn with_capacity(nodes: usize, edges: usize) -> Self {
        BoundedGraph {
            graph: Graph::with_capacity(nodes, edges),
        }
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> Default for BoundedGraph<N, E, Ty, Ix> {
    fn default() -> Self {
        BoundedGraph {
            graph: Graph::default(),
        }
    }
}

impl<N: BoundedNode<Ix>, E, Ty: EdgeType, Ix: IndexType> Build for BoundedGraph<N, E, Ty, Ix> {
    fn add_node(&mut self, weight: Self::NodeWeight) -> Self::NodeId {
        self.add_node(weight)
    }

    // TODO: This panics. Should it?
    fn update_edge(
        &mut self,
        a: Self::NodeId,
        b: Self::NodeId,
        weight: Self::EdgeWeight,
    ) -> Self::EdgeId {
        self.update_edge(a, b, weight).unwrap()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use petgraph::dot::{Config, Dot};
    use Direction::Incoming;

    #[test]
    fn edge_number_bounded_node() {
        #[derive(Debug, Default)]
        pub struct BoundedNodeIndex {
            name: String,
            inputs: usize,
            outputs: usize,
        }

        impl BoundedNodeIndex {
            pub fn new(name: String, inputs: usize, outputs: usize) -> Self {
                Self {
                    name,
                    inputs,
                    outputs,
                }
            }
        }

        impl<Ix: IndexType> EdgeNumberBoundedNode<Ix> for BoundedNodeIndex {
            fn max_incoming_edges(&self) -> Ix {
                Ix::new(self.inputs)
            }

            fn max_outgoing_edges(&self) -> Ix {
                Ix::new(self.outputs)
            }
        }

        impl SimpleEdgeNumberBoundedNode for BoundedNodeIndex {}

        let mut deps = BoundedGraph::<BoundedNodeIndex, &str>::new();
        let pg = deps.add_node(BoundedNodeIndex::new(
            "petgraph".to_string(),
            0usize,
            3usize,
        ));
        let fb = deps.add_node(BoundedNodeIndex::new(
            "fixedbitset".to_string(),
            4usize,
            0usize,
        ));
        let qc = deps.add_node(BoundedNodeIndex::new(
            "quickcheck".to_string(),
            1usize,
            2usize,
        ));
        let rand = deps.add_node(BoundedNodeIndex::new("rand".to_string(), 1usize, 2usize));
        let libc = deps.add_node(BoundedNodeIndex::new("libc".to_string(), 1usize, 2usize));
        deps.extend_with_edges(&[(pg, fb), (pg, qc), (qc, rand), (rand, libc), (qc, libc)]);

        println!(
            "{:?}",
            Dot::with_config(deps.as_graph(), &[Config::EdgeNoLabel])
        );

        assert_eq!(deps[pg].name, "petgraph");
        assert_eq!(deps.edge_count(), 4); // One should be missing.
        assert_eq!(deps.edges_directed(libc, Incoming).count(), 1); // It should be missing from here.

        deps.update_edge(qc, fb, "").unwrap();
        assert_eq!(deps.edge_count(), 5);
    }

    #[test]
    fn advanced_node() {
        #[derive(Debug, Default, PartialEq, Eq)]
        pub enum Color {
            #[default]
            Red,
            Blue,
        }

        #[derive(Debug, Default)]
        pub struct ColoredBoundedNodeIndex<Ix: IndexType> {
            name: String,
            inputs: usize,
            outputs: usize,
            color: Color,
            _phantom: std::marker::PhantomData<Ix>,
        }

        impl<Ix: IndexType> ColoredBoundedNodeIndex<Ix> {
            pub fn new(name: String, inputs: usize, outputs: usize, color: Color) -> Self {
                Self {
                    name,
                    inputs,
                    outputs,
                    color,
                    _phantom: std::marker::PhantomData::<Ix>,
                }
            }
        }

        trait ColoredBoundedNode<Ix: IndexType>: BoundedNode<Ix> + EdgeNumberBoundedNode<Ix> {
            fn color(&self) -> &Color;
        }

        impl<Ix: IndexType> ColoredBoundedNode<Ix> for ColoredBoundedNodeIndex<Ix> {
            fn color(&self) -> &Color {
                &self.color
            }
        }

        impl<Ix: IndexType> EdgeNumberBoundedNode<Ix> for ColoredBoundedNodeIndex<Ix> {
            fn max_incoming_edges(&self) -> Ix {
                Ix::new(self.inputs)
            }

            fn max_outgoing_edges(&self) -> Ix {
                Ix::new(self.outputs)
            }
        }

        impl<Ix: IndexType> BoundedNode<Ix> for ColoredBoundedNodeIndex<Ix> {
            fn can_add_edge(
                &self,
                dir: Direction,
                existing_edge_count: usize,
                other_node: &Self,
            ) -> bool {
                if self.color != other_node.color {
                    return false;
                }
                self.has_edge_space(dir, existing_edge_count)
            }
        }

        let mut deps = BoundedGraph::<ColoredBoundedNodeIndex<usize>, &str, Directed, usize>::new();
        let pg = deps.add_node(ColoredBoundedNodeIndex::new(
            "petgraph".to_string(),
            0usize,
            3usize,
            Color::Red,
        ));
        let fb = deps.add_node(ColoredBoundedNodeIndex::new(
            "fixedbitset".to_string(),
            4usize,
            0usize,
            Color::Red,
        ));
        let qc = deps.add_node(ColoredBoundedNodeIndex::new(
            "quickcheck".to_string(),
            1usize,
            2usize,
            Color::Blue,
        ));
        deps.extend_with_edges(&[(pg, fb), (pg, qc)]);
        println!(
            "{:?}",
            Dot::with_config(deps.as_graph(), &[Config::EdgeNoLabel])
        );
        assert_eq!(deps[pg].name, "petgraph");
        assert_eq!(deps[pg].color(), &Color::Red);
    }
}