Class ConcurrentIntIntMap

java.lang.Object
org.antlr.v4.runtime.atn.ConcurrentIntIntMap

final class ConcurrentIntIntMap extends Object
Thread-safe int → int map for rare-write / frequent-read workloads.

Used by the LL(1) prediction cache on ATN: many parser threads may read the shared ATN while occasional writers insert newly discovered single-token predictions. Reads are lock-free and allocation-free; writes copy-on-write under a monitor and publish via a volatile reference.

Missing keys return MISSING (0). Predicted alternatives are always ≥ 1 (ATN.INVALID_ALT_NUMBER is 0), so the HPPC default empty value matches the domain without a second probe.

Mutating operations that must report a previous value or enforce ConcurrentMap-style atomicity (put(int, int), putIfAbsent(int, int), remove(int), replace(int, int, int)) complete under the same monitor so ConcurrentIntIntMapView can honor the JDK map contract without a dual store.

Not part of any public or protected API. HPPC types stay package-private implementation detail.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int[]
     
    private final Object
     
    private com.carrotsearch.hppc.IntIntHashMap
    Published snapshot.
    (package private) static final int
    Sentinel returned by get(int) when the key is absent.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    (package private) void
    Drops every entry (used by ATN.clearDFA()).
    (package private) boolean
    containsValue(int value)
    Whether any published entry has the given value.
    (package private) int
    get(int key)
     
    (package private) int
    put(int key, int value)
    Associates key with value.
    (package private) int
    putIfAbsent(int key, int value)
    Associates key with value only if absent.
    (package private) int
    remove(int key)
    Removes the mapping for key if present.
    (package private) boolean
    remove(int key, int expectedValue)
    Removes the mapping for key only if it is currently mapped to expectedValue.
    (package private) int
    replace(int key, int newValue)
    Replaces the entry for key only if present.
    (package private) boolean
    replace(int key, int oldValue, int newValue)
    Replaces the entry for key only if currently mapped to oldValue.
    (package private) int
    Approximate size of the published snapshot (for diagnostics/tests).
    (package private) int[]
    Immutable snapshot of all entries as interleaved key, value pairs (length 2 * size()).

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • MISSING

      static final int MISSING
      Sentinel returned by get(int) when the key is absent. Equal to ATN.INVALID_ALT_NUMBER so callers can treat zero as a miss.
      See Also:
    • EMPTY_ENTRIES

      private static final int[] EMPTY_ENTRIES
    • lock

      private final Object lock
    • map

      private volatile com.carrotsearch.hppc.IntIntHashMap map
      Published snapshot. Readers load once and consult the snapshot without further synchronization.
  • Constructor Details

    • ConcurrentIntIntMap

      ConcurrentIntIntMap()
  • Method Details

    • get

      int get(int key)
      Parameters:
      key - packed map key
      Returns:
      stored value, or MISSING if absent
    • put

      int put(int key, int value)
      Associates key with value.
      Returns:
      the previous value associated with key, or MISSING if there was no mapping
    • putIfAbsent

      int putIfAbsent(int key, int value)
      Associates key with value only if absent.
      Returns:
      the existing value if the key was present, or MISSING if the mapping was installed
    • remove

      int remove(int key)
      Removes the mapping for key if present.
      Returns:
      the previous value, or MISSING if absent
    • remove

      boolean remove(int key, int expectedValue)
      Removes the mapping for key only if it is currently mapped to expectedValue.
      Returns:
      true if the entry was removed
    • replace

      boolean replace(int key, int oldValue, int newValue)
      Replaces the entry for key only if currently mapped to oldValue.
      Returns:
      true if the value was replaced
    • replace

      int replace(int key, int newValue)
      Replaces the entry for key only if present.
      Returns:
      the previous value, or MISSING if absent
    • clear

      void clear()
      Drops every entry (used by ATN.clearDFA()).
    • size

      int size()
      Approximate size of the published snapshot (for diagnostics/tests).
    • snapshotEntries

      int[] snapshotEntries()
      Immutable snapshot of all entries as interleaved key, value pairs (length 2 * size()). Used by ConcurrentIntIntMapView.entrySet() so the JDK map façade can iterate without exposing HPPC types. Safe under concurrent writers: iteration walks a frozen array published with the volatile map load.
    • containsValue

      boolean containsValue(int value)
      Whether any published entry has the given value. Scans the current snapshot (lock-free). Used by the JDK view's containsValue.