-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistset.py
124 lines (120 loc) · 3.85 KB
/
listset.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
class LISTSET:
#this function is defined for y = f(x) such that x corresponds to y and vice versa in this the list doesnot have a repeated valuee
def __init__(self,domain=set()):
self.domain = domain
self.List = []
def append(self,val):
if(val in self.domain and val not in self.List):
self.List.append(val)
else:
print('append error',val)
def insert(self,val,pos):
if(val in self.domain and val not in self.List):
self.List.insert(val,pos)
else:
print('insert error',val,pos)
def __len__(self):
return len(self.List)
def __repr__(self):
return str(self.List)
def __add__(self,other):
if(other is listset):
self.domain = self.domain.union(other.domain)
self.List = self.List + other.List
else:
print('add error',other)
def remove(self,val):
if(val in self.List):
self.List.remove(val)
else:
print('remove error',val)
def pop(self,ind):
if(ind < len(self.List)):
self.List.pop(ind)
else:
print('pop error',ind)
def iter_domain(self): #it iterates over the domain of the list rather than the list itself
return self.iter('domain')
def iter_unfilled(self): #it iterates over the domain of the list rather than the list itself
return self.iter('unfilled')
def iter_List(self):
return iter(self,'List')
def iter(self,name):
it = None
if(name=='domain'):
it = self.domain
if(name=='unfilled'):
it = self.domain-set(self.List)
if(name=='List'):
it = self.List
for i in it:
yield i
class relation:
def __init__(self,X=[],Y=[]):
if(type(X) is LISTSET):
self.X = X
elif(type(X) is list):
self.X = LISTSET(X)
else:
print('wrong X',X)
if(type(Y) is LISTSET):
self.Y = Y
elif(type(Y) is list):
self.Y = LISTSET(Y)
else:
print('wrong Y',Y)
def relationVerify(self):
if(len(self.X)!=len(self.Y)):
return False
return True
def append(self,*args):
if(len(args)==2):
self.X.append(args[0])
self.Y.append(args[1])
else:
print('append arguments count mismatch')
def insert(self,*args):
if(len(args)==2):
self.X.insert(args[0])
self.Y.insert(args[1])
else:
print('insert arguments count mismatch')
def remove(self,val,tag):
if(tag=='X'):
ind = self.X.List.index(val)
self.X.remove(val)
self.Y.pop(ind)
if(tag=='Y'):
ind = self.Y.List.index(val)
self.Y.remove(val)
self.X.pop(ind)
def pop(self,ind):
self.X.pop(ind)
self.Y.pop(ind)
def __repr__(self):
r = ",".join(map(lambda a:"-".join([str(a[0]),str(a[1])]),zip(self.X.List,self.Y.List)))
return r
def iter_domain(self): #it iterates over the domain of the list rather than the list itself
return zip(self.X.iter('domain'),self.Y.iter('domain'))
def iter_unfilled(self): #it iterates over the domain of the list rather than the list itself
return zip(self.X.iter('unfilled'),self.Y.iter('unfilled'))
def iter_List(self):
return zip(self.X.iter('List'),self.Y.iter('List'))
#for testing
NUMBER_OF_CARS = 10
slots = LISTSET(set(range(NUMBER_OF_CARS)))
slots.append(4)
slots.append(5)
print(slots)
print(type(slots.iter_unfilled()))
for i in slots.iter_unfilled():
print(i)
slots.append(4)
print(slots)
slots.pop(1)
print(slots)
g = relation([1,2,3],[1,2,3])
g.append(1,1)
g.append(2,3)
for i in g.iter_List():
print(i)