std.array
Reed's standard library. Imported with use std.array; not on disk.
Functions
func array_new(count: i32) -> &I32Values
An i32 array of count zeros.
A negative count yields an empty array rather than trapping.
- Parameters
- count — how many elements
- Returns
- a new zero-filled array
- See also
array_filled
Source
func array_new(count: i32) -> &I32Values {
return new I32Values[count < 0 ? 0 : count]{};
}
func array_filled(count: i32, value: i32) -> &I32Values
An i32 array of count copies of value.
- Parameters
- count — how many elements
- value — what to fill with
- Returns
- a new filled array
Source
func array_filled(count: i32, value: i32) -> &I32Values {
let out: &I32Values = new I32Values[count < 0 ? 0 : count]{};
out.fill(0, value, #out);
return out;
}
func array_range(from: i32, to: i32) -> &I32Values
An i32 array holding from, from + 1, ..., to - 1.
Empty when from >= to, matching for i in from..to.
- Parameters
- from — the first value, inclusive
- to — the value to stop before
- Returns
- a new array holding the range
Source
func array_range(from: i32, to: i32) -> &I32Values {
let n: i32 = to > from ? to - from : 0;
let out: &I32Values = new I32Values[n]{};
for i in 0..n {
out[i] = from + i;
}
return out;
}
func reverse_span(values: &I32Values, from: i32, to: i32) -> void
Reverses values[from..to] in place. Shared by rotate; not part of the public surface because the public reverse in std.sort covers the whole-array case.
Source
func reverse_span(values: &I32Values, from: i32, to: i32) {
var lo: i32 = from;
var hi: i32 = to - 1;
loop swap() {
if (lo >= hi) { break swap(); }
let t: i32 = values[lo];
values[lo] = values[hi];
values[hi] = t;
lo = lo + 1;
hi = hi - 1;
continue swap();
}
}
Methods
func I32Values.clone(self: &I32Values) -> &I32Values
An independent copy of self.
- Returns
- a new array with the same elements
Source
func I32Values.clone(self: &I32Values) -> &I32Values {
let out: &I32Values = new I32Values[#self]{};
out.copy(0, self, 0, #self);
return out;
}
func I32Values.slice(self: &I32Values, from: i32, to: i32) -> &I32Values
The elements from from up to but not including to, as a new array.
Both endpoints clamp into range and an inverted range yields an empty array.
- Parameters
- from — the first index, inclusive
- to — the index to stop before
- Returns
- a new array holding the selected elements
Source
func I32Values.slice(self: &I32Values, from: i32, to: i32) -> &I32Values {
let lo: i32 = from < 0 ? 0 : (from > #self ? #self : from);
let hi: i32 = to < 0 ? 0 : (to > #self ? #self : to);
if (hi <= lo) { return new I32Values[0]{}; }
let out: &I32Values = new I32Values[hi - lo]{};
out.copy(0, self, lo, hi - lo);
return out;
}
func I32Values.concat(self: &I32Values, rhs: &I32Values) -> &I32Values
self followed by rhs, as a new array.
- Parameters
- rhs — the array to append
- Returns
- a new array holding both
Source
func I32Values.concat(self: &I32Values, rhs: &I32Values) -> &I32Values {
let out: &I32Values = new I32Values[#self + #rhs]{};
out.copy(0, self, 0, #self);
out.copy(#self, rhs, 0, #rhs);
return out;
}
func I32Values.index_of(self: &I32Values, value: i32) -> i32
The index of the first element equal to value, or -1.
- Parameters
- value — what to search for
- Returns
- the index, or
-1 when absent
- See also
I32Values.contains
Source
func I32Values.index_of(self: &I32Values, value: i32) -> i32 {
for i in 0..#self {
if (self[i] == value) { return i; }
}
return -1;
}
func I32Values.contains(self: &I32Values, value: i32) -> i32
Whether any element equals value.
- Parameters
- value — what to search for
- Returns
1 when present, 0 otherwise
Source
func I32Values.contains(self: &I32Values, value: i32) -> i32 {
return self.index_of(value) >= 0;
}
func I32Values.count(self: &I32Values, value: i32) -> i32
How many elements equal value.
- Parameters
- value — what to count
- Returns
- the number of matching elements
Source
func I32Values.count(self: &I32Values, value: i32) -> i32 {
var found: i32 = 0;
for i in 0..#self {
if (self[i] == value) { found = found + 1; }
}
return found;
}
func I32Values.sum(self: &I32Values) -> i32
The sum of every element, wrapping on overflow like +.
- Returns
- the sum
Source
func I32Values.sum(self: &I32Values) -> i32 {
var total: i32 = 0;
for i in 0..#self {
total = total + self[i];
}
return total;
}
func I32Values.min(self: &I32Values) -> (i32, i32)
The smallest element, or (0, 0) when the array is empty.
- Returns
(value, ok)
- See also
I32Values.max
Source
func I32Values.min(self: &I32Values) -> (i32, i32) {
if (#self == 0) { return (0, 0); }
var best: i32 = self[0];
for i in 1..#self {
if (self[i] < best) { best = self[i]; }
}
return (best, 1);
}
func I32Values.max(self: &I32Values) -> (i32, i32)
The largest element, or (0, 0) when the array is empty.
- Returns
(value, ok)
- See also
I32Values.min
Source
func I32Values.max(self: &I32Values) -> (i32, i32) {
if (#self == 0) { return (0, 0); }
var best: i32 = self[0];
for i in 1..#self {
if (self[i] > best) { best = self[i]; }
}
return (best, 1);
}
func I32Values.equals(self: &I32Values, rhs: &I32Values) -> i32
Whether every element of self equals the corresponding element of rhs.
Arrays of different lengths are never equal.
- Parameters
- rhs — the array to compare against
- Returns
1 when equal, 0 otherwise
Source
func I32Values.equals(self: &I32Values, rhs: &I32Values) -> i32 {
if (#self != #rhs) { return 0; }
for i in 0..#self {
if (self[i] != rhs[i]) { return 0; }
}
return 1;
}
func I32Values.rotate(self: &I32Values, count: i32) -> void
Rotates the array left by count positions, in place.
A negative count rotates right. Counts beyond the length wrap, so rotating a 5-element array by 7 is the same as rotating it by 2.
Uses the three-reversal method: two partial reversals plus one whole reversal, which is in place and needs no scratch array.
- Parameters
- count — how far to rotate left
Source
func I32Values.rotate(self: &I32Values, count: i32) {
let n: i32 = #self;
if (n < 2) { return; }
var k: i32 = count % n;
if (k < 0) { k = k + n; }
if (k == 0) { return; }
reverse_span(self, 0, k);
reverse_span(self, k, n);
reverse_span(self, 0, n);
}
Types
| I32Values | A fixed-length array of i32 values. |
| I64Values | A fixed-length array of i64 values. |
| F64Values | A fixed-length array of f64 values. |
| AnyValues | A fixed-length array of nullable references, for heterogeneous contents. |
array I32Values { mut i32 }
A fixed-length array of i32 values. The standard library's canonical integer array: std.list, std.sort, and std.set all use this exact type.
array I64Values { mut i64 }
A fixed-length array of i64 values.
array F64Values { mut f64 }
A fixed-length array of f64 values.
array AnyValues { mut ?any }
A fixed-length array of nullable references, for heterogeneous contents.