10 #ifndef TEUCHOS_HASHSET_H
11 #define TEUCHOS_HASHSET_H
37 inline HashSet(
int capacity=19);
43 inline void put(
const Key& key);
46 inline void remove(
const Key& key);
49 inline int size()
const {
return count_;}
58 inline std::string
toString()
const ;
64 inline int nextPrime(
int newCap)
const ;
69 mutable Key mostRecentKey_;
77 std::ostream& operator<<(std::ostream& os, const HashSet<Key>& h);
79 template<
class Key>
inline
80 std::string toString(
const HashSet<Key>& h) {
return h.toString();}
83 template<
class Key>
inline
85 : data_(), count_(0), capacity_(
HashUtils::nextPrime(capacity))
87 data_.resize(capacity_);
90 template<
class Key>
inline
94 = data_[hashCode(key) % capacity_];
96 for (
int i=0; i<candidates.
length(); i++)
98 const Key& c = candidates[i];
107 template<
class Key>
inline
110 int index = hashCode(key) % capacity_;
115 for (
int i=0; i<local.
length(); i++)
127 if (count_ > capacity_)
129 capacity_ = HashUtils::nextPrime(capacity_+1);
132 index = hashCode(key) % capacity_;
135 data_[index].append(key);
140 template<
class Key>
inline
145 for (
int i=0; i<data_.length(); i++)
147 for (
int j=0; j<data_[i].length(); j++)
149 int newIndex = hashCode(data_[i][j]) % capacity_;
150 tmp[newIndex].append(data_[i][j]);
157 template<
class Key>
inline
163 for (
int i=0; i<data_.length(); i++)
165 for (
int j=0; j<data_[i].length(); j++)
174 template<
class Key>
inline
179 for (
int i=0; i<data_.length(); i++)
181 for (
int j=0; j<data_[i].length(); j++)
188 template<
class Key>
inline
191 std::string rtn =
"HashSet[";
195 for (
int i=0; i<data_.length(); i++)
197 for (
int j=0; j<data_[i].length(); j++)
199 if (!first) rtn +=
", ";
201 rtn += Teuchos::toString(data_[i][j]);
209 template<
class Key>
inline
214 "HashSet<Key>::remove: key "
215 << Teuchos::toString(key)
216 <<
" not found in HashSet"
220 int h = hashCode(key) % capacity_;
223 for (
int i=0; i<candidates.
length(); i++)
225 if (candidates[i] == key)
235 template<
class Key>
inline
236 std::ostream& operator<<(std::ostream& os, const HashSet<Key>& h)
238 return os << h.toString();
244 #endif // TEUCHOS_HASHSET_H
void remove(int i)
Remove the i-th element from the array, with optional boundschecking.
void reserve(size_type n)
Utilities for generating hashcodes. This is not a true hash ! For all ints and types less than ints i...
Array< T > & append(const T &x)
Add a new entry at the end of the array.
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
Macro for throwing an exception with breakpointing to ease debugging.
Teuchos header file which uses auto-configuration information to include necessary C++ headers...
bool containsKey(const Key &key) const
Check for the presence of a key.
Templated hashtable-based set.
std::string toString() const
Write to a std::string.
void resize(size_type new_size, const value_type &x=value_type())
HashSet(int capacity=19)
Create an empty HashSet.
void put(const Key &key)
Put a new object into the table.
Array< Key > arrayify() const
Get list of keys in Array form.
Templated array class derived from the STL std::vector.
int length() const
Return number of elements in the array.
int size() const
Get the number of elements in the table.
Utilities for generating hashcodes.
void remove(const Key &key)
Remove from the table the element given by key.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...