Skip to content
Snippets Groups Projects
Commit 1bccad0b authored by Chris's avatar Chris
Browse files

Update BSet.py for first performance improvement.

parent 3d864091
No related branches found
No related tags found
No related merge requests found
...@@ -12,20 +12,15 @@ class BSet: ...@@ -12,20 +12,15 @@ class BSet:
if len(args) == 1 and type(args[0]) is immutables.Map: if len(args) == 1 and type(args[0]) is immutables.Map:
self.__set = args[0] self.__set = args[0]
else: else:
_set = {x: x for x in args} self.__set = immutables.Map({x: x for x in args})
self.__set = immutables.Map(_set)
def __str__(self) -> 'str': def __str__(self) -> 'str':
return '{' + ', '.join([str(x) for x in self.__set]) + '}' return '{' + ', '.join([str(x) for x in self.__set]) + '}'
def __eq__(self, other): def __eq__(self, other):
if self is other: if not isinstance(other, BSet):
return True
if other is None or type(self) != type(other):
return False
if not self.__set == other.__set:
return False return False
return True return self.__set == other.__set
def __ne__(self, other): def __ne__(self, other):
return not self.__eq__(other) return not self.__eq__(other)
...@@ -33,14 +28,17 @@ class BSet: ...@@ -33,14 +28,17 @@ class BSet:
def __len__(self): def __len__(self):
return len(self.__set) return len(self.__set)
def __contains__(self, item):
return item in self.__set
def union(self, other=None): def union(self, other=None):
if other is None: if other is None:
if len(self.__set) == 0: if len(self.__set) == 0:
return BSet() return BSet()
elif type(next(iter(self.__set))) == BSet: elif isinstance(next(iter(self.__set)), BSet):
return reduce(lambda a, e: a.update(e), self, BSet()) return BSet(reduce(lambda a, e: a.update(e.getSet()), self, immutables.Map()))
elif type(next(iter(self.__set))) == BRelation: elif isinstance(next(iter(self.__set)), BRelation):
return reduce(lambda a, e: a.update(e), self, BRelation()) return reduce(lambda a, e: a.union(e), self, BRelation())
return BSet(self.__set.update(other.getSet())) return BSet(self.__set.update(other.getSet()))
...@@ -48,22 +46,20 @@ class BSet: ...@@ -48,22 +46,20 @@ class BSet:
if other is None: if other is None:
if len(self.__set) == 0: if len(self.__set) == 0:
return BSet() return BSet()
elif type(next(iter(self.__set))) == BSet: elif isinstance(next(iter(self.__set)), BSet):
return reduce(lambda a, e: a.intersect(e), self, BSet()) return reduce(lambda a, e: a.intersect(e), self, BSet())
elif type(next(iter(self.__set))) == BRelation: elif isinstance(next(iter(self.__set)), BRelation):
return reduce(lambda a, e: a.intersect(e), self, BRelation()) return reduce(lambda a, e: a.intersect(e), self, BRelation())
_set = self.__set _set = self.__set
for element in _set: other_set = other.__set
if not element in other.__set: _set = reduce(lambda current_set, el: current_set.delete(el) if el not in other_set else current_set, _set, _set)
_set = _set.delete(element)
return BSet(_set) return BSet(_set)
def difference(self, other: 'BSet') -> 'BSet': def difference(self, other: 'BSet') -> 'BSet':
_set = self.__set _set = self.__set
for element in other.__set: other_set = other.__set
if element in _set: _set = reduce(lambda current_set, el: current_set.delete(el) if el in _set else current_set, other_set, _set)
_set = _set.delete(element)
return BSet(_set) return BSet(_set)
def card(self) -> 'BInteger': def card(self) -> 'BInteger':
...@@ -246,7 +242,7 @@ class BSet: ...@@ -246,7 +242,7 @@ class BSet:
@staticmethod @staticmethod
def interval(a: 'BInteger', b: 'BInteger') -> 'BSet': def interval(a: 'BInteger', b: 'BInteger') -> 'BSet':
r = list(map(BInteger, range(a.intValue(), b.intValue() + 1))) r = map(BInteger, range(a.intValue(), b.intValue() + 1))
return BSet(*r) return BSet(*r)
def __hash__(self): def __hash__(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment