source: utilitiespy/tudelft/utilities/immutablelist/Tuples.py@ 1269

Last change on this file since 1269 was 1169, checked in by wouter, 3 months ago

#368 utilitiespy is now py.typed

File size: 1.6 KB
Line 
1from typing import TypeVar
2from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
3from tudelft.utilities.immutablelist.Tuple import Tuple
4from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
5
6
7T1 = TypeVar('T1')
8T2 = TypeVar('T2')
9
10class Tuples(AbstractImmutableList[Tuple[T1,T2]]):
11 '''
12 Generate list of Tuple[T1, T2] with all combinations of 1 element from list1
13 and one from list2. Sometimes called "cartesian product". You can use this
14 recursively to generate bigger products; but you can also use {@link Outer}.
15 @param <T1> type of the first element of the tuple
16 @param <T2> type of the second element of the tuple
17 @param <T> type of the elements
18 '''
19
20 def __init__(self, list1:ImmutableList[T1],
21 list2:ImmutableList[T2] ):
22 '''
23 contains all possible tuples with first element from list1 and second
24 from list2
25
26 @param list1 first element list
27 @param list2 second element list
28 '''
29
30 self._list1 = list1
31 self._list2 = list2
32 self._size = list1.size()* list2.size()
33
34 def get(self, index:int) -> Tuple[T1, T2] :
35 indices = divmod(index,self._list1.size())
36 return Tuple(self._list1.get(indices[1]), self._list2.get(indices[0]))
37
38 def size(self) ->int:
39 return self._size
40
41
42 def __hash__(self):
43 return hash((self._list1, self._list2))
44
45 def __eq__(self, other):
46 return isinstance(other, self.__class__) \
47 and self._list1==other._list1\
48 and self._list2==other._list2
Note: See TracBrowser for help on using the repository browser.