Last change
on this file since 1003 was 1003, checked in by wouter, 3 months ago |
#341 added Tuples. No test because there IS NO test in java. Needs fixing
|
File size:
1.3 KB
|
Rev | Line | |
---|
[1003] | 1 | from typing import TypeVar, Collection
|
---|
| 2 | from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
|
---|
| 3 | from tudelft.utilities.immutablelist.Tuple import Tuple
|
---|
| 4 |
|
---|
| 5 |
|
---|
| 6 | T1 = TypeVar('T1')
|
---|
| 7 | T2 = TypeVar('T2')
|
---|
| 8 |
|
---|
| 9 | class Tuples (ImmutableList[Tuple[T1,T2]]):
|
---|
| 10 | '''
|
---|
| 11 | Generate list of Tuple[T1, T2] with all combinations of 1 element from list1
|
---|
| 12 | and one from list2. Sometimes called "cartesian product". You can use this
|
---|
| 13 | recursively to generate bigger products; but you can also use {@link Outer}.
|
---|
| 14 | @param <T1> type of the first element of the tuple
|
---|
| 15 | @param <T2> type of the second element of the tuple
|
---|
| 16 | @param <T> type of the elements
|
---|
| 17 | '''
|
---|
| 18 |
|
---|
| 19 | def __init__(self, list1:ImmutableList[T1],
|
---|
| 20 | list2:ImmutableList[T2] ):
|
---|
| 21 | '''
|
---|
| 22 | contains all possible tuples with first element from list1 and second
|
---|
| 23 | from list2
|
---|
| 24 |
|
---|
| 25 | @param list1 first element list
|
---|
| 26 | @param list2 second element list
|
---|
| 27 | '''
|
---|
| 28 |
|
---|
| 29 | self._list1 = list1
|
---|
| 30 | self._list2 = list2
|
---|
| 31 | self._size = list1.size()* list2.size()
|
---|
| 32 |
|
---|
| 33 | def get(self, index:int) -> Tuple[T1, T2] :
|
---|
| 34 | indices = divmod(index,self.list1.size())
|
---|
| 35 | return Tuple(self.list1.get(indices[1]), self.list2.get(indices[0]))
|
---|
| 36 |
|
---|
| 37 | def size(self) ->int:
|
---|
| 38 | return self._size
|
---|
| 39 |
|
---|
| 40 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.