Class CacheMap<T>

java.lang.Object
ca.uqac.lif.cep.tuples.CacheMap<T>
All Implemented Interfaces:
Map<String,T>

public final class CacheMap<T> extends Object implements Map<String,T>
Implementation of an immutable map.
  • After its instantiation, the object is immutable: all its fields are declared private final and no method can ever change their value. (As a matter of fact, all its methods are also final.) This entails that methods that normally should be able to modify the contents of a Map (e.g. put() or clear() here have no effect.
  • Internally, the tuple uses plain arrays (instead of a HashMap) for storing keys and values. For tuples with, a small number of keys, this should actually provide faster access than a HashMap. In all cases, arrays use up less memory than a HashMap.
  • Because of this, one can also ask for the index of a key/value pair, and obtain a value based on its index (rather than its key). Assuming that all tuples in a stream have their key/value pairs arranged in the same order, this means one can ask for the index of a key a first time, and from that point on query the remaining tuples by directly providing the index.
Author:
Sylvain Hallé
  • Constructor Details

    • CacheMap

      public CacheMap(String[] names, Object[] values)
    • CacheMap

      public CacheMap(String[] names)
  • Method Details

    • clear

      public final void clear()
      Specified by:
      clear in interface Map<String,T>
    • containsKey

      public final boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,T>
    • containsValue

      public final boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<String,T>
    • entrySet

      public final Set<Map.Entry<String,T>> entrySet()
      Specified by:
      entrySet in interface Map<String,T>
    • get

      public final T get(Object key)
      Specified by:
      get in interface Map<String,T>
    • getValue

      public final Object getValue(int index)
    • getIndexOf

      public final int getIndexOf(String s)
      Get the index of a key in the current map
      Parameters:
      s - The key
      Returns:
      The index of that key, or -1 if key does not exist
    • getIndexOf

      public final int getIndexOf(String key, int index, T out)
      Retrieves a value, possibly using an index. This allows one to both get the direct index of a value in the map, if not known, and to fetch that value.
       Object o;
       cached_index = map.getIndexOf("mykey", cached_index, o);
       
      This will put the value corresponding to mykey in o, and update cached_index to the position in the array where this key was found. Later calls to getIndexOf will use that value to directly access the element, rather than look for it.
      Parameters:
      key - The key to get in the map
      index - If negative, the method will look for the key in the map to get the value. If greater than or equal to 0, the method will directly use that value to fetch the object to return.
      out - After the call, will contain a reference to the value one is looking for
      Returns:
      The index value
    • isEmpty

      public final boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,T>
    • keySet

      public final Set<String> keySet()
      Specified by:
      keySet in interface Map<String,T>
    • put

      public final T put(String key, T value)
      Specified by:
      put in interface Map<String,T>
    • putAt

      public final void putAt(int index, T value)
    • putAll

      public final void putAll(T[] values)
    • putAll

      public final void putAll(Map<? extends String,? extends T> m)
      Specified by:
      putAll in interface Map<String,T>
    • remove

      public final T remove(Object key)
      Specified by:
      remove in interface Map<String,T>
    • size

      public final int size()
      Specified by:
      size in interface Map<String,T>
    • values

      public final Collection<T> values()
      Specified by:
      values in interface Map<String,T>
    • toString

      public String toString()
      Overrides:
      toString in class Object