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 2047405 : bool set_synchronize(const SetType *set1, const SetType *set2, 22 : AddFunctor add_fn, DelFunctor del_fn) { 23 2047405 : typename SetType::iterator it1 = set1->begin(), next1 = set1->begin(); 24 2047266 : typename SetType::const_iterator it2 = set2->begin(); 25 2047246 : bool modified = false; 26 5763527 : while (it1 != set1->end() && it2 != set2->end()) { 27 3716193 : if (*it1 < *it2) { 28 15134 : ++next1; 29 15134 : modified = true; 30 15134 : del_fn(it1); 31 15135 : it1 = next1; 32 3701198 : } else if (*it1 > *it2) { 33 4514 : modified = true; 34 4514 : add_fn(it2); 35 4514 : ++it2; 36 : } else { 37 3696684 : ++it1; 38 3696696 : ++it2; 39 : } 40 3716281 : next1 = it1; 41 : } 42 3053350 : for (next1 = it1; it1 != set1->end(); it1 = next1) { 43 1005744 : ++next1; 44 1005719 : modified = true; 45 1005719 : del_fn(it1); 46 : } 47 3066070 : for (; it2 != set2->end(); ++it2) { 48 1018516 : modified = true; 49 1018516 : add_fn(it2); 50 : } 51 2047542 : return modified; 52 : } 53 : 54 : #endif // BASE_SET_UTIL_H