Browse Source

Deletion is still broken. F.

git-svn-id: https://bucket.mit.edu/svn/nilm/nilmdb@11378 ddd99763-3ecb-0310-9145-efcb8ce7c51f
tags/bxinterval-return
Jim Paris 11 years ago
parent
commit
6aee52d980
4 changed files with 34 additions and 5 deletions
  1. +2
    -0
      nilmdb/interval.py
  2. +5
    -3
      nilmdb/rbtree.py
  3. +2
    -2
      setup.cfg
  4. +25
    -0
      tests/test_rbtree.py

+ 2
- 0
nilmdb/interval.py View File

@@ -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:


+ 5
- 3
nilmdb/rbtree.py View File

@@ -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:


+ 2
- 2
setup.cfg View File

@@ -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


+ 25
- 0
tests/test_rbtree.py View File

@@ -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")

Loading…
Cancel
Save