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

      ) : container.Buffer T mutate.this, mutable_element
is


  # A mutable array might be inaccessible if we are running in a thread that is
  # not permitted by the underlying mutate instance to access its mutable elements
  # at this point. See `blocking_mutate` and `mutate.exclusive`.
  #
  public redef is_accessible bool
  =>
    is_exclusive


  # length of this array
  #
  public redef length i64 => data.length.as_i64


  # get element at given index i
  #
  public redef index [ ] (I type : integer, i I) T
  =>
    check_and_replace
    data[i.as_i64]


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


  # 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
   (
    I type : integer,

    # length of the array to create
    length I,

    # initial value for elements
    init I->T

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

    for x := I.zero, x+1
    while x < length
    do data[x.as_i32] := init x

    mutate.this.env.array T data


  # 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.as_i64

    data := fuzion.sys.internal_array_init T length.as_i32

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

    mutate.this.env.array T data


  # create an uninitialized mutate.array, for use in container.Dynamic_Array
  #
  module type.empty(I type : integer, len I) mutate.this.array T =>
    mutate.this.env.array T (fuzion.sys.internal_array_init T len.as_i32)


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

last changed: 2026-07-21