Contiguous non-owning view of items of the same schema.
The checks of at, cutFirst, cutLast, first, last, popFirst, popLast, and slice are assertions: a check that is provably false at compile time fails compilation (also with -ndebug); an unknown check runs while DEBUG is on (the default) and, on failure, terminates the program with its message (Key is out of bounds, Span is empty, Offset is out of bounds, Size is out of bounds) and exit status 2, and is omitted when the selected DEBUG definition is off, for example with -ndebug.
Span
Methods
Postconditions
- Data and size match
other.
Postconditions
- Result can be
NILif the item count is0.
iter
(-- iter) Returns a forward iteration source over the current items.Postconditions
- The iteration range matches
span.
iterReverse
(-- iter) Returns a reverse iteration source over the current items.Postconditions
- The iteration starts at the last item and proceeds backward.
next
(-- item valid) Returns a Ref to the current item and advances the view by one item when valid.Postconditions
Preconditions
- current item count > 0.
Preconditions
- current item count > 0.
size
(-- count) Returns the current item count.slice
(offset size -- theSpan) Returns a span over the selected subrange.Preconditions
offsetin [0..current item count].size(the argument) in [0..current item count - offset].
Example: a two-item Span slice
Slicing two items from index 1 gives a span of size 2 whose items are mutable references, shown as Int32 Ref during compilation.
"Span" use
"control" use
{} () {} [
span: (10 20 30 40) toSpan;
part: 1 2 @span.slice;
part.size printStack _:;
0 @part.at printStack _:;
1 @part.at printStack _:;
] "main" exportFunction
Expected Output During Compilation
2
Int32 Ref
Int32 Ref
span
(-- theSpan) Returns a span over the same range.spanStatic
(-- theSpan) Returns a SpanStatic over the same range, with the item count stored in the schema.Preconditions
- The current item count is known at compile time.
- SpanStatic is loaded at global level.
stringView
(-- theStringView) Requires Nat8 items and an Int32 count, with String loaded at global level. Returns an immutable StringView over the same bytes and size.Example: reverse iteration
iterReverse returns the items from last to first, 30, 20, 10, and then FALSE.
"Span" use
"String" use
"control" use
{} Int32 {} [
span: (10 20 30) toSpan;
it: @span.iterReverse;
item0: ok0: @it.next;;
item1: ok1: @it.next;;
item2: ok2: @it.next;;
item3: ok3: @it.next;;
("ok0=" ok0 LF
"v0=" item0 new LF
"ok1=" ok1 LF
"v1=" item1 new LF
"ok2=" ok2 LF
"v2=" item2 new LF
"ok3=" ok3 LF) printList
0
] "main" exportFunction
Expected Output
ok0=TRUE
v0=30
ok1=TRUE
v1=20
ok2=TRUE
v2=10
ok3=FALSE
toSpan (inputs... source -- outputs...)
Converts supported sources to a Span or uses the .span member of the source or of the target of a Ref source; a callable member is called with its own stack effect.
- A
Textsource, known or unknown, becomes a span over the UTF-8 code units (immutableNat8items); the size is its byte count (no terminator), which must fit inInt32; knownNILTextis invalid; unknownNILTextyields an empty span whose data reference isNILat run time. - Non-Meta uniform Struct sources with In-place items, not
Ref,Text, orCodeitems, become spans over their items when the source has no static fields and nospanorCALLfield, and no item is, or contains outside static fields, a non-Meta uniform Dict that holds a static field unless the source has only one item or an item's storage size would not change if all static fields of non-Meta uniform Dicts that the item is or contains were solid. A non-NILRef, known or unknown, whose target is such a Struct and whose target schema is that Struct's schema becomes a span over the Struct's items. - Any source, or the target of a
Refsource, that is not called at the point of conversion, whose selected.spanmember and every field of that name declared after it are notprivate, and whose selected member can form its result uses that result, whether or not it is a span; reading or writing the stored value of a solid field, or calling aCodefield, of the target or of In-place items within it needs a non-NILRef; writing also needs a mutable view, and using solid field values at compile time needs a knownRef.
Example: span over a List
A span made with toSpan from (10 20 30) has size 3, with 10 at index 0 and 30 at index 2.
"Span" use
"String" use
"control" use
{} Int32 {} [
numbers: (10 20 30) toSpan;
("size=" numbers.size LF
"first=" 0 @numbers.at new LF
"last=" 2 @numbers.at new LF) printList
0
] "main" exportFunction
Expected Output
size=3
first=10
last=30
toSpan2 (spanData spanSize -- theSpan)
Constructs Span directly from item data and item count.
spanDatasupplies the item schema and baseRef.spanSizesupplies the item count.- Supply
spanSizeas a nonnegativeInt32count andspanDataas a reference to at leastspanSizecontiguous live items.
Example: toSpan2 over existing data
toSpan2 builds a two-item span from the data address of another span, so its items are Int32 Ref during compilation.
"Span" use
"control" use
{} () {} [
span: (10 20 30) toSpan;
part: @span.data 2 toSpan2;
part.size printStack _:;
0 @part.at printStack _:;
1 @part.at printStack _:;
] "main" exportFunction
Expected Output During Compilation
2
Int32 Ref
Int32 Ref
Example: toSpan2 at run time
A two-item span built from the data of (10 20 30 40) reads 10 and 20.
"Span" use
"String" use
"control" use
{} Int32 {} [
span: (10 20 30 40) toSpan;
part: @span.data 2 toSpan2;
("size=" part.size LF
"first=" 0 @part.at new LF
"second=" 1 @part.at new LF) printList
0
] "main" exportFunction
Expected Output
size=2
first=10
second=20
See also
- Array: Growable array.
- SpanStatic: Contiguous non-owning view of items of the same schema with a schema-level fixed size.
- String: UTF-8 string views, owned strings, formatting helpers, and text conversion utilities.
- algorithm: Collection interfaces, comparison helpers, iteration adapters, and view slicing utilities.