source: utilitiespy/tudelft/utilities/immutablelist/FixedList.py@ 962

Last change on this file since 962 was 285, checked in by wouter, 3 years ago

#97 added Outer and FixedList plus tests. Version now 1.0.1

File size: 1.4 KB
Line 
1from typing import TypeVar, Collection
2from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
3
4
5E = TypeVar('E')
6
7class FixedList (ImmutableList[E]):
8 '''
9 a list with a fixed (finite, typically small) number of elements.
10
11 @param <T> type of the elements
12 '''
13
14 def __init__(self, lst:Collection[E]):
15 '''
16 Copies elements of given list into an immutable list. SO the resulting list
17 is really immutable, although the components may still be mutable.
18
19 @param lst the source list.
20 '''
21 self._list = list(lst)
22
23 def FixedList(self) :
24 self._list = []
25
26
27 def __iter__(self):
28 return iter(self._list)
29
30 def get(self, index:int) -> E:
31 return self._list[index]
32
33 def size(self) ->int:
34 return len(self._list)
35
36 def __repr__(self):
37 return repr(self._list)
38
39 def __hash__(self):
40 return hash(tuple(self._list))
41
42 def __eq__(self, other):
43 return isinstance(other, self.__class__) \
44 and self._list==other._list
45
46 def contains(self, value) -> bool:
47 '''
48 @param value value to look for
49 @return Returns true iff this list contains the specified value. More
50 formally, returns true if and only if this list contains at least one
51 element e such that (o==null ? e==null : o.equals(e)).
52 '''
53 return value in self._list
54
Note: See TracBrowser for help on using the repository browser.