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
use std::ops::Index;
use std::slice::Iter;

/// Hollow usize vector sorted with generic indices
///
/// Allow manipulation of big vector which contains a lot of zeroes.
/// This type of vector is very useful to represent the connection between locations and
/// transitions in order to avoid creating a matrix mainly filled with zeros.
#[derive(Debug, PartialEq, Default, Clone)]
pub struct Marking<T: Ord + Copy> {
    values: Vec<(T, usize)>,
}

impl<T> Index<T> for Marking<T>
where
    T: Ord + Copy,
{
    type Output = usize;

    fn index(&self, index: T) -> &Self::Output {
        // Search for the index in the array with complexity O(ln(n))
        // If the index is found, returns the value stored there, otherwise returns zero.
        match self.values.binary_search_by(|&v| v.0.cmp(&index)) {
            Ok(pos) => &self.values[pos].1,
            Err(_) => &0,
        }
    }
}

/// Iterator over a fusion of two markings, allows to iter over two markings and check equality
/// without searching all values for example
pub struct DualMarkingIterator<'m, T>
where
    T: Ord + Copy,
{
    /// Counter of the left marking
    current_left: usize,
    /// Counter of the right marking
    current_right: usize,
    /// Reference to the left marking
    left: &'m Marking<T>,
    /// Reference to the right marking
    right: &'m Marking<T>,
}

impl<'m, T> Iterator for DualMarkingIterator<'m, T>
where
    T: Ord + Copy,
{
    type Item = (T, usize, usize);

    fn next(&mut self) -> Option<Self::Item> {
        // We reach the end of both iterators
        if self.current_left >= self.left.len() && self.current_right >= self.right.len() {
            None
        } else if self.current_left >= self.left.len() {
            // We reach the end of the left iterator, so we continue with values of the right iterator
            self.current_right += 1;
            Some((
                self.right.values[self.current_right - 1].0,
                0,
                self.right.values[self.current_right - 1].1,
            ))
        } else if self.current_right >= self.right.len() {
            // We reach the end of the right iterator, so we continue with values of the left iterator
            self.current_left += 1;
            Some((
                self.left.values[self.current_left - 1].0,
                self.left.values[self.current_left - 1].1,
                0,
            ))
        } else if self.left.values[self.current_left].0 == self.right.values[self.current_right].0 {
            // Both index of each iterators are equals, so we return both values
            self.current_left += 1;
            self.current_right += 1;
            Some((
                self.left.values[self.current_left - 1].0,
                self.left.values[self.current_left - 1].1,
                self.right.values[self.current_right - 1].1,
            ))
        } else if self.left.values[self.current_left].0 < self.right.values[self.current_right].0 {
            // Left index is less than right index, so we increment left counter and return its value
            self.current_left += 1;
            Some((
                self.left.values[self.current_left - 1].0,
                self.left.values[self.current_left - 1].1,
                0,
            ))
        } else if self.left.values[self.current_left].0 > self.right.values[self.current_right].0 {
            // Right index is less than left index, so we increment right counter and return its value
            self.current_right += 1;
            Some((
                self.left.values[self.current_right - 1].0,
                0,
                self.right.values[self.current_right - 1].1,
            ))
        } else {
            None
        }
    }
}

impl<T> Marking<T>
where
    T: Ord + Copy,
{
    /// Return a iterator over all present elements in the marking
    #[must_use]
    pub fn iter(&self) -> Iter<'_, (T, usize)> {
        self.values.iter()
    }

    /// Return an iterator over two marking at same time.
    /// Useful to compare marking with a complexity of O(2n) instead of O(2nln(n))
    #[must_use]
    pub fn iter_with<'m>(&'m self, other: &'m Self) -> DualMarkingIterator<'m, T> {
        DualMarkingIterator {
            current_left: 0,
            current_right: 0,
            left: self,
            right: other,
        }
    }

    /// Remove all items in marking
    pub fn clear(&mut self) {
        self.values.clear();
    }

    /// Returns the number of elements in the marking.
    #[must_use]
    pub fn len(&self) -> usize {
        self.values.len()
    }

    /// Returns [`true`] if the vector contains no elements.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.values.is_empty()
    }

    /// Increment the value contained in the marking by weight.
    ///
    /// If the tag does not contain a value associated with the index, weight is inserted into the
    /// index.
    pub fn insert_or_add(&mut self, index: T, weight: usize) {
        match self.values.binary_search_by(|&v| v.0.cmp(&index)) {
            Ok(pos) => {
                if self.values[pos].1 == 0 && weight != 0 {}
                self.values[pos].1 += weight;
            }
            Err(pos) => {
                self.values.insert(pos, (index, weight));
            }
        }
    }

    /// Keeps the minimum value between that contained in the marking and weight.
    ///
    /// If the tag does not contain a value associated with the index, weight is inserted into the index.
    pub fn insert_or_min(&mut self, index: T, weight: usize) {
        match self.values.binary_search_by(|&v| v.0.cmp(&index)) {
            Ok(pos) => {
                if weight == 0 && self.values[pos].1 != 0 {}
                self.values[pos].1 = self.values[pos].1.min(weight);
            }
            Err(pos) => {
                self.values.insert(pos, (index, weight));
            }
        }
    }

    /// Keeps the maximum value between that contained in the marking and weight.
    ///
    /// If the tag does not contain a value associated with the index, weight is inserted into the index.
    pub fn insert_or_max(&mut self, index: T, weight: usize) {
        match self.values.binary_search_by(|&v| v.0.cmp(&index)) {
            Ok(pos) => {
                self.values[pos].1 = self.values[pos].1.max(weight);
            }
            Err(pos) => {
                self.values.insert(pos, (index, weight));
            }
        }
    }

    /// Delete a specific index from the marking
    pub fn delete(&mut self, index: T) {
        if let Ok(index) = self.values.binary_search_by(|&v| v.0.cmp(&index)) {
            self.values.remove(index);
        }
    }
}