Python_OrderedDict原理

SC

class OrderedDict(dict):
    'Dictionary that remembers insertion order'

    # An inherited dict maps keys to values.
    # The inherited dict provides __getitem__, __len__, __contains__, and get.
    # The remaining methods are order-aware.<其余方法都基于顺序的>
    # Big-O running times for all methods are the same as regular dictionaries.

    # The internal self.__map dict maps keys to links in a doubly linked list.<将key和循环双向链表的指针关联在一起>
    # The circular doubly linked list starts and ends with a sentinel element.
    # The sentinel element never gets deleted (this simplifies the algorithm).
    # Each link is stored as a list of length three:  [PREV, NEXT, KEY].

    def __init__(*args, **kwds):
        '''Initialize an ordered dictionary.  The signature is the same as
        regular dictionaries, but keyword arguments are not recommended because
        their insertion order is arbitrary.<不推荐使用关键字参数来初始化,因为它们的插入顺序是无序的>

        note:
        root[:];
         __map

        '''
        if not args:
            raise TypeError("descriptor '__init__' of 'OrderedDict' object "
                            "needs an argument")
        self = args[0]  # 实例
        args = args[1:] # 键值对
        if len(args) > 1:
            raise TypeError('expected at most 1 arguments, got %d' % len(args))
        try:
            self.__root
        except AttributeError:
            self.__root = root = []  # sentinel node
            root[:] = [root, root, None]
            self.__map = {}
        self.__update(*args, **kwds)

    def __setitem__(self, key, value, dict_setitem=dict.__setitem__):
        # 添加新元素时,在链表尾部创建新链表,并使用新键值对更新字典
        'od.__setitem__(i, y) <==> od[i]=y'
        # Setting a new item creates a new link at the end of the linked list,
        # and the inherited dictionary is updated with the new key/value pair.
        if key not in self:
            root = self.__root
            last = root[0]  # 头节点的前驱节点
            last[1] = root[0] = self.__map[key] = [last, root, key]  # key的前驱节点为last,后继节点为root利用self.__map来定位key
        return dict_setitem(self, key, value)

    def __delitem__(self, key, dict_delitem=dict.__delitem__):
        'od.__delitem__(y) <==> del od[y]'
        # Deleting an existing item uses self.__map to find the link which gets
        # removed by updating the links in the predecessor and successor nodes.
        dict_delitem(self, key)
        link_prev, link_next, _ = self.__map.pop(key)
        link_prev[1] = link_next  # update link_prev[NEXT]
        link_next[0] = link_prev  # update link_next[PREV]

    def __iter__(self):
        'od.__iter__() <==> iter(od)'
        # Traverse the linked list in order.
        root = self.__root
        curr = root[1]  # start at the first node<首节点>
        while curr is not root:  # 每个节点包含三大部分
            yield curr[2]  # yield the curr[KEY]
            curr = curr[1]  # move to next node

    def __reversed__(self):
        'od.__reversed__() <==> reversed(od)'
        # Traverse the linked list in reverse order.
        root = self.__root
        curr = root[0]  # start at the last node
        while curr is not root:
            yield curr[2]  # yield the curr[KEY]
            curr = curr[0]  # move to previous node

    def clear(self):
        'od.clear() -> None.  Remove all items from od.'
        root = self.__root
        root[:] = [root, root, None]
        self.__map.clear()
        dict.clear(self)

双向循环链表长啥样

Python_OrderedDict原理_第1张图片

Shoulders of Giants

双向循环链表和链表实现LRU------PLUS-链表相关操作

OrderedDict原理

你可能感兴趣的:(算法)