# HG changeset patch # User Goffi # Date 1591599293 -7200 # Node ID 84f77f04aa088e02a90a1d5220328342babdf00a # Parent 449dfbfcdbcc7d1592090eaafd256ae236e9f9dc tools (common/utils): new OrderedSet class diff -r 449dfbfcdbcc -r 84f77f04aa08 sat/tools/common/utils.py --- a/sat/tools/common/utils.py Mon Jun 01 11:15:16 2020 +0200 +++ b/sat/tools/common/utils.py Mon Jun 08 08:54:53 2020 +0200 @@ -44,3 +44,32 @@ ori[k] = v return ori +class OrderedSet(collections.abc.MutableSet): + """A mutable sequence which doesn't keep duplicates""" + # TODO: complete missing set methods + + def __init__(self, values=None): + self._dict = {} + if values is not None: + self.update(values) + + def __len__(self): + return len(self._dict) + + def __iter__(self): + return iter(self._dict.keys()) + + def __contains__(self, item): + return item in self._dict + + def add(self, item): + self._dict[item] = None + + def discard(self, item): + try: + del self._dict[item] + except KeyError: + pass + + def update(self, items): + self._dict.update({i: None for i in items})