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 
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 
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 
229  virtual Iterator<E>* iterator() {
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_ */
HashSet< E > & operator=(const Collection< E > &collection)
Definition: HashSet.h:167
HashSet(const HashSet< E > &collection)
Constructs a new set containing the elements in the specified HashSet.
Definition: HashSet.h:134
The root interface in the collection hierarchy.
Definition: Collection.h:68
This class provides a skeletal implementation of the Collection interface, to minimize the effort req...
Definition: AbstractCollection.h:58
A collection that contains no duplicate elements.
Definition: Set.h:45
virtual std::string toString() const
Definition: HashSet.h:268
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:196
HashSet(const Collection< E > &collection)
Constructs a new set containing the elements in the specified collection.
Definition: HashSet.h:114
#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 ~HashSet()
Definition: HashSet.h:145
virtual Iterator< E > * iterator()
Returns an Iterator on the elements of this.
Definition: HashSet.h:229
virtual int size() const =0
Returns the number of elements in this collection.
Defines an object that can be used to iterate over the elements of a collection.
Definition: Iterator.h:34
virtual int size() const
Returns the number of elements in this.
Definition: HashSet.h:264
virtual Iterator< E > * iterator() const
Definition: HashSet.h:233
HashMap< E, Set< E > *, HASHCODE > * backingMap
Definition: HashSet.h:73
virtual bool add(const E &value)
Adds the specified element to this set if it is not already present.
Definition: HashSet.h:186
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:237
HashSet(int capacity)
Constructs a new, empty set; the backing HashMap instance has the specified initial capacity and defa...
Definition: HashSet.h:90
HashSet(int capacity, float loadFactor)
Constructs a new instance of.
Definition: HashSet.h:101
std::string toString() const
virtual bool isEmpty() const
Returns true if this.
Definition: HashSet.h:219
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
Hash table based implementation of the Map interface.
Definition: HashMap.h:95
Definition: NoSuchElementException.h:31
This class provides a skeletal implementation of the Set interface to minimize the effort required to...
Definition: AbstractSet.h:46
virtual bool contains(const E &value) const
Searches this.
Definition: HashSet.h:208
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
HashSet()
Constructs a new, empty set; the backing HashMap instance has default initial capacity (16) and load ...
Definition: HashSet.h:81