0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CstrHashMap.h
Go to the documentation of this file.
1 /* -*- c++ -*-
2  * Copyright (C) 2007-2015 Hypertable, Inc.
3  *
4  * This file is part of Hypertable.
5  *
6  * Hypertable is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 3
9  * of the License, or any later version.
10  *
11  * Hypertable is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Hypertable. If not, see <http://www.gnu.org/licenses/>
18  */
19 
26 #ifndef HYPERTABLE_CSTR_HASHMAP_H
27 #define HYPERTABLE_CSTR_HASHMAP_H
28 
29 #include <Common/CstrHashTraits.h>
30 
31 #include <unordered_map>
32 
33 namespace Hypertable {
34 
43 template <typename DataT, class TraitsT = CstrHashTraits<> >
44 class CstrHashMap : public std::unordered_map<const char *, DataT,
45  typename TraitsT::hasher,
46  typename TraitsT::key_equal> {
47 private:
48  typedef std::unordered_map<const char *, DataT, typename TraitsT::hasher,
49  typename TraitsT::key_equal> Base;
50 public:
51  typedef typename Base::iterator iterator;
52  typedef typename Base::key_type key_type;
53  typedef typename Base::value_type value_type;
54  typedef typename TraitsT::key_allocator key_allocator;
55  typedef std::pair<iterator, bool> InsRet;
56 
57 public:
60 
66  CstrHashMap(size_t n_buckets) : Base(n_buckets) {}
67 
76  InsRet insert(const char *key, const DataT &data) {
77  return insert_key(key, Base::insert(value_type(key, data)));
78  }
79 
84  key_allocator &key_alloc() { return m_alloc; }
85 
87  void clear() { Base::clear(); }
88 
89 private:
91  key_allocator m_alloc;
92 
101  InsRet insert_key(const char *key, InsRet rv) {
102  if (rv.second) {
103  char *keycopy = m_alloc.dup(key);
104  const_cast<key_type &>(rv.first->first) = keycopy;
105  }
106  return rv;
107  }
108 };
109 
112 } // namespace Hypertable
113 
114 #endif
void clear()
Clears the map and deletes all elements.
Definition: CstrHashMap.h:87
key_allocator & key_alloc()
Returns the key allocator; required for testing.
Definition: CstrHashMap.h:84
std::pair< iterator, bool > InsRet
Definition: CstrHashMap.h:55
CstrHashMap()
Default constructor creates an empty map.
Definition: CstrHashMap.h:59
A hash map for storing and lookup char * strings efficiently.
Definition: CstrHashMap.h:44
Base::key_type key_type
Definition: CstrHashMap.h:52
Base::value_type value_type
Definition: CstrHashMap.h:53
TraitsT::key_allocator key_allocator
Definition: CstrHashMap.h:54
Helper structures for CstrHashMap.
InsRet insert(const char *key, const DataT &data)
Inserts a new string/data pair in the map.
Definition: CstrHashMap.h:76
CstrHashMap(size_t n_buckets)
Overloaded Constructor creates an empty map with a specified number of buckets.
Definition: CstrHashMap.h:66
Hypertable definitions
std::unordered_map< const char *, DataT, typename TraitsT::hasher, typename TraitsT::key_equal > Base
Definition: CstrHashMap.h:49
InsRet insert_key(const char *key, InsRet rv)
Helper function to insert a string; if the string already exists then a duplicate of the string is cr...
Definition: CstrHashMap.h:101
Base::iterator iterator
Definition: CstrHashMap.h:51
key_allocator m_alloc
The key_allocator - i.e.
Definition: CstrHashMap.h:91