Line data Source code
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 2045284 : bool set_synchronize(const SetType *set1, const SetType *set2, 22 : AddFunctor add_fn, DelFunctor del_fn) { 23 2045284 : typename SetType::iterator it1 = set1->begin(), next1 = set1->begin(); 24 2044801 : typename SetType::const_iterator it2 = set2->begin(); 25 2045168 : bool modified = false; 26 5767052 : while (it1 != set1->end() && it2 != set2->end()) { 27 3721859 : if (*it1 < *it2) { 28 15623 : ++next1; 29 15623 : modified = true; 30 15623 : del_fn(it1); 31 15622 : it1 = next1; 32 3706399 : } else if (*it1 > *it2) { 33 4402 : modified = true; 34 4402 : add_fn(it2); 35 4402 : ++it2; 36 : } else { 37 3701905 : ++it1; 38 3701915 : ++it2; 39 : } 40 3721884 : next1 = it1; 41 : } 42 3051787 : for (next1 = it1; it1 != set1->end(); it1 = next1) { 43 1006330 : ++next1; 44 1006321 : modified = true; 45 1006321 : del_fn(it1); 46 : } 47 3065081 : for (; it2 != set2->end(); ++it2) { 48 1019646 : modified = true; 49 1019646 : add_fn(it2); 50 : } 51 2045440 : return modified; 52 : } 53 : 54 : #endif // BASE_SET_UTIL_H