Contiguous non-owning view of items of the same schema with a schema-level fixed size.
The item count is a static field of the schema (schemaName gives SpanStatic<Int32, 3>): it is known at compile time, spans of different sizes have different schemas, and slice and toSpanStatic2 need a known size. Unlike Span, the view cannot change its size.
The range checks of at and slice and the size check of assign 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 terminates the program with its message (Key is out of bounds, Offset is out of bounds, Size is out of bounds, Inconsistent sizes) and exit status 2, and is omitted with -ndebug. The known-size and item-schema requirements are compile-time requirements.
SpanStatic
Methods
Preconditions
other.sizeequals the current static size.
Postconditions
- The span refers to
other's storage afterwards (itsdataisother.data); no items are copied. - The static size is unchanged.
iterReverse
(-- iter) Returns a reverse iteration source over the current items.Postconditions
- The iteration starts at the last item and proceeds backward.
- The result is an iterator Dict with the method
next (-- item valid):itemis aRefto the current item (the item is mutable only when the span's stored reference is mutable anditerReversewas called through a mutable view of the span;nextitself needs a mutable view of the iterator),validisTRUEwhen an item was available; when it isTRUE, the iterator moves one item towards the first item.
size
(-- count) Returns the current item count.slice
(offset size -- theSpan) Returns a static span over the selected subrange.Preconditions
offsetin [0..current item count].size(the argument) in [0..current item count - offset].sizeis known at compile time: it becomes part of the result's schema (offsetmay be unknown).
Examples
"SpanStatic" use
"control" use
{} () {} [
span: (10 20 30 40) toSpanStatic;
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
spanStatic
(-- theSpan) Returns a static span over the same range.stringView
(-- theStringView) Requires Nat8 items and an Int32 count. Returns an immutable StringView over the same bytes and size; it does not format arbitrary items as text.Runtime example: reverse iteration
"SpanStatic" use
"String" use
"control" use
{} Int32 {} [
span: (10 20 30) toSpanStatic;
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
toSpanStatic (source -- theSpan)
Converts supported sources to SpanStatic.
- A known, non-
NILTextsource becomes a static span of immutableNat8items over its UTF-8 bytes; the size is its byte count (no terminator). - Homogeneous non-
RefStruct sources become static spans over their items. - Any source that has a
.spanStaticmethod uses that method result.
Runtime example
"SpanStatic" use
"String" use
"control" use
{} Int32 {} [
numbers: (10 20 30) toSpanStatic;
("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
toSpanStatic2 (spanData spanSize -- theSpan)
Constructs SpanStatic directly from item data and item count.
spanDatasupplies the item schema and baseRef.spanSizemust be known at compile time; it becomes part of the resulting schema (SpanStatic<Int32, 2>).
Examples
"SpanStatic" use
"control" use
{} () {} [
span: (10 20 30) toSpanStatic;
part: @span.data 2 toSpanStatic2;
part.size printStack _:;
0 @part.at printStack _:;
1 @part.at printStack _:;
] "main" exportFunction
Expected Output During Compilation
2
Int32 Ref
Int32 Ref
Runtime example
"SpanStatic" use
"String" use
"control" use
{} Int32 {} [
span: (10 20 30 40) toSpanStatic;
part: 1 @span.at 2 toSpanStatic2;
("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=20
second=30