Module BC_CachedArray


module BC_CachedArray: sig  end
Arrays implemented through a caching mechanism (weak pointers). Might be useful when handling data values that don't fit into memory. (USE NORMAL ARRAYS if your data is small, and take a long time to compute).

WARNING: Since values are only recomputed when necessary, and we cannot do a `dirty' check, data put in, or referenced by the cached arrays SHOULD NOT BE CHANGED.

A Weak Hash with a similar interface (automatic recalculation of the value, when it is lost) might be more useful.



type 'a t
the type of cached arrays.

val init : int -> (int -> unit -> 'a) -> 'a t
Initialize a new cached array. init n f creates a cached array of size n, where the ith element of the array will have value f i ().
val make : int -> 'a -> 'a t
make n v makes a cached array of size n filled with value v.
val get : 'a t -> int -> 'a
Get an element from the array. Recalculates the value with the associated function if necessary.
val set : 'a t -> int -> (unit -> 'a) -> unit
set arr i f sets the value at the ith position to f ().
val map : ('a -> 'b) -> 'a t -> 'b t
Map on cached arrays.
val to_array : 'a t -> 'a array
convert the cached array to a normal array.
val of_array : 'a array -> 'a t
convert the array to a cached array. The array is NOT copied, so changing the original array could result in unpredictable behavior for the cached array. This function alone won't save any memory, because we will always need to remember the original array, but it might be useful when making the cache permanent with to_array, and then caching again with of_array.
val map_to_array : ('a -> 'b) -> 'a t -> 'b array
map and then convert the results to a normal array.
val map_snapshot : ('a -> 'b) -> 'a t -> 'b t
map_snapshot f a creates a snapshot of a cached array. That is, all values of f for each element a are calculated, and then converted to a new cache array. map_snapshot f a is the same as of_array} (Array.map f (to_array a)) but doesn't create the intermediate arrays. Probably useful when the results of f are SMALL sized, but may take a very LONG TIME to compute. (but then again, you might want to use BC_CachedArray.map_to_array instead)
val length : 'a t -> int
return the length of the cached array.
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
fold left for cached array.
val init_matrix : dimx:int ->
dimy:int ->
(x:int -> y:int -> unit -> 'a) -> 'a t t
two dimensional cached arrays.
val matrix_get : 'a t t -> x:int -> y:int -> 'a
get an element from a two dimensional cached array.
val matrix_set : 'a t t ->
x:int -> y:int -> (unit -> 'a) -> unit
Assign a new value to an element of a two dimensional cached array.
val map_matrix : ('a -> 'b) ->
'a t t -> 'b t t
map on a two dimensional cached array.