git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@11378 ddd99763-3ecb-0310-9145-efcb8ce7c51ftags/bxinterval-return
@@ -249,6 +249,7 @@ class IntervalSet(object): | |||||
return out | return out | ||||
def intersection(self, interval): | def intersection(self, interval): | ||||
### PROBABLY WRONG | |||||
""" | """ | ||||
Compute a sequence of intervals that correspond to the | Compute a sequence of intervals that correspond to the | ||||
intersection between `self` and the provided interval. | intersection between `self` and the provided interval. | ||||
@@ -274,6 +275,7 @@ class IntervalSet(object): | |||||
yield subset | yield subset | ||||
def intersects(self, other): | def intersects(self, other): | ||||
### PROBABLY WRONG | |||||
"""Return True if this IntervalSet intersects another interval""" | """Return True if this IntervalSet intersects another interval""" | ||||
node = self.tree.find_left(other.start, other.end) | node = self.tree.find_left(other.start, other.end) | ||||
if node is None: | if node is None: | ||||
@@ -18,8 +18,8 @@ class RBNode(object): | |||||
self.start = start | self.start = start | ||||
self.end = end | self.end = end | ||||
self.red = False | self.red = False | ||||
self.left = self | |||||
self.right = self | |||||
self.left = None | |||||
self.right = None | |||||
def __str__(self): | def __str__(self): | ||||
if self.red: | if self.red: | ||||
@@ -164,7 +164,9 @@ class RBTree(object): | |||||
# Deletion | # Deletion | ||||
def delete(self, z): | def delete(self, z): | ||||
"""Delete RBNode z from RBTree and rebalance as necessary""" | |||||
if z.left is None or z.right is None: | |||||
raise AttributeError("you can only delete a node object " | |||||
+ "from the tree; use find() to get one") | |||||
if z.left is self.nil or z.right is self.nil: | if z.left is self.nil or z.right is self.nil: | ||||
y = z | y = z | ||||
else: | else: | ||||
@@ -12,8 +12,8 @@ stop= | |||||
verbosity=2 | verbosity=2 | ||||
#tests=tests/test_cmdline.py | #tests=tests/test_cmdline.py | ||||
#tests=tests/test_layout.py | #tests=tests/test_layout.py | ||||
#tests=tests/test_rbtree.py | |||||
tests=tests/test_interval.py | |||||
tests=tests/test_rbtree.py | |||||
#tests=tests/test_interval.py | |||||
#tests=tests/test_client.py | #tests=tests/test_client.py | ||||
#tests=tests/test_timestamper.py | #tests=tests/test_timestamper.py | ||||
#tests=tests/test_serializer.py | #tests=tests/test_serializer.py | ||||
@@ -21,3 +21,28 @@ class TestRBTree: | |||||
# There was a typo that gave the RBTree a loop in this case. | # There was a typo that gave the RBTree a loop in this case. | ||||
# Verify that the dot isn't too big. | # Verify that the dot isn't too big. | ||||
assert(len(s.splitlines()) < 30) | assert(len(s.splitlines()) < 30) | ||||
#@unittest.skip("needs GTK") | |||||
def test_rbtree_big(self): | |||||
import random | |||||
random.seed(1234) | |||||
# make a set of 500 intervals | |||||
rb = RBTree() | |||||
j = 500 | |||||
for i in random.sample(xrange(j),j): | |||||
print "inserting", i | |||||
rb.insert(RBNode(None, i, i+1)) | |||||
# show the graph | |||||
rb.render_dot_live("after insert") | |||||
# remove about half of them | |||||
for i in random.sample(xrange(j),j): | |||||
if random.randint(0,1): | |||||
print "deleting", i | |||||
if i == 300: | |||||
rb.render_dot_live("before deleting 300") | |||||
rb.delete(rb.find(i, i+1)) | |||||
# show the graph | |||||
rb.render_dot_live("after delete") |