Exemplo n.º 1
0
 def Push(self, item):
     Top = self.getTop()
     element = Node(item)
     if isinstance(element, Node) and self.isEmpty():
         self._setTop(element)
         self.len += 1
     elif isinstance(element, Node):
         element.setPrev(Top)
         Top.setNext(element)
         self._setTop(element)
         self.len += 1
Exemplo n.º 2
0
 def Enqueue(self, item):
     Tail = self.tail
     element = Node(item)
     if isinstance(element, Node) and not self.isEmpety():
         element.setNext(Tail)
         Tail.setPrev(element)
         self._setTail(element)
         self.len += 1
     elif isinstance(element, Node):
         self._setHead(element)
         self.len += 1
Exemplo n.º 3
0
 def put(self, key, value):
     """
     首先判断缓存容量 0直接返回说明缓存无法保存数据
     判断key是否已经存在缓存里
     存在把旧的node拿出来更新value 删除旧节点,添加新的节点
     不存在先判断缓存是否满 (满先pop头部节点,在添加尾部,不满直接添加尾部)
     :param key:
     :param value:
     :return:
     """
     if self.capacity == 0:
         return
     if key in self.map:
         node = self.map.get(key)
         # 删除旧节点
         self.list.remove(node)
         # 新节点默认添加尾部
         node.value = value
         self.list.append(node)
     else:
         if self.size == self.capacity:
             node = self.list.pop()
             # 在本地map映射中删除节点
             del self.map[node.key]
             self.size -= 1
         node = Node(key, value)
         self.list.append(node)
         # 同时本地映射缓存保存key和node
         self.map[key] = node
         self.size += 1
Exemplo n.º 4
0
 def put(self, key, value):
     if key in self.map:
         node = self.map.get(key)
         self.list.remove(node)
         node.value = value
         self.list.append_front(node)
     else:
         node = Node(key, value)
         if self.list.size >= self.list.capacity:
             old_node = self.list.remove()
             self.map.pop(old_node.key)
         self.list.append_front(node)
         self.map[key] = node
Exemplo n.º 5
0
 def put(self, key, value):
     node = Node(key, value)
     if key in self.map:
         old_node = self.map[key]
         self.list.remove(old_node)
         self.list.append_front(node)
     else:
         if self.size == self.capacity:
             old_node = self.list.remove()
             self.map.pop(old_node.key)
             self.size -= 1
         self.map[key] = node
         self.list.append_front(node)
         self.size += 1
Exemplo n.º 6
0
 def put(self, key, value):
     if key in self.map:
         node = self.map[key]
         self.double_linked_list.remove(node)
         node.value = node
         self.double_linked_list.append(node)
     else:
         if self.size == self.capacity:
             node = self.double_linked_list.pop()
             del self.map[node.key]
             self.size -= 1
         node = Node(key, value)
         self.map[key] = node
         self.double_linked_list.append(node)
         self.size += 1
Exemplo n.º 7
0
 def put(self, key, value):
     # key在缓存里 把节点拿出来更新
     if key in self.map:
         node = self.map.get(key)
         self.list.remove(node)
         node.value = value
         self.list.append_front(node)
     else:
         # 不在缓存创建新的节点
         node = Node(key, value)
         # 缓存已经满了且不再缓存里,删除掉尾部节点,变成了缓存未满
         if self.list.size >= self.list.capacity:
             old_node = self.list.remove()
             self.map.pop(old_node.key)
         self.list.append_front(node)
         self.map[key] = node
Exemplo n.º 8
0
 def put(self, key, value):
     if self.capacity == 0:
         return
     if key in self.map:
         node = self.map.get(key)
         self.list.remove(node)
         node.value = value
         self.list.append(node)
     else:
         if self.size == self.capacity:
             node = self.list.pop()
             del self.map[node.key]
             self.size -= 1
         node = Node(key, value)
         self.list.append(node)
         self.map[key] = node
         self.size += 1
Exemplo n.º 9
0
    def put(self,key,value):
        key=str(key)
        if key not in self.map:     # 缓存中没有这个空间的地址,则为新增
            # 分两种情况,缓存满了和缓存没满
            if self.size >= self.capacity:  # 满了
                old_node=self.dll.pop()      # 从链表尾部弹出尾部节点
                self.map.pop(old_node.key)   # 发生置换,从缓存中删除该旧节点
                self.size-=1

                # 从缓存链表中弹出的数据应该置换(写入)到主存中
                self.ram[old_node.key]=old_node

            # 没满则不作额外操作
            node = Node(key,value)
            self.map[key]=node
            self.dll.unshift(node)
            self.size+=1
        else:   # 修改
            self.dll.remove(self.map[key])  # 将该节点放到链表头部
            self.dll.unshift(self.map[key])
            self.map[key]=value     # 修改该节点的值
Exemplo n.º 10
0
    def put(self,key,value):
        key = str(key)
        newNode = Node(key,value)
        if key in self.map: # 如果缓存中已有这个地址的数据,说明是更新
            node = self.map[key]
            self.doubleLinkedList.remove(node)
            self.doubleLinkedList.push(newNode)
            self.map[key]=newNode
        else:   # 新增
            if self.size==self.capacity:    # 如果缓存写满了,则进行先进先出置换
                node = self.doubleLinkedList.shift()    # 获取弹出的节点,待会要从map中删除这个节点
                # print(node.key)
                del(self.map[node.key])
                self.size-=1

                # 将弹出来的节点写一份到内存中
                if node.key not in self.ram:
                    self.ram[node.key] = node

            self.doubleLinkedList.push(newNode)
            self.map[key]=newNode
            self.size+=1
Exemplo n.º 11
0
from DoubleLinkedList import Node

Head = Node('Hady')

Second_Node = Node('World')
Head.Next = Second_Node
Second_Node.Pre = Head

Third_Node = Node('Hello')
Second_Node.Next = Third_Node
Third_Node.Pre = Second_Node

print(Third_Node.Value)
print(Third_Node.Pre.Value)
print(Third_Node.Pre.Pre.Value)
Exemplo n.º 12
0
        # key=str(key)
        if key not in self.ram:
            return False

        node = self.ram[key]
        self.put(key,node.value)


    def print(self):
        self.dll.print()
        print(self.map)
        print(self.ram)
        print("\n")

if __name__=="__main__":
    ram = {str(i):Node(i,i) for i in range(1,21)}  # 定义内存
    
    # 定义缓存
    cache=LRUCache(capacity=3,ram=ram)

    # CPU往缓存中写入数据
    cache.put(33,33)
    cache.put(100,100)
    cache.put(2,2)  # 主存置换到缓存的数据
    cache.print()

    cache.get(10)
    cache.get(1000)
    cache.put(4,6)  # 修改key为4的地址空间的数据值为6
    cache.print()
Exemplo n.º 13
0
                if node.key not in self.ram:
                    self.ram[node.key] = node

            self.doubleLinkedList.push(newNode)
            self.map[key]=newNode
            self.size+=1

    def print(self):
        self.doubleLinkedList.print()
        print(self.map)
        print(self.ram)
        print("\n")


if __name__=="__main__":
    ram={str(i):Node(key=i,value=i) for i in range(1,21)}    # 定义已占用的内存空间和每个内存空间的数据,key是地址,value是数据

    # 假设一个缓存有3个空间
    cache = FIFOCache(capacity=3,ram=ram)

    # 在类外put的都是CPU写入缓存的数据,而在__getFromRam方法put的都是从主存置换到缓存的数据
    cache.put(25,25)        # key大于20的都是内存中不存在的,说明是CPU运算产生的数据写入了缓存
    cache.put(12,12)
    cache.put(100,100)
    cache.print()
    cache.put(50,50)
    cache.print()
    print(cache.get(10))
    print(cache.get(5))
    print(cache.get(2))
    print(cache.get(100))
Exemplo n.º 14
0
 def __init__(self, key, value):
     self.freq = 0
     Node.__init__(self, key, value)