diff options
author | Dylan Baker <dylan@pnwbakers.com> | 2017-04-27 11:50:30 -0700 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2017-04-28 17:25:13 +0300 |
commit | 4f7723649ab01f17311b50817577479b33e7c763 (patch) | |
tree | 2e60b944de13cf08e16ef23044199ed20eb0d85d /mesonbuild/mesonlib.py | |
parent | f74f78dd89fb94923c5888246e319330bcb412a1 (diff) | |
download | meson-4f7723649ab01f17311b50817577479b33e7c763.zip meson-4f7723649ab01f17311b50817577479b33e7c763.tar.gz meson-4f7723649ab01f17311b50817577479b33e7c763.tar.bz2 |
Implement an actual set interface for the OrderedSet class. Closes #1670
This uses the ABC's in collections to implement an OrderedSet class.
Internally an OrderedDict is still wrapped so that the ordering is
maintained, this adds the full interface and behavior of an Set, but
with ordering by first insertion.
Diffstat (limited to 'mesonbuild/mesonlib.py')
-rw-r--r-- | mesonbuild/mesonlib.py | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/mesonbuild/mesonlib.py b/mesonbuild/mesonlib.py index ae2f88c..0263768 100644 --- a/mesonbuild/mesonlib.py +++ b/mesonbuild/mesonlib.py @@ -687,17 +687,37 @@ def get_filenames_templates_dict(inputs, outputs): values['@OUTDIR@'] = '.' return values -class OrderedSet(collections.OrderedDict): - ''' - A 'set' equivalent that preserves the order in which items are added. - - This is a hack implementation that wraps OrderedDict. It may not be the - most efficient solution and might need fixing to override more methods. - ''' +class OrderedSet(collections.MutableSet): + """A set that preserves the order in which items are added, by first + insertion. + """ def __init__(self, iterable=None): + self.__container = collections.OrderedDict() if iterable: self.update(iterable) + def __contains__(self, value): + return value in self.__container + + def __iter__(self): + return iter(self.__container.keys()) + + def __len__(self): + return len(self.__container) + + def __repr__(self): + # Don't print 'OrderedSet("")' for an empty set. + if self.__container: + return 'OrderedSet("{}")'.format('", "'.join(self.__container.keys())) + return 'OrderedSet()' + + def add(self, value): + self.__container[value] = None + + def discard(self, value): + if value in self.__container: + del self.__container[value] + def update(self, iterable): for item in iterable: - self[item] = True + self.__container[item] = None |