its mandatory to call deleteDialog for the pip screen
[enigma2-plugins.git] / epgrefresh / src / OrderedSet.py
1 # Python OrderedSet implementation
2 # taken from http://code.activestate.com/recipes/576694-orderedset/
3 from __future__ import print_function
4 import collections
5
6 KEY, PREV, NEXT = range(3)
7
8 class OrderedSet(collections.MutableSet):
9
10     def __init__(self, iterable=None):
11         self.end = end = [] 
12         end += [None, end, end]         # sentinel node for doubly linked list
13         self.map = {}                   # key --> [key, prev, next]
14         if iterable is not None:
15             self |= iterable
16
17     def __len__(self):
18         return len(self.map)
19
20     def __contains__(self, key):
21         return key in self.map
22
23     def add(self, key):
24         if key not in self.map:
25             end = self.end
26             curr = end[PREV]
27             curr[NEXT] = end[PREV] = self.map[key] = [key, curr, end]
28
29     def discard(self, key):
30         if key in self.map:        
31             key, prev, next = self.map.pop(key)
32             prev[NEXT] = next
33             next[PREV] = prev
34
35     def __iter__(self):
36         end = self.end
37         curr = end[NEXT]
38         while curr is not end:
39             yield curr[KEY]
40             curr = curr[NEXT]
41
42     def __reversed__(self):
43         end = self.end
44         curr = end[PREV]
45         while curr is not end:
46             yield curr[KEY]
47             curr = curr[PREV]
48
49     def pop(self, last=True):
50         if not self:
51             raise KeyError('set is empty')
52         key = next(reversed(self)) if last else next(iter(self))
53         self.discard(key)
54         return key
55
56     def __repr__(self):
57         if not self:
58             return '%s()' % (self.__class__.__name__,)
59         return '%s(%r)' % (self.__class__.__name__, list(self))
60
61     def __eq__(self, other):
62         if isinstance(other, OrderedSet):
63             return len(self) == len(other) and list(self) == list(other)
64         return set(self) == set(other)
65
66     def __del__(self):
67         self.clear()                    # remove circular references
68
69     union = lambda self, other: self.__or__(other)
70
71 if __name__ == '__main__':
72     print(OrderedSet('abracadaba'))
73     print(OrderedSet('simsalabim'))
74     print(OrderedSet('asdf').union('moep'))