0
|
1 #!/usr/bin/env python3
|
|
2 # Copyright 2015 Sander
|
|
3 # Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
4 # of this software and associated documentation files (the "Software"), to deal
|
|
5 # in the Software without restriction, including without limitation the rights
|
|
6 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
7 # copies of the Software, and to permit persons to whom the Software is
|
|
8 # furnished to do so, subject to the following conditions:
|
|
9 # The above copyright notice and this permission notice shall be included in
|
|
10 # all copies or substantial portions of the Software.
|
|
11 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
12 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
13 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
14 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
15 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
16 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
17 # SOFTWARE.
|
|
18 import collections
|
|
19
|
|
20 class OrderedSet(collections.MutableSet):
|
|
21
|
|
22 def __init__(self, iterable=None):
|
|
23 self.end = end = []
|
|
24 end += [None, end, end] # sentinel node for doubly linked list
|
|
25 self.map = {} # key --> [key, prev, next]
|
|
26 if iterable is not None:
|
|
27 self |= iterable
|
|
28
|
|
29 def __len__(self):
|
|
30 return len(self.map)
|
|
31
|
|
32 def __contains__(self, key):
|
|
33 return key in self.map
|
|
34
|
|
35 def add(self, key):
|
|
36 if key not in self.map:
|
|
37 end = self.end
|
|
38 curr = end[1]
|
|
39 curr[2] = end[1] = self.map[key] = [key, curr, end]
|
|
40
|
|
41 def discard(self, key):
|
|
42 if key in self.map:
|
|
43 key, prev, next = self.map.pop(key)
|
|
44 prev[2] = next
|
|
45 next[1] = prev
|
|
46
|
|
47 def __iter__(self):
|
|
48 end = self.end
|
|
49 curr = end[2]
|
|
50 while curr is not end:
|
|
51 yield curr[0]
|
|
52 curr = curr[2]
|
|
53
|
|
54 def __reversed__(self):
|
|
55 end = self.end
|
|
56 curr = end[1]
|
|
57 while curr is not end:
|
|
58 yield curr[0]
|
|
59 curr = curr[1]
|
|
60
|
|
61 def pop(self, last=True):
|
|
62 if not self:
|
|
63 raise KeyError('set is empty')
|
|
64 key = self.end[1][0] if last else self.end[2][0]
|
|
65 self.discard(key)
|
|
66 return key
|
|
67
|
|
68 def __repr__(self):
|
|
69 if not self:
|
|
70 return '%s()' % (self.__class__.__name__,)
|
|
71 return '%s(%r)' % (self.__class__.__name__, list(self))
|
|
72
|
|
73 def __eq__(self, other):
|
|
74 if isinstance(other, OrderedSet):
|
|
75 return len(self) == len(other) and list(self) == list(other)
|
|
76 return set(self) == set(other)
|
|
77
|
|
78
|
|
79 if __name__ == '__main__':
|
|
80 s = OrderedSet('abracadaba')
|
|
81 t = OrderedSet('simsalabim')
|
|
82 print(s | t)
|
|
83 print(s & t)
|
|
84 print(s - t)
|