OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
set_util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015 Juniper Networks, Inc. All rights reserved.
3  */
4 #ifndef BASE_SET_UTIL_H
5 #define BASE_SET_UTIL_H
6 
7 //
8 // set_synchronize
9 //
10 // Given two sets synchronize the 1st (current) set with the 2nd (future)
11 // one by invoking add or delete functors for values that are:
12 // add - present in the 2nd set but not in the 1st;
13 // delete - present in the 1st set but not in the 2nd;
14 //
15 // The add/delete functors are responsible for adding/deleting appropriate
16 // elements to/from the 1st set.
17 //
18 // Returns true if any set is modified, false if they are identical.
19 //
20 template <typename SetType, typename AddFunctor, typename DelFunctor>
21 bool set_synchronize(const SetType *set1, const SetType *set2,
22  AddFunctor add_fn, DelFunctor del_fn) {
23  typename SetType::iterator it1 = set1->begin(), next1 = set1->begin();
24  typename SetType::const_iterator it2 = set2->begin();
25  bool modified = false;
26  while (it1 != set1->end() && it2 != set2->end()) {
27  if (*it1 < *it2) {
28  ++next1;
29  modified = true;
30  del_fn(it1);
31  it1 = next1;
32  } else if (*it1 > *it2) {
33  modified = true;
34  add_fn(it2);
35  ++it2;
36  } else {
37  ++it1;
38  ++it2;
39  }
40  next1 = it1;
41  }
42  for (next1 = it1; it1 != set1->end(); it1 = next1) {
43  ++next1;
44  modified = true;
45  del_fn(it1);
46  }
47  for (; it2 != set2->end(); ++it2) {
48  modified = true;
49  add_fn(it2);
50  }
51  return modified;
52 }
53 
54 #endif // BASE_SET_UTIL_H
bool set_synchronize(const SetType *set1, const SetType *set2, AddFunctor add_fn, DelFunctor del_fn)
Definition: set_util.h:21