annotate resfinder/cge/output/orderedset.py @ 0:a16d245332d6 draft default tip

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