Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

mutate/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 mutate.array
#
# -----------------------------------------------------------------------

# create a mutable array.
#
module:public array (
       # element type
       public T type,

       # length of the array to create
       public redef length i64,

       # contents of the array
       module data fuzion.sys.internal_array T,
       _ unit

      ) : container.Mutable_Array T mutate.this, mutable_element
pre
  safety: (length ≥ 0) && (length ≤ data.length.as_i64)
is

  # get element at given index i
  #
  public redef index [ ] (i i64) T
  =>
    data[i.as_i32]


  # set element at given index i to given value o
  #
  public redef set [ ] (i i64, o T) unit
  =>
    check_and_replace
    data[i.as_i32] := o


  # add an element at the end of this array
  #
  public redef add (o T) unit
  =>
    check_and_replace
    d := if (data.length.as_i64 > length) data
         else
           new_data := fuzion.sys.internal_array_init T (max 8 data.length*2)
           for i in indices do
             new_data[i.as_i32] := data[i.as_i32]
           new_data
    d[length.as_i32] := o
    set data := d
    set length := length+1


  # create immutable array from this
  #
  public redef as_array universe.array T =>
    array length.as_i32 (i -> data[i])


  # create a list from this array
  #
  public redef as_list list T =>
    # since array is mutable,
    # we first copy the elements
    # to an immutable array.
    as_array.as_list


  # initialize one-dimensional mutable array
  #
  public type.new
   (LM type : mutate,

    # length of the array to create
    length i64,

    # initial value for elements
    init T

   ) container.Mutable_Array T LM
  =>
    data := fuzion.sys.internal_array_init T length.as_i32

    for x in data.indices do
      data[x] := init

    LM.env.array length data unit


  # initialize an empty mutable array of type T
  #
  public type.new (LM type : mutate) container.Mutable_Array T LM =>
    LM.env.array 0 (fuzion.sys.internal_array_init T 0) unit


# convenience feature for creating a new array,
# in some cases even with type inference
#
public new_array (T type, length i64, init T) container.Mutable_Array T mutate.this =>
  (mutate.array T).new mutate.this length init

last changed: 2025-06-17