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

container/Mutable_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 Mutable_Map
#
# -----------------------------------------------------------------------

# Mutable_Map -- an abstract mutable map from keys K to values V
#
public Mutable_Map(public K type : property.equatable, public 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 =>
    get k


  # convenience feature to add a key-value pair to this map
  # does the same as `add k v`
  #
  public set [] (k K, v V) unit =>
    put k v


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


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


  # get a sequence of all keys in this map
  #
  public keys =>
    # NYI: BUG: t.0 does not work here, see #4408
    items.map t->t.values.0


  # get a sequence of all values in this map
  #
  public values =>
    # NYI: BUG: t.1 does not work here, see #4408
    items.map t->t.values.1


  # get the value stored in this map at key k, nil if k is not
  # contained in this map
  #
  public get(k K) option V => abstract


  # get the value stored in this map at key k,
  # if it does not exist, v is added and returned
  #
  public get_or_put(k K, v V) V =>
    match get k
      ex V => ex
      nil  => put k v
              v


  # add key-value pair to this map
  #
  # if key already exists, NYI what should happen?
  #
  public put(k K, v V) unit => abstract


  # add all key-value pairs to this map
  #
  # if key already exists, NYI what should happen?
  #
  public put_all(kvs Sequence (tuple K V)) =>
    for kv in kvs do
      put kv.values.0 kv.values.1


  # remove key from map
  #
  public remove(k K) option V => abstract


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


  # create an immutable map from this
  #
  # NYI: maybe add default implementation when there is a naive implementation only requiring property.equatable
  #
  public as_map container.Map K V => abstract


  # initializer for this mutable map
  #
  # every mutable map implementation needs to implement this
  #
  public type.empty Mutable_Map.this => abstract


/*

  NYI: BUG?: To solve this, you could change the type of the target 'k' to 'container.Mutable_Map.type.K' or convert the type of the assigned value to 'container.Mutable_Map.K'.


  # from_entries -- routine to initialize a Mutable_Map from
  # a sequence of key value tuples
  #
  public type.from_entries(kvs Sequence (tuple K V)) Mutable_Map.this =>
    m := empty
    for kv in kvs do
      m.put kv.values.0 kv.values.1
    m

*/
last changed: 2025-01-23