115 template <
typename Self,
typename Fn>
116 static auto Visit(Self& self, Fn fn) {
117 if (std::holds_alternative<detail::DenseElementStorage>(self.impl_)) {
118 return fn(std::get<detail::DenseElementStorage>(self.impl_));
120 return fn(std::get<detail::SparseElementStorage>(self.impl_));
130 int64_t
Add(
const absl::string_view name) {
131 return Visit(*
this, [name](
auto& impl) {
return impl.Add(name); });
137 bool Erase(
const int64_t
id) {
return AsSparse().Erase(
id); }
141 return Visit(*
this, [
id](
auto& impl) {
return impl.exists(
id); });
146 absl::StatusOr<absl::string_view>
GetName(
const int64_t
id)
const {
147 return Visit(*
this, [
id](
auto& impl) {
return impl.GetName(
id); });
154 return Visit(*
this, [](
auto& impl) {
return impl.next_id(); });
159 std::vector<int64_t>
AllIds()
const;
163 return Visit(*
this, [](
auto& impl) {
return impl.size(); });
172 AsSparse().ensure_next_id_at_least(
id);
178 if (
auto* sparse = std::get_if<detail::SparseElementStorage>(&impl_)) {
181 impl_ = detail::SparseElementStorage(
182 std::move(std::get<detail::DenseElementStorage>(impl_)));
183 return std::get<detail::SparseElementStorage>(impl_);
186 std::variant<detail::DenseElementStorage, detail::SparseElementStorage> impl_;