array3.fz
# This file is part of the Fuzion language implementation.
#
# The Fuzion language implementation is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, version 3 of the License.
#
# The Fuzion language implementation is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
# License for more details.
#
# You should have received a copy of the GNU General Public License along with The
# Fuzion language implementation. If not, see <https://www.gnu.org/licenses/>.
# -----------------------------------------------------------------------
#
# Tokiwa Software GmbH, Germany
#
# Source code of Fuzion standard library feature array(l1,l2,l3)
#
# Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------
# array(length0, length1, length2) -- three-dimensional immutable array
#
# array provides three-dimensional immutable arrays. These are actually
# one-dimensional immutable arrays with an additional access function with
# three index parameters.
#
public array3(T type,
length0, length1, length2 i32,
init3 (i32, i32, i32) -> T)
pre
safety: length0 ≥ 0
safety: length1 ≥ 0
safety: length2 ≥ 0
safety: length0 *? length1 *? length2 >=? 0
=>
internal_array := fuzion.sys.internal_array_init T length0*length1*length2
for i1 in 0..length0-1 do
for i2 in 0..length1-1 do
for i3 in 0..length2-1 do
internal_array[(i1 * length1 + i2) * length2 + i3] := init3 i1 i2 i3
array3 T length0 length1 length2 internal_array unit unit
# array(length0, length1, length2) -- three-dimensional immutable array
#
# array provides three-dimensional immutable arrays. These are actually
# one-dimensional immutable arrays with an additional access function with
# three index parameters.
#
private:public array3(
public T type,
public length0, length1, length2 i32,
redef internal_array fuzion.sys.internal_array T,
_ unit,
_ unit)
: array T internal_array unit unit unit
pre
safety: length0 ≥ 0
safety: length1 ≥ 0
safety: length2 ≥ 0
safety: length0 *? length1 *? length2 >=? 0
is
# indices range in first dimension
#
public indices0 => 0..length0-1
# indices range in second dimension
#
public indices1 => 0..length1-1
# indices range in third dimension
#
public indices2 => 0..length2-1
public index [ ] (i0, i1, i2 i32) T
pre
safety: 0 ≤ i0 < length0
safety: 0 ≤ i1 < length1
safety: 0 ≤ i2 < length2
=>
array3.this[(i0 * length1 + i1) * length2 + i2]
# create a string representation of this array including all the string
# representations of its contents, separated by ',' and enclosed in '['
# and ']'. Arrays in inner dimensions are grouped using '[' and ']'.
#
public redef as_string : character_encodings =>
dim_1(i i32) =>
(" [" + ascii.lf_str
+ indices1
.map(j -> dim_2 i j)
.fold String.concat("," + ascii.lf_str)
+ ascii.lf_str + " ]")
dim_2(i i32, j i32) =>
from := (i * length1 + j) * length2
to := from + length2
" " + slice from to
("[" + ascii.lf_str
+ indices0
.map(i -> dim_1 i)
.fold String.concat("," + ascii.lf_str)
+ ascii.lf_str + "]")
# get a list of tuples indices and elements in this array
#
public enumerate3 list (tuple i32 i32 i32 T) =>
if length = 0
nil
else
enumerate_cons 0 0 0
# create a cons cell for a list of tuples of this array's indices and elements
# starting at the given indices.
#
enumerate_cons (i, j, k i32) : Cons (tuple i32 i32 i32 T) (list (tuple i32 i32 i32 T))
pre
debug: 0 ≤ i < length0
debug: 0 ≤ j < length1
debug: 0 ≤ k < length2
is
head => (i, j, k, index[] i j k)
tail list (tuple i32 i32 i32 T) =>
if k < length2-1 then enumerate_cons i j k+1
else if j < length1-1 then enumerate_cons i j+1 0
else if i < length0-1 then enumerate_cons i+1 0 0
else nil
last changed: 2024-03-07