|  | 
|  | 1 | +# Copyright 2013, Michael H. Goldwasser | 
|  | 2 | +# | 
|  | 3 | +# Developed for use with the book: | 
|  | 4 | +# | 
|  | 5 | +#    Data Structures and Algorithms in Python | 
|  | 6 | +#    Michael T. Goodrich, Roberto Tamassia, and Michael H. Goldwasser | 
|  | 7 | +#    John Wiley & Sons, 2013 | 
|  | 8 | +# | 
|  | 9 | +# This program is free software: you can redistribute it and/or modify | 
|  | 10 | +# it under the terms of the GNU General Public License as published by | 
|  | 11 | +# the Free Software Foundation, either version 3 of the License, or | 
|  | 12 | +# (at your option) any later version. | 
|  | 13 | +# | 
|  | 14 | +# This program is distributed in the hope that it will be useful, | 
|  | 15 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 16 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 17 | +# GNU General Public License for more details. | 
|  | 18 | +# | 
|  | 19 | +# You should have received a copy of the GNU General Public License | 
|  | 20 | +# along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|  | 21 | + | 
|  | 22 | +from .hash_map_base import HashMapBase | 
|  | 23 | + | 
|  | 24 | +class ProbeHashMap(HashMapBase): | 
|  | 25 | +  """Hash map implemented with linear probing for collision resolution.""" | 
|  | 26 | +  _AVAIL = object()       # sentinal marks locations of previous deletions | 
|  | 27 | + | 
|  | 28 | +  def _is_available(self, j): | 
|  | 29 | +    """Return True if index j is available in table.""" | 
|  | 30 | +    return self._table[j] is None or self._table[j] is ProbeHashMap._AVAIL | 
|  | 31 | + | 
|  | 32 | +  def _find_slot(self, j, k): | 
|  | 33 | +    """Search for key k in bucket at index j. | 
|  | 34 | +
 | 
|  | 35 | +    Return (success, index) tuple, described as follows: | 
|  | 36 | +    If match was found, success is True and index denotes its location. | 
|  | 37 | +    If no match found, success is False and index denotes first available slot. | 
|  | 38 | +    """ | 
|  | 39 | +    firstAvail = None | 
|  | 40 | +    while True:                                | 
|  | 41 | +      if self._is_available(j): | 
|  | 42 | +        if firstAvail is None: | 
|  | 43 | +          firstAvail = j                      # mark this as first avail | 
|  | 44 | +        if self._table[j] is None: | 
|  | 45 | +          return (False, firstAvail)          # search has failed | 
|  | 46 | +      elif k == self._table[j]._key: | 
|  | 47 | +        return (True, j)                      # found a match | 
|  | 48 | +      j = (j + 1) % len(self._table)          # keep looking (cyclically) | 
|  | 49 | + | 
|  | 50 | +  def _bucket_getitem(self, j, k): | 
|  | 51 | +    found, s = self._find_slot(j, k) | 
|  | 52 | +    if not found: | 
|  | 53 | +      raise KeyError('Key Error: ' + repr(k))        # no match found | 
|  | 54 | +    return self._table[s]._value | 
|  | 55 | + | 
|  | 56 | +  def _bucket_setitem(self, j, k, v): | 
|  | 57 | +    found, s = self._find_slot(j, k) | 
|  | 58 | +    if not found: | 
|  | 59 | +      self._table[s] = self._Item(k,v)               # insert new item | 
|  | 60 | +      self._n += 1                                   # size has increased | 
|  | 61 | +    else: | 
|  | 62 | +      self._table[s]._value = v                      # overwrite existing | 
|  | 63 | + | 
|  | 64 | +  def _bucket_delitem(self, j, k): | 
|  | 65 | +    found, s = self._find_slot(j, k) | 
|  | 66 | +    if not found: | 
|  | 67 | +      raise KeyError('Key Error: ' + repr(k))        # no match found | 
|  | 68 | +    self._table[s] = ProbeHashMap._AVAIL             # mark as vacated | 
|  | 69 | + | 
|  | 70 | +  def __iter__(self): | 
|  | 71 | +    for j in range(len(self._table)):                # scan entire table | 
|  | 72 | +      if not self._is_available(j): | 
|  | 73 | +        yield self._table[j]._key | 
0 commit comments