微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何访问哈希图中的溢出列表?

如何解决如何访问哈希图中的溢出列表?

我正在为一个数据结构类开发一个 hashmap 程序,并且已经实现了一个溢出链表,如果数组中的位置已经被占用,它会存储一个键值对。我正在尝试解决 setitem 方法并将其从伪代码转换为 python。

from Array import *
from OList import *

class MyMap:
    def __init__(self,size):
        self.array = Array(size)
        self.size = size
        self.numsearches = 0
        self.numcompares = 0
        self.debug = False

    def gethash(self,string):
        '''  The algorithm is to take the ASCII value of each character and multiply it by
            the position in the string where the positions start at 1 (not 0). This prevents
            transposition collisions.
        '''
        hashnumber = sum([(i+1)*ord(string[i]) for i in range(0,len(string))]) % self.size 
        if self.debug:   print("gethash(" + string + ")=" + str(hashnumber))
        return hashnumber

    def _occupied(self,index):         
        '''   This is a private method,that's why its name starts with underscore. '''
        return self.array[index] is not None

    def __setitem__(self,key,value):                   # CHANGES IN THIS METHOD (see below)
        if self.debug:
            print("__setting__ " + key + "  =  " + value)
        index = self.gethash(key)
        if self._occupied(index):
            if self.debug:
                if key == '***':
                    pass
        #  if the slot's key is "***" then 
        #          see if the key is in the OList
        #                     if so,then update the value for that node
        #          else
        #                     put the new key/value into the main slot and leave the OList alone
        #  else if the slot is None,then put the key/value pair into the main array slot
        #  else the key may be in the OList 
        #          so just call  OList.updateOrAppend(key,value)
        # (12 lines of code)

        else:
            self.array[index] = [key,value,OList()]

我不需要整个方法,我只需要知道如何查看密钥是否在 OList 中。 我省略了一些与解决此问题无关的 OList 方法,因为 OList 是一个冗长的模块。 OList 模块如下:

class OList:
    class Node:
        def __init__(self,value):
            self.key = key
            self.value = value
            self.next = None
        def __str__(self):
            return "key:" + self.key +"   value:" + self.value

    def __init__(self):
        self.head = None
        self.tail = None

    def append(self,value):
        newnode = OList.Node(key,value)
        if self.head is None:
            self.head = newnode
            self.tail = newnode
        else:
            self.tail.next = newnode
            self.tail = newnode

    def find(self,key):
        count = 0
        runner = self.head
        while runner != None:
            count += 1
            if runner.key == key:
                return (count,runner)
            runner = runner.next
        else:
            return (count,None)

    def updateOrAppend(self,value):
        count,thenode = self.find(key)
        if thenode == None:
            self.append(key,value)
        else:
            thenode.value = value

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。