from tudelft.utilities.immutablelist.AbstractImmutableList import AbstractImmutableList from typing import TypeVar from tudelft.utilities.immutablelist.ImmutableList import ImmutableList E = TypeVar('E') class Repeat (AbstractImmutableList[E]): ''' Creates repeated list. @param type of the elements ''' def __init__(self,lst: ImmutableList[E] , n:int, isHeadToTail:bool): ''' A n-time repeat of another list. @param list the list to be repeated @param n the number of repeats of the list @param isHeadToTail suppose list=[1,2,3] and n=2. if true, the lists are repeated head to tail, eg [1,2,3,1,2,3]. if false, the lists are repeated per-element, eg [1,1,2,2,3,3]. ''' if lst == None or n == None: raise ValueError("list and n must be not null") self.__lst:ImmutableList[E] = lst self.__n:int = n self.__isHeadToTail:bool = isHeadToTail self.__listsize:int = lst.size() self.__fullsize:int = self.__listsize * n #Override def get(self, index:int) ->E: if index >= self.__fullsize: raise IndexError("Requested index " + str(index) + " > list size") if self.__isHeadToTail: return self.__lst.get(index % self.__listsize) else: # divide returns floor(a/b) return self.__lst.get(index // self.__n) #Override def size(self) ->int: return self.__fullsize