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: 8 25 32.0 %
Date: 2026-09-28 02:13:17 Functions: 2 6 33.3 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.14