VMware GemFire Native C++ Reference  9.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
HashMapT.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #ifndef GEODE_HASHMAPT_H_
4 #define GEODE_HASHMAPT_H_
5 
6 /*
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements. See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License. You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22 
23 #include "geode_globals.hpp"
24 #include "HashMapOfSharedBase.hpp"
25 #include "Cacheable.hpp"
26 #include "CacheableKey.hpp"
27 #include "Exception.hpp"
28 
32 namespace apache {
33 namespace geode {
34 namespace client {
35 
37 template <typename TKEY, typename TVAL>
38 class HashMapT {
39  private:
40  HashMapOfSharedBase m_map;
41 
42  public:
43  class Iterator {
44  private:
46 
47  inline Iterator(const HashMapOfSharedBase::Iterator& iter) : m_iter(iter) {}
48 
49  // Never defined.
50  Iterator();
51 
52  public:
53  inline const TKEY first() const { return staticCast<TKEY>(m_iter.first()); }
54 
55  inline const TVAL second() const {
56  return staticCast<TVAL>(m_iter.second());
57  }
58 
59  inline Iterator& operator++() {
60  ++m_iter;
61  return *this;
62  }
63 
64  inline void operator++(int) { m_iter++; }
65 
66  inline bool operator==(const Iterator& other) const {
67  return (m_iter == other.m_iter);
68  }
69 
70  inline bool operator!=(const Iterator& other) const {
71  return (m_iter != other.m_iter);
72  }
73 
74  friend class HashMapT;
75  };
76 
77  static int32_t hasher(const SharedBasePtr& p) {
78  return apache::geode::client::hashFunction<TKEY>(staticCast<TKEY>(p));
79  }
80 
81  static bool equal_to(const SharedBasePtr& x, const SharedBasePtr& y) {
82  return apache::geode::client::equalToFunction<TKEY>(staticCast<TKEY>(x),
83  staticCast<TKEY>(y));
84  }
85 
87  inline int32_t size() const { return static_cast<int32_t>(m_map.size()); }
88 
90  inline int32_t max_size() const {
91  return static_cast<int32_t>(m_map.max_size());
92  }
93 
95  inline bool empty() const { return m_map.empty(); }
96 
98  inline int32_t bucket_count() const {
99  return static_cast<int32_t>(m_map.bucket_count());
100  }
101 
103  inline void resize(int32_t n) { m_map.resize(n); }
104 
106  inline void swap(HashMapT& other) { m_map.swap(other.m_map); }
107 
111  inline bool insert(const TKEY& k, const TVAL& v) {
112  return m_map.insert(k, v);
113  }
114 
116  inline void update(const TKEY& k, const TVAL& v) { m_map[k] = v; }
117 
119  inline int32_t erase(const TKEY& k) { return m_map.erase(k); }
120 
122  inline void clear() { m_map.clear(); }
123 
125  inline bool contains(const TKEY& k) const { return m_map.contains(k); }
126 
128  inline Iterator find(const TKEY& k) const { return Iterator(m_map.find(k)); }
129 
131  int32_t count(const SharedBasePtr& k) const { return m_map.count(k); }
132 
136  inline TVAL operator[](const TKEY& k) { return staticCast<TVAL>(m_map[k]); }
137 
139  inline Iterator begin() const { return Iterator(m_map.begin()); }
140 
142  inline Iterator end() const { return Iterator(m_map.end()); }
143 
145  inline HashMapT& operator=(const HashMapT& other) {
146  m_map = other.m_map;
147  return *this;
148  }
149 
153  inline HashMapT() : m_map(hasher, equal_to) {}
154 
158  inline HashMapT(int32_t n) : m_map(n, hasher, equal_to) {}
159 
161  inline HashMapT(const HashMapT& other) : m_map(other.m_map) {}
162 
164  inline ~HashMapT() {}
165 };
166 
169 
174 class CPPCACHE_EXPORT HashMapOfCacheable : public _HashMapOfCacheable,
175  public SharedBase {
176  public:
178  typedef _HashMapOfCacheable::Iterator Iterator;
179 
181  inline HashMapOfCacheable() : _HashMapOfCacheable() {}
182 
184  inline HashMapOfCacheable(int32_t n) : _HashMapOfCacheable(n) {}
185 
188  : _HashMapOfCacheable(other) {}
189 
190  private:
192 };
193 
198 class CPPCACHE_EXPORT HashMapOfException : public _HashMapOfException,
199  public SharedBase {
200  public:
202  typedef _HashMapOfException::Iterator Iterator;
203 
205  inline HashMapOfException() : _HashMapOfException() {}
206 
208  inline HashMapOfException(int32_t n) : _HashMapOfException(n) {}
209 
212  : _HashMapOfException(other) {}
213 
214  private:
216 };
217 
220 } // namespace client
221 } // namespace geode
222 } // namespace apache
223 
224 #endif // GEODE_HASHMAPT_H_
Iterator find(const SharedBasePtr &k) const
Finds an element whose key is k.
HashMapOfCacheable()
Creates an empty hash map.
Definition: HashMapT.hpp:181
TVAL operator[](const TKEY &k)
Returns a copy of the object that is associated with a particular key.
Definition: HashMapT.hpp:136
HashMapOfException(int32_t n)
Creates an empty hash map with at least n buckets.
Definition: HashMapT.hpp:208
void update(const TKEY &k, const TVAL &v)
Updates a value whose key must exist.
Definition: HashMapT.hpp:116
Each enum represents a predefined RegionAttributes in a Cache.
Definition: Assert.hpp:31
HashMapOfException()
Creates an empty hash map.
Definition: HashMapT.hpp:205
Iterator end() const
Get an iterator pointing to the end of hash_map.
int32_t bucket_count() const
Returns the number of buckets used by the hash_map.
HashMapT(int32_t n)
Creates an empty hash map with at least n buckets and hash function hasher<TKEY> and equal to functio...
Definition: HashMapT.hpp:158
bool insert(const TKEY &k, const TVAL &v)
Inserts the <k, v> pair into the hash map, when k does not exist in the hash map. ...
Definition: HashMapT.hpp:111
HashMapOfCacheable(int32_t n)
Creates an empty hash map with at least n buckets.
Definition: HashMapT.hpp:184
int32_t size() const
Returns the size of the hash map.
Definition: HashMapT.hpp:87
#define CPPCACHE_EXPORT
Defines a Geode CPPCACHE export.
Definition: geode_base.hpp:58
HashMapT(const HashMapT &other)
Copy constructor.
Definition: HashMapT.hpp:161
HashMapT()
Creates an empty hash map with hash function hasher<TKEY> and equal to function equal_to<TKEY>.
Definition: HashMapT.hpp:153
int32_t max_size() const
Returns the largest possible size of the hash_map.
Iterator begin() const
Get an iterator pointing to the start of hash_map.
Iterator begin() const
Get an iterator pointing to the start of hash_map.
Definition: HashMapT.hpp:139
HashMapT & operator=(const HashMapT &other)
Assignment operator.
Definition: HashMapT.hpp:145
Iterator end() const
Get an iterator pointing to the end of hash_map.
Definition: HashMapT.hpp:142
HashMap of TKEY to TVAL.
Definition: HashMapT.hpp:38
void resize(int32_t n)
Increases the bucket count to at least n.
int32_t erase(const TKEY &k)
Erases the element whose key is k.
Definition: HashMapT.hpp:119
HashMapOfCacheable(const HashMapOfCacheable &other)
Copy constructor.
Definition: HashMapT.hpp:187
void clear()
Erases all of the elements.
Definition: HashMapT.hpp:122
Defines a reference counted shared pointer.
Definition: SharedPtr.hpp:52
Interface of an iterator for HashMapOfSharedBase.
Definition: HashMapOfSharedBase.hpp:57
bool empty() const
true if the hash map&#39;s size is 0.
Definition: HashMapT.hpp:95
_HashMapOfException::Iterator Iterator
Iterator class for the hash map.
Definition: HashMapT.hpp:202
void swap(HashMapOfSharedBase &other)
Swaps the contents of two hash_maps.
void resize(int32_t n)
Increases the bucket count to at least n.
Definition: HashMapT.hpp:103
This abstract base class is the base class of all user objects that have the shared capability of ref...
Definition: SharedBase.hpp:40
int32_t count(const SharedBasePtr &k) const
Counts the number of elements whose key is k.
bool contains(const TKEY &k) const
Check if a given key k exists in the hash map.
Definition: HashMapT.hpp:125
bool insert(const SharedBasePtr &k, const SharedBasePtr &v)
Inserts the <k, v> pair into the hash_map, when k does not exist in the hash_map. ...
Represents a HashMap of SharedBase
Definition: HashMapOfSharedBase.hpp:48
bool contains(const SharedBasePtr &k) const
Check if a given key k exists in the hash_map.
int32_t count(const SharedBasePtr &k) const
Counts the number of elements whose key is k.
Definition: HashMapT.hpp:131
A map of CacheableKey objects to Cacheable that also extends SharedBase for smart pointers...
Definition: HashMapT.hpp:174
int32_t max_size() const
Returns the largest possible size of the hash map.
Definition: HashMapT.hpp:90
HashMapOfException(const HashMapOfException &other)
Copy constructor.
Definition: HashMapT.hpp:211
int32_t bucket_count() const
Returns the number of buckets used by the hash map.
Definition: HashMapT.hpp:98
int32_t erase(const SharedBasePtr &k)
Erases the element whose key is k.
bool empty() const
true if the hash_map&#39;s size is 0.
int32_t size() const
Returns the size of the hash_map.
_HashMapOfCacheable::Iterator Iterator
Iterator class for the hash map.
Definition: HashMapT.hpp:178
Iterator find(const TKEY &k) const
Finds an element whose key is k.
Definition: HashMapT.hpp:128
void swap(HashMapT &other)
Swaps the contents of two hash maps.
Definition: HashMapT.hpp:106
~HashMapT()
Destructor: the destructor of m_map would do required stuff.
Definition: HashMapT.hpp:164
A map of CacheableKey objects to Exception that also extends SharedBase for smart pointers...
Definition: HashMapT.hpp:198
This namespace contains all the Geode C++ API classes, enumerations and globals.
void clear()
Erases all of the elements.

Pivotal GemFire C++ Cache API Documentation