source: utilitiespy/tudelft/utilities/immutablelist/JoinedList.py@ 1310

Last change on this file since 1310 was 314, checked in by wouter, 3 years ago

#105 added JoinedList, MapList, Tuple, release 1.0.5

File size: 1.3 KB
Line 
1from typing import TypeVar, Generic, List
2from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
3from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
4
5E = TypeVar('E')
6
7class JoinedList(Generic[E], AbstractImmutableList[E]):
8 '''
9 Creates conjunction of lists. Eg Join of [1,2],[3,4],[5] -> [1,2,3,4,5]
10
11 @param <E> type of the elements
12 '''
13
14 def __init__(self, items: List[ImmutableList[E]]):
15 self._lists = items.copy()
16 self._size= sum([l.size() for l in items ] )
17
18
19 #Override
20 def get(self , index:int) ->E :
21 remaining = index
22
23 for list in self._lists:
24 s = list.size()
25 if remaining < s:
26 return list.get(remaining)
27 remaining = remaining-s
28 raise IndexError("" + str(index))
29
30 def With( self, list:ImmutableList[E]) -> "JoinedList[E]" :
31 '''
32 @param list the list to add
33 @return new JoinedList with the given list added at end of existing
34 lists.
35 '''
36 newlists = self._lists.copy()
37 newlists.append(list)
38 return JoinedList(newlists)
39
40 #Override
41 def size(self) ->int:
42 return self._size
43
44 #@Override
45 def __hash__(self):
46 return hash( (self._size, tuple(self._lists)))
47
48 #Override
49 def __eq__(self, other):
50 return isinstance(other, self.__class__) \
51 and self._lists == other._lists
52
Note: See TracBrowser for help on using the repository browser.