]>
Dogcows Code - chaz/yoink/blob - src/Moof/stlplus/hash.hpp
3 ////////////////////////////////////////////////////////////////////////////////
5 // Author: Andy Rushton
6 // Copyright: (c) Southampton University 1999-2004
7 // (c) Andy Rushton 2004-2009
8 // License: BSD License, see ../docs/license.html
10 // A chained hash table using STL semantics
12 ////////////////////////////////////////////////////////////////////////////////
13 #include "containers_fixes.hpp"
14 #include "exceptions.hpp"
15 #include "safe_iterator.hpp"
21 ////////////////////////////////////////////////////////////////////////////////
24 template<typename K
, typename T
, class H
, class E
> class hash
;
25 template<typename K
, typename T
, class H
, class E
> class hash_element
;
27 ////////////////////////////////////////////////////////////////////////////////
30 template<typename K
, typename T
, class H
, class E
, typename V
>
31 class hash_iterator
: public safe_iterator
<hash
<K
,T
,H
,E
>,hash_element
<K
,T
,H
,E
> >
34 friend class hash
<K
,T
,H
,E
>;
36 // local type definitions
37 // an iterator points to a value whilst a const_iterator points to a const value
39 typedef hash_iterator
<K
,T
,H
,E
,std::pair
<const K
,T
> > iterator
;
40 typedef hash_iterator
<K
,T
,H
,E
,const std::pair
<const K
,T
> > const_iterator
;
41 typedef hash_iterator
<K
,T
,H
,E
,V
> this_iterator
;
45 // constructor to create a null iterator - you must assign a valid value to this iterator before using it
46 // any attempt to dereference or use a null iterator is an error
47 // the only valid thing you can do is assign an iterator to it
51 // Type conversion methods allow const_iterator and iterator to be converted
52 // convert an iterator/const_iterator to a const_iterator
53 const_iterator
constify(void) const;
54 // convert an iterator/const_iterator to an iterator
55 iterator
deconstify(void) const;
57 // increment operators used to step through the set of all values in a hash
58 // it is only legal to increment a valid iterator
59 // there's no decrement - I've only implemented this as a unidirectional iterator
61 this_iterator
& operator ++ (void)
62 throw(null_dereference
,end_dereference
);
64 this_iterator
operator ++ (int)
65 throw(null_dereference
,end_dereference
);
67 // test useful for testing whether iteration has completed
68 bool operator == (const this_iterator
& r
) const;
69 bool operator != (const this_iterator
& r
) const;
70 bool operator < (const this_iterator
& r
) const;
72 // access the value - a const_iterator gives you a const value, an iterator a non-const value
73 // it is illegal to dereference an invalid (i.e. null or end) iterator
74 reference
operator*(void) const
75 throw(null_dereference
,end_dereference
);
76 pointer
operator->(void) const
77 throw(null_dereference
,end_dereference
);
80 friend class hash_element
<K
,T
,H
,E
>;
82 // constructor used by hash to create a non-null iterator
83 // you cannot create a valid iterator except by calling a hash method that returns one
84 explicit hash_iterator(hash_element
<K
,T
,H
,E
>* element
);
85 // constructor used to create an end iterator
86 explicit hash_iterator(const hash
<K
,T
,H
,E
>* owner
);
87 // used to create an alias of an iterator
88 explicit hash_iterator(const safe_iterator
<hash
<K
,T
,H
,E
>, hash_element
<K
,T
,H
,E
> >& iterator
);
91 ////////////////////////////////////////////////////////////////////////////////
95 // H = hash function object with the profile 'unsigned H(const K&)'
96 // E = equal function object with profile 'bool E(const K&, const K&)' defaults to equal_to which in turn calls '=='
98 template<typename K
, typename T
, class H
, class E
= std::equal_to
<K
> >
102 typedef unsigned size_type
;
105 typedef T mapped_type
;
106 typedef std::pair
<const K
, T
> value_type
;
107 typedef hash_iterator
<K
,T
,H
,E
,value_type
> iterator
;
108 typedef hash_iterator
<K
,T
,H
,E
,const value_type
> const_iterator
;
110 // construct a hash table with specified number of bins
111 // the default 0 bins means leave it to the table to decide
112 // specifying 0 bins also enables auto-rehashing, otherwise auto-rehashing defaults off
113 hash(unsigned bins
= 0);
116 // copy and equality copy the data elements but not the size of the copied table
118 hash
& operator = (const hash
&);
120 // test for an empty table and for the size of a table
121 // efficient because the size is stored separately from the table contents
122 bool empty(void) const;
123 unsigned size(void) const;
125 // test for equality - two hashes are equal if they contain equal values
126 bool operator == (const hash
&) const;
127 bool operator != (const hash
&) const;
129 // switch auto-rehash on
130 void auto_rehash(void);
131 // switch auto-rehash off
132 void manual_rehash(void);
133 // force a rehash now
134 // default of 0 means implement built-in size calculation for rehashing (recommended - it doubles the number of bins)
135 void rehash(unsigned bins
= 0);
136 // test the loading ratio, which is the size divided by the number of bins
137 // use this if you are doing your own rehashing
138 // the recommendation is to double the bins when the loading exceeds 0.5 which is what auto-rehashing does
139 float loading(void) const;
141 // test for the presence of a key
142 bool present(const K
& key
) const;
143 // provide map equivalent key count function (0 or 1, as not a multimap)
144 size_type
count(const K
& key
) const;
146 // insert a new key/data pair - replaces any previous value for this key
147 iterator
insert(const K
& key
, const T
& data
);
148 // insert a copy of the pair into the table (std::map compatible)
149 std::pair
<iterator
, bool> insert(const value_type
& value
);
150 // insert a new key and return the iterator so that the data can be filled in
151 iterator
insert(const K
& key
);
153 // remove a key/data pair from the hash table
154 bool erase(const K
& key
);
155 // remove all elements from the hash table
157 // provide the std::map equivalent clear function
160 // find a key and return an iterator to it
161 // The iterator is like a pointer to a pair<const K,T>
162 // end() is returned if the find fails
163 const_iterator
find(const K
& key
) const;
164 iterator
find(const K
& key
);
166 // returns the data corresponding to the key
167 // const version is used for const hashes and cannot change the hash, so failure causes an exception
168 // non-const version is for non-const hashes and is like map - it creates a new key/data pair if find fails
169 const T
& operator[] (const K
& key
) const throw(std::out_of_range
);
170 T
& operator[] (const K
& key
);
172 // iterators allow the hash table to be traversed
173 // iterators remain valid unless an item is removed or unless a rehash happens
174 const_iterator
begin(void) const;
175 iterator
begin(void);
176 const_iterator
end(void) const;
181 friend class hash_element
<K
,T
,H
,E
>;
182 friend class hash_iterator
<K
,T
,H
,E
,std::pair
<const K
,T
> >;
183 friend class hash_iterator
<K
,T
,H
,E
,const std::pair
<const K
,T
> >;
188 hash_element
<K
,T
,H
,E
>** m_values
;
191 ////////////////////////////////////////////////////////////////////////////////
193 } // end namespace stlplus
This page took 0.073548 seconds and 4 git commands to generate.