array.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
#
# Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------
# array -- one-dimensional immutable array
#
# This is the result type of array(type, i32, i32 -> T) which creates an
# initialized immutable array
#
# Note: This uses dummy unit-type args to avoid
# name clash with routine array(T,length,init).
#
module:public array(
public T type,
module internal_array fuzion.sys.internal_array T,
_ unit,
_ unit,
_ unit) : container.abstract_array T is
internal_array.freeze
# the length of the array
#
public redef length => internal_array.length
# get the contents of this array at the given index
#
public redef index [ ] (i i32) T
=>
internal_array[i]
# create a new array with element i set to v. Grow the array in case i == length.
#
# Complexity: O(array.this.length)
#
public put (i i32, v T) array T
pre
safety: 0 ≤ i ≤ length
=>
# NYI: This is very inefficient since it copies the whole array. Should
# better use a persistent array implementation such as persistent hash array
# mapped trie.
array (max length i+1) (ix -> if (ix = i) v else array.this[ix])
# create a new array with element i set to v. Grow the array in case i >= length.
# New array elements at indices array.this.length..i-1 will be set to z.
#
# Complexity: O(max(i, array.this.length))
#
public put (i i32, v T, z T) array T
pre
safety: 0 ≤ i
=>
# NYI: This is very inefficient since it copies the whole array. Should
# better use a persistent array implementation such as persistent hash array
# mapped trie.
array (max length i+1) (ix -> if (ix = i) v else if (ix ≥ length) z else array.this[ix])
# collect the contents of this Sequence into an array
#
public fixed redef as_array =>
array.this
# array -- create initialized one-dimensional immutable array
#
public type.new(length i32, init i32 -> T) array T
pre
safety: length ≥ 0
=>
indices => 0..length-1
internal := fuzion.sys.internal_array_init T length
for x in indices do
internal[x] := init x
array internal unit unit unit
# equality
#
public fixed redef type.equality(a, b array T) => (Sequence T).type.equality a b
# array -- create initialized one-dimensional immutable array
#
public array(T type, length i32, init i32 -> T) =>
(array T).new length init
last changed: 2025-01-30