@@ -209,15 +209,17 @@ class ValueStore
209209
210210 // Returns a mutable value for an ID.
211211 auto Get (IdType id) -> RefType {
212- CARBON_DCHECK (id.index >= 0 , " {0}" , id);
213- auto [chunk_index, pos] = IdToChunkIndices (id);
212+ auto raw_index = TryGetRawIndex (id);
213+ CARBON_DCHECK (raw_index >= 0 , " {0}" , id);
214+ auto [chunk_index, pos] = RawIndexToChunkIndices (raw_index);
214215 return chunks_[chunk_index].Get (pos);
215216 }
216217
217218 // Returns the value for an ID.
218219 auto Get (IdType id) const -> ConstRefType {
219- CARBON_DCHECK (id.index >= 0 , " {0}" , id);
220- auto [chunk_index, pos] = IdToChunkIndices (id);
220+ auto raw_index = TryGetRawIndex (id);
221+ CARBON_DCHECK (raw_index >= 0 , " {0}" , id);
222+ auto [chunk_index, pos] = RawIndexToChunkIndices (raw_index);
221223 return chunks_[chunk_index].Get (pos);
222224 }
223225
@@ -310,9 +312,18 @@ class ValueStore
310312 }
311313
312314 auto GetIdTag () const -> IdTag { return tag_; }
315+ // Removes the IdTag from the `id` and returns the absolute index into the
316+ // store. Not valid to call for negative values, which are not part of the
317+ // store.
313318 auto GetRawIndex (IdT id) const -> int32_t {
319+ auto index = TryGetRawIndex (id);
320+ CARBON_DCHECK (index >= 0 );
321+ return index;
322+ }
323+ // Like GetRawIndex() but accepts negative values like `None` and passes them
324+ // through, even though they would not be a valid index into an array.
325+ auto TryGetRawIndex (IdT id) const -> int32_t {
314326 auto index = tag_.Remove (id.index );
315- CARBON_DCHECK (index >= 0 , " {0}" , index);
316327#ifndef NDEBUG
317328 if (index >= size_) {
318329 // Attempt to decompose id.index to include extra detail in the check
@@ -470,12 +481,6 @@ class ValueStore
470481 return {chunk, pos};
471482 }
472483
473- // Converts an id into an index into the set of chunks, and an offset into
474- // that specific chunk.
475- auto IdToChunkIndices (IdType id) const -> std::pair<int32_t, int32_t> {
476- return RawIndexToChunkIndices (GetRawIndex (id));
477- }
478-
479484 // Number of elements added to the store. The number should never exceed what
480485 // fits in an `int32_t`, which is checked in non-optimized builds in Add().
481486 int32_t size_ = 0 ;
0 commit comments