activemq-cpp-3.8.2
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 
23 #include <decaf/util/AbstractSet.h>
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 
31 namespace decaf {
32 namespace 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  }
83 
91  HashSet(int capacity) : AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>(capacity)) {
92  }
93 
103  HashSet(int capacity, float loadFactor) :
104  AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>(capacity, loadFactor)) {
105  }
106 
116  HashSet(const Collection<E>& collection) : AbstractSet<E>(), backingMap() {
117 
118  this->backingMap = new HashMap<E, Set<E>*, HASHCODE>(
119  (collection.size() < 6 ? 11 : collection.size() * 2));
120 
121  decaf::lang::Pointer<Iterator<E> > iter(collection.iterator());
122  while (iter->hasNext()) {
123  this->add(iter->next());
124  }
125  }
126 
127  virtual ~HashSet() {
128  try {
129  delete this->backingMap;
130  }
132  }
133 
134  protected:
135 
143  HashSet(HashMap<E, Set<E>*, HASHCODE>* backingMap) :
144  AbstractSet<E>(), backingMap(backingMap) {
145  }
146 
147  public:
148 
149  HashSet<E>& operator= (const Collection<E>& collection) {
150  this->clear();
151  this->addAll(collection);
152  return *this;
153  }
154 
155  public:
156 
168  virtual bool add(const E& value) {
169  return this->backingMap->put(value, this);
170  }
171 
178  virtual void clear() {
179  this->backingMap->clear();
180  }
181 
189 // virtual Object clone() {
190 // try {
191 // HashSet<E> clone = (HashSet<E>) super.clone();
192 // clone.backingMap = (HashMap<E, HashSet<E>>) backingMap.clone();
193 // return clone;
194 // } catch (CloneNotSupportedException e) {
195 // return null;
196 // }
197 // }
198 
207  virtual bool contains(const E& value) const {
208  return this->backingMap->containsKey(value);
209  }
210 
218  virtual bool isEmpty() const {
219  return this->backingMap->isEmpty();
220  }
221 
228  virtual Iterator<E>* iterator() {
229  return this->backingMap->keySet().iterator();
230  }
231 
232  virtual Iterator<E>* iterator() const {
233  return this->backingMap->keySet().iterator();
234  }
235 
248  virtual bool remove(const E& value) {
249  try {
250  this->backingMap->remove(value);
252  return false;
253  }
254 
255  return true;
256  }
257 
263  virtual int size() const {
264  return this->backingMap->size();
265  }
266 
267  virtual std::string toString() const {
268 
269  std::string result;
270 
271  result.append("decaf::util::HashSet [ size = ");
272  result.append(decaf::lang::Integer::toString(this->size()));
273  result.append(" ]");
274 
275  return result;
276  }
277 
278  };
279 
280 }}
281 
282 #endif /* _DECAF_UTIL_HASHSET_H_ */
HashSet< E > & operator=(const Collection< E > &collection)
Definition: HashSet.h:149
The root interface in the collection hierarchy.
Definition: Collection.h:68
A collection that contains no duplicate elements.
Definition: Set.h:45
This class implements the Set interface, backed by a hash table (actually a HashMap instance)...
Definition: HashSet.h:70
virtual void clear()
Removes all elements from this.
Definition: HashSet.h:178
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:985
HashSet(const Collection< E > &collection)
Constructs a new set containing the elements in the specified collection.
Definition: HashSet.h:116
#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
virtual bool contains(const E &value) const
Returns a new.
Definition: HashSet.h:207
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:999
virtual ~HashSet()
Definition: HashSet.h:127
virtual Iterator< E > * iterator()
Returns an Iterator on the elements of this.
Definition: HashSet.h:228
virtual int size() const =0
Returns the number of elements in this collection.
virtual void clear()
Removes all of the mappings from this map (optional operation).
Definition: HashMap.h:923
virtual bool containsKey(const K &key) const
Returns true if this map contains a mapping for the specified key.
Definition: HashMap.h:947
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
virtual bool isEmpty() const
Returns true if this.
Definition: HashSet.h:218
HashMap< E, Set< E > *, HASHCODE > * backingMap
Definition: HashSet.h:73
virtual Iterator< E > * iterator() const
Definition: HashSet.h:232
virtual bool add(const E &value)
Adds the specified element to this set if it is not already present.
Definition: HashSet.h:168
virtual std::string toString() const
Definition: HashSet.h:267
virtual decaf::util::Iterator< E > * iterator()=0
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:223
virtual int size() const
Definition: HashMap.h:943
HashSet(int capacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and defa...
Definition: HashSet.h:91
virtual int size() const
Returns the number of elements in this.
Definition: HashSet.h:263
HashSet(int capacity, float loadFactor)
Constructs a new instance of.
Definition: HashSet.h:103
virtual bool isEmpty() const
Definition: HashMap.h:939
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:143
Hash table based implementation of the Map interface.
Definition: HashMap.h:95
std::string toString() const
Definition: NoSuchElementException.h:31
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition: AbstractSet.h:46
Decaf&#39;s implementation of a Smart Pointer that is a template on a Type and is Thread Safe if the defa...
Definition: Pointer.h:53
virtual Set< K > & keySet()
Returns a Set view of the keys contained in this map.
Definition: HashMap.h:1025
HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load ...
Definition: HashSet.h:81
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements...
Definition: AprPool.h:25