-
Notifications
You must be signed in to change notification settings - Fork 36
/
delta-crdts.cc
2053 lines (1779 loc) · 44 KB
/
delta-crdts.cc
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//-------------------------------------------------------------------
//
// File: delta-crdts.cc
//
// @author Carlos Baquero <[email protected]>
//
// @copyright 2014-2016 Carlos Baquero
//
// This file is provided to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
// @doc
// Reference implementation in C++ of delta enabled CRDTs
// @end
//
//
//-------------------------------------------------------------------
#include <set>
#include <unordered_set>
#include <map>
#include <list>
#include <tuple>
#include <vector>
#include <string>
#include <iostream>
#include <type_traits>
using namespace std;
template< bool b >
struct join_selector {
template< typename T >
static T join( const T& l, const T& r )
{
T res;
res=l;
res.join(r);
return res;
}
};
template<>
struct join_selector < true > {
template< typename T >
static T join( const T& l, const T& r )
{
T res;
res=max(l,r);
return res;
}
};
// Join with C++ traits
template<typename T> // Join two objects, deriving a new one
T join(const T& l, const T& r) // assuming copy constructor
{
return join_selector< is_arithmetic<T>::value >::join(l,r);
}
template<typename A, typename B> // Join two pairs of objects
pair<A,B> join(const pair<A,B>& l, const pair<A,B>& r)
{
pair<A,B> res;
res.first=join(r.first,l.first);
res.second=join(r.second,l.second);
return res;
}
template<typename A, typename B> // Join lexicographic of two pairs of objects
pair<A,B> lexjoin(const pair<A,B>& l, const pair<A,B>& r)
{
pair<A,B> res;
if (r==l) return res=r;
if (l.first > r.first) return res=l;
if (r.first > l.first) return res=r;
// Left is equal, so join right
if (r.first == l.first)
{
res.first=r.first;
res.second=join(r.second,l.second);
return res;
}
// Otherwise A is not a total order so keep res empty to signal error
return res;
}
template<typename A, typename B> // Output a pair
ostream &operator<<( ostream &output, const pair<A,B>& o)
{
output << "(" << o.first << "," << o.second << ")";
return output;
}
template<typename T> // Output a set
ostream &operator<<( ostream &output, const set<T>& o)
{
output << "( ";
for (const auto& e : o) output << e << " ";
output << ")";
return output;
}
template<typename T> // get a point among two points
vector<T> among(const vector<T> & l, const vector<T> & r, int j=0)
{
// Overall strategy is to first try wide advances to the right,
// as compact as possible. If that fails, go with fine grain (less compact)
// advances until eventually succeed.
assert (l < r);
vector<T> res;
// adjust res as forwardly compact as possible
for (int is = 0; is <= l.size(); is++)
{
res.assign(l.begin(),l.begin()+is); // get initial segment
if ( is < l.size() ) // if partial segment, try appending one
{
res.push_back(true);
if ( res >= l && res < r ) break; // see if we are there
}
}
assert (res >= l && res < r);
if (res > l) return res;
//vector<T> res=l;
// forward finer and finer
for(int i = 0; i < j; i++){
res.push_back(false);
}
res.push_back(true);
while (res >= r)
{
res.back()=false;
for(int i = 0; i < j; i++){
res.push_back(false);
}
res.push_back(true);
}
assert (res > l && res < r);
return res;
}
template<typename T> // Output a vector
ostream &operator<<( ostream &output, const vector<T>& o)
{
output << "[";
for (const auto & e : o) output << e;
output << "]";
return output;
}
// Autonomous causal context, for context sharing in maps
template<typename K>
class dotcontext
{
public:
map<K,int> cc; // Compact causal context
set<pair<K,int> > dc; // Dot cloud
dotcontext<K> & operator=(const dotcontext<K> & o)
{
if (&o == this) return *this;
cc=o.cc; dc=o.dc;
return *this;
}
friend ostream &operator<<( ostream &output, const dotcontext<K>& o)
{
output << "Context:";
output << " CC ( ";
for (const auto & ki : o.cc)
output << ki.first << ":" << ki.second << " ";
output << ")";
output << " DC ( ";
for (const auto & ki : o.dc)
output << ki.first << ":" << ki.second << " ";
output << ")";
return output;
}
bool dotin (const pair<K,int> & d) const
{
const auto itm = cc.find(d.first);
if (itm != cc.end() && d.second <= itm->second) return true;
if (dc.count(d)!=0) return true;
return false;
}
void compact()
{
// Compact DC to CC if possible
//typename map<K,int>::iterator mit;
//typename set<pair<K,int> >::iterator sit;
bool flag; // may need to compact several times if ordering not best
do
{
flag=false;
for(auto sit = dc.begin(); sit != dc.end();)
{
auto mit=cc.find(sit->first);
if (mit==cc.end()) // No CC entry
if (sit->second == 1) // Can compact
{
cc.insert(*sit);
dc.erase(sit++);
flag=true;
}
else ++sit;
else // there is a CC entry already
if (sit->second == cc.at(sit->first) + 1) // Contiguous, can compact
{
cc.at(sit->first)++;
dc.erase(sit++);
flag=true;
}
else
if (sit->second <= cc.at(sit->first)) // dominated, so prune
{
dc.erase(sit++);
// no extra compaction oportunities so flag untouched
}
else ++sit;
}
}
while(flag==true);
}
pair<K,int> makedot(const K & id)
{
// On a valid dot generator, all dots should be compact on the used id
// Making the new dot, updates the dot generator and returns the dot
// pair<typename map<K,int>::iterator,bool> ret;
auto kib=cc.insert(pair<K,int>(id,1));
if (kib.second==false) // already there, so update it
(kib.first->second)+=1;
//return dot;
return pair<K,int>(*kib.first);
}
void insertdot(const pair<K,int> & d, bool compactnow=true)
{
// Set
dc.insert(d);
if (compactnow) compact();
}
void join (const dotcontext<K> & o)
{
if (this == &o) return; // Join is idempotent, but just dont do it.
// CC
//typename map<K,int>::iterator mit;
//typename map<K,int>::const_iterator mito;
auto mit=cc.begin(); auto mito=o.cc.begin();
do
{
if (mit != cc.end() && (mito == o.cc.end() || mit->first < mito->first))
{
// cout << "cc one\n";
// entry only at here
++mit;
}
else if (mito != o.cc.end() && (mit == cc.end() || mito->first < mit->first))
{
// cout << "cc two\n";
// entry only at other
cc.insert(*mito);
++mito;
}
else if ( mit != cc.end() && mito != o.cc.end() )
{
// cout << "cc three\n";
// in both
cc.at(mit->first)=max(mit->second,mito->second);
++mit; ++mito;
}
} while (mit != cc.end() || mito != o.cc.end());
// DC
// Set
for (const auto & e : o.dc)
insertdot(e,false);
compact();
}
};
template <typename T, typename K>
class dotkernel
{
public:
map<pair<K,int>,T> ds; // Map of dots to vals
dotcontext<K> cbase;
dotcontext<K> & c;
// if no causal context supplied, used base one
dotkernel() : c(cbase) {}
// if supplied, use a shared causal context
dotkernel(dotcontext<K> &jointc) : c(jointc) {}
// dotkernel(const dotkernel<T,K> &adk) : c(adk.c), ds(adk.ds) {}
dotkernel<T,K> & operator=(const dotkernel<T,K> & adk)
{
if (&adk == this) return *this;
if (&c != &adk.c) c=adk.c;
ds=adk.ds;
return *this;
}
friend ostream &operator<<( ostream &output, const dotkernel<T,K>& o)
{
output << "Kernel: DS ( ";
for (const auto & dv : o.ds)
output << dv.first.first << ":" << dv.first.second <<
"->" << dv.second << " ";
output << ") ";
cout << o.c;
return output;
}
void join (const dotkernel<T,K> & o)
{
if (this == &o) return; // Join is idempotent, but just dont do it.
// DS
// will iterate over the two sorted sets to compute join
//typename map<pair<K,int>,T>::iterator it;
//typename map<pair<K,int>,T>::const_iterator ito;
auto it=ds.begin(); auto ito=o.ds.begin();
do
{
if ( it != ds.end() && ( ito == o.ds.end() || it->first < ito->first))
{
// dot only at this
if (o.c.dotin(it->first)) // other knows dot, must delete here
ds.erase(it++);
else // keep it
++it;
}
else if ( ito != o.ds.end() && ( it == ds.end() || ito->first < it->first))
{
// dot only at other
if(! c.dotin(ito->first)) // If I dont know, import
ds.insert(*ito);
++ito;
}
else if ( it != ds.end() && ito != o.ds.end() )
{
// dot in both
++it; ++ito;
}
} while (it != ds.end() || ito != o.ds.end() );
// CC
c.join(o.c);
}
void deepjoin (const dotkernel<T,K> & o)
{
if (this == &o) return; // Join is idempotent, but just dont do it.
// DS
// will iterate over the two sorted sets to compute join
//typename map<pair<K,int>,T>::iterator it;
//typename map<pair<K,int>,T>::const_iterator ito;
auto it=ds.begin(); auto ito=o.ds.begin();
do
{
if ( it != ds.end() && ( ito == o.ds.end() || it->first < ito->first))
{
// dot only at this
if (o.c.dotin(it->first)) // other knows dot, must delete here
ds.erase(it++);
else // keep it
++it;
}
else if ( ito != o.ds.end() && ( it == ds.end() || ito->first < it->first))
{
// dot only at other
if(! c.dotin(ito->first)) // If I dont know, import
ds.insert(*ito);
++ito;
}
else if ( it != ds.end() && ito != o.ds.end() )
{
// dot in both
// check it payloads are diferent
if (it->second != ito->second)
{
// if payloads are not equal, they must be mergeable
// use the more general binary join
it->second=::join(it->second,ito->second);
}
++it; ++ito;
}
} while (it != ds.end() || ito != o.ds.end() );
// CC
c.join(o.c);
}
dotkernel<T,K> add (const K& id, const T& val)
{
dotkernel<T,K> res;
// get new dot
pair<K,int> dot=c.makedot(id);
// add under new dot
ds.insert(pair<pair<K,int>,T>(dot,val));
// make delta
res.ds.insert(pair<pair<K,int>,T>(dot,val));
res.c.insertdot(dot);
return res;
}
// Add that returns the added dot, instead of kernel delta
pair<K,int> dotadd (const K& id, const T& val)
{
// get new dot
pair<K,int> dot=c.makedot(id);
// add under new dot
ds.insert(pair<pair<K,int>,T>(dot,val));
return dot;
}
dotkernel<T,K> rmv (const T& val) // remove all dots matching value
{
dotkernel<T,K> res;
//typename map<pair<K,int>,T>::iterator dsit;
for(auto dsit=ds.begin(); dsit != ds.end();)
{
if (dsit->second == val) // match
{
res.c.insertdot(dsit->first,false); // result knows removed dots
ds.erase(dsit++);
}
else
++dsit;
}
res.c.compact(); // Maybe several dots there, so atempt compactation
return res;
}
dotkernel<T,K> rmv (const pair<K,int>& dot) // remove a dot
{
dotkernel<T,K> res;
auto dsit=ds.find(dot);
if (dsit != ds.end()) // found it
{
res.c.insertdot(dsit->first,false); // result knows removed dots
ds.erase(dsit++);
}
res.c.compact(); // Atempt compactation
return res;
}
dotkernel<T,K> rmv () // remove all dots
{
dotkernel<T,K> res;
for (const auto & dv : ds)
res.c.insertdot(dv.first,false);
res.c.compact();
ds.clear(); // Clear the payload, but remember context
return res;
}
};
template <typename V=int, typename K=string>
class gcounter
{
private:
map<K,V> m;
K id;
public:
gcounter() {} // Only for deltas and those should not be mutated
gcounter(K a) : id(a) {} // Mutable replicas need a unique id
gcounter inc(V tosum={1}) // argument is optional
{
gcounter<V,K> res;
m[id]+=tosum;
res.m[id]=m[id];
return res;
}
bool operator == ( const gcounter<V,K>& o ) const
{
return m==o.m;
}
V local() // get local counter value // CBM make this const
{
V res=0;
res += m[id];
return res;
}
V read() const // get counter value
{
V res=0;
for (const auto& kv : m) // Fold+ on value list
res += kv.second;
return res;
}
void join(const gcounter<V,K>& o)
{
for (const auto& okv : o.m)
m[okv.first]=max(okv.second,m[okv.first]);
}
friend ostream &operator<<( ostream &output, const gcounter<V,K>& o)
{
output << "GCounter: ( ";
for (const auto& kv : o.m)
output << kv.first << "->" << kv.second << " ";
output << ")";
return output;
}
};
template <typename V=int, typename K=string>
class pncounter
{
private:
gcounter<V,K> p,n;
public:
pncounter() {} // Only for deltas and those should not be mutated
pncounter(K a) : p(a), n(a) {} // Mutable replicas need a unique id
pncounter inc(V tosum={1}) // Argument is optional
{
pncounter<V,K> res;
res.p = p.inc(tosum);
return res;
}
pncounter dec(V tosum={1}) // Argument is optional
{
pncounter<V,K> res;
res.n = n.inc(tosum);
return res;
}
V local() // get local counter value
{
V res=p.local()-n.local();
return res;
}
V read() const // get counter value
{
V res=p.read()-n.read();
return res;
}
void join(const pncounter& o)
{
p.join(o.p);
n.join(o.n);
}
friend ostream &operator<<( ostream &output, const pncounter<V,K>& o)
{
output << "PNCounter:P:" << o.p << " PNCounter:N:" << o.n;
return output;
}
};
template <typename V=int, typename K=string>
class lexcounter
{
private:
map<K,pair<int,V> > m;
K id;
public:
lexcounter() {} // Only for deltas and those should not be mutated
lexcounter(K a) : id(a) {} // Mutable replicas need a unique id
lexcounter inc(V tosum=1) // Argument is optional
{
lexcounter<V,K> res;
// m[id].first+=1; // optional
m[id].second+=tosum;
res.m[id]=m[id];
return res;
}
lexcounter dec(V tosum=1) // Argument is optional
{
lexcounter<V,K> res;
m[id].first+=1; // mandatory
m[id].second-=tosum;
res.m[id]=m[id];
return res;
}
V read() const // get counter value
{
V res=0;
for (const auto& kv : m) // Fold+ on value list
res += kv.second.second;
return res;
}
void join(const lexcounter<V,K>& o)
{
for (const auto& okv : o.m)
m[okv.first]=lexjoin(okv.second,m[okv.first]);
}
friend ostream &operator<<( ostream &output, const lexcounter<V,K>& o)
{
output << "LexCounter: ( ";
for (const auto& kv : o.m)
output << kv.first << "->" << kv.second << " ";
output << ")";
return output;
}
};
template<typename V, typename K=string>
class ccounter // Causal counter, variation of Riak_dt_emcntr and lexcounter
{
private:
// To re-use the kernel there is an artificial need for dot-tagged bool payload
dotkernel<V,K> dk; // Dot kernel
K id;
public:
ccounter() {} // Only for deltas and those should not be mutated
ccounter(K k) : id(k) {} // Mutable replicas need a unique id
ccounter(K k, dotcontext<K> &jointc) : id(k), dk(jointc) {}
dotcontext<K> & context()
{
return dk.c;
}
friend ostream &operator<<( ostream &output, const ccounter<V,K>& o)
{
output << "CausalCounter:" << o.dk;
return output;
}
ccounter<V,K> inc (const V& val=1)
{
ccounter<V,K> r;
set<pair<K,int>> dots; // dots to remove, should be only 1
V base = {}; // typically 0
for(const auto & dsit : dk.ds)
{
if (dsit.first.first == id) // there should be a single one such
{
base=max(base,dsit.second);
dots.insert(dsit.first);
}
}
for(const auto & dot : dots)
r.dk.join(dk.rmv(dot));
r.dk.join(dk.add(id,base+val));
return r;
}
ccounter<V,K> dec (const V& val=1)
{
ccounter<V,K> r;
set<pair<K,int>> dots; // dots to remove, should be only 1
V base = {}; // typically 0
for(const auto & dsit : dk.ds)
{
if (dsit.first.first == id) // there should be a single one such
{
base=max(base,dsit.second);
dots.insert(dsit.first);
}
}
for(const auto & dot : dots)
r.dk.join(dk.rmv(dot));
r.dk.join(dk.add(id,base-val));
return r;
}
ccounter<V,K> reset () // Other nodes might however upgrade their counts
{
ccounter<V,K> r;
r.dk=dk.rmv();
return r;
}
V read ()
{
V v = {}; // Usually 0
for (const auto & dse : dk.ds)
v+=dse.second;
return v;
}
void join (ccounter<V,K> o)
{
dk.join(o.dk);
}
};
template<typename T>
class gset
{
private:
set<T> s;
public:
gset() {}
// gset(string id, dotcontext<K> &jointdc) {}
// For map compliance reply with an empty context
// dotcontext<K> context()
// {
// return dotcontext<K>();
// }
set<T> read () const { return s; }
bool operator == ( const gset<T>& o ) const { return s==o.s; }
bool in (const T& val)
{
return s.count(val);
}
friend ostream &operator<<( ostream &output, const gset<T>& o)
{
output << "GSet: " << o.s;
return output;
}
gset<T> add (const T& val)
{
gset<T> res;
s.insert(val);
res.s.insert(val);
return res;
}
void join (const gset<T>& o)
{
s.insert(o.s.begin(), o.s.end());
}
};
template<typename T, typename K=string> // Map embedable datatype
class twopset
{
private:
set<T> s;
set<T> t; // removed elements are added to t and removed from s
public:
twopset() {}
twopset(string id, dotcontext<K> &jointdc) {}
// For map compliance reply with an empty context
dotcontext<K> context()
{
return dotcontext<K>();
}
set<T> read () { return s; }
bool operator == ( const twopset<T>& o ) const
{
return s==o.s && t==o.t;
}
bool in (const T& val)
{
return s.count(val);
}
friend ostream &operator<<( ostream &output, const twopset<T>& o)
{
output << "2PSet: S" << o.s << " T " << o.t;
return output;
}
twopset<T> add (const T& val)
{
twopset<T> res;
if (t.count(val) == 0) // only add if not in tombstone set
{
s.insert(val);
res.s.insert(val);
}
return res;
}
twopset<T> rmv (const T& val)
{
twopset<T> res;
s.erase(val);
t.insert(val); // add to tombstones
res.t.insert(val);
return res;
}
twopset<T> reset ()
{
twopset<T> res;
for (auto const & val : s)
{
t.insert(val);
res.t.insert(val);
}
s.clear();
return res;
}
void join (const twopset<T>& o)
{
for (const auto& ot : o.t) // see other tombstones
{
t.insert(ot); // insert them locally
if (s.count(ot) == 1) // remove val if present
s.erase(ot);
}
for (const auto& os : o.s) // add other vals, if not tombstone
{
if (t.count(os) == 0) s.insert(os);
}
}
};
template<typename E, typename K=string> // Map embedable datatype
class aworset // Add-Wins Observed-Remove Set
{
private:
dotkernel<E,K> dk; // Dot kernel
K id;
public:
aworset() {} // Only for deltas and those should not be mutated
aworset(K k) : id(k) {} // Mutable replicas need a unique id
aworset(K k, dotcontext<K> &jointc) : id(k), dk(jointc) {}
dotcontext<K> & context()
{
return dk.c;
}
friend ostream &operator<<( ostream &output, const aworset<E,K>& o)
{
output << "AWORSet:" << o.dk;
return output;
}
set<E> read ()
{
set<E> res;
for (const auto &dv : dk.ds)
res.insert(dv.second);
return res;
}
bool in (const E& val)
{
typename map<pair<K,int>,E>::iterator dsit;
for(dsit=dk.ds.begin(); dsit != dk.ds.end();++dsit)
{
if (dsit->second == val)
return true;
}
return false;
}
aworset<E,K> add (const E& val)
{
aworset<E,K> r;
r.dk=dk.rmv(val); // optimization that first deletes val
r.dk.join(dk.add(id,val));
return r;
}
aworset<E,K> rmv (const E& val)
{
aworset<E,K> r;
r.dk=dk.rmv(val);
return r;
}
aworset<E,K> reset()
{
aworset<E,K> r;
r.dk=dk.rmv();
return r;
}
void join (aworset<E,K> o)
{
dk.join(o.dk);
// Further optimization can be done by keeping for val x and id A
// only the highest dot from A supporting x.
}
};
template<typename E, typename K=string> // Map embedable datatype
class rworset // Remove-Wins Observed-Remove Set
{
private:
dotkernel<pair<E,bool>,K> dk; // Dot kernel
K id;
public:
rworset() {} // Only for deltas and those should not be mutated
rworset(K k) : id(k) {} // Mutable replicas need a unique id
rworset(K k, dotcontext<K> &jointc) : id(k), dk(jointc) {}
dotcontext<K> & context()
{
return dk.c;
}
friend ostream &operator<<( ostream &output, const rworset<E,K>& o)
{
output << "RWORSet:" << o.dk;
return output;
}
set<E> read ()
{
set<E> res;
map<E,bool> elems;
typename map<pair<K,int>,pair<E,bool> >::iterator dsit;
pair<typename map<E,bool>::iterator,bool> ret;
for(dsit=dk.ds.begin(); dsit != dk.ds.end();++dsit)
{
ret=elems.insert(pair<E,bool>(dsit->second));
if (ret.second==false) // val already exists
{
elems.at(ret.first->first) &= dsit->second.second; // Fold by &&
}
}
typename map<E,bool>::iterator mit;
for (mit=elems.begin(); mit != elems.end(); ++mit)
{
if (mit->second == true) res.insert(mit->first);
}
return res;
}
bool in (const E& val) // Could
{
// Code could be slightly faster if re-using only part of read code
set<E> s=read();
if ( s.find(val) != s.end() ) return true;
return false;
}
rworset<E,K> add (const E& val)
{
rworset<E,K> r;
r.dk=dk.rmv(pair<E,bool>(val,true)); // Remove any observed add token
r.dk.join(dk.rmv(pair<E,bool>(val,false))); // Remove any observed remove token
r.dk.join(dk.add(id,pair<E,bool>(val,true)));
return r;
}
rworset<E,K> rmv (const E& val)