import { Range } from "./range" ; import { getRandomInt } from "./rand" ; export class SegmentedRange extends Range { constructor ( start , readonly segmentLengths ) { super ( start , segmentLengths . reduce ( ( a , v ) => a + v , 0 ) ) ; } getSegmentForIndex ( index ) { let segmentEnd = 0 ; for ( let i = 0 ; i < this . segmentLengths . length ; i ++ ) { segmentEnd += this . segmentLengths [ i ] ; if ( index < segmentEnd ) { return i ; } } return - 1 ; } getRandomSegmentIndex ( ) { const rangeIndex = getRandomInt ( 0 , this . length ) ; return this . getSegmentForIndex ( rangeIndex ) ; } }