Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some performance improvements to Poset #22

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions grandalf/routing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ def setpath(self, pts):
# tail and head.
def route_with_lines(e, pts):
assert hasattr(e, "view")
tail_pos = intersectR(e.v[0].view, topt=pts[1])
head_pos = intersectR(e.v[1].view, topt=pts[-2])
try:
tail_pos = intersectR(e.v[0].view, topt=pts[1])
except ValueError:
# TODO: Create a test case to reproduce it and submit upstream.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a note, I'm having this error on some models... if you want I can remove it from the pull request.

tail_pos = pts[1]
try:
head_pos = intersectR(e.v[1].view, topt=pts[-2])
except ValueError:
# TODO: Create a test case to reproduce it and submit upstream.
head_pos = pts[-2]
pts[0] = tail_pos
pts[-1] = head_pos
e.view.head_angle = getangle(pts[-2], pts[-1])
Expand Down
39 changes: 19 additions & 20 deletions grandalf/utils/poset.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
class Poset(object):
def __init__(self, L):
self.o = OrderedDict()
self._unordered = set()
for obj in L:
self.add(obj)

Expand All @@ -32,12 +33,14 @@ def add(self, obj):
if obj in self:
return self.get(obj)
else:
self._unordered.add(obj)
self.o[obj] = obj
return obj

def remove(self, obj):
if obj in self:
if obj in self._unordered:
obj = self.get(obj)
self._unordered.discard(obj)
del self.o[obj]
return obj
return None
Expand All @@ -59,19 +62,15 @@ def __iter__(self):
yield obj

def __cmp__(self, other):
s1 = set(other.o.values())
s2 = set(self.o.values())
s1 = other._unordered
s2 = self._unordered
return cmp(s1, s2)

def __eq__(self, other):
s1 = set(other.o.values())
s2 = set(self.o.values())
return s1 == s2
return self._unordered == other._unordered

def __ne__(self, other):
s1 = set(other.o.values())
s2 = set(self.o.values())
return s1 != s2
return not self == other

def copy(self):
return Poset(self.o.values())
Expand All @@ -97,8 +96,8 @@ def update(self, other):
self.o.update(other.o)

def __and__(self, other):
s1 = set(self.o.values())
s2 = set(other.o.values())
s1 = self._unordered
s2 = other._unordered
return Poset(s1.intersection(s2))

def intersection(self, *args):
Expand All @@ -108,8 +107,8 @@ def intersection(self, *args):
return p

def __xor__(self, other):
s1 = set(self.o.values())
s2 = set(other.o.values())
s1 = self._unordered
s2 = other._unordered
return Poset(s1.symmetric_difference(s2))

def symmetric_difference(self, *args):
Expand All @@ -119,8 +118,8 @@ def symmetric_difference(self, *args):
return p

def __sub__(self, other):
s1 = set(self.o.values())
s2 = set(other.o.values())
s1 = self._unordered
s2 = other._unordered
return Poset(s1.difference(s2))

def difference(self, *args):
Expand All @@ -133,16 +132,16 @@ def __contains__(self, obj):
return obj in self.o

def contains__cmp__(self, obj):
return obj in self.o.values()
return obj in self._unordered

def issubset(self, other):
s1 = set(self.o.values())
s2 = set(other.o.values())
s1 = self._unordered
s2 = other._unordered
return s1.issubset(s2)

def issuperset(self, other):
s1 = set(self.o.values())
s2 = set(other.o.values())
s1 = self._unordered
s2 = other._unordered
return s1.issuperset(s2)

__le__ = issubset
Expand Down
51 changes: 51 additions & 0 deletions tests/test_poset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
from grandalf.utils.poset import Poset


def test_poset():
import pytest
poset = Poset(())
poset.add(3)
poset.add(1)
poset.add(2)

assert list(iter(poset)) == [3, 1, 2]

poset.remove(1)
assert list(iter(poset)) == [3, 2]
assert str(poset) == '0.| 3\n1.| 2'.replace('\r\n', '\n').replace('\r', '\n')

poset.add(4)
assert list(iter(poset)) == [3, 2, 4]

assert poset.index(2) == 1

assert poset.get(2) == 2
assert poset.get(5) is None

assert poset[0] == 3
assert poset[1] == 2

with pytest.raises(IndexError):
poset[22]

assert len(poset) == 3

cp = poset.copy()
assert cp == poset
assert list(iter(cp)) == [3, 2, 4]

cp.remove(3)
assert cp != poset
cp.add(3)

# A bit weird: equality doesn't depend on order (eq considers it unsorted).
assert cp == poset
assert not cp != poset

cp.add(17)
cp.add(9)
assert list(iter(cp)) == [2, 4, 3, 17, 9]
assert list(iter(poset)) == [3, 2, 4]

poset.update(cp)
assert list(iter(poset)) == [3, 2, 4, 17, 9]