VMware GemFire Native C++ Reference  9.2.4
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 
167 typedef HashMapT<CacheableKeyPtr, CacheablePtr> _HashMapOfCacheable;
168 typedef HashMapT<CacheableKeyPtr, ExceptionPtr> _HashMapOfException;
169 
175  public SharedBase {
176  public:
178  typedef _HashMapOfCacheable::Iterator Iterator;
179 
182 
184  inline HashMapOfCacheable(int32_t n) : _HashMapOfCacheable(n) {}
185 
188  : _HashMapOfCacheable(other) {}
189 
190  private:
191  const HashMapOfCacheable& operator=(const HashMapOfCacheable&);
192 };
193 
199  public SharedBase {
200  public:
202  typedef _HashMapOfException::Iterator Iterator;
203 
206 
208  inline HashMapOfException(int32_t n) : _HashMapOfException(n) {}
209 
212  : _HashMapOfException(other) {}
213 
214  private:
215  const HashMapOfException& operator=(const HashMapOfException&);
216 };
217 
218 typedef SharedPtr<HashMapOfCacheable> HashMapOfCacheablePtr;
219 typedef SharedPtr<HashMapOfException> HashMapOfExceptionPtr;
220 } // namespace client
221 } // namespace geode
222 } // namespace apache
223 
224 #endif // GEODE_HASHMAPT_H_
apache::geode::client::HashMapT::HashMapT
HashMapT()
Creates an empty hash map with hash function hasher<TKEY> and equal to function equal_to<TKEY>.
Definition: HashMapT.hpp:153
apache::geode::client::HashMapT::insert
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
CacheableKey.hpp
apache::geode::client::HashMapT::operator[]
TVAL operator[](const TKEY &k)
Returns a copy of the object that is associated with a particular key.
Definition: HashMapT.hpp:136
apache::geode::client::HashMapOfSharedBase
Represents a HashMap of SharedBase
Definition: HashMapOfSharedBase.hpp:48
apache::geode::client::HashMapOfSharedBase::resize
void resize(int32_t n)
Increases the bucket count to at least n.
apache::geode::client::HashMapOfSharedBase::bucket_count
int32_t bucket_count() const
Returns the number of buckets used by the hash_map.
apache
Each enum represents a predefined RegionAttributes in a {}.
Definition: Assert.hpp:31
apache::geode::client::HashMapOfSharedBase::size
int32_t size() const
Returns the size of the hash_map.
apache::geode::client::HashMapOfException::HashMapOfException
HashMapOfException()
Creates an empty hash map.
Definition: HashMapT.hpp:205
apache::geode::client::HashMapT::resize
void resize(int32_t n)
Increases the bucket count to at least n.
Definition: HashMapT.hpp:103
apache::geode::client::HashMapOfSharedBase::erase
int32_t erase(const SharedBasePtr &k)
Erases the element whose key is k.
apache::geode::client::HashMapOfSharedBase::find
Iterator find(const SharedBasePtr &k) const
Finds an element whose key is k.
apache::geode::client::HashMapOfException::HashMapOfException
HashMapOfException(int32_t n)
Creates an empty hash map with at least n buckets.
Definition: HashMapT.hpp:208
Cacheable.hpp
apache::geode::client::HashMapT::end
Iterator end() const
Get an iterator pointing to the end of hash_map.
Definition: HashMapT.hpp:142
apache::geode::client::HashMapT::empty
bool empty() const
true if the hash map's size is 0.
Definition: HashMapT.hpp:95
apache::geode::client::HashMapOfCacheable::HashMapOfCacheable
HashMapOfCacheable()
Creates an empty hash map.
Definition: HashMapT.hpp:181
apache::geode::client::HashMapOfSharedBase::contains
bool contains(const SharedBasePtr &k) const
Check if a given key k exists in the hash_map.
apache::geode::client::HashMapOfCacheable
A map of CacheableKey objects to Cacheable that also extends SharedBase for smart pointers.
Definition: HashMapT.hpp:174
apache::geode::client::HashMapOfSharedBase::max_size
int32_t max_size() const
Returns the largest possible size of the hash_map.
apache::geode::client::HashMapOfSharedBase::count
int32_t count(const SharedBasePtr &k) const
Counts the number of elements whose key is k.
apache::geode::client::HashMapOfSharedBase::end
Iterator end() const
Get an iterator pointing to the end of hash_map.
apache::geode::client::HashMapT::swap
void swap(HashMapT &other)
Swaps the contents of two hash maps.
Definition: HashMapT.hpp:106
apache::geode::client::HashMapT::begin
Iterator begin() const
Get an iterator pointing to the start of hash_map.
Definition: HashMapT.hpp:139
apache::geode::client::HashMapOfSharedBase::swap
void swap(HashMapOfSharedBase &other)
Swaps the contents of two hash_maps.
apache::geode::client::HashMapT
HashMap of TKEY to TVAL.
Definition: HashMapT.hpp:38
apache::geode::client::HashMapT::HashMapT
HashMapT(const HashMapT &other)
Copy constructor.
Definition: HashMapT.hpp:161
apache::geode::client::HashMapOfSharedBase::clear
void clear()
Erases all of the elements.
apache::geode::client::HashMapOfSharedBase::begin
Iterator begin() const
Get an iterator pointing to the start of hash_map.
apache::geode::client::HashMapT::size
int32_t size() const
Returns the size of the hash map.
Definition: HashMapT.hpp:87
apache::geode::client::HashMapOfSharedBase::insert
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.
apache::geode::client::HashMapT::operator=
HashMapT & operator=(const HashMapT &other)
Assignment operator.
Definition: HashMapT.hpp:145
apache::geode::client::HashMapOfCacheable::Iterator
_HashMapOfCacheable::Iterator Iterator
Iterator class for the hash map.
Definition: HashMapT.hpp:178
apache::geode::client::HashMapT::erase
int32_t erase(const TKEY &k)
Erases the element whose key is k.
Definition: HashMapT.hpp:119
apache::geode::client::HashMapT::~HashMapT
~HashMapT()
Destructor: the destructor of m_map would do required stuff.
Definition: HashMapT.hpp:164
apache::geode::client::HashMapOfSharedBase::empty
bool empty() const
true if the hash_map's size is 0.
apache::geode::client::HashMapOfSharedBase::Iterator
Interface of an iterator for HashMapOfSharedBase.
Definition: HashMapOfSharedBase.hpp:57
geode
Exception.hpp
apache::geode::client::HashMapT::max_size
int32_t max_size() const
Returns the largest possible size of the hash map.
Definition: HashMapT.hpp:90
CPPCACHE_EXPORT
#define CPPCACHE_EXPORT
Defines a Geode CPPCACHE export.
Definition: geode_base.hpp:58
HashMapOfSharedBase.hpp
apache::geode::client::HashMapT::update
void update(const TKEY &k, const TVAL &v)
Updates a value whose key must exist.
Definition: HashMapT.hpp:116
apache::geode::client::HashMapT::contains
bool contains(const TKEY &k) const
Check if a given key k exists in the hash map.
Definition: HashMapT.hpp:125
apache::geode::client::HashMapT::find
Iterator find(const TKEY &k) const
Finds an element whose key is k.
Definition: HashMapT.hpp:128
apache::geode::client::HashMapT::clear
void clear()
Erases all of the elements.
Definition: HashMapT.hpp:122
apache::geode::client::HashMapOfCacheable::HashMapOfCacheable
HashMapOfCacheable(int32_t n)
Creates an empty hash map with at least n buckets.
Definition: HashMapT.hpp:184
apache::geode::client::HashMapT::count
int32_t count(const SharedBasePtr &k) const
Counts the number of elements whose key is k.
Definition: HashMapT.hpp:131
apache::geode::client::SharedBase
Definition: SharedBase.hpp:40
apache::geode::client::HashMapT::bucket_count
int32_t bucket_count() const
Returns the number of buckets used by the hash map.
Definition: HashMapT.hpp:98
apache::geode::client::HashMapOfCacheable::HashMapOfCacheable
HashMapOfCacheable(const HashMapOfCacheable &other)
Copy constructor.
Definition: HashMapT.hpp:187
apache::geode::client::HashMapT::HashMapT
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
apache::geode::client::SharedPtr
Defines a reference counted shared pointer.
Definition: SharedPtr.hpp:52
apache::geode::client::HashMapOfException
A map of CacheableKey objects to Exception that also extends SharedBase for smart pointers.
Definition: HashMapT.hpp:198
apache::geode::client::HashMapOfException::Iterator
_HashMapOfException::Iterator Iterator
Iterator class for the hash map.
Definition: HashMapT.hpp:202
apache::geode::client::HashMapOfException::HashMapOfException
HashMapOfException(const HashMapOfException &other)
Copy constructor.
Definition: HashMapT.hpp:211

Pivotal GemFire C++ Cache API Documentation