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,

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

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

       _ 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 type : idx, i I) T
  =>
    data[i]


  # set element at given index i to given value o
  #
  public redef set [ ] (I type : idx, i I, 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]
           new_data
    d[length.as_i32] := o
    set data := d
    set length := length+1


  # create immutable array of length len from this
  #
  public as_array(len i32) universe.array T
    pre debug: len <= length.as_i32
  =>
    array len (i -> data[i])


  # 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
   (
    # length of the array to create
    length i64,

    # initial value for elements
    init T

   ) mutate.this.array T
  =>
    data := fuzion.sys.internal_array_init T length.as_i32

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

    mutate.this.env.array data length unit


  # initialize one-dimensional mutable array from a Sequence
  #
  public type.from_Sequence(s Sequence T) mutate.this.array T
  =>
    ab := s.as_array_backed

    length := ab.count

    data := fuzion.sys.internal_array_init T length

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

    mutate.this.env.array data length.as_i64 unit


  # initialize an empty mutable array of type T
  #
  public type.empty mutate.this.array T =>
    mutate.this.env.array (fuzion.sys.internal_array_init T 0) 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) mutate.this.array T
=>
  (mutate.this.array T).new length init

last changed: 2026-03-05