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
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Transition;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Place;

// A generic identifier for places and transitions.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Id<T> {
    pub(super) index: usize,
    kind: T,
}

/// The identifier meant to represent transitions.
pub type Tid = Id<Transition>;

impl Tid {
    #[inline]
    pub(super) fn new(index: usize) -> Self {
        Self {
            index,
            kind: Transition,
        }
    }

    #[inline(always)]
    pub(super) fn assert(self, count: usize) {
        assert!(self.index < count, "Transition id out of range");
    }
}

/// The identifier meant to represent places.
pub type Pid = Id<Place>;

impl Pid {
    #[inline]
    pub(super) fn new(index: usize) -> Self {
        Self { index, kind: Place }
    }

    #[inline(always)]
    pub(super) fn assert(self, count: usize) {
        assert!(self.index < count, "Place id out of range");
    }
}

/// An iterator over some ids.
pub struct Ids<T> {
    pub(super) start: usize,
    pub(super) end: usize,
    pub(super) kind: T,
}

impl<T: Copy> Iterator for Ids<T> {
    type Item = Id<T>;

    fn next(&mut self) -> Option<Id<T>> {
        if self.start < self.end {
            let id = Id {
                index: self.start,
                kind: self.kind,
            };
            self.start += 1;
            Some(id)
        } else {
            None
        }
    }
}

impl<T: Copy> DoubleEndedIterator for Ids<T> {
    fn next_back(&mut self) -> Option<Id<T>> {
        if self.start < self.end {
            self.end -= 1;
            Some(Id {
                index: self.end,
                kind: self.kind,
            })
        } else {
            None
        }
    }
}

impl<T: Copy> ExactSizeIterator for Ids<T> {
    fn len(&self) -> usize {
        self.end - self.start
    }
}