This repository was archived by the owner on Oct 13, 2024. It is now read-only.
forked from rspeer/ordered-set
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_set_adapter.py
More file actions
143 lines (103 loc) · 3.55 KB
/
Copy pathordered_set_adapter.py
File metadata and controls
143 lines (103 loc) · 3.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# -*- coding:utf-8 -*-
# !/usr/bin/env python3
"""
minghu6:
Class OrderedSetAdapter can operate with Python set and return Python set.
This class operates as supplements to the OrderedSet (modified by mine)
"""
from ordered_set import OrderedSet
def _acquire_set(f):
def wrapper(this, other):
if not isinstance(other, (OrderedSetAdapter, set)):
raise TypeError(('both of operation object are expected '
'OrderedSetAdapter or Python set'))
else:
return f(this, other)
return wrapper
class OrderedSetAdapter(OrderedSet):
@_acquire_set
def __eq__(self, other):
return set(self) == set(other)
@_acquire_set
def __gt__(self, other):
return set(self) > set(other)
@_acquire_set
def __lt__(self, other):
return set(self) < set(other)
@_acquire_set
def __ge__(self, other):
return set(self) >= set(other)
@_acquire_set
def __le__(self, other):
return set(self) <= set(other)
def issubset(self, other):
return self.__le__(other)
def issuperset(self, other):
return self.__ge__(other)
@_acquire_set
def __and__(self, other):
return set(self) & set(other)
def __iand__(self, other):
self = OrderedSetAdapter(self.__and__(other))
return self
def intersection(self, *others):
res = self.__and__(others[0])
for other in others[1:]:
res = res.intersection(other)
return res
def intersection_update(self, *others):
res = self.intersection(*others)
self.clear()
self.update(res)
def isdisjoint(self, other):
if len(self.intersection(other)) == 0:
return True
else:
return False
@_acquire_set
def __or__(self, other):
return set(self).union(set(other))
def union(self, *others):
res = self.__or__(others[0])
for other in others[1:]:
res = res.union(other)
return res
@_acquire_set
def __sub__(self, other):
return set(self) - set(other)
def __isub__(self, other):
self = OrderedSetAdapter(self.__sub__(other))
return self
def difference(self, *others):
res = self.__sub__(others[0])
for other in others[1:]:
res = res.difference(other)
return res
def difference_update(self, *others):
res = self.difference(*others)
self.clear()
self.update(res)
def __ior__(self, other):
self = OrderedSetAdapter(self.__or__(other))
return self
def __rand__(self, other):
return self.__and__(other)
def __ror__(self, other):
return self.__or__(other)
@_acquire_set
def __xor__(self, other):
return set(self) ^ set(other)
def __ixor__(self, other):
self = OrderedSetAdapter(set(self) ^ set(other))
return self
@_acquire_set
def __rxor__(self, other):
return set(other) | set(self)
def __ne__(self, other):
return not self.__eq__(other)
def symmetric_difference(self, other):
return self ^ other
def symmetric_difference_update(self, other):
res = self.__xor__(other)
self.clear()
self.update(res)