00001 from __future__ import with_statement 00002 import threading 00003 00004 class Multiset: 00005 def __init__(self): 00006 self.elements = {} 00007 self.mutex = threading.Lock() 00008 self.__iter__ = self.elements.iterkeys 00009 self.__contains__ = self.elements.__contains__ 00010 00011 def add(self, k): 00012 with self.mutex: 00013 if k in self.elements: 00014 self.elements[k] += 1 00015 else: 00016 self.elements[k] = 1 00017 00018 def remove(self, k): 00019 with self.mutex: 00020 if self.elements[k] > 0: 00021 self.elements[k] -= 1 00022 else: 00023 del self.elements[k]