Module type PrivateSignatures.SCHUNK
The module ShareableChunk implements a shareable chunk, that is, a chunk that is either owned by a single owner or shared between multiple owners. This is its signature.
type 'a chunk= 'a EChunk.ttype view= EChunk.View.view
type 'a tA schunk is either shared or uniquely owned. When it is shared, it cannot be modified. When it is uniquely owned, it can be modified. As far as the user is concerned, this is transparent: the user does not decide whether to allocate a new schunk or modify an existing schunk in place.
However, the user is responsible for presenting an appropriate owner identity
owhen creating or using a schunk. (This will not be repeated in the specification of every operation.)A schunk records the identity of its creator. It is considered uniquely owned by its creator unless its creator is
Owner.none, in which case it is considered shared. Later on, when an operation is performed on this schunk with identityo, the functionOwner.is_uniquely_ownedis used to compare the schunk's creator witho. The outcome of this test determines whether the schunk is uniquely owned or shared, therefore can or cannot be modified in place.
type 'a measure=|MUnit : 'a measure|MSWeight : 'a t measureBeing able to test whether a measure is the unit measure -- which maps every element to a weight of one -- is useful. This allows optimizations in some functions, such as
reach. For this reason, instead of defining the type'a measureas a synonym for'a -> weight, we define it as a (generalized) algebraic data type.
val apply : 'a measure -> 'a -> weightapply m xapplies the measurexto the elementxand returns its weight.
val check : 'a measure -> owner -> 'a t -> unitcheck m o pverifies that the schunkpis valid, i.e., that its internal invariant is satisfied. This check is relative to the measuremand to the identityowith which this schunk is accessed.
val is_uniquely_owned : 'a t -> owner -> boolis_uniquely_owned p odetermines whether the schunkpis uniquely owned by ownero.
val default : 'a t -> 'adefault preturns the default element that was provided when the schunkpwas created.
val length : 'a t -> Sek__.PublicTypeAbbreviations.lengthlength pis the length of the sequencep. A dummy schunk has length 0.
val data : 'a t -> 'a arraydata pis the raw array that underlies the chunkp. This function is dangerous; it should be used only to implement efficient iterators.
val capacity : 'a t -> Sek__.PublicTypeAbbreviations.capacitycapacity pis the capacity of the chunkp, that is, the maximum number of elements that this chunk can hold.
val is_empty : 'a t -> boolis_empty pis equivalent tolength p = 0.
val is_full : 'a t -> boolis_full pis equivalent tolength p = capacity p.
val create : 'a -> Sek__.PublicTypeAbbreviations.capacity -> owner -> 'a tcreate d n vcreates a schunk with default elementd, capacityn, and ownero.
val dummy : 'a -> 'a tdummy dcreates a dummy (invalid!) schunk.
val is_dummy : 'a t -> boolis_dummy ptells whetherpis a dummy schunk, that is, whether it has been built bydummy.
val of_chunk_destructive : 'a chunk -> owner -> 'a tof_chunk_destructive c ocreates a new schunk, based on the ephemeral chunkc, with ownero. The ownership of the chunkcis lost. The elements of the chunkcare assumed to have unit weight.
val to_chunk : 'a t -> owner -> 'a chunkto_chunk p oconverts the schunkpto an ephemeral chunk. Ifpis uniquely owned byo, then it is destroyed and its support is re-used directly; no copy is required. Ifpis shared, then a copy is performed and a new chunk is allocated. Likeof_chunk, this operation assumes that every element has unit weight.
val of_array_segment : 'a -> Sek__.PublicTypeAbbreviations.capacity -> 'a array -> Sek__.PublicTypeAbbreviations.index -> Sek__.PublicTypeAbbreviations.length -> owner -> 'a tof_array_segment d n a head size ocreates a schunk with owneroby copying data from the array segment defined by the arraya, the start indexhead, and the sizesize. The schunk has capacitynand default valued.sizemust be less than or equal ton. The elements are assumed to have unit weight.
val make : 'a -> Sek__.PublicTypeAbbreviations.capacity -> Sek__.PublicTypeAbbreviations.length -> 'a -> owner -> 'a tmake d n k v ocreates a schunk that containskcopies of the valuev. The schunk has default valued, capacityn, and ownero. The elements are assumed to have unit weight.
val init : 'a -> Sek__.PublicTypeAbbreviations.capacity -> Sek__.PublicTypeAbbreviations.length -> Sek__.PublicTypeAbbreviations.index -> (Sek__.PublicTypeAbbreviations.index -> 'a) -> owner -> 'a tinit d n k i f ocreates a fresh schunk that contains the sequence of values produced by the callsf i,f (i+1), ...f (i+k-1), in this order. The schunk has default valued, capacityn, and ownero. The elements are assumed to have unit weight.
val peek : pov -> 'a t -> 'apeek pov preturns the first or last element of the schunkp, depending on the point-of-viewpov.pmust be nonempty.
val push : pov -> 'a t -> 'a -> 'a measure -> owner -> 'a tpush pov p x m vpushes the elementxat the front (resp. back) of the schunkp, depending on the point-of-viewpov.pmust not be full. Eitherpis updated in place, or a new schunk is allocated; this is transparent.
val pop : pov -> 'a t -> 'a measure -> owner -> 'a * 'a tpop pov p m vpops an element off the front (resp. back) of the schunkp, depending on the point-of-viewpov.pmust not be empty. Eitherpis updated in place, or a new schunk is allocated; this is transparent.
val get : 'a t -> Sek__.PublicTypeAbbreviations.index -> 'aget p ireturns the element found at indexiin the sequencep.imust be comprised between 0 included andlength pexcluded.
val concat : 'a t -> 'a t -> owner -> 'a tconcat p1 p2 oconcatenates the sequencesp1andp2, yielding a sequence. The schunksp1andp2must have the same capacity. The length of the concatenated sequence must not exceed this capacity: that is,length p1 + length p2 <= capacity p1must hold.
val reach : 'a measure -> 'a t -> weight -> weight * Sek__.PublicTypeAbbreviations.indexreach m p wreturns a pair of the total weight of the elements that precedexin the sequencepand the index in the sequence of the elementxwithin which the weightwfalls. The weight indexwmust be comprised between 0 included andweight pexcluded.
val reach_from : 'a measure -> 'a t -> Sek__.PublicTypeAbbreviations.index -> weight -> weight -> weight * Sek__.PublicTypeAbbreviations.indexreach_from m p pindex pwindex wis a generalization ofreach. Compared withreach, it takes two extra arguments, an indexpindexand a corresponding weight indexpwindex, which describe a starting position in the schunkp. (pwindexmust be consistent withpindex: that is, it must be the sum of the weights of the elements whose index is less thanpindex.) The schunkpis scanned from the positionpindex: it is scanned forward ifpwindex <= wholds, and backwards ifw < pwindexholds.
val get_by_weight : 'a measure -> 'a t -> weight -> weight * 'aget_by_weight m p wfinds the elementxat weight-indexwin the schunkp. It returns a pair of an adjusted weight-index (namelywminus the weight of the elements that precedex) and the elementx.
val update_by_weight : 'a measure -> owner -> 'a update -> 'a t updateupdate_by_weight m o f p wfinds the elementxat weight-indexwin the schunkp, adjusts the weight-indexwby subtracting from it the weight of the elements that precedex, then passesxandwto the update functionf, which produces a new elementx'. The elementxis then replaced withx'in the schunk. The elementsxandx'must have the same weight.
val three_way_split : 'a t -> weight -> 'a measure -> owner -> 'a t * 'a * 'a tthree_way_split p i m osplits the sequencepinto a sequencep1, followed with an elementx, followed with a sequencep2, such that the weightifalls within the elementx, that is,weight p1 <= i < weight p1 + apply m xholds. The weightimust be comprised between 0 included andweight pexcluded.
val take : 'a t -> weight -> 'a measure -> owner -> 'a t * 'atakeis a specialized version ofthree_way_split. Instead of returning a triplep1, x, p2, it returns a pairp1, x.
val drop : 'a t -> weight -> 'a measure -> owner -> 'a * 'a tdropis a specialized version ofthree_way_split. Instead of returning a triplep1, x, p2, it returns a pairx, p2.
val remaining_length : pov -> Sek__.PublicTypeAbbreviations.index -> 'a t -> Sek__.PublicTypeAbbreviations.lengthremaining_length pov i preturns the number of elements found ahead of the element at indexi(included).
val segment : pov -> Sek__.PublicTypeAbbreviations.index -> Sek__.PublicTypeAbbreviations.length -> 'a t -> 'a Sek__.PublicTypeAbbreviations.segmentsegment pov i k preturns the description of a segment of at mostkconsecutive elements, starting from the one at indexiand progressing in the directionpov. The argumentkmust be no greater thanremaining_length pov i p.For additional details, see the documentation of
segmentin the signatureECHUNKabove.
val segment_max : pov -> Sek__.PublicTypeAbbreviations.index -> 'a t -> 'a Sek__.PublicTypeAbbreviations.segmentsegment_max pov i sis a shorthand forsegment pov i k swherektakes its maximum permitted value, that is,remaining_length pov i p.
val iter_segments : pov -> 'a t -> 'a Sek__.PublicTypeAbbreviations.segmentsiter_segments pov preturns a sequence of one or two array segments that store the data of the schunkp.
val iteri_segments_front : 'a t -> (Sek__.PublicTypeAbbreviations.index -> ('a array * Sek__.PublicTypeAbbreviations.index * Sek__.PublicTypeAbbreviations.length) -> unit) -> unititeri_segments_frontiterates over the segments in a schunk, while maintaining the index (with respect to the schunk) of the head of the current segment. It is restricted to theFrontdirection.