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