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

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

# Map -- an abstract map from keys K to values V
#
public Map(public K, V type) ref is

  # number of entries in this map
  #
  public size i32 => abstract


  # get the value k is mapped to, or nil if none.
  #
  public index [] (k K) option V => abstract


  # get a sequence of all key/value pairs in this map
  #
  public items Sequence (tuple K V) => abstract


  # get a sequence of all keys in this map
  #
  public keys Sequence K =>
    items.map K (t -> (k,_) := t; k)


  # check if key k is present in the set of keys
  #
  public has (k K) => Map.this[k]??


  # get a sequence of all values in this map
  #
  public values Sequence V =>
    items.map V (t -> (_,v) := t; v)


  # create a string containing all mappings
  #
  public redef as_string =>
    for
      r := "", r + c + "($k => {index[] k})"
      k in keys
      c := "", ", "
    else
      r