Skip to content

Commit eaf6b94

Browse files
committed
get-raw-index
1 parent 43e09e8 commit eaf6b94

File tree

3 files changed

+28
-19
lines changed

3 files changed

+28
-19
lines changed

toolchain/base/canonical_value_store.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ class CanonicalValueStore {
6969
mem_usage.Add(MemUsage::ConcatLabel(label, "set_"), bytes, bytes);
7070
}
7171

72-
auto GetRawIndex(IdT id) const -> int32_t { return values_.GetRawIndex(id); }
73-
7472
auto GetIdTag() const -> IdTag { return values_.GetIdTag(); }
73+
auto GetRawIndex(IdT id) const -> int32_t { return values_.GetRawIndex(id); }
74+
auto TryGetRawIndex(IdT id) const -> int32_t {
75+
return values_.TryGetRawIndex(id);
76+
}
7577

7678
private:
7779
class KeyContext;

toolchain/base/relational_value_store.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ namespace Carbon {
2626
//
2727
// When adding to the store, the user provides the related
2828
// `RelatedStoreT::IdType` along with the value being stored, and gets back the
29-
// ID of the value in the store.
29+
// ID of the value in the store. The ids share an IdTag with the related store.
3030
//
3131
// This store requires more storage space than normal ValueStore does, as it
3232
// requires storing a bit for presence of each `RelatedStore::IdType`. And it
@@ -45,34 +45,36 @@ class RelationalValueStore {
4545
// Given the related ID and a value, stores the value and returns a mapped ID
4646
// to reference it in the store.
4747
auto Add(RelatedIdType related_id, ValueType value) -> IdT {
48-
auto related_index = related_store_->GetRawIndex(related_id);
48+
auto related_index = related_store_->TryGetRawIndex(related_id);
49+
CARBON_DCHECK(related_index >= 0, "{0}", related_id);
4950
if (static_cast<size_t>(related_index) >= values_.size()) {
5051
values_.resize(related_index + 1);
5152
}
5253
auto& opt = values_[related_index];
5354
CARBON_CHECK(!opt.has_value(),
5455
"Add with `related_id` that was already added to the store");
5556
opt.emplace(std::move(value));
56-
return IdT(related_store_->GetIdTag().Apply(related_index));
57+
return IdT(related_id.index);
5758
}
5859

5960
// Returns the ID of a value in the store if the `related_id` was previously
6061
// used to add a value to the store, or None.
6162
auto TryGetId(RelatedIdType related_id) const -> IdT {
62-
auto related_index = related_store_->GetRawIndex(related_id);
63+
auto related_index = related_store_->TryGetRawIndex(related_id);
64+
CARBON_DCHECK(related_index >= 0, "{0}", related_id);
6365
if (static_cast<size_t>(related_index) >= values_.size()) {
6466
return IdT::None;
6567
}
6668
auto& opt = values_[related_index];
6769
if (!opt.has_value()) {
6870
return IdT::None;
6971
}
70-
return IdT(related_store_->GetIdTag().Apply(related_index));
72+
return IdT(related_id.index);
7173
}
7274

7375
// Returns a value for an ID.
7476
auto Get(IdT id) const -> ConstRefType {
75-
auto index = related_store_->GetIdTag().Remove(id.index);
77+
auto index = related_store_->TryGetRawIndex(RelatedIdType(id.index));
7678
CARBON_DCHECK(index >= 0, "{0}", id);
7779
return *values_[index];
7880
}

toolchain/base/value_store.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)