00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef _DECAF_UTIL_HASHSET_H_
00019 #define _DECAF_UTIL_HASHSET_H_
00020
00021 #include <decaf/util/Config.h>
00022
00023 #include <decaf/util/AbstractSet.h>
00024 #include <decaf/util/HashMap.h>
00025 #include <decaf/util/HashCode.h>
00026 #include <decaf/lang/Pointer.h>
00027 #include <decaf/lang/Integer.h>
00028 #include <decaf/util/ConcurrentModificationException.h>
00029 #include <decaf/lang/exceptions/UnsupportedOperationException.h>
00030
00031 namespace decaf {
00032 namespace util {
00033
00069 template<typename E, typename HASHCODE = HashCode<E> >
00070 class HashSet : public AbstractSet<E> {
00071 protected:
00072
00073 HashMap<E, Set<E>*, HASHCODE>* backingMap;
00074
00075 public:
00076
00081 HashSet() : AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>()) {
00082 }
00083
00091 HashSet(int capacity) : AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>(capacity)) {
00092 }
00093
00103 HashSet(int capacity, float loadFactor) :
00104 AbstractSet<E>(), backingMap(new HashMap<E, Set<E>*, HASHCODE>(capacity, loadFactor)) {
00105 }
00106
00116 HashSet(const Collection<E>& collection) : AbstractSet<E>(), backingMap() {
00117
00118 this->backingMap = new HashMap<E, Set<E>*, HASHCODE>(
00119 (collection.size() < 6 ? 11 : collection.size() * 2));
00120
00121 decaf::lang::Pointer<Iterator<E> > iter(collection.iterator());
00122 while (iter->hasNext()) {
00123 this->add(iter->next());
00124 }
00125 }
00126
00127 virtual ~HashSet() {
00128 try {
00129 delete this->backingMap;
00130 }
00131 DECAF_CATCHALL_NOTHROW()
00132 }
00133
00134 protected:
00135
00143 HashSet(HashMap<E, Set<E>*, HASHCODE>* backingMap) :
00144 AbstractSet<E>(), backingMap(backingMap) {
00145 }
00146
00147 public:
00148
00149 HashSet<E>& operator= (const Collection<E>& collection) {
00150 this->clear();
00151 this->addAll(collection);
00152 return *this;
00153 }
00154
00155 public:
00156
00168 virtual bool add(const E& value) {
00169 return this->backingMap->put(value, this);
00170 }
00171
00178 virtual void clear() {
00179 this->backingMap->clear();
00180 }
00181
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00207 virtual bool contains(const E& value) const {
00208 return this->backingMap->containsKey(value);
00209 }
00210
00218 virtual bool isEmpty() const {
00219 return this->backingMap->isEmpty();
00220 }
00221
00228 virtual Iterator<E>* iterator() {
00229 return this->backingMap->keySet().iterator();
00230 }
00231
00232 virtual Iterator<E>* iterator() const {
00233 return this->backingMap->keySet().iterator();
00234 }
00235
00248 virtual bool remove(const E& value) {
00249 try {
00250 this->backingMap->remove(value);
00251 } catch(decaf::util::NoSuchElementException& ex) {
00252 return false;
00253 }
00254
00255 return true;
00256 }
00257
00263 virtual int size() const {
00264 return this->backingMap->size();
00265 }
00266
00267 virtual std::string toString() const {
00268
00269 std::string result;
00270
00271 result.append("decaf::util::HashSet [ size = ");
00272 result.append(decaf::lang::Integer::toString(this->size()));
00273 result.append(" ]");
00274
00275 return result;
00276 }
00277
00278 };
00279
00280 }}
00281
00282 #endif