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
use std::fmt::Debug;
use std::hash::Hash;

/// Count trait to be used in `MultiSet`.
pub trait Count: Copy {
    /// Return a zero count.
    fn zero() -> Self;

    /// Add to the count.
    fn add(&mut self, other: Self);
}

impl Count for u64 {
    /// Return a zero count.
    fn zero() -> Self {
        0
    }

    /// Add to the count.
    fn add(&mut self, other: Self) {
        *self += other;
    }
}

impl Count for (u64, u64) {
    /// Return a zero count.
    fn zero() -> Self {
        (0, 0)
    }

    /// Add to the count.
    fn add(&mut self, other: Self) {
        self.0 += other.0;
        self.1 += other.1;
    }
}

/// Actor trait to be used in `Clock`'s or `TClock`'s.
pub trait Actor: Debug + Clone + Hash + Eq + Ord {}
impl<A: Debug + Clone + Hash + Eq + Ord> Actor for A {}

/// EventSet trait to be implemented by `MaxSet`, `BelowExSet` and `AboveExSet`.
pub trait EventSet: IntoIterator + Clone + Debug {
    /// Returns a new instance.
    fn new() -> Self;

    /// Creates a new instance from `event`.
    fn from_event(event: u64) -> Self {
        let mut eset = Self::new();
        eset.add_event(event);
        eset
    }

    /// Creates a new instance from a range of events.
    fn from_event_range(start: u64, end: u64) -> Self {
        let mut eset = Self::new();
        eset.add_event_range(start, end);
        eset
    }

    /// Creates a new instance from several `events`.
    fn from_events<I: IntoIterator<Item = u64>>(iter: I) -> Self {
        let mut eset = Self::new();
        for event in iter {
            eset.add_event(event);
        }
        eset
    }

    /// Generates the next event.
    fn next_event(&mut self) -> u64;

    /// Adds an event to the set.
    fn add_event(&mut self, event: u64) -> bool;

    /// Adds a range of events to the set.
    fn add_event_range(&mut self, start: u64, end: u64) -> bool {
        let mut res = false;
        (start..=end).for_each(|event| {
            let added = self.add_event(event);
            res = res || added;
        });
        res
    }

    /// Checks if an event is part of the set.
    fn is_event(&self, event: u64) -> bool;

    /// Returns all events seen as a pair.
    ///
    /// For `MaxSet`:
    /// - the first component is the highest event
    /// - the second component is empty
    ///
    /// For `BelowExSet`:
    /// - the first component is the highest event
    /// - the second component is a set of exceptions
    ///
    /// For `AboveExSet`:
    /// - the first component is the highest event in a contiguous sequence
    /// - the second component is a set of outstanding events
    ///
    /// If we've seen events [1, 2, 3, 5, 6], this function returns in
    /// - `MaxSet`: (6, [])
    /// - `BelowExSet`: (6, \[4\])
    /// - `AboveExSet`: (3, \[5, 6\])
    fn events(&self) -> (u64, Vec<u64>);

    /// Returns the frontier (the highest contiguous event seen).
    fn frontier(&self) -> u64;

    /// Merges `other` `EventSet` into `self`.
    fn join(&mut self, other: &Self);
}