当前位置: 首页>>代码示例>>Python>>正文


Python LinkedList.push_front方法代码示例

本文整理汇总了Python中linkedlist.LinkedList.push_front方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.push_front方法的具体用法?Python LinkedList.push_front怎么用?Python LinkedList.push_front使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在linkedlist.LinkedList的用法示例。


在下文中一共展示了LinkedList.push_front方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_pop_front

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import push_front [as 别名]
 def test_pop_front(self):
     llist = LinkedList()
     for i in xrange(10):
         llist.push_front(i)
     i = 9
     while llist:
         item = llist.pop_front()
         self.assertEqual(item, i)
         i -= 1
开发者ID:sthe0,项目名称:task3,代码行数:11,代码来源:test.py

示例2: LRUCache

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import push_front [as 别名]
class LRUCache(object):
    def __init__(self, capacity):
        self.init(capacity)

    def init(self, capacity):
        self.capacity = capacity
        self.table = {}
        self.data = LinkedList()
        self.hitcnt = self.misscnt = self.expirecnt = 0
        self.lock = threading.Lock()

    def stats(self):
        per = 1.0 * self.hitcnt / max(1, self.misscnt + self.expirecnt + self.misscnt);
        return "size = %d, hitcnt = %d, misscnt = %d, expirecnt = %d, hit per=%.4lf\n" % (
                len(self.table), self.hitcnt, self.misscnt, self.expirecnt, per)

    def delete(self, key):
        with self.lock:
            if key in self.table:
                node = self.table[key]
                self.data.remove(node)
                del self.table[key]
                return True

    def get(self, key):
        """
        get the cache value with specifiy key
        return None either key is not found or value is expired
        """
        with self.lock:
            if key in self.table:
                node = self.table[key]
                if node.elem.expire < time.time():
                    #expire
                    self.expirecnt += 1
                    self.data.remove(node)
                    del self.table[key]
                else:
                    self.hitcnt += 1
                    self.data.move_to_front(node)
                    return node.elem.value
            else:
                self.misscnt += 1

    def setex(self, key, seconds, value):
        with self.lock:
            expire = int(time.time()) + seconds
            if key in self.table:
                #already exists
                node = self.table[key]
                node.elem = Entry(key, value, expire)
                self.data.move_to_front(node)
            else:
                self._add(key, expire, value)

    def _add(self, key, expire, value):
        node = self.data.push_front(Entry(key, value, expire))
        self.table[key] = node 
        if len(self.table) > self.capacity:
            node = self.data.back()
            self.data.remove(node)
            del self.table[node.elem.key]
开发者ID:Lin53,项目名称:playground,代码行数:64,代码来源:test_lru.py

示例3: test_push_front

# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import push_front [as 别名]
 def test_push_front(self):
     llist = LinkedList()
     for i in xrange(10):
         llist.push_front(i)
     for i, node in enumerate(llist):
         self.assertEqual(node.value, 9 - i)
开发者ID:sthe0,项目名称:task3,代码行数:8,代码来源:test.py


注:本文中的linkedlist.LinkedList.push_front方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。