From 383559e4790b7849b90a7c3abc8575ebed0386e4 Mon Sep 17 00:00:00 2001 From: Jeremy Fincher Date: Sun, 24 Oct 2004 07:34:12 +0000 Subject: [PATCH] Added MultiSet. --- src/structures.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/structures.py b/src/structures.py index 0adb71a18..1640b0b88 100644 --- a/src/structures.py +++ b/src/structures.py @@ -391,4 +391,29 @@ class TwoWayDictionary(dict): dict.__delitem__(self, value) +class MultiSet(object): + def __init__(self, seq=()): + self.d = {} + for elt in seq: + self.add(elt) + + def add(self, elt): + try: + self.d[elt] += 1 + except KeyError: + self.d[elt] = 1 + + def remove(self, elt): + self.d[elt] -= 1 + if not self.d[elt]: + del self.d[elt] + + def __getitem__(self, elt): + return self.d[elt] + + def __contains__(self, elt): + return elt in self.d + + + # vim:set shiftwidth=4 tabstop=8 expandtab textwidth=78: