本文整理汇总了Python中linkedlist.LinkedList.clear方法的典型用法代码示例。如果您正苦于以下问题:Python LinkedList.clear方法的具体用法?Python LinkedList.clear怎么用?Python LinkedList.clear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类linkedlist.LinkedList
的用法示例。
在下文中一共展示了LinkedList.clear方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_clear
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import clear [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())
示例2: test_clear_empty_list
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import clear [as 别名]
def test_clear_empty_list(self):
ll = LinkedList()
ll.clear()
self.assertTrue(ll.isEmpty())
示例3: Test_LinkedList
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import clear [as 别名]
class Test_LinkedList(unittest.TestCase):
def setUp(self):
self.ll = LinkedList(4)
self.ll.prepend(5)
def test_simple(self):
self.assertEqual(5, self.ll.peek())
self.assertEqual(2, len(self.ll))
def test_prepend(self):
ll = LinkedList(3)
self.assertEqual(1, len(ll))
self.assertEqual(3, ll.peek())
ll.prepend(2)
self.assertEqual(2, len(ll))
self.assertEqual(2, ll.peek())
ll.prepend(1)
self.assertEqual(3, len(ll))
self.assertEqual(1, ll.peek())
def test_append(self):
self.ll.append(6)
self.assertEqual(5, self.ll.peek())
self.assertEqual(3, len(self.ll))
self.assertEqual(4, self.ll.next.peek())
self.assertEqual(6, self.ll.next.next.peek())
def test_len(self):
self.ll.append(6)
self.assertEqual(3, len(self.ll))
def test_len_uninitialized(self):
ll = LinkedList()
ll.prepend(5)
self.assertEqual(1, len(ll))
def test_pop(self):
head_value = self.ll.pop()
self.assertEqual(5, head_value)
self.assertEqual(1, len(self.ll))
self.assertEqual(4, self.ll.peek())
def test_pop_to_empty(self):
ll = LinkedList(5)
self.assertEqual(1, len(ll))
self.assertEqual(ll.next, None)
self.assertFalse(ll.isEmpty())
head_value = ll.pop()
self.assertEqual(head_value, 5)
self.assertEqual(0, len(ll))
self.assertEqual(ll.next, None)
self.assertTrue(ll.isEmpty())
def test_isEmpty(self):
ll = LinkedList()
self.assertTrue(ll.isEmpty())
def test_pop_empty(self):
ll = LinkedList()
self.assertRaises(ListEmptyError, ll.pop)
def test_add(self):
ll = LinkedList(4)
ll.append(6)
ll2 = LinkedList(10)
ll2.append(12)
ll2.append(13)
ll = ll + ll2
self.assertEqual(5, len(ll))
def test_clear(self):
self.assertFalse(self.ll.isEmpty())
self.assertTrue(len(self.ll) == 2)
self.ll.clear()
self.assertTrue(self.ll.isEmpty())
def test_clear_empty_list(self):
ll = LinkedList()
ll.clear()
self.assertTrue(ll.isEmpty())
def test_reversed(self):
ll = LinkedList(7)
ll.append(8)
ll.append(9)
self.assertEqual(3, len(ll))
self.assertEqual(7, ll.peek())
self.assertEqual("7->8->9", str(ll))
rev = ll.reversed()
self.assertEqual("9->8->7", str(rev))
self.assertEqual(9, rev.peek())
self.assertEqual(3, len(ll))
self.assertEqual(7, ll.peek())
self.assertEqual("7->8->9", str(ll))
def test_reverse(self):
ll = LinkedList(7)
ll.append(8)
ll.append(9)
self.assertEqual(3, len(ll))
#.........这里部分代码省略.........
示例4: Receiver
# 需要导入模块: from linkedlist import LinkedList [as 别名]
# 或者: from linkedlist.LinkedList import clear [as 别名]
class Receiver():
def __init__(self, packet_sender):
print 'Init Receiver'
# TODO: have this be a DuplexStream instead of an EventEmitter.
# TODO: the Receiver should never send raw packets to the end host. It should
# only be acknowledgement packets. Please see [1]
self.ee = EventEmitter()
self._synced = False
self._next_sequence_number = 0
def sort_by_sequence(packet_a, packet_b):
return packet_a.get_sequence_number() - packet_b.get_sequence_number()
self._packets = LinkedList(sort_by_sequence)
self._packet_sender = packet_sender
self._closed = False
def receive(self, packet):
if self._closed:
# Since this is closed, don't do anything.
return
# Ignores packets that have a sequence number less than the next sequence
# number
if not packet.getIsSynchronize() and packet.getSequenceNumber() < self._sync_sequence_number:
return
if packet.getIsSynchronize() and not self._synced:
# This is the beginning of the stream.
if packet.getSequenceNumber() is self._sync_sequence_number:
self._packet_sender.send(Packet.createAcknowledgementPacket(packet.getSequenceNumber()))
return
# Send the packet upstream, send acknowledgement packet to end host, and
# increment the next expected packet.
self._packets.clear()
self.ee.emit('data', packet.getPayload())
self._packet_sender.send(Packet.createAcknowledgementPacket(packet.getSequenceNumber()))
self._packets.insert(packet)
self._next_sequence_number = packet.getSequenceNumber() + 1
self._synced = True
self._sync_sequence_number = packet.getSequenceNumber()
if packet.getIsReset():
self.ee.emit('_reset')
self._synced = False
# We're done.
return
elif (packet.getIsReset()):
self.ee.emit('_reset')
self.ee._synced = False
elif not self._synced:
# If we are not synchronized with sender, then this means that we should
# wait for the end host to send a synchronization packet.
# We are done.
return
elif packet.getSequenceNumber() < self._syncSequenceNumber:
# This is a troll packet. Ignore it.
return
elif packet.getSequenceNumber() >= (self._packets.currentValue().getSequenceNumber() + constants.WINDOW_SIZE):
# This means that the next packet received is not within the window size.
self.ee.emit('_window_size_exceeded')
return
# This means that we should simply insert the packet. If the packet's
# sequence number is the one that we were expecting, then send it upstream,
# acknowledge the packet, and increment the next expected sequence number.
#
# Once acknowledged, check to see if there aren't any more pending packets
# after the current packet. If there are, then check to see if the next
# packet is the expected packet number. If it is, then start the
# acknowledgement process anew.
result = self._packets.insert(packet)
if result is LinkedList.InsertionResult.INSERTED:
self._pushIfExpectedSequence(packet)
elif result is LinkedList.InsertionResult.EXISTS:
self._packet_sender.send(Packet.createAcknowledgementPacket(packet.getSequenceNumber()))
def _pushIfExpectedSequence(self, packet):
if packet.get_sequence_number() is self._next_sequence_number:
self.ee.emit('data', packet.getPayload())
# [1] Never send packets directly!
self._packet_sender.send(Packet.createAcknowledgementPacket(packet.getSequenceNumber()))
self._nextSequenceNumber += 1
self._packets.seek()
#.........这里部分代码省略.........