Module EChunk.View
type view
A view is an immutable object, which represents a certain range in a chunk. It can be thought of as a pair of a
head
index and asize
.
val head : view -> Sek__.PublicTypeAbbreviations.index
head v
is the head of the viewv
, that is, the index in the underlying chunk of the first element of the viewv
.
val size : view -> Sek__.PublicTypeAbbreviations.length
size v
is the size of the viewv
, that is, the number of elements in this view.
val check : 'a t -> view -> unit
check c v
verifies that the viewv
is valid with respect to the chunkv
, i.e., that its internal invariant is satisfied.
val dummy : view
dummy
is a dummy view. It may be invalid and must not be used in any way.
val iter_segments : Sek__.PrivateSignatures.pov -> ('a t * view) -> 'a Sek__.PublicTypeAbbreviations.segments
iter_segments pov (c, v) f
returns a sequence of up to two array segments that cover the viewv
of the chunkc
.
val peek : Sek__.PrivateSignatures.pov -> 'a t -> view -> 'a
peek pov c v
returns the first or last element of the viewv
in chunkc
, depending on the point-of-viewpov
. The viewv
must be nonempty.
val get : 'a t -> view -> Sek__.PublicTypeAbbreviations.index -> 'a
get c v i
returns the element found at indexi
in the viewv
of the sequencec
.v
must be a valid view with respect toc
, andi
must be comprised between 0 included andsize v
excluded.
val set : 'a t -> view -> Sek__.PublicTypeAbbreviations.index -> 'a -> unit
set c v i x
replaces the element found at indexi
in the viewv
of the sequencec
withx
.v
must be a valid view with respect toc
, andi
must be comprised between 0 included andsize v
excluded.
val three_way_split : 'a t -> view -> Sek__.PublicTypeAbbreviations.index -> view * 'a * view
three_way_split c v i
returns the elementx
found at indexi
in the viewv
of the sequencec
, along with two views that cover the elements ofv
found left and right ofx
.v
must be a valid view with respect toc
, andi
must be comprised between 0 included andsize v
excluded.
val take : 'a t -> view -> Sek__.PublicTypeAbbreviations.index -> view * 'a
take
is a specialized version ofthree_way_split
. Instead of returning a tripleview1, x, view2
, it returns a pairview1, x
.
val drop : 'a t -> view -> Sek__.PublicTypeAbbreviations.index -> 'a * view
drop
is a specialized version ofthree_way_split
. Instead of returning a tripleview1, x, view2
, it returns a pairx, view2
.
val is_flush : Sek__.PrivateSignatures.pov -> 'a t -> view -> bool
is_flush pov c v
determines whether the front (resp. back) of the viewv
coincides with the front (resp. back) of the chunkc
.
val is_aligned : 'a t -> view -> bool
is_aligned c v
determines whether the viewv
covers all of the sequencec
. It is equivalent tois_flush Front c v && is_flush Back c v
.
val push : Sek__.PrivateSignatures.pov -> 'a t -> view -> view
push pov c v
is the view obtained by pushing one element in front of (resp. at the back of) the viewv
. The viewv
must not cover all ofc
.
val pop : Sek__.PrivateSignatures.pov -> 'a t -> view -> view
pop pov c v
is the view obtained by popping one element in front of (resp. at the back of) the viewv
. The viewv
must be nonempty.
val restrict : 'a t -> view -> Sek__.PublicTypeAbbreviations.index -> Sek__.PublicTypeAbbreviations.length -> view
restrict c v i k
is the view obtained by restricting the viewv
to the subview defined by the relative indexi
and the lengthk
.
val copy : Sek__.PrivateSignatures.pov -> 'a t -> view -> 'a t -> view -> view
copy pov c1 v1 c2 v2
copies the data in the viewv1
of chunkc1
to the front (resp. back) of viewv2
ofc2
, depending on the point-of-viewpov
. The viewv1
andv2
must be valid with respect to the chunksc1
andc2
, respectively. There must be enough room for the data, that is,size v1 + size v2 <= capacity c2
must hold. The function returns a view that is valid with respect toc2
and includesv2
plus the newly-copied data.
val sub : 'a t -> view -> 'a t
sub c v
creates a new chunk that contains the data covered by the viewv
of the chunkc
. The copied data is located in viewv
of the new chunkc'
, soChunk.view c'
isv
.
val iter : Sek__.PrivateSignatures.pov -> ('a -> unit) -> ('a t * view) -> unit
iter pov f (c, v)
applies the functionf
in turn to every element in viewv
of chunkc
, in the order specified by the point-of-viewpov
.
val fold_left : ('b -> 'a -> 'b) -> 'b -> ('a t * view) -> 'b
fold_left f (c, v) seed
applies the functionf
in turn to every element in viewv
of chunkc
, while maintaining an accumulator.
val segment : Sek__.PrivateSignatures.pov -> Sek__.PublicTypeAbbreviations.index -> Sek__.PublicTypeAbbreviations.length -> 'a t -> view -> 'a Sek__.PublicTypeAbbreviations.segment
segment pov i k c v
returns the description of a segment of at mostk
consecutive elements, starting from the one at indexi
and progressing in the directionpov
. The indexi
is relative to the viewv
: it must be in the interval[0, size v)
. The lengthk
must be no greater than the number of elements between indexi
(included) and the end of the viewv
in the direction of iteration.segment Front i k c v
returns a triple(a, j, m)
such that the elements of the viewv
in the interval[i, i + m)
are stored in the arraya
in the segment[j, j + m)
.segment Back i k c v
returns a triple(a, j, m)
such that the elements of the viewv
in the interval(i - m, i]
are stored in the arraya
in the segment[j, j + m)
.The value
m
is the maximal value such thatm <= k
and the elements starting at indexi
in the direction of iteration are stored consecutively in memory. Ifk > 0
holds, then it is guaranteed thatm > 0
holds as well.In the current implementation, all elements of a view can be covered by using at most two segments. Thus, after
segment Front i k c v
returns(a, j, m)
, eitherm = k
holds and all elements of the view have been covered, or callingsegment Front (i + k) (k - m) c
returns a segment that covers all of the remaining elements of the view.
val print : view -> PPrint.document
print
is a view printer. It is intended to be used only while debugging.