activemq-cpp-3.9.5
HashSet.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18#ifndef _DECAF_UTIL_HASHSET_H_
19#define _DECAF_UTIL_HASHSET_H_
20
21#include <decaf/util/Config.h>
22
24#include <decaf/util/HashMap.h>
25#include <decaf/util/HashCode.h>
26#include <decaf/lang/Pointer.h>
27#include <decaf/lang/Integer.h>
30
31namespace decaf {
32namespace util {
33
69 template<typename E, typename HASHCODE = HashCode<E> >
70 class HashSet : public AbstractSet<E> {
71 protected:
72
74
75 public:
76
81 HashSet() : AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>()) {}
82
90 HashSet(int capacity) : AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>(capacity)) {}
91
101 HashSet(int capacity, float loadFactor) :
102 AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>(capacity, loadFactor)) {
103 }
104
114 HashSet(const Collection<E>& collection) : AbstractSet<E>(), backingMap() {
115
116 this->backingMap = new HashMap<E, Set<E>*, HASHCODE>(
117 (collection.size() < 6 ? 11 : collection.size() * 2));
118
119 decaf::lang::Pointer<Iterator<E> > iter(collection.iterator());
120 while (iter->hasNext()) {
121 this->add(iter->next());
122 }
123 }
124
134 HashSet(const HashSet<E>& collection) : AbstractCollection<E>(), AbstractSet<E>(), backingMap() {
135
136 this->backingMap = new HashMap<E, Set<E>*, HASHCODE>(
137 (collection.size() < 6 ? 11 : collection.size() * 2));
138
139 decaf::lang::Pointer<Iterator<E> > iter(collection.iterator());
140 while (iter->hasNext()) {
141 this->add(iter->next());
142 }
143 }
144
145 virtual ~HashSet() {
146 try {
147 delete this->backingMap;
148 }
150 }
151
152 protected:
153
161 HashSet(HashMap<E, Set<E>*, HASHCODE>* backingMap) :
163 }
164
165 public:
166
167 HashSet<E>& operator= (const Collection<E>& collection) {
168 this->clear();
169 this->addAll(collection);
170 return *this;
171 }
172
173 public:
174
186 virtual bool add(const E& value) {
187 return this->backingMap->put(value, this);
188 }
189
196 virtual void clear() {
197 this->backingMap->clear();
198 }
199
208 virtual bool contains(const E& value) const {
209 return this->backingMap->containsKey(value);
210 }
211
219 virtual bool isEmpty() const {
220 return this->backingMap->isEmpty();
221 }
222
230 return this->backingMap->keySet().iterator();
231 }
232
233 virtual Iterator<E>* iterator() const {
234 return this->backingMap->keySet().iterator();
235 }
236
249 virtual bool remove(const E& value) {
250 try {
251 this->backingMap->remove(value);
253 return false;
254 }
255
256 return true;
257 }
258
264 virtual int size() const {
265 return this->backingMap->size();
266 }
267
268 virtual std::string toString() const {
269
270 std::string result;
271
272 result.append("decaf::util::HashSet [ size = ");
273 result.append(decaf::lang::Integer::toString(this->size()));
274 result.append(" ]");
275
276 return result;
277 }
278
279 };
280
281}}
282
283#endif /* _DECAF_UTIL_HASHSET_H_ */
std::string toString() const
virtual decaf::util::Iterator< E > * iterator()=0
Decaf's implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition Pointer.h:53
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition AbstractCollection.h:58
virtual bool addAll(const Collection< E > &collection)
Adds all of the elements in the specified collection to this collection.The behavior of this operatio...
Definition AbstractCollection.h:237
AbstractSet()
Definition AbstractSet.h:50
The root interface in the collection hierarchy.
Definition Collection.h:69
virtual int size() const =0
Returns the number of elements in this collection.
Hash table based implementation of the Map interface.
Definition HashMap.h:95
virtual bool put(const K &key, const V &value)
Associates the specified value with the specified key in this map (optional operation).
Definition HashMap.h:995
virtual void clear()
Removes all of the mappings from this map (optional operation).
Definition HashMap.h:933
virtual bool isEmpty() const
Definition HashMap.h:949
virtual int size() const
Definition HashMap.h:953
virtual V remove(const K &key)
Removes the value (key/value pair) for the specified key from the map, returns a copy of the value th...
Definition HashMap.h:1009
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition HashMap.h:1035
virtual bool containsKey(const K &key) const
Returns true if this map contains a mapping for the specified key.
Definition HashMap.h:957
HashSet(const HashSet< E > &collection)
Constructs a new set containing the elements in the specified HashSet.
Definition HashSet.h:134
virtual Iterator< E > * iterator()
Returns an Iterator on the elements of this HashSet.
Definition HashSet.h:229
HashSet(HashMap< E, Set< E > *, HASHCODE > *backingMap)
Protected constructor for use by subclasses that wish to use an alternate type of backing Map.
Definition HashSet.h:161
virtual std::string toString() const
Definition HashSet.h:268
virtual bool remove(const E &value)
Removes the specified element from this set if it is present.
Definition HashSet.h:249
HashSet(int capacity, float loadFactor)
Constructs a new instance of HashSet with the specified capacity and load factor.
Definition HashSet.h:101
virtual int size() const
Returns the number of elements in this HashSet.
Definition HashSet.h:264
HashSet< E > & operator=(const Collection< E > &collection)
Definition HashSet.h:167
HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load ...
Definition HashSet.h:81
virtual void clear()
Removes all elements from this HashSet, leaving it empty.
Definition HashSet.h:196
HashSet(int capacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and defa...
Definition HashSet.h:90
HashMap< E, Set< E > *, HASHCODE > * backingMap
Definition HashSet.h:73
HashSet(const Collection< E > &collection)
Constructs a new set containing the elements in the specified collection.
Definition HashSet.h:114
virtual ~HashSet()
Definition HashSet.h:145
virtual bool contains(const E &value) const
Searches this HashSet for the specified object.
Definition HashSet.h:208
virtual bool isEmpty() const
Returns true if this HashSet has no elements, false otherwise.
Definition HashSet.h:219
virtual Iterator< E > * iterator() const
Definition HashSet.h:233
virtual bool add(const E &value)
Adds the specified element to this set if it is not already present.
Definition HashSet.h:186
Defines an object that can be used to iterate over the elements of a collection.
Definition Iterator.h:34
Definition NoSuchElementException.h:31
A collection that contains no duplicate elements.
Definition Set.h:45
#define DECAF_CATCHALL_NOTHROW()
A catch-all that does not throw an exception, one use would be to catch any exception in a destructor...
Definition ExceptionDefines.h:62
Definition AbstractCollection.h:33
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.
Definition AprPool.h:25