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

array2.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)
#
#  Author: Fridtjof Siebert (siebert@tokiwa.software)
#
# -----------------------------------------------------------------------

# array(length0, length1) -- two-dimensional immutable array
#
# array provides two-dimensional immutable arrays. These are actually
# one-dimensional immutable arrays with an additional access function with
# two index parameters.
#
public array2(redef T type,
       public length0, length1 i32,
       init2 (i32, i32) -> T)
 : array T (fuzion.sys.internal_array_init T length0*length1) unit unit unit
  pre
    safety: length0 ≥ 0,
    safety: length1 ≥ 0,
    safety: length0 *? length1 >=? 0
is

  # indices range in first dimension
  #
  public indices0 => 0..length0-1


  # indices range in second dimension
  #
  public indices1 => 0..length1-1

  # all pairs of indices: (0,0), (0,1), (0,2), .. (length0-1, length1-1)
  #
  public index_pairs =>
    indices0.flat_map_sequence (tuple i32 i32) (i ->
      indices1.map j->(i,j))


  for i1 in indices0 do
    for i2 in indices1 do
      internal_array[i1 * length1 + i2] := init2 i1 i2

  public index [ ] (i1, i2 i32) T
    pre
      safety: 0 ≤ i1 < length0,
      safety: 0 ≤ i2 < length1
  is
    array2.this[i1 * length1 + 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 =>
    ("["
      + ascii.lf_str
      + indices0
          .map(i -> "  " + (slice i*length1 (i+1)*length1))
          .fold(String.type.concat("," + ascii.lf_str))
      + ascii.lf_str + "]")


  # get a list of tuples indices and elements in this array
  #
  public enumerate2 list (tuple i32 i32 T) /* NYI: '(i32, i32, T)' does not work yet */ is
    if length = 0
      nil
    else
      enumerate_cons 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 i32) : Cons (i32, i32, T) (list (tuple i32 i32 T))
    pre
      debug: 0 ≤ i < length0,
      debug: 0 ≤ j < length1
  is
    head => (i, j, index[] i j)
    tail list (tuple i32 i32 T) is
      if      j < length1-1 then enumerate_cons i j+1
      else if i < length0-1 then enumerate_cons i+1 0
      else                       nil