gryf 0.2.1

Graph data structure library with focus on convenience, versatility, correctness and performance.
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
use std::marker::PhantomData;

use arbitrary::Arbitrary;

use crate::core::{
    EdgeSet, GraphAdd, GraphBase, GraphFull, GraphMut, GraphRef, Neighbors, VertexSet,
    base::{EdgeRef, NeighborRef, VertexRef},
    error::{AddEdgeError, AddEdgeErrorKind, AddVertexError, AddVertexErrorKind},
    id::{EdgeId, IdType, IntegerIdType, VertexId},
    marker::{Direction, EdgeType},
};

use super::arbitrary::MutOp;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Arbitrary)]
pub enum RemovalBehavior {
    SwapRemove,
    TombStone,
}

#[derive(Debug, Clone, PartialEq, Eq, Arbitrary)]
pub struct ModelParams {
    allow_multi_edges: bool,
    allow_loops: bool,
    max_vertex_count: Option<usize>,
    max_edge_count: Option<usize>,
    max_remove_vertices: Option<usize>,
    max_remove_edges: Option<usize>,
    removal_behavior: RemovalBehavior,
}

impl Default for ModelParams {
    fn default() -> Self {
        Self {
            allow_multi_edges: false,
            allow_loops: false,
            max_vertex_count: None,
            max_edge_count: None,
            max_remove_vertices: None,
            max_remove_edges: None,
            removal_behavior: RemovalBehavior::SwapRemove,
        }
    }
}

impl ModelParams {
    pub fn allow_multi_edges(self) -> Self {
        Self {
            allow_multi_edges: true,
            ..self
        }
    }

    pub fn allow_loops(self) -> Self {
        Self {
            allow_loops: true,
            ..self
        }
    }

    pub fn max_vertex_count(self, value: usize) -> Self {
        Self {
            max_vertex_count: Some(value),
            ..self
        }
    }

    pub fn max_edge_count(self, value: usize) -> Self {
        Self {
            max_edge_count: Some(value),
            ..self
        }
    }

    pub fn max_remove_vertices(self, value: usize) -> Self {
        Self {
            max_remove_vertices: Some(value),
            ..self
        }
    }

    pub fn no_remove_vertices(self) -> Self {
        Self {
            max_remove_vertices: Some(0),
            ..self
        }
    }

    pub fn max_remove_edges(self, value: usize) -> Self {
        Self {
            max_remove_edges: Some(value),
            ..self
        }
    }

    pub fn no_remove_edges(self) -> Self {
        Self {
            max_remove_edges: Some(0),
            ..self
        }
    }

    pub fn no_remove(self) -> Self {
        self.no_remove_vertices().no_remove_edges()
    }

    pub fn swap_remove(self) -> Self {
        self.removal_behavior(RemovalBehavior::SwapRemove)
    }

    pub fn tomb_stone(self) -> Self {
        self.removal_behavior(RemovalBehavior::TombStone)
    }

    pub fn removal_behavior(self, value: RemovalBehavior) -> Self {
        Self {
            removal_behavior: value,
            ..self
        }
    }
}

#[derive(Debug, Clone)]
pub struct Model<V, E, Ty> {
    vertices: Vec<Option<V>>,
    edges: Vec<Option<E>>,
    neighbors: Vec<Option<(usize, usize)>>,
    removed_vertices: usize,
    removed_edges: usize,
    ty: PhantomData<Ty>,
    params: ModelParams,
}

impl<V, E, Ty: EdgeType> Model<V, E, Ty> {
    pub fn new(params: ModelParams) -> Self {
        Self {
            vertices: Vec::new(),
            edges: Vec::new(),
            neighbors: Vec::new(),
            removed_vertices: 0,
            removed_edges: 0,
            ty: PhantomData,
            params,
        }
    }

    pub fn check(&self, op: &MutOp<V, E>) -> bool {
        match op {
            MutOp::RemoveVertex(_)
                if self.params.max_remove_vertices == Some(self.removed_vertices) =>
            {
                return false;
            }
            MutOp::RemoveEdge(_, _) if self.params.max_remove_edges == Some(self.removed_edges) => {
                return false;
            }
            _ => {}
        }

        match op {
            MutOp::AddEdge(from, to, _) if !self.params.allow_loops && from == to => return false,
            _ => {}
        }

        if !self.params.allow_multi_edges
            && let MutOp::AddEdge(from, to, _) = op
        {
            let n = self.vertex_bound();
            if let (Some(from), Some(to)) = (from.get(n), to.get(n))
                && self.edge_exists(from, to)
            {
                return false;
            }
        }

        true
    }

    fn edge_exists(&self, from: usize, to: usize) -> bool {
        self.neighbors
            .iter()
            .filter_map(Option::as_ref)
            .copied()
            .any(|(u, v)| u == from && v == to || (!Ty::is_directed() && u == to && v == from))
    }
}

impl<V, E, Ty: EdgeType> GraphBase for Model<V, E, Ty> {
    type VertexId = VertexId;
    type EdgeId = EdgeId;
    type EdgeType = Ty;
}

impl<V, E, Ty: EdgeType> VertexSet for Model<V, E, Ty> {
    type VerticesByIdIter<'a>
        = IdIter<'a, VertexId, V>
    where
        Self: 'a;

    fn vertices_by_id(&self) -> Self::VerticesByIdIter<'_> {
        IdIter::new(&self.vertices)
    }

    fn vertex_count(&self) -> usize {
        let holes = match self.params.removal_behavior {
            RemovalBehavior::SwapRemove => 0,
            RemovalBehavior::TombStone => self.removed_vertices,
        };
        self.vertices.len() - holes
    }

    fn vertex_bound(&self) -> usize {
        self.vertices.len()
    }
}

impl<V, E, Ty: EdgeType> EdgeSet for Model<V, E, Ty> {
    type EdgesByIdIter<'a>
        = IdIter<'a, EdgeId, E>
    where
        Self: 'a;

    type EdgeIdIter<'a>
        = EdgeIdIter<'a, Ty>
    where
        Self: 'a;

    fn edges_by_id(&self) -> Self::EdgesByIdIter<'_> {
        IdIter::new(&self.edges)
    }

    fn edge_id(&self, from: &Self::VertexId, to: &Self::VertexId) -> Self::EdgeIdIter<'_> {
        let (from, to) = (from.as_usize(), to.as_usize());
        EdgeIdIter::new([from, to], &self.neighbors)
    }

    fn endpoints(&self, id: &Self::EdgeId) -> Option<(Self::VertexId, Self::VertexId)> {
        let &(from, to) = self.neighbors.get(id.as_usize())?.as_ref()?;
        Some((VertexId::from_usize(from), VertexId::from_usize(to)))
    }

    fn edge_count(&self) -> usize {
        let holes = match self.params.removal_behavior {
            RemovalBehavior::SwapRemove => 0,
            RemovalBehavior::TombStone => self.removed_edges,
        };
        self.edges.len() - holes
    }

    fn edge_bound(&self) -> usize {
        self.edges.len()
    }
}

impl<V, E, Ty: EdgeType> GraphRef<V, E> for Model<V, E, Ty> {
    type VertexRef<'a>
        = VertexRef<'a, VertexId, V>
    where
        Self: 'a,
        V: 'a;

    type VerticesIter<'a>
        = VerticesIter<'a, V>
    where
        Self: 'a,
        V: 'a;

    type EdgeRef<'a>
        = EdgeRef<'a, Self::VertexId, Self::EdgeId, E>
    where
        Self: 'a,
        E: 'a;

    type EdgesIter<'a>
        = EdgesIter<'a, E>
    where
        Self: 'a,
        E: 'a;

    fn vertices(&self) -> Self::VerticesIter<'_> {
        VerticesIter::new(&self.vertices)
    }

    fn edges(&self) -> Self::EdgesIter<'_> {
        EdgesIter::new(&self.edges, &self.neighbors)
    }

    fn vertex(&self, id: &Self::VertexId) -> Option<&V> {
        self.vertices.get(id.as_usize()).and_then(Option::as_ref)
    }

    fn edge(&self, id: &Self::EdgeId) -> Option<&E> {
        self.edges.get(id.as_usize()).and_then(Option::as_ref)
    }
}

impl<V, E, Ty: EdgeType> GraphMut<V, E> for Model<V, E, Ty> {
    fn vertex_mut(&mut self, id: &Self::VertexId) -> Option<&mut V> {
        self.vertices
            .get_mut(id.as_usize())
            .and_then(Option::as_mut)
    }

    fn edge_mut(&mut self, id: &Self::EdgeId) -> Option<&mut E> {
        self.edges.get_mut(id.as_usize()).and_then(Option::as_mut)
    }
}

impl<V, E, Ty: EdgeType> GraphAdd<V, E> for Model<V, E, Ty> {
    fn try_add_vertex(&mut self, vertex: V) -> Result<Self::VertexId, AddVertexError<V>> {
        if Some(self.vertex_count()) == self.params.max_vertex_count {
            return Err(AddVertexError::new(
                vertex,
                AddVertexErrorKind::CapacityOverflow,
            ));
        }

        let id = VertexId::from_usize(self.vertices.len());
        self.vertices.push(Some(vertex));

        Ok(id)
    }

    fn try_add_edge(
        &mut self,
        from: &Self::VertexId,
        to: &Self::VertexId,
        edge: E,
    ) -> Result<Self::EdgeId, AddEdgeError<E>> {
        if self.vertex(from).is_none() {
            return Err(AddEdgeError::new(edge, AddEdgeErrorKind::TailAbsent));
        }

        if self.vertex(to).is_none() {
            return Err(AddEdgeError::new(edge, AddEdgeErrorKind::HeadAbsent));
        }

        if Some(self.edge_count()) == self.params.max_edge_count {
            return Err(AddEdgeError::new(edge, AddEdgeErrorKind::CapacityOverflow));
        }

        let (from, to) = (from.as_usize(), to.as_usize());

        if !self.params.allow_multi_edges && self.edge_exists(from, to) {
            return Err(AddEdgeError::new(edge, AddEdgeErrorKind::MultiEdge));
        }

        let id = EdgeId::from_usize(self.edges.len());
        self.edges.push(Some(edge));
        self.neighbors.push(Some((from, to)));

        Ok(id)
    }
}

impl<V, E, Ty: EdgeType> GraphFull<V, E> for Model<V, E, Ty> {
    fn remove_vertex(&mut self, id: &Self::VertexId) -> Option<V> {
        self.vertex(id)?;

        let index = id.as_usize();
        self.removed_vertices += 1;

        match self.params.removal_behavior {
            RemovalBehavior::SwapRemove => {
                let mut i = 0;
                while i < self.edges.len() {
                    if let Some(&(from, to)) = self.neighbors[i].as_ref()
                        && (from == index || to == index)
                    {
                        self.edges.swap_remove(i);
                        self.neighbors.swap_remove(i);
                        continue;
                    }

                    i += 1;
                }

                self.vertices.swap_remove(index)
            }
            RemovalBehavior::TombStone => {
                self.neighbors
                    .iter_mut()
                    .zip(self.edges.iter_mut())
                    .filter(|(n, _)| {
                        if let Some(&(from, to)) = n.as_ref() {
                            from == index || to == index
                        } else {
                            false
                        }
                    })
                    .for_each(|(n, e)| {
                        n.take();
                        e.take();
                        self.removed_edges += 1;
                    });

                self.vertices[index].take()
            }
        }
    }

    fn remove_edge(&mut self, id: &Self::EdgeId) -> Option<E> {
        self.edge(id)?;

        let index = id.as_usize();
        self.removed_edges += 1;

        match self.params.removal_behavior {
            RemovalBehavior::SwapRemove => {
                self.neighbors.swap_remove(index);
                self.edges.swap_remove(index)
            }
            RemovalBehavior::TombStone => {
                self.neighbors[index].take();
                self.edges[index].take()
            }
        }
    }
}

impl<V, E, Ty: EdgeType> Neighbors for Model<V, E, Ty> {
    type NeighborRef<'a>
        = NeighborRef<VertexId, EdgeId>
    where
        Self: 'a;

    type NeighborsIter<'a>
        = NeighborsIter<'a, Ty>
    where
        Self: 'a;

    fn neighbors_undirected(&self, from: &Self::VertexId) -> Self::NeighborsIter<'_> {
        NeighborsIter::new(from.as_usize(), &self.neighbors, None)
    }

    fn neighbors_directed(&self, from: &Self::VertexId, dir: Direction) -> Self::NeighborsIter<'_> {
        NeighborsIter::new(from.as_usize(), &self.neighbors, Some(dir))
    }
}

#[derive(Debug)]
pub struct IdIter<'a, Id, T> {
    data: &'a [Option<T>],
    index: usize,
    ty: PhantomData<Id>,
}

impl<'a, Id, T> IdIter<'a, Id, T> {
    fn new(data: &'a [Option<T>]) -> Self {
        Self {
            data,
            index: 0,
            ty: PhantomData,
        }
    }
}

impl<Id: IntegerIdType, T> Iterator for IdIter<'_, Id, T> {
    type Item = Id;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (head, tail) = self.data.split_first()?;
            self.data = tail;

            if head.is_some() {
                return Some(Id::from_usize(self.index));
            }

            self.index += 1;
        }
    }
}

#[derive(Debug)]
pub struct VerticesIter<'a, T> {
    data: &'a [Option<T>],
    index: usize,
}

impl<'a, T> VerticesIter<'a, T> {
    pub fn new(data: &'a [Option<T>]) -> Self {
        Self { data, index: 0 }
    }
}

impl<'a, T> Iterator for VerticesIter<'a, T> {
    type Item = VertexRef<'a, VertexId, T>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (head, tail) = self.data.split_first()?;
            self.data = tail;

            let id = VertexId::from_usize(self.index);
            self.index += 1;

            if let Some(attr) = head {
                return Some(VertexRef { id, attr });
            }
        }
    }
}

#[derive(Debug)]
pub struct EdgesIter<'a, T> {
    data: &'a [Option<T>],
    neighbors: &'a [Option<(usize, usize)>],
    index: usize,
}

impl<'a, T> EdgesIter<'a, T> {
    pub fn new(data: &'a [Option<T>], neighbors: &'a [Option<(usize, usize)>]) -> Self {
        Self {
            data,
            neighbors,
            index: 0,
        }
    }
}

impl<'a, T> Iterator for EdgesIter<'a, T> {
    type Item = EdgeRef<'a, VertexId, EdgeId, T>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (head, tail) = self.data.split_first()?;
            self.data = tail;

            let (pair, tail) = self.neighbors.split_first()?;
            self.neighbors = tail;

            let id = EdgeId::from_usize(self.index);
            self.index += 1;

            if let Some(attr) = head {
                let (from, to) = pair.unwrap();
                return Some(EdgeRef {
                    id,
                    attr,
                    from: VertexId::from_usize(from),
                    to: VertexId::from_usize(to),
                });
            }
        }
    }
}

#[derive(Debug)]
pub struct EdgeIdIter<'a, Ty> {
    between: [usize; 2],
    neighbors: &'a [Option<(usize, usize)>],
    index: usize,
    ty: PhantomData<Ty>,
}

impl<'a, Ty> EdgeIdIter<'a, Ty> {
    pub fn new(between: [usize; 2], neighbors: &'a [Option<(usize, usize)>]) -> Self {
        Self {
            between,
            neighbors,
            index: 0,
            ty: PhantomData,
        }
    }
}

impl<'a, Ty: EdgeType> Iterator for EdgeIdIter<'a, Ty> {
    type Item = EdgeId;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (pair, tail) = self.neighbors.split_first()?;
            self.neighbors = tail;

            let index = EdgeId::from_usize(self.index);
            self.index += 1;

            if let Some(&(from, to)) = pair.as_ref() {
                let from_to = from == self.between[0] && to == self.between[1];
                let to_from =
                    !Ty::is_directed() && to == self.between[0] && from == self.between[1];

                if from_to || to_from {
                    return Some(index);
                }
            }
        }
    }
}

#[derive(Debug)]
pub struct NeighborsIter<'a, Ty> {
    from: usize,
    neighbors: &'a [Option<(usize, usize)>],
    dir: Option<Direction>,
    index: usize,
    ty: PhantomData<Ty>,
}

impl<'a, Ty> NeighborsIter<'a, Ty> {
    pub fn new(
        from: usize,
        neighbors: &'a [Option<(usize, usize)>],
        dir: Option<Direction>,
    ) -> Self {
        Self {
            from,
            neighbors,
            dir,
            index: 0,
            ty: PhantomData,
        }
    }
}

impl<'a, Ty: EdgeType> Iterator for NeighborsIter<'a, Ty> {
    type Item = NeighborRef<VertexId, EdgeId>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let (pair, tail) = self.neighbors.split_first()?;
            self.neighbors = tail;

            let id = EdgeId::from_usize(self.index);
            self.index += 1;

            match (pair.as_ref(), self.dir, Ty::is_directed()) {
                // If vertex matches source and the graph is undirected.
                (Some(&(from, to)), _, false)
                // If the vertex matches source and the graph is directed but
                // the direction does not matter.
                | (Some(&(from, to)), None, true)
                // If the vertex matches source and the graph is directed and
                // the direction is outgoing.
                | (Some(&(from, to)), Some(Direction::Outgoing), true)
                    if from == self.from =>
                {
                    return Some(NeighborRef{
                        id: VertexId::from_usize(to),
                        edge: id,
                        pred: VertexId::from_usize(from),
                        dir: Direction::Outgoing,
                    });
                }
                // If the vertex matches destination and the graph is
                // undirected, we still consider the edge outgoing.
                (Some(&(from, to)), _, false) if to == self.from => {
                    return Some(NeighborRef {
                        id: VertexId::from_usize(from),
                        edge: id,
                        pred: VertexId::from_usize(to),
                        dir: Direction::Outgoing,
                    });
                }
                // If the vertex matches destination and the graph is directed
                // but the direction does not matter.
                (Some(&(from, to)), None, true)
                // If the vertex matches destination and the graph is directed
                // and the direction is incoming.
                | (Some(&(from, to)), Some(Direction::Incoming), true)
                    if to == self.from =>
                {
                    return Some(NeighborRef {
                        id: VertexId::from_usize(from),
                        edge: id,
                        pred: VertexId::from_usize(to),
                        dir: Direction::Incoming,
                    });
                }
                _ => {}
            }
        }
    }
}