fuzion-lang.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.
#
private:public array2(
      T type,
      public length0, length1 i64,

      # the underlying one dimensional array
      #
      # multidimensional indices are mapped to one dimensional indices as follows:
      #
      # [x,y] -> [x * length1 + y]
      #
      # resulting in the order:
      #
      # arr[0,0], arr[0,1], arr[0,2], ..., arr[1,0], arr[1,1], arr[1,2], ...,
      # arr[2,0], arr[2,1], arr[2,2], ...
      #
      public base array T,
      _ unit,
      _ unit) : property.hashable
  pre
    safety: length0 ? 0
    safety: length1 ? 0
    safety: length0 *? length1 >=? 0
is


  # equality of two arrays2 is true iff `a` and `b` have the same dimensions and for all
  # `i in indices0` and `j in indices1` we have `a[i][j] = b[i][j]`.
  #
  # For performance, this will compare the length of the arrays first.
  #
  # This is defined only if T : property.equatable, it will result in a compile-time panic
  # if this is not the case.
  #
  public redef fixed type.equality(a, b array2 T) bool
  =>
    if T : property.equatable then
      (a.length0 = b.length0 &&
       a.length1 = b.length1 &&
       (for ae in a.base
            be in b.base
        until ae != be
          false
        else
          true))
    else
      compile_time_panic  # array is not equatable since element type is not


  # NOT implemented, calling this fails at compile time
  #
  # There is no natural total ordering for two-dimensional arrays
  #
  public fixed type.lteq(a, b array2 T) bool =>
    compile_time_panic # no natural total ordering for two-dimensional arrays


  # create hash code for this array2.
  #
  # This should satisfy the following condition:
  #
  #   (T.equality a b) : (T.hash_code a = T.hash_code b)
  #
  # This will result in a compile-time `panic` in case T : property.hashable does not hold.
  #
  # The algorithm used here is a variation of (XXXHash)[https://xxhash.com] as used in
  # (Python's tuple)[https://github.com/python/cpython/blob/849a80ec412c36bbca5d400a7db5645b8cf54f1f/Objects/tupleobject.c#L305]:
  #
  #   we start with a constant `hash_prime1`
  #   for each value:
  #     we take its hash code * `hash_prime2` and add it
  #     then we rotate by `hash_rotate`
  #     and multiply by `hash_prime3`
  #
  public redef fixed type.hash_code(a array2 T) u64
  =>
    if T : property.hashable then
      h0 := xxh_next
              (xxh_next xxh_first 0xe3b0c44298fc1c14) # created using https://cryptotools.dev/
              # include dimensions so reshaped arrays with identical elements hash differently
              # e.g. [[1,2,3],[4,5,6]] != [[1,2],[3,4],[5,6]]
              (a.length0.as_u64 << 32 | a.length1.as_u64)
      a.base.map (T.hash_code)
       .foldf h0 xxh_next
    else
      compile_time_panic  # array is not hashable since element type is not


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


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

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


  public index [ ] (I type : integer, i1, i2 I) T
    pre
      safety: 0 ? i1.as_i64 < length0
      safety: 0 ? i2.as_i64 < length1
  =>
    array2.this.base[i1.as_i64 * length1 + i2.as_i64]


  # 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  String : encodings =>
    c =>
      indices0
        .map (i -> "  " + (base.slice i*length1 (i+1)*length1))
        .fold (String.concat ","+ascii.lf_str)
    "[$(ascii.lf_str)$c$(ascii.lf_str)]"


  # get a list of tuples indices and elements in this array
  #
  public enumerate2 list (i64, i64, T) =>
    if base.length = 0 then 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 i64) : Cons (i64, i64, T) (list (tuple i64 i64 T))
    pre
      debug: 0 ? i < length0
      debug: 0 ? j < length1
  is
    public redef head tuple i64 i64 T => (i, j, index[] _ i j)
    public redef tail list (tuple i64 i64 T) =>
      if      j < length1-1 then enumerate_cons i j+1
      else if i < length0-1 then enumerate_cons i+1 0
      else                       nil


  # the transpose of the array
  #
  # flips the array along its diagonal
  #
  public transpose array2 T =>
    (array2 T).new i64 length1 length0 i,j->index[](_, j, i)


  # create an array2 from a Sequence of a Sequence of elements
  #
  public type.create2(s Sequence (Sequence T)) array2 T =>
    a := s.map (.as_array)
          .as_array
    (array2 T).new _ a.count a[0].count i,j->a[i][j]



  # 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 type.new(I type : integer,
                  length0, length1 I,
                  init (I, I) -> T
                 ) array2 T
    pre
      safety: length0 ? 0
      safety: length1 ? 0
      safety: length0.as_i64 *? length1.as_i64 >=? 0
      debug : length0.fits_in_i64
      debug : length1.fits_in_i64
  =>

    base := array T I length0*length1 (i->init (i/length1) (i%length1))

    array2 T length0.as_i64 length1.as_i64 base unit unit



# 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(T type,
              I type : integer,
              length0, length1 I,
              init (I, I) -> T) array2 T
  pre
    safety: length0 ? 0
    safety: length1 ? 0
    safety: length0.as_i64 *? length1.as_i64 >=? 0
    debug : length0.fits_in_i64
    debug : length1.fits_in_i64
=>
  (array2 T).new I length0 length1 init

last changed: 2026-10-07