Function |
Input |
Output |
Description |
size() |
- |
Integer |
Return the number of
items in D. |
isEmpty() |
- |
Boolean |
Test whether D is empty. |
elements() |
- |
Iterator of objects
(elements) |
Returns the elements
stored in D. |
keys() |
- |
Iterator of objects (keys) | Returns the keys stored in D. |
find(k) |
Object (key) | Position |
If D contain an item
with key equal to k,
then return the position of such an item. If not, a null
position is returned. |
findAll(k) |
Object (key) | Iterator of Positions | Return an iterator of
positions for all items whose key equals k. |
insertItem(k,e) |
Objects k (key) and e (element) | - |
Insert an item with
element e
and key k
into D. |
removeElement(k) |
Object (key) |
- |
Remove an item with key
equal to k
from D. An error
condition occurs if D
has no such item. |
removeAllElements(k) |
Object (key) | - |
Remove the items with key equal to k from D. |
|
int hashCode(int x)64-bit integer if we have 32-bit integer hash function
{ return x; }
int hashCode(long x)Polynomial Hash Codes
{ typedef unsigned long ulong;
return hashCode(static_cast<int>(static_cast<ulong>(x) >> 32)
+ static_cast<int>(x));
}
Experimental Resultsint hashCode(const char* p, int len) // hash a character array
{ unsigned int h = 0;
for (int i = 0; i < len; i++)
{ h = (h << 5)|(h >> 27); // 5-bit cyclic shift
h += (unsigned int)p[i]; // add in next character
}
return hashCode(int(h));
}
Shift |
Collisions Total |
Collisions Max |
0 |
23739 |
86 |
1 |
10517 |
21 |
5 |
4 |
2 |
6 |
6 |
2 |
11 |
453 |
4 |
C++ provides an operation called a reinterpret_cast, to cast between such unrelated types.int hashCode(const double& x) // hash a double
{ int len = sizeof(x);
const char* p = reinterpret_cast<const char *>(&x);
return hashCode(p, len);
}
|
|
Algorithm
find(k) i ← h(k) p ← 0 repeat c ← A[i] if c = ∅ return Position(null) else if c.key() = k return Position(c) else i ← (i + 1) mod N p ← p + 1 until p = N return Position(null) |
|