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 181607 : bool set_synchronize(const SetType *set1, const SetType *set2, 22 : AddFunctor add_fn, DelFunctor del_fn) { 23 181607 : typename SetType::iterator it1 = set1->begin(), next1 = set1->begin(); 24 181578 : typename SetType::const_iterator it2 = set2->begin(); 25 181564 : bool modified = false; 26 349580 : while (it1 != set1->end() && it2 != set2->end()) { 27 168037 : if (*it1 < *it2) { 28 0 : ++next1; 29 0 : modified = true; 30 0 : del_fn(it1); 31 0 : it1 = next1; 32 168055 : } else if (*it1 > *it2) { 33 3 : modified = true; 34 3 : add_fn(it2); 35 3 : ++it2; 36 : } else { 37 168037 : ++it1; 38 168037 : ++it2; 39 : } 40 168016 : next1 = it1; 41 : } 42 296222 : for (next1 = it1; it1 != set1->end(); it1 = next1) { 43 114598 : ++next1; 44 114612 : modified = true; 45 114612 : del_fn(it1); 46 : } 47 296269 : for (; it2 != set2->end(); ++it2) { 48 114664 : modified = true; 49 114664 : add_fn(it2); 50 : } 51 181597 : return modified; 52 : } 53 : 54 : #endif // BASE_SET_UTIL_H