-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathosets.py
170 lines (134 loc) · 4.68 KB
/
osets.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# -*- coding: UTF-8 -*-
"""
This is part of Yappy
osets.py -- a Set private implementation
Copyright (C) 2000-2003 Rogério Reis & Nelma Moreira {rvr,nam}@ncc.up.pt
Version: $Id: osets.py,v 1.3 2004/02/18 10:54:48 rvr Exp $
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
@author: Rogério Reis & Nelma Moreira {rvr,nam}@ncc.up.pt
(several changes by César García Osorio cgosorio@ubu.es)
"""
class Set(object):
""" Sets: that is lists without order or repetition.
May be used everywhere lists are used... because they rely on them."""
def __init__(self, a_list=None):
new_list = []
if a_list:
for item in a_list:
if item not in new_list:
new_list.append(item)
self.members = new_list
def __getitem__(self, index):
return self.members[index]
def __setitem__(self, index, value):
self.members[index] = value
def __getattr__(self, name):
return getattr(self.members, name)
def __add__(self, other):
new = Set(self.members[:])
for val in other:
if val not in new:
new.append(val)
return new
def __iadd__(self, other):
return self + other
def __radd__(self, other):
return self + other
def __sub__(self, other):
new = Set(self.members[:])
for val in other:
try:
del new.members[new.index(val)]
except ValueError:
continue
return new
def __cmp__(self, other):
if len(self) == len(other):
if not len(self - other):
return 0
return 1
def __len__(self):
return len(self.members)
def __str__(self):
return str(self.members)
def __repr__(self):
return "Set %s" % str(self.members)
def __getslice__(self, low, high):
return Set(self.members[low:high])
def __delslice__(self, low, high):
for i in range(low, max(high+1, len(self.members)-1)):
del self.members[i]
def __delitem__(self, key):
del self.members[key]
def append(self, member):
"Append an element if it not already in the set"
if member not in self.members:
self.members.append(member)
def s_append(self, member):
"Append an element if it not already in the set, returning 1 if appended"
exit_value = 0
if member not in self.members:
self.members.append(member)
exit_value = 1
return exit_value
def empty(self):
"Check if the set is empty"
return len(self.members) == 0
def s_extend(self, other):
"Extend set and return 1 if extended"
exit_value = 0
for val in other:
if val not in self:
self.members.append(val)
exit_value = 1
return exit_value
def sort(self):
"Sort element in the set"
self.members.sort()
def index(self, index):
"Get the index of an element in the set"
return self.members.index(index)
def remove(self, val):
"Remove an element from the set"
try:
del self.members[self.index(val)]
except ValueError:
pass
def copy(self):
"Make a copy of the set"
return Set(self.members[:])
def first(self):
"Return first element in the set"
return self.members[0]
def dup(self):
"Duplicates a set (shallow copy)"
new = Set()
new.members = self.members[:]
return new
def __eq__(self, other):
if isinstance(other, list):
return set(self.members) == set(other)
elif isinstance(other, Set):
return set(self.members) == set(other.members)
else:
raise TypeError("Incompatible type for Set comparison")
if __name__ == '__main__':
oset1 = Set()
oset1.append(1)
oset1.append(2)
oset2 = Set()
oset2.append(2)
oset2.append(1)
print(oset1 == oset2)
print(oset1 == [1, 2])
# print(oset1 == 2) # This raise an exception