allocate method

({bool grew, bool isNew, int nid}) allocate(
  1. TKey key
)

Allocates a nid for key. Idempotent for already-registered keys.

  • nid — the handle to use.
  • isNewtrue if this call registered the key. false means the returned nid was already in use and no per-nid initialization is required.
  • grewtrue if the call appended a fresh slot at the tail (i.e. length increased). false means a slot was recycled from the free list. Callers that hold per-nid dense arrays must grow those arrays to match length when grew is true; when grew is false but isNew is true, the per-nid slot at nid carries stale data from a prior occupant and must be reset.

Implementation

({int nid, bool isNew, bool grew}) allocate(TKey key) {
  final existing = _keyToNid[key];
  if (existing != null) {
    return (nid: existing, isNew: false, grew: false);
  }
  final int nid;
  final bool grew;
  if (_freeNids.isNotEmpty) {
    nid = _freeNids.removeLast();
    _nidToKey[nid] = key;
    grew = false;
  } else {
    nid = _nextNid++;
    _nidToKey.add(key);
    grew = true;
  }
  _keyToNid[key] = nid;
  return (nid: nid, isNew: true, grew: grew);
}