Class ShapedMap

All Implemented Interfaces:
Callable, FunctionItem, GroundedValue, Item, Sequence
Direct Known Subclasses:
SparseShapedMap

public class ShapedMap extends MapItem
A ShapedMap is an implementation of XDM maps optimised for the case where many maps have the same set of string-valued keys.

It has the additional property that the keys() method delivers the keys in a consistent order.

The class implements the method getWithPlan(AtomicValue, Stack) so that a lookup expression such as $record?field using a statically-known lookup key is able to use the slot number established on a previous call of the same expression, avoiding the cost of a hash lookup.

  • Field Details

  • Constructor Details

    • ShapedMap

      public ShapedMap(Shape shape, GroundedValue... values)
      Create an instance of a ShapedMap whose structure is defined by the supplied Shape
      Parameters:
      shape - the shape of the map, identifying the keys that are present
      values - the values to appear in the map, corresponding one-to-one with the keys that are defined in the Shape
  • Method Details

    • initialPut

      public void initialPut(int slot, GroundedValue value)
    • get

      public GroundedValue get(AtomicValue key)
      Get an entry from the Map
      Specified by:
      get in class MapItem
      Parameters:
      key - the value of the key
      Returns:
      the value associated with the given key, or null if the key is not present in the map.
    • getByPosition

      public GroundedValue getByPosition(int slot)
      Get a value by its zero-based position
    • getWithPlan

      public GroundedValue getWithPlan(AtomicValue key, Stack<LookupPlan> plans)
      Get an entry from the Map, supplying an execution plan which the callee can add information to.
      Overrides:
      getWithPlan in class MapItem
      Parameters:
      key - the value of the key
      plans - a list of suggested lookup plans; the callee is free to use these or ignore them as it wishes, and can also add new plans for use on future evaluation of the same expression. The plan is specific to a particular key value: in practice, the method is only used when the lookup key is known statically.
      Returns:
      the value associated with the given key, or null if the key is not present in the map.
    • get

      public GroundedValue get(String key)
      Get an entry from the map, supplying a Java String as the key
      Parameters:
      key - the key
      Returns:
      the relevant entry if present, or null if absent.
    • getU

      public GroundedValue getU(UnicodeString key)
      Get an entry from the map, supplying a UnicodeString as the key
      Overrides:
      getU in class MapItem
      Parameters:
      key - the key
      Returns:
      the relevant entry if present, or null if absent.
    • contains

      public boolean contains(String key)
      Ask whether a given string is present as a key in the map
      Parameters:
      key - the key being tested
      Returns:
      true if the key is present
    • size

      public int size()
      Get the size of the map
      Specified by:
      size in class MapItem
      Returns:
      the number of keys/entries present in this map
    • keys

      public AtomicIterator keys()
      Get the set of all key values in the map.
      Overrides:
      keys in class MapItem
      Returns:
      a set containing all the key values present in the map. Normally the order is unpredictable, but for a ShapedMap the order is defined by the Shape.
    • keyValuePairs

      public Iterable<KeyValuePair> keyValuePairs()
      Get the set of all key-value pairs in the map
      Specified by:
      keyValuePairs in class MapItem
      Returns:
      an iterable containing all the key-value pairs
    • put

      public MapItem put(AtomicValue key, GroundedValue value)
      Create a new map containing the existing entries in the map plus an additional entry, without modifying the original. If there is already an entry with the specified key, this entry is replaced by the new entry.
      Specified by:
      put in class MapItem
      Parameters:
      key - the key of the new entry
      value - the value associated with the new entry
      Returns:
      the new map containing the additional entry
    • remove

      public MapItem remove(AtomicValue key)
      Remove an entry from the map
      Specified by:
      remove in class MapItem
      Parameters:
      key - the key of the entry to be removed
      Returns:
      a new map in which the requested entry has been removed; or this map unchanged if the specified key was not present
    • getItemType

      public ItemType getItemType(TypeHierarchy th)
      Get the type of the map. This method is used largely for diagnostics, to report the type of a map when it differs from the required type.
      Overrides:
      getItemType in class MapItem
      Parameters:
      th - the type hierarchy cache
      Returns:
      the type of this map