source: utilitiespy/tudelft/utilities/immutablelist/Outer.py@ 1031

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

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

File size: 1.5 KB
RevLine 
[285]1from typing import TypeVar, List
2
3from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
4from tudelft.utilities.immutablelist.FixedList import FixedList
5from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
6
7
8T = TypeVar('T')
9
10class Outer (AbstractImmutableList[ImmutableList[T]]) :
11 '''
12 outer product of lists: a list containing all possible combinations of one
13 element from each of the provided lists.
14
15 @param T the element type of all lists that we receive.
16 '''
17
18 def __init__(self, lists: List[ImmutableList[T]] ):
19 '''
20 outer product of lists: a list containing all possible combinations of
21 one element from each of the provided lists
22
23 @param lists
24 '''
25 self._sourceLists = list(lists)
26
27 if len(lists) == 0:
28 s = 0
29 else:
30 s = 1
31 for l in lists:
32 s = s * l.size()
33 self._size = s;
34
35 def get(self, index:int) ->ImmutableList[T]:
36 element:List[T] = [];
37 i = index;
38 for item in range(len(self._sourceLists)):
39 l:ImmutableList[T] = self._sourceLists[item]
40 n = l.size()
41 element.append(l.get(i % n))
42 i = i // n
43 return FixedList(element);
44
45
46 def size(self) -> int:
47 return self._size
48
49 def __hash__(self):
50 return hash(tuple(self._sourceLists))
51
52 def __eq__(self, other):
53 return isinstance(other, self.__class__) \
54 and self._sourceLists==other._sourceLists
55
Note: See TracBrowser for help on using the repository browser.