source: utilitiespy/tudelft/utilities/immutablelist/Repeat.py@ 1357

Last change on this file since 1357 was 1346, checked in by wouter, 9 days ago

#407 added Repeat to test.

File size: 1.4 KB
Line 
1from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList
2from typing import TypeVar
3from tudelft.utilities.immutablelist.ImmutableList import ImmutableList
4
5E = TypeVar('E')
6class Repeat (AbstractImmutableList[E]):
7 '''
8 Creates repeated list.
9
10 @param <E> type of the elements
11 '''
12
13 def __init__(self,lst: ImmutableList[E] , n:int, isHeadToTail:bool):
14 '''
15 A n-time repeat of another list.
16
17 @param list the list to be repeated
18 @param n the number of repeats of the list
19 @param isHeadToTail suppose list=[1,2,3] and n=2. if true, the lists are
20 repeated head to tail, eg [1,2,3,1,2,3]. if false,
21 the lists are repeated per-element, eg [1,1,2,2,3,3].
22 '''
23 if lst == None or n == None:
24 raise ValueError("list and n must be not null")
25 self.__lst:ImmutableList[E] = lst
26 self.__n:int = n
27 self.__isHeadToTail:bool = isHeadToTail
28 self.__listsize:int = lst.size()
29 self.__fullsize:int = self.__listsize * n
30
31 #Override
32 def get(self, index:int) ->E:
33 if index >= self.__fullsize:
34 raise IndexError("Requested index " + str(index) + " > list size")
35 if self.__isHeadToTail:
36 return self.__lst.get(index % self.__listsize)
37 else:
38 # divide returns floor(a/b)
39 return self.__lst.get(index // self.__n)
40
41 #Override
42 def size(self) ->int:
43 return self.__fullsize
44
Note: See TracBrowser for help on using the repository browser.