本文整理汇总了Python中linkedlist.LinkedList.back方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.back方法的具体用法?Python LinkedList.back怎么用?Python LinkedList.back使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.back方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_empty_list
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import back [as 别名]
def test_empty_list(self):
llist = LinkedList()
self.assertEqual(len(llist), 0)
self.assertIsNone(llist.next())
self.assertIsNone(llist.prev())
self.assertEqual(llist.prev(), llist.back())
self.assertEqual(llist.next(), llist.front())
self.assertEqual(llist.back(), llist.front())
self.assertFalse(bool(llist))
示例2: test_non_empty_list
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import back [as 别名]
def test_non_empty_list(self):
llist = LinkedList()
llist.push_back(1)
self.assertIsNotNone(llist.next())
self.assertIsNotNone(llist.prev())
self.assertEqual(llist.prev(), llist.back())
self.assertEqual(llist.next(), llist.front())
self.assertEqual(llist.back(), llist.front())
self.assertTrue(bool(llist))
示例3: test_clear
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import back [as 别名]
def test_clear(self):
llist = LinkedList()
for i in xrange(10):
llist.push_back(i)
llist.clear()
self.assertFalse(bool(llist))
self.assertIsNone(llist.back())
示例4: RuleList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import back [as 别名]
class RuleList(object):
def __init__(self, max_size = 10):
self.__list = LinkedList()
self.__cmp = BoolCmp(1e-9)
self.__max_size = max_size
self.__rule_set = set()
def insert(self, informativity, rule):
if rule in self.__rule_set:
return
self.__rule_set.add(rule)
if not self.__list or self.__cmp(self.__list.back().value[0], informativity) > 0:
self.__list.push_back([informativity, rule])
else:
for node in self.__list:
if self.__cmp(node.value[0], informativity) > 0:
continue
if self.__cmp(node.value[0], informativity) == 0:
node.value.append(rule)
if self.__cmp(node.value[0], informativity) < 0:
self.__list.insert_before(node, [informativity, rule])
break
if len(self.__rule_set) - (len(self.__list.back().value) - 1) >= self.__max_size:
for item in self.__list.back().value[1:]:
self.__rule_set.remove(item)
self.__list.remove(self.__list.back())
def clear(self):
self.__list = LinkedList()
self.__rule_set = set()
def __iter__(self):
for node in self.__list:
for item in node.value[1:]:
yield node.value[0], item
def __len__(self):
return len(self.__rule_set)
def __contains__(self, rule):
return rule in self.__rule_set
示例5: test_remove
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import back [as 别名]
def test_remove(self):
llist = LinkedList()
for i in xrange(10):
llist.push_back(i)
for item in llist:
if item.value % 2 == 0:
llist.remove(item)
self.assertEqual([node.value for node in llist], range(1,10,2))
for i in xrange(5):
llist.remove(llist.back())
self.assertFalse(bool(llist))
示例6: LRUCache
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import back [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]