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
use std::cmp::{Ordering};

use rustc_serialize::{Encodable, Decodable};

use super::VClock;

/// `GCounter` is a grow-only witnessed counter.
///
/// # Examples
///
/// ```
/// use crdts::GCounter;
/// let (mut a, mut b) = (GCounter::new(), GCounter::new());
/// a.increment("A".to_string());
/// b.increment("B".to_string());
/// assert_eq!(a.value(), b.value());
/// assert!(a == b);
/// a.increment("A".to_string());
/// assert!(a > b);
/// ```
#[derive(Debug, Eq, Clone, Hash, RustcEncodable, RustcDecodable)]
pub struct GCounter<A: Ord + Clone + Encodable + Decodable> {
    inner: VClock<A>
}

impl<A: Ord + Clone + Encodable + Decodable> Ord for GCounter<A> {
    fn cmp(&self, other: &GCounter<A>) -> Ordering {
        let (c, oc) = (self.value(), other.value());
        c.cmp(&oc)
    }
}

impl<A: Ord + Clone + Encodable + Decodable> PartialOrd for GCounter<A> {
    fn partial_cmp(&self, other: &GCounter<A>) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<A: Ord + Clone + Encodable + Decodable> PartialEq for GCounter<A> {
    fn eq(&self, other: &GCounter<A>) -> bool {
        let (c, oc) = (self.value(), other.value());
        c == oc
    }
}

impl<A: Ord + Clone + Encodable + Decodable> GCounter<A> {
    /// Produces a new `GCounter`.
    pub fn new() -> GCounter<A> {
        GCounter {
            inner: VClock::new()
        }
    }

    /// Increments a particular actor's counter.
    pub fn increment(&mut self, actor: A) {
        self.inner.increment(actor);
    }

    /// Returns the current sum of this counter.
    pub fn value(&self) -> u64 {
        self.inner.dots.values().fold(0, |acc, count| acc + count)
    }

    /// Merge another gcounter into this one, without
    /// regard to dominance.
    ///
    /// # Examples
    ///
    /// ```
    /// use crdts::GCounter;
    /// let (mut a, mut b, mut c) = (GCounter::new(), GCounter::new(), GCounter::new());
    /// a.increment("A".to_string());
    /// b.increment("B".to_string());
    /// c.increment("A".to_string());
    /// c.increment("B".to_string());
    /// a.merge(b);
    /// assert_eq!(a, c);
    pub fn merge(&mut self, other: GCounter<A>) {
        self.inner.merge(other.inner);
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_basic() {
        let (mut a, mut b) = (GCounter::new(), GCounter::new());
        a.increment("A".to_string());
        b.increment("B".to_string());
        assert_eq!(a.value(), b.value());
        assert!(a == b);
        a.increment("A".to_string());
        assert!(a > b);
    }
}