Skip to content

Commit

Permalink
Implement iterator on nodes by level
Browse files Browse the repository at this point in the history
  • Loading branch information
azhukov committed Sep 28, 2015
1 parent 062b2e2 commit 52005e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
11 changes: 11 additions & 0 deletions skiplist-py/iterators.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class LevelNodeIterator(object):
def __init__(self, skip_list, level=0):
self.s = skip_list
self.l = level

def __iter__(self):
node = self.s.head.next[self.l]
while node.next:
yield node
node = node.next[self.l]
raise StopIteration
9 changes: 9 additions & 0 deletions skiplist-py/tests.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import unittest

from skiplist import Skiplist, NIL
from iterators import LevelNodeIterator


class DataStructTestCase(unittest.TestCase):
Expand Down Expand Up @@ -42,5 +43,13 @@ def test_str(self):
self.assertEqual('skiplist([])', str(sl))
sl['1'] = 1
self.assertEqual('skiplist([\'1\'])', str(sl))


class LevelIteratorTestCase(unittest.TestCase):
def test_iterator_default(self):
s = Skiplist(foo=1, bar=2)
self.assertListEqual(sorted(['foo', 'bar']), sorted(node.key for node in LevelNodeIterator(s)))


if __name__ == '__main__':
unittest.main()

0 comments on commit 52005e9

Please sign in to comment.