コード例 #1
0
ファイル: PNCounter.cs プロジェクト: peschkaj/MoarDT
 public PNCounter(string clientId = null,
                   Dictionary<string, ulong> p = null, 
                   Dictionary<string, ulong> n = null)
 {
     _clientId = String.IsNullOrEmpty(clientId) ? DefaultClientId() : clientId;
     P = new GCounter(_clientId, counterContents: p);
     N = new GCounter(_clientId, counterContents: n);
 }
コード例 #2
0
ファイル: GCounterTest.cs プロジェクト: peschkaj/MoarDT
        public void ValuesConverge()
        {
            var gca = new GCounter("1");
            var gcb = new GCounter("2");
            var gcc = new GCounter("3");

            gca++;
            gcb.Increment(10);
            gcc.Increment(4);

            var gcResult = GCounter.Merge(GCounter.Merge(gca, gcb), gcc);

            gcResult.Value.ShouldEqual(15UL);
        }
コード例 #3
0
ファイル: GCounterTest.cs プロジェクト: peschkaj/MoarDT
        public void MergingTwoGCountersReturnsTheLargestValue()
        {
            var gca = new GCounter();
            var gcb = new GCounter();

            gca++;
            gca++;
            gcb++;

            var gcaVal = gca.Value;
            var gcbVal = gcb.Value;

            var gcnew = GCounter.Merge(gca, gcb);

            gcnew.Value.ShouldEqual(gcaVal);
            gcnew.Value.ShouldNotEqual(gcbVal);
        }
コード例 #4
0
ファイル: GCounter.cs プロジェクト: peschkaj/MoarDT
        public static GCounter Merge(GCounter gca, GCounter gcb, string clientId = null)
        {
            /* let ∀i ∈ [0,n − 1] : Z.P[i] = max(X.P[i],Y.P[i]) */
            var keys = gca.Payload.Keys.Union(gcb.Payload.Keys);
            var newContents = new Dictionary<string, ulong>();

            foreach (var key in keys)
            {
                if (!gca.Payload.ContainsKey(key) && gcb.Payload.ContainsKey(key))
                    newContents[key] = gcb.Payload[key];
                else if (gca.Payload.ContainsKey(key) && !gcb.Payload.ContainsKey(key))
                    newContents[key] = gca.Payload[key];
                else
                    newContents[key] = Math.Max(gca.Payload[key], gcb.Payload[key]);
            }

            return new GCounter(clientId ?? DefaultClientId(),
                                counterContents: newContents);
        }
コード例 #5
0
ファイル: GCounter.cs プロジェクト: peschkaj/MoarDT
        public bool Equals(GCounter other)
        {
            /* (∀i ∈ [0, n − 1] : X.P [i] ≤ Y.P [i]) */
            if (ReferenceEquals(null, other))
                return false;

            if (ReferenceEquals(this, other))
                return true;

            if (Payload.Equals(other.Payload))
                return true;

            return Payload.Count() == other.Payload.Count()
                   && Payload.Keys.Intersect(other.Payload.Keys).Count() == Payload.Count()
                   && Payload.Equals(other.Payload);
        }
コード例 #6
0
ファイル: PNCounter.cs プロジェクト: peschkaj/MoarDT
 public PNCounter(ulong currentValue = default(ulong))
 {
     positive = new GCounter();
     negative = new GCounter();
     _currentValue = currentValue;
 }
コード例 #7
0
ファイル: GCounterTest.cs プロジェクト: peschkaj/MoarDT
        public void NewGCountersAreZero()
        {
            var gca = new GCounter();

            gca.Value.ShouldEqual(0UL);
        }