0.9.8.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BlobHashSet.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 Common_BlobHashSet_h
27 #define Common_BlobHashSet_h
28 
29 #include <Common/BlobHashTraits.h>
30 
31 #include <unordered_set>
32 
33 namespace Hypertable {
34 
43 template <class TraitsT = BlobHashTraits<> >
44 class BlobHashSet : public std::unordered_set<Blob, typename TraitsT::hasher,
45  typename TraitsT::key_equal> {
46 private:
47  typedef std::unordered_set<Blob, typename TraitsT::hasher,
48  typename TraitsT::key_equal> Base;
49 
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 
59 
65  BlobHashSet(size_t n_buckets) : Base(n_buckets) { }
66 
75  InsRet insert(const void *buf, size_t len) {
76  return insert_blob(len, Base::insert(value_type(buf, len)));
77  }
78 
85  InsRet insert(const String &s) {
86  return insert_blob(s.size(), Base::insert(value_type(s.data(), s.size())));
87  }
88 
95  InsRet insert(const Blob &blob) {
96  return insert_blob(blob.size, Base::insert(blob));
97  }
98 
105  iterator find(const Blob &blob) { return Base::find(blob); }
106 
112  iterator find(const String &s) {
113  return Base::find(Blob(s.data(), s.size()));
114  }
115 
120  key_allocator &key_alloc() { return m_alloc; }
121 
122 private:
124  key_allocator m_alloc;
125 
134  InsRet insert_blob(size_t len, InsRet rv) {
135  if (rv.second)
136  const_cast<const void *&>(rv.first->start) =
137  m_alloc.dup(rv.first->start, len);
138 
139  return rv;
140  }
141 };
142 
145 }
146 
147 #endif
std::string String
A String is simply a typedef to std::string.
Definition: String.h:44
std::pair< iterator, bool > InsRet
Definition: BlobHashSet.h:55
Base::value_type value_type
Definition: BlobHashSet.h:53
A Blob structure holds a data pointer and a size.
BlobHashSet()
Constructor creates an empty set.
Definition: BlobHashSet.h:58
InsRet insert(const Blob &blob)
Insert function for a Blob object.
Definition: BlobHashSet.h:95
TraitsT::key_allocator key_allocator
Definition: BlobHashSet.h:54
InsRet insert_blob(size_t len, InsRet rv)
Helper function to insert a blob; if the blob already exists then a duplicate blob is created and ins...
Definition: BlobHashSet.h:134
Base::key_type key_type
Definition: BlobHashSet.h:52
iterator find(const String &s)
Find function for String objects.
Definition: BlobHashSet.h:112
InsRet insert(const String &s)
Insert function for a String object.
Definition: BlobHashSet.h:85
BlobHashSet(size_t n_buckets)
Overloaded Constructor creates an empty set with a specified number of buckets.
Definition: BlobHashSet.h:65
std::unordered_set< Blob, typename TraitsT::hasher, typename TraitsT::key_equal > Base
Definition: BlobHashSet.h:48
Hypertable definitions
Helper structures for BlobHashSet.
InsRet insert(const void *buf, size_t len)
Inserts a new Blob into the set.
Definition: BlobHashSet.h:75
A HashSet for storing and looking up blobs efficiently.
Definition: BlobHashSet.h:44
Base::iterator iterator
Definition: BlobHashSet.h:51
key_allocator m_alloc
The key_allocator - i.e.
Definition: BlobHashSet.h:124
iterator find(const Blob &blob)
Looks up the blob and returns an iterator to it.
Definition: BlobHashSet.h:105
key_allocator & key_alloc()
Returns the allocator; used for testing.
Definition: BlobHashSet.h:120