-
Notifications
You must be signed in to change notification settings - Fork 267
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
__all__ = [ | ||
'Multiset' | ||
] | ||
|
||
|
||
class Multiset: | ||
def __init__(self, *args): | ||
# TODO: Implement dict in pydatastructs | ||
self.counter = dict() | ||
from pydatastructs.trees import RedBlackTree | ||
self.tree = RedBlackTree() | ||
self._n = 0 | ||
for arg in args: | ||
self.add(arg) | ||
|
||
def add(self, element): | ||
self.counter[element] = self.counter.get(element, 0) + 1 | ||
self._n += 1 | ||
if self.counter[element] == 1: | ||
self.tree.insert(element) | ||
|
||
def remove(self, element): | ||
if self.counter[element] == 1: | ||
self.tree.delete(element) | ||
if self.counter.get(element, 0) > 0: | ||
self._n -= 1 | ||
self.counter[element] -= 1 | ||
|
||
def lower_bound(self, element): | ||
return self.tree.lower_bound(element) | ||
|
||
def upper_bound(self, element): | ||
return self.tree.upper_bound(element) | ||
|
||
def __contains__(self, element): | ||
return self.counter.get(element, 0) > 0 | ||
|
||
def __len__(self): | ||
return self._n | ||
|
||
def count(self, element): | ||
return self.counter.get(element, 0) |
39 changes: 39 additions & 0 deletions
39
pydatastructs/miscellaneous_data_structures/tests/test_multiset.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from pydatastructs.miscellaneous_data_structures import Multiset | ||
|
||
def test_Multiset(): | ||
|
||
ms = Multiset() | ||
ms.add(5) | ||
ms.add(5) | ||
ms.add(3) | ||
ms.add(7) | ||
assert len(ms) == 4 | ||
assert 5 in ms | ||
assert ms.count(5) == 2 | ||
assert ms.count(3) == 1 | ||
assert ms.count(-3) == 0 | ||
assert not 4 in ms | ||
ms.remove(5) | ||
assert 5 in ms | ||
assert ms.lower_bound(5) == 5 | ||
assert ms.upper_bound(5) == 7 | ||
|
||
ms = Multiset(5, 3, 7, 2) | ||
|
||
assert len(ms) == 4 | ||
assert 5 in ms | ||
assert ms.count(7) == 1 | ||
assert not 4 in ms | ||
assert ms.lower_bound(3) == 3 | ||
assert ms.upper_bound(3) == 5 | ||
assert ms.upper_bound(7) is None | ||
|
||
ms.remove(5) | ||
|
||
assert len(ms) == 3 | ||
assert not 5 in ms | ||
|
||
ms.add(4) | ||
|
||
assert 4 in ms | ||
assert len(ms) == 4 |