LCOV - code coverage report
Current view: top level - libs/common - set.hpp (source / functions) Hit Total Coverage
Test: cleared_cor.info Lines: 7 7 100.0 %
Date: 2019-03-07 14:46:43 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /**
       2             :  * Copyright Soramitsu Co., Ltd. All Rights Reserved.
       3             :  * SPDX-License-Identifier: Apache-2.0
       4             :  */
       5             : 
       6             : #ifndef IROHA_SET_HPP
       7             : #define IROHA_SET_HPP
       8             : 
       9             : #include <unordered_set>
      10             : 
      11             : namespace iroha {
      12             :   /**
      13             :        * Merge collections with unique elements
      14             :        * @tparam Collection - type of collection
      15             :        * @tparam TargetType - type of elements in collection
      16             :        * @tparam Hasher - class for hashing TargetType objects
      17             :        * @param left - first collection
      18             :        * @param right - second collection
      19             :        * @return collection with type Collection, that contain unique union of elements
      20             :        */
      21             :   template<typename Hasher,
      22             :       typename Collection,
      23             :       typename TargetType = typename Collection::value_type>
      24             :   auto merge_unique(Collection left, Collection right) {
      25             :     std::unordered_set<TargetType, Hasher>
      26             :         unique_set(left.begin(), left.end());
      27             : 
      28             :     unique_set.insert(right.begin(), right.end());
      29             :     return Collection(unique_set.begin(), unique_set.end());
      30             :   }
      31             : 
      32             :   /**
      33             :    * Provide merge of sets based on mering same elements
      34             :    * @tparam Set - type of set
      35             :    * @tparam Merge - type of merge predicate
      36             :    * @param left - first set
      37             :    * @param right - second set
      38             :    * @param merge - merge predicate
      39             :    * @return new set, that contains union of elements,
      40             :    * where same elements merged inside
      41             :    */
      42             :   template<typename Set, typename Merge>
      43             :   Set set_union(const Set &left, const Set &right, Merge &&merge) {
      44             :     Set out;
      45             :     out.insert(left.begin(), left.end());
      46             :     for (auto &&tx : right) {
      47             :       auto iter = out.find(tx);
      48             :       if (iter != out.end()) {
      49             :         merge(*iter, tx);
      50             :       } else {
      51             :         out.insert(tx);
      52             :       }
      53             :     }
      54             :     return out;
      55             :   }
      56             : 
      57             :   /**
      58             :    * Provide difference operation on set
      59             :    * @tparam Set - type of set
      60             :    * @return difference of sets.
      61             :    */
      62             :   template<typename Set>
      63             :   Set set_difference(const Set &left, const Set &right) {
      64          27 :     Set out;
      65          48 :     for (auto &&element : left) {
      66          21 :       if (right.find(element) == right.end()) {
      67          19 :         out.insert(element);
      68          19 :       }
      69             :     }
      70          27 :     return out;
      71          27 :   }
      72             : } // namespace iroha
      73             : #endif //IROHA_SET_HPP

Generated by: LCOV version 1.13