{-# LANGUAGE Safe #-}{-# LANGUAGE Strict #-}--------------------------------------------------------------------------------moduleData.IArray(IArray,add,drop,empty,head,idx,init,size,tail,toList)where--------------------------------------------------------------------------------importPreludehiding(drop,head,init,tail)importData.Bits(Bits,FiniteBits,bitSizeMaybe,shiftL,shiftR,testBit,(.|.))importData.Maybe(catMaybes,fromMaybe)--------------------------------------------------------------------------------typeContainer=IntegertypeLength=IntegertypeBitSize=IntdataIArraya=IArrayContainerLengthBitSize--------------------------------------------------------------------------------instanceShow(IArraya)whereshow(IArray_ls)="IArray (length: "++showl++", bit size: "++shows++")"--------------------------------------------------------------------------------(.<.)::Bitsa=>a->Int->a(.>.)::Bitsa=>a->Int->aempty::(Integrala,Bitsa,FiniteBitsa)=>IArrayainit::(Integrala,Bitsa,FiniteBitsa)=>a->IArrayahead::(Integrala,Bitsa,FiniteBitsa)=>IArraya->Maybeatail::(Integrala,Bitsa,FiniteBitsa)=>IArraya->Maybe(IArraya)drop::(Integrala,Bitsa,FiniteBitsa)=>Int->IArraya->Maybe(IArraya)size::(Integrala,Bitsa,FiniteBitsa)=>IArraya->Integeridx::(Integrala,Bitsa,FiniteBitsa)=>Int->IArraya->Maybeaadd::(Integrala,Bitsa,FiniteBitsa)=>a->IArraya->IArrayatoList::(Integrala,Bitsa,FiniteBitsa)=>IArraya->[a]--------------------------------------------------------------------------------(.<.)xy=x`shiftL`y(.>.)xy=x`shiftR`y-- O(1)empty=IArray000-- O(1)initx=IArray(fromIntegralx)1(fromMaybe0$bitSizeMaybex)-- O(1)headia@(IArray_l_)=ifl==0thenNothingelseidx0ia-- O(n) as we need to re-create the underlaying Integer containertail(IArraycls)=ifl==0thenNothingelseJust$IArray(c.>.s)(l-1)s-- O(n) as we need to re-create the underlaying Integer containerdropn(IArraycls)=if0<=n'&&n'<lthenJust$IArray(c.>.(n*s))(l-n')selseNothingwheren'=fromIntegraln-- O(1)size(IArray_l_)=l-- O(1) based on the assumption that bit lookups on Integers are also O(1)idxn(IArraycls)=if0<=n&&fromIntegraln<lthenJust$auxselseNothingwhere-- 1 .<. 3 + 0 .<. 2 + 1 .<. 1 + 0 .<. 0 (bin) equals 10 (dec)aux0=bti$testBitc(n*s)auxi=(bti$testBitc(n*s+i)).<.i+aux(i-1)btiFalse=0btiTrue=1-- O(n) as we need to re-create the underlaying Integer containeraddx(IArraycls)=IArray(fromIntegralx.<.n.|.c)(l+1)bwheren=fromIntegrall*bb=if0==sthenfromMaybe0$bitSizeMaybexelses-- O(n) based on the assumption that bit lookups on Integers are also O(1)toListia@(IArray_l_)=catMaybes$aux$fromIntegerlwhereaux0=[idx0ia]auxn=idxnia:aux(n-1)