source: utilitiespy/tudelft/utilities/immutablelist/ArrayListSet.py@ 730

Last change on this file since 730 was 695, checked in by wouter, 12 months ago

add->append (misses in unit test...)

File size: 1.1 KB
RevLine 
[694]1from typing import TypeVar, Generic, List
2
3E = TypeVar('E')
4
5class ArrayListSet(Generic[E],List[E]):
6
7 def __hash__(self):
8 hashCode = 1
9 for e in self:
10 hashCode += 0 if e == None else hash(e)
11 return hashCode
12
13 def __eq__(self, o) -> bool:
14 if not isinstance(o, ArrayListSet):
15 return False
16
17 list2:ArrayListSet = o
18 if len(self) != len(list2):
19 return False
20
21 for e1 in self:
22 if not e1 in list2:
23 return False
24
25 return True
26
27 # we can't declare index:int , python says mismatch with superclass!
28 def __setitem__(self, index, element):
29 if element in self:
30 self.remove(element)
31 return super().__setitem__(index, element)
32
33 def append(self, e) :
34 if e in self:
35 return False
36 return super().append(e)
37
38 def insert(self, index, element):
39 if element in self:
40 return;
41 super().insert(index, element)
42
43 def extend(self, c) :
44 for e in c:
[695]45 self.append(c)
[694]46
Note: See TracBrowser for help on using the repository browser.