Example Pseudo Code
The following pseudocode is an implementation of an open addressing hash table with linear probing and single-slot stepping, a common approach that is effective if the hash function is good. Each of the lookup, set and remove functions use a common internal function find_slot to locate the array slot that either does or should contain a given key.
record pair { key, value } var pair array slot function find_slot(key) i := hash(key) modulo num_slots // search until we either find the key, or find an empty slot. while ( (slot is occupied) and ( slot.key ≠ key ) ) do i := (i + 1) modulo num_slots repeat return i function lookup(key) i := find_slot(key) if slot is occupied // key is in table return slot.value else // key is not in table return not found function set(key, value) i := find_slot(key) if slot is occupied slot.value := value else if the table is almost full rebuild the table larger (note 1) i := find_slot(key) slot.key := key slot.value := valueAnother example showing open addressing technique. Presented function is converting each part(4) of an internet protocol address, where NOT, XOR, OR and AND are bitwise operations and << and >> are left and right logical shifts:
// key_1,key_2,key_3,key_4 are following 3-digit numbers - parts of ip address xxx.xxx.xxx.xxx function ip(key parts) j := 1 do key := (key_2 << 2) key := (key + (key_3 << 7)) key := key + (j OR key_4 >> 2) * (key_4) * (j + key_1) XOR j key := key AND _prime_ // _prime_ is a prime number j := (j+1) while collision return key- note 1
- Rebuilding the table requires allocating a larger array and recursively using the set operation to insert all the elements of the old array into the new larger array. It is common to increase the array size exponentially, for example by doubling the old array size.
Another technique for removal is simply to mark the slot as deleted. However this eventually requires rebuilding the table simply to remove deleted records. The methods above provide O(1) updating and removal of existing records, with occasional rebuilding if the high water mark of the table size grows.
The O(1) remove method above is only possible in linearly probed hash tables with single-slot stepping. In the case where many records are to be deleted in one operation, marking the slots for deletion and later rebuilding may be more efficient.
Read more about this topic: Open Addressing
Famous quotes containing the words pseudo and/or code:
“Logic is the last scientific ingredient of Philosophy; its extraction leaves behind only a confusion of non-scientific, pseudo problems.”
—Rudolf Carnap (18911970)
“Many people will say to working mothers, in effect, I dont think you can have it all. The phrase for have it all is code for have your cake and eat it too. What these people really mean is that achievement in the workplace has always come at a priceusually a significant personal price; conversely, women who stayed home with their children were seen as having sacrificed a great deal of their own ambition for their families.”
—Anne C. Weisberg (20th century)