This function returns an array, which i-th element is a list, that
contains all values associated with i in the list of associations, in that order.
In other words, the result of lArray bnds assoc is
lArray bnds assoc = array bnds [(i,[e|(i',e)<-assoc,i'==i] | i<-range bnds].
The important difference between the two sides of previous equation is that
lArray works in linear time, ie. each member in the assoc list is accessed
exactly once, and lArray is lazy in its second argument. It means the assoc
list is left intact until an i-th element of the resulting array needs to be evaluated.
At that time the assoc list is examined until the first pair (i,e') is found.
Until such pair is found, processed elements of assoc are stored at appropriate indexes
in the resulting array.
The implementation uses temporary array for such processed elements of assoc list,
that were not yet 'requested' [or 'evaluated'] in the resulting array. This array
can be freed when all lists of the resulting array are fully evaluated.
Here is an example of how to use the monolithic lazy array. Given a graph of type
Array Int [Int] we construct the dfs numbering from a given vertex.
Obviously, only the first element of the lArray is needed - lArrayFirst,
lArrayMaybe or Data.LazyArray.Lowlevel are better choice here.
dnum::Array Int [Int]->Int->Array Int (Maybe Int)
dnum g s = amap listToMaybe marks where
list = dfs' [s] 0
marks = lArray (bounds g) list
dfs' [] _ = []
dfs' (s:ss) n = (s,n) : if n == head (marks!s) then dfs' ((g!s)++ss) (n+1) else dfs' ss n
|