mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 09:44:55 +01:00
Refactor ValueStore to use custom iterators and ranges (#7506)
Replace the llvm::map_range based anonymous ranges returned by values() and enumerate() with custom range and iterator types. This avoids exposing complex template return types and improves encapsulation. But most importantly, it is *much* cheaper to compile. This change: - Defines ValueStoreIterator, ValueStoreEnumerateIterator, and ValueStoreRange templates in the Internal namespace. - Exposes them via aliases in ValueStore: Iterator, ConstIterator, Range, MutableRange, and EnumerateRange. - Supports safe implicit conversion from Iterator to ConstIterator. - Supports operator-> even when the value type is returned by value (e.g., llvm::StringRef) by conditionally const-qualifying the pointer type. - Ensures C++20 comparison consistency with custom operator<=> and operator== definitions. - Restricts construction of these iterators and ranges to ValueStore methods by using private constructors and friend declarations. Assisted-by: Antigravity with Gemini
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "toolchain/base/value_ids.h"
|
||||
|
||||
namespace Carbon::Testing {
|
||||
@@ -44,5 +45,27 @@ TEST(ValueStore, Real) {
|
||||
EXPECT_THAT(real2.is_decimal, Eq(real2_copy.is_decimal));
|
||||
}
|
||||
|
||||
TEST(ValueStore, StringRefIteratorArrow) {
|
||||
ValueStore<IdentifierId, llvm::StringRef> store;
|
||||
store.Add("hello");
|
||||
|
||||
// Non-const values() returns MutableRange which uses Iterator
|
||||
auto mutable_range = store.values();
|
||||
auto it = mutable_range.begin();
|
||||
// This calls Iterator::operator->
|
||||
EXPECT_EQ(it->size(), 5);
|
||||
}
|
||||
|
||||
TEST(ValueStore, IteratorConversion) {
|
||||
ValueStore<IdentifierId, llvm::StringRef> store;
|
||||
store.Add("hello");
|
||||
|
||||
auto mutable_range = store.values();
|
||||
ValueStore<IdentifierId, llvm::StringRef>::Iterator it =
|
||||
mutable_range.begin();
|
||||
ValueStore<IdentifierId, llvm::StringRef>::ConstIterator cit = it;
|
||||
EXPECT_EQ(*cit, "hello");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace Carbon::Testing
|
||||
|
||||
Reference in New Issue
Block a user