[1003] | 1 | from typing import TypeVar, Collection
|
---|
| 2 | from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
|
---|
| 3 | from tudelft.utilities.immutablelist.Tuple import Tuple
|
---|
[1005] | 4 | from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
|
---|
[1003] | 5 |
|
---|
| 6 |
|
---|
| 7 | T1 = TypeVar('T1')
|
---|
| 8 | T2 = TypeVar('T2')
|
---|
| 9 |
|
---|
[1005] | 10 | class Tuples(AbstractImmutableList[Tuple[T1,T2]]):
|
---|
[1003] | 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] :
|
---|
[1005] | 35 | indices = divmod(index,self._list1.size())
|
---|
| 36 | return Tuple(self._list1.get(indices[1]), self._list2.get(indices[0]))
|
---|
[1003] | 37 |
|
---|
| 38 | def size(self) ->int:
|
---|
| 39 | return self._size
|
---|
| 40 |
|
---|
| 41 |
|
---|
[1005] | 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 |
---|