From 9ba6df5503ab64420f09d965313d62fcc161564a Mon Sep 17 00:00:00 2001 From: Goffi Date: Mon, 8 Jun 2020 08:54:53 +0200 Subject: [PATCH] tools (common/utils): new OrderedSet class --- sat/tools/common/utils.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/sat/tools/common/utils.py b/sat/tools/common/utils.py index a7e4054a..aa7a2dbe 100644 --- a/sat/tools/common/utils.py +++ b/sat/tools/common/utils.py @@ -44,3 +44,32 @@ def recursive_update(ori: dict, update: dict): 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})