5 #ifndef BALL_DATATYPE_HASHSET_H
6 #define BALL_DATATYPE_HASHSET_H
12 #ifndef BALL_COMMON_HASH_H
16 #ifndef BALL_CONCEPT_FORWARDITERATOR_H
20 #ifndef BALL_CONCEPT_VISITOR_H
24 #ifndef BALL_DATATYPE_FOREACH_H
28 #ifndef BALL_CONCEPT_PREDICATE_H
32 #ifndef BALL_CONCEPT_PROCESSOR_H
332 virtual void clear();
360 void get(
HashSet& hash_set)
const;
490 bool has(
const Key& key)
const;
517 virtual void dump(std::ostream& s = std::cout,
Size depth = 0)
const;
577 void deleteBuckets_();
579 Position hashBucket_(
const Key& key)
const;
595 vector<Node*> bucket_;
602 capacity_(initial_capacity),
603 bucket_(number_of_buckets)
613 : size_(hash_set.size_),
614 capacity_(hash_set.capacity_),
615 bucket_(hash_set.bucket_.size())
623 for (item = hash_set.bucket_[bucket]; item != 0; item = item->
next)
633 if (&hash_set ==
this)
642 size_ = hash_set.size_;
643 capacity_ = hash_set.capacity_;
644 bucket_.resize(hash_set.bucket_.size());
652 for (item = hash_set.bucket_[bucket]; item != 0; item = item->
next)
654 bucket_[bucket] = newNode_(item->
value, bucket_[bucket]);
667 for (node = bucket_[bucket]; node != 0; node = next_node)
669 next_node = node->
next;
706 std::swap(size_, hash_set.size_);
707 std::swap(capacity_, hash_set.capacity_);
708 std::swap(bucket_, hash_set.bucket_);
717 std::list<Key> erase_list;
718 for (
Iterator it = begin(); it != end(); ++it)
722 erase_list.push_back(*it);
727 typename list<Key>::iterator list_it = erase_list.begin();
728 for (; list_it != erase_list.end(); ++list_it)
754 return operator |= (rhs);
803 if (
this == &hash_set)
811 for (; it != hash_set.
end(); ++it)
836 return operator | (rhs);
843 return (
Size)bucket_.size();
872 Node* node_ptr = bucket_[bucket];
873 while (node_ptr != 0)
875 if (node_ptr->
value == key)
881 node_ptr = node_ptr->
next;
891 return (const_cast<HashSet*>(
this))->find(key);
901 if (needRehashing_() ==
true)
906 Position bucket = hashBucket_(item);
908 Node* next = bucket_[bucket];
909 bucket_[bucket] = newNode_(item, next);
912 it.
getTraits().position_ = bucket_[bucket];
916 return std::pair<Iterator, bool>(it,
true);
923 return insert(item).first;
931 Node* node_ptr = bucket_[bucket];
933 while (node_ptr != 0 && node_ptr->
value != key)
936 node_ptr = node_ptr->
next;
944 if (node_ptr == bucket_[bucket])
946 bucket_[bucket] = node_ptr->
next;
953 deleteNode_(node_ptr);
967 if ((pos == end()) || (size_ == 0))
981 for (; (prev != 0) && (prev->
next != pos.
getTraits().position_); prev = prev->
next) {};
1014 last_bucket = (
Position)(bucket_.size() - 1);
1017 if (f.
getTraits().bucket_ > last_bucket)
1024 Size no_deletions = 0;
1027 for (; bucket <= last_bucket; bucket++)
1029 if (bucket_[bucket] == 0)
1036 if ((bucket == f.
getTraits().bucket_) && (bucket_[bucket] != f.
getTraits().position_))
1039 Node* n = bucket_[bucket];
1043 if (bucket == last_bucket)
1049 for (n = next; (n != 0) && (n != l.
getTraits().position_); n = next)
1067 for (n = next; n != 0; n = next)
1077 else if (bucket < last_bucket)
1081 for (
Node* n = bucket_[bucket]; n != 0; n = next)
1087 bucket_[bucket] = 0;
1089 else if (bucket == last_bucket)
1094 Node* n = bucket_[bucket];
1096 for (; (n != 0) && (n != l.
getTraits().position_); n = next)
1103 bucket_[bucket] = l.
getTraits().position_;
1108 size_ -= no_deletions;
1111 template <
class Key>
1115 visitor.visit(*
this);
1118 template <
class Key>
1122 return (find(key) != end());
1125 template <
class Key>
1129 return (size_ == 0);
1132 template <
class Key>
1135 if (size_ != hash_set.size_)
1142 while (it1 != end())
1154 template <
class Key>
1158 return !(*
this == hash_set);
1161 template <
class Key>
1169 for (node = bucket_[bucket]; node != 0; node = node->
next)
1173 if (node->
next == 0)
1180 return (size_ == size);
1183 template <
class Key>
1191 s <<
" size: " << getSize() << std::endl;
1194 s <<
" # buckets: " << getBucketSize() << std::endl;
1197 s <<
" capacity: " << getCapacity() << std::endl;
1200 s <<
" load factor: " << (
float)size_ / (
float)bucket_.size() << std::endl;
1205 s <<
" bucket " << bucket <<
": ";
1206 for (
Node* ptr = bucket_[bucket]; ptr != 0; ptr = ptr->
next)
1208 s <<
"(" << (
void*)ptr <<
") ";
1210 s <<
"(0)" << std::endl;
1216 template <
class Key>
1219 if (processor.
start() ==
false)
1229 result = processor(*it);
1237 return processor.
finish();
1240 template <
class Key>
1247 template <
class Key>
1254 template <
class Key>
1259 Node* next_node = 0;
1260 for (i = 0; i < bucket_.size(); i++)
1265 next_node = node->next;
1273 template <
class Key>
1278 return new Node(value, next);
1281 template <
class Key>
1288 template <
class Key>
1292 return (size_ >= capacity_);
1295 template <
class Key>
1302 template <
class Key>
1303 void HashSet<Key>::rehash_()
1309 vector<Node*> old_buckets(bucket_);
1313 bucket_.resize(capacity_);
1315 for (i = 0; i < capacity_; i++)
1325 for (node = old_buckets[i]; node != 0; node = next_node)
1327 next_node = node->next;
1328 Position new_bucket = hashBucket_(node->value);
1329 node->next = bucket_[new_bucket];
1330 bucket_[new_bucket] = node;
1336 #endif // BALL_DATATYPE_HASHSET_H
Size getBucketSize() const
HashIndex Hash(const T &key)
ConstIterator const_iterator
GeneralException()
Default constructor.
std::pair< Iterator, bool > insert(const ValueType &item)
IteratorPosition position_
const HashSet * getContainer() const
Iterator find(const Key &key)
friend class IteratorTraits
bool apply(UnaryProcessor< ValueType > &processor)
bool operator==(const HashSet &hash_set) const
ConstForwardIterator< HashSet< Key >, ValueType, PointerType, IteratorTraits > ConstIterator
static ConstForwardIterator end(const Container &container)
ForwardIterator< HashSet< Key >, ValueType, PointerType, IteratorTraits > Iterator
HashSet operator-(const HashSet &rhs) const
virtual Node * newNode_(const ValueType &value, Node *next) const
Initial number of buckets.
const HashSet & operator-=(const HashSet &rhs)
const Key * const_pointer
Size erase(const KeyType &key)
virtual HashIndex hash(const Key &key) const
virtual bool needRehashing_() const
bool operator!=(const HashSet &hash_set) const
virtual void dump(std::ostream &s=std::cout, Size depth=0) const
HashSet operator&(const HashSet &rhs) const
bool operator!=(const IteratorTraits &traits) const
ConstIterator begin() const
IteratorPosition & getPosition()
HashSet(Size initial_capacity=INITIAL_CAPACITY, Size number_of_buckets=INITIAL_NUMBER_OF_BUCKETS)
BALL_INLINE const Traits & getTraits() const
Get a constant reference to the traits of this iterator.
static const Position INVALID_POSITION
void swap(HashSet &hash_set)
IteratorTraits(const IteratorTraits &traits)
const HashSet & operator+=(const HashSet &rhs)
const ValueType & getData() const
void set(const HashSet &hash_set)
const HashSet & operator&=(const HashSet &rhs)
void get(HashSet &hash_set) const
static ForwardIterator end(const Container &container)
HashSet operator|(const HashSet &rhs) const
IteratorTraits & operator=(const IteratorTraits &traits)
const Key & const_reference
const HashSet & operator=(const HashSet &rhs)
const IteratorPosition & getPosition() const
IllegalKey(const char *file, int line)
#define BALL_DUMP_STREAM_PREFIX(os)
BALL_EXPORT HashIndex getNextPrime(HashIndex l)
static ConstForwardIterator begin(const Container &container)
virtual void deleteNode_(Node *node) const
#define BALL_DUMP_STREAM_SUFFIX(os)
#define BALL_DUMP_DEPTH(os, depth)
bool operator==(const IteratorTraits &traits) const
static ForwardIterator begin(const Container &container)
const HashSet & operator|=(const HashSet &rhs)
HashSet operator+(const HashSet &rhs) const
Node(const KeyType &my_key, const Node *my_next)
bool has(const Key &key) const
Initial capacity of the hash set.
virtual void host(Visitor< HashSet< Key > > &visitor)
IteratorTraits(const HashSet &hash_set)
ConstIterator end() const