LCOV - code coverage report
Current view: top level - libs/cache - collection_set.hpp (source / functions) Hit Total Coverage
Test: coverage_cleared.info Lines: 10 10 100.0 %
Date: 2018-12-05 17:11:35 Functions: 12 12 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_COLLECTION_SET_HPP
       7             : #define IROHA_COLLECTION_SET_HPP
       8             : 
       9             : #include <shared_mutex>
      10             : #include <unordered_set>
      11             : 
      12             : namespace iroha {
      13             :   namespace set {
      14             : 
      15             :     /**
      16             :      * Class provides concurrent implementation of collection which operates
      17             :      * with collections on insert and remove
      18             :      *
      19             :      * @tparam Key - type of holding values
      20             :      * @tparam Hash - hash representation of Key values
      21             :      * @tparam KeyEqual - equality type
      22             :      */
      23             :     template <typename Key,
      24             :               typename Hash = std::hash<Key>,
      25             :               typename KeyEqual = std::equal_to<Key>>
      26             :     class CollectionSet {
      27             :      public:
      28             :       CollectionSet() = default;
      29             : 
      30             :       using value_type = Key;
      31             : 
      32             :       /**
      33             :        * Merge our and passed collection
      34             :        * @tparam Collection - type of passed collection
      35             :        * @param collection - elements to insert
      36             :        */
      37             :       template <typename Collection>
      38             :       void insertValues(Collection &&collection) {
      39           4 :         std::lock_guard<std::shared_timed_mutex> lock(mutex_);
      40           4 :         set_.insert(collection.begin(), collection.end());
      41           4 :       }
      42             : 
      43             :       /**
      44             :        * Remove all elements which are occured in the passed collection
      45             :        * @tparam Collection - type of passed collection
      46             :        * @param collection - elements to remove
      47             :        */
      48             :       template <typename Collection>
      49             :       void removeValues(Collection &&collection) {
      50           1 :         std::lock_guard<std::shared_timed_mutex> lock(mutex_);
      51           4 :         for (auto &&val : collection) {
      52           3 :           set_.erase(val);
      53             :         }
      54           1 :       }
      55             : 
      56             :       /**
      57             :        * Blocking walk through the collection
      58             :        * @tparam Callable - type of functor
      59             :        * @param callable - functor for invocation on each element
      60             :        */
      61             :       template <typename Callable>
      62             :       void forEach(Callable &&callable) const {
      63           1 :         std::shared_lock<std::shared_timed_mutex> lock(mutex_);
      64           1 :         std::for_each(set_.begin(), set_.end(), callable);
      65           1 :       }
      66             : 
      67             :      private:
      68             :       std::unordered_set<Key, Hash, KeyEqual> set_;
      69             :       mutable std::shared_timed_mutex mutex_;
      70             :     };
      71             :   }  // namespace set
      72             : }  // namespace iroha
      73             : 
      74             : #endif  // IROHA_COLLECTION_SET_HPP

Generated by: LCOV version 1.13