Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Open addressing
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==Example pseudocode== 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, occupied flag (initially unset) } '''var''' pair slot[0], slot[1], ..., slot[num_slots - 1] '''function''' find_slot(key) i := hash(key) modulo num_slots ''// search until we either find the key, or find an empty slot.'' '''while''' (slot[i] is occupied) and (slot[i].key β key) i := (i + 1) modulo num_slots '''return''' i '''function''' lookup(key) i := find_slot(key) '''if''' slot[i] is occupied ''// key is in table'' '''return''' slot[i].value '''else''' ''// key is not in table'' '''return''' not found '''function''' set(key, value) i := find_slot(key) '''if''' slot[i] is occupied ''// we found our key'' slot[i].value := value '''return''' '''if''' the table is almost full rebuild the table larger ''(note 1)'' i := find_slot(key) mark slot[i] as occupied slot[i].key := key slot[i].value := value ; 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 [[exponential growth|exponentially]], for example by doubling the old array size. '''function''' remove(key) i := find_slot(key) '''if''' slot[i] is unoccupied '''return''' ''// key is not in the table'' mark slot[i] as unoccupied j := i '''loop''' ''(note 2)'' j := (j + 1) modulo num_slots '''if''' slot[j] is unoccupied '''exit loop''' k := hash(slot[j].key) modulo num_slots ''// determine if k lies cyclically in (i,j]'' ''// i β€ j: | i..k..j |'' ''// i > j: |.k..j i....| or |....j i..k.|'' '''if''' i β€ j '''if''' (i < k) and (k β€ j) '''continue loop''' '''else''' '''if''' (k β€ j) or (i < k) '''continue loop''' mark slot[i] as occupied slot[i].key := slot[j].key slot[i].value := slot[j].value mark slot[j] as unoccupied i := j ; note 2 : For all records in a cluster, there must be no vacant slots between their natural hash position and their current position (else lookups will terminate before finding the record). At this point in the pseudocode, {{var|i}} is a vacant slot that might be invalidating this property for subsequent records in the cluster. {{var|j}} is such a subsequent record. {{var|k}} is the raw hash where the record at {{var|j}} would naturally land in the hash table if there were no collisions. This test is asking if the record at {{var|j}} is invalidly positioned with respect to the required properties of a cluster now that {{var|i}} is vacant. 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.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)