Line data Source code
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 2089297 : bool set_synchronize(const SetType *set1, const SetType *set2, 23 : AddFunctor add_fn, DelFunctor del_fn) { 24 2089297 : typename SetType::iterator it1 = set1->begin(), next1 = set1->begin(); 25 2089093 : typename SetType::const_iterator it2 = set2->begin(); 26 2089026 : bool modified = false; 27 5850843 : while (it1 != set1->end() && it2 != set2->end()) { 28 3761823 : if (*it1 < *it2) { 29 15850 : ++next1; 30 15850 : modified = true; 31 15850 : del_fn(it1); 32 15852 : it1 = next1; 33 3746140 : } else if (*it1 > *it2) { 34 4409 : modified = true; 35 4409 : add_fn(it2); 36 4410 : ++it2; 37 : } else { 38 3741549 : ++it1; 39 3741679 : ++it2; 40 : } 41 3761817 : next1 = it1; 42 : } 43 3095429 : for (next1 = it1; it1 != set1->end(); it1 = next1) { 44 1005959 : ++next1; 45 1006063 : modified = true; 46 1006063 : del_fn(it1); 47 : } 48 3109006 : for (; it2 != set2->end(); ++it2) { 49 1019667 : modified = true; 50 1019667 : add_fn(it2); 51 : } 52 2089312 : return modified; 53 : } 54 : 55 : #endif // BASE_SET_UTIL_H