Fork Vasum on GitHub Official Vasum Wiki on Tizen.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
counting-map.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Contact: Piotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License
17  */
18 
25 #ifndef COMMON_UTILS_COUNTING_MAP_HPP
26 #define COMMON_UTILS_COUNTING_MAP_HPP
27 
28 #include <unordered_map>
29 
30 namespace utils {
31 
32 
37 template<class Key>
38 class CountingMap {
39 public:
40  size_t increment(const Key& key)
41  {
42  auto res = mMap.insert(typename Map::value_type(key, 1));
43  if (!res.second) {
44  ++res.first->second;
45  }
46  return res.first->second;
47  }
48 
49  size_t decrement(const Key& key)
50  {
51  auto it = mMap.find(key);
52  if (it == mMap.end()) {
53  return 0;
54  }
55  if (--it->second == 0) {
56  mMap.erase(it);
57  return 0;
58  }
59  return it->second;
60  }
61 
62  void clear()
63  {
64  mMap.clear();
65  }
66 
67  size_t get(const Key& key) const
68  {
69  auto it = mMap.find(key);
70  return it == mMap.end() ? 0 : it->second;
71  }
72 
73  bool empty() const
74  {
75  return mMap.empty();
76  }
77 private:
78  typedef std::unordered_map<Key, size_t> Map;
80 };
81 
82 
83 } // namespace utils
84 
85 
86 #endif // COMMON_UTILS_COUNTING_MAP_HPP
size_t increment(const Key &key)
Definition: counting-map.hpp:40
bool empty() const
Definition: counting-map.hpp:73
Structure used to count elements.
Definition: counting-map.hpp:38
std::string key(const Arg1 &a1, const Args &...args)
Concatenates all parameters into one std::string.
Definition: kvstore-visitor-utils.hpp:60
void clear()
Definition: counting-map.hpp:62
Map mMap
Definition: counting-map.hpp:79
std::unordered_map< Key, size_t > Map
Definition: counting-map.hpp:78
size_t decrement(const Key &key)
Definition: counting-map.hpp:49