allocate method
Allocates a nid for key. Idempotent for already-registered keys.
nid— the handle to use.isNew—trueif this call registered the key.falsemeans the returned nid was already in use and no per-nid initialization is required.grew—trueif the call appended a fresh slot at the tail (i.e. length increased).falsemeans a slot was recycled from the free list. Callers that hold per-nid dense arrays must grow those arrays to match length whengrewistrue; whengrewisfalsebutisNewistrue, the per-nid slot atnidcarries 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);
}