LCOV - code coverage report
Current view: top level - root/contrail/src/contrail-common/base - set_util.h (source / functions) Hit Total Coverage
Test: OpenSDN C/C++ coverage (all TARGET_SET jobs) Lines: 21 25 84.0 %
Date: 2026-11-16 02:06:09 Functions: 3 6 50.0 %
Legend: Lines: hit not hit

          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       89952 : bool set_synchronize(const SetType *set1, const SetType *set2,
      23             :                      AddFunctor add_fn, DelFunctor del_fn) {
      24       89952 :     typename SetType::iterator it1 = set1->begin(), next1 = set1->begin();
      25       89890 :     typename SetType::const_iterator it2 = set2->begin();
      26       89879 :     bool modified = false;
      27      167650 :     while (it1 != set1->end() && it2 != set2->end()) {
      28       77864 :         if (*it1 < *it2) {
      29           0 :             ++next1;
      30           0 :             modified = true;
      31           0 :             del_fn(it1);
      32           0 :             it1 = next1;
      33       77848 :         } else if (*it1 > *it2) {
      34           3 :             modified = true;
      35           3 :             add_fn(it2);
      36           3 :             ++it2;
      37             :         } else {
      38       77819 :             ++it1;
      39       77809 :             ++it2;
      40             :         }
      41       77771 :         next1 = it1;
      42             :     }
      43      148956 :     for (next1 = it1; it1 != set1->end(); it1 = next1) {
      44       59065 :         ++next1;
      45       59057 :         modified = true;
      46       59057 :         del_fn(it1);
      47             :     }
      48      148997 :     for (; it2 != set2->end(); ++it2) {
      49       59114 :         modified = true;
      50       59114 :         add_fn(it2);
      51             :     }
      52       89891 :     return modified;
      53             : }
      54             : 
      55             : #endif  // BASE_SET_UTIL_H

Generated by: LCOV version 1.14