diff options
| author | robot-piglet <[email protected]> | 2026-01-16 08:51:13 +0300 |
|---|---|---|
| committer | robot-piglet <[email protected]> | 2026-01-16 09:05:06 +0300 |
| commit | 4eadc36e1a0fc9dcef2ab571094f2ca5e78f8611 (patch) | |
| tree | 90fee328bd22033a5ed5a9f12d5be6818259b3c9 /library/cpp/containers | |
| parent | a88493a9362234e7ca6494a473d7467ecd88a156 (diff) | |
Intermediate changes
commit_hash:c5b262d1aad0ba81383708696467e0f0630050c0
Diffstat (limited to 'library/cpp/containers')
| -rw-r--r-- | library/cpp/containers/ordered_map/ordered_map.h | 39 |
1 files changed, 22 insertions, 17 deletions
diff --git a/library/cpp/containers/ordered_map/ordered_map.h b/library/cpp/containers/ordered_map/ordered_map.h index 22422901a84..ac1fbd5b78d 100644 --- a/library/cpp/containers/ordered_map/ordered_map.h +++ b/library/cpp/containers/ordered_map/ordered_map.h @@ -137,19 +137,13 @@ namespace NOrderedMap { } template <class... TArgs> - iterator insert(TArgs&&... args) { + std::pair<iterator, bool> insert(TArgs&&... args) { auto& value = TBase::emplace_back(std::forward<TArgs>(args)...); auto [mapIt, ok] = Map_.try_emplace(&Traits::GetSortProjector(value), --TBase::end()); if (!ok) { - iterator forDeleteFromList = mapIt->second; - //TODO: it is possible to skip double-lookup if be possible to mutate key by iterator, or steal previous value by insert - Map_.erase(mapIt); - TBase::erase(forDeleteFromList); - auto insertRes = Map_.try_emplace(&Traits::GetSortProjector(value), --TBase::end()); - Y_ASSERT(insertRes.second); - return insertRes.first->second; + TBase::pop_back(); } - return mapIt->second; + return {mapIt->second, ok}; } template <class... TArgs> @@ -158,11 +152,9 @@ namespace NOrderedMap { auto [mapIt, ok] = Map_.try_emplace(&Traits::GetSortProjector(value), --TBase::end()); if (!ok) { iterator forDeleteFromList = mapIt->second; - //TODO: it is possible to skip double-lookup if be possible to mutate key by iterator, or steal previous value by insert - Map_.erase(mapIt); + mapIt->first.Data = &Traits::GetSortProjector(value); + mapIt->second = --TBase::end(); TBase::erase(forDeleteFromList); - bool isOk = Map_.try_emplace(&Traits::GetSortProjector(value), --TBase::end()).second; - Y_ASSERT(isOk); } return value; } @@ -234,7 +226,7 @@ namespace NOrderedMap { } clear(); SortBy(tmp, Traits::GetSortProjector); - for (auto& x : tmp) { + for (auto&& x : std::move(tmp)) { emplace_back(std::move(x)); } } @@ -261,22 +253,35 @@ namespace NOrderedMap { } private: + struct TPtrBackdoorKey { + mutable const TKey* Data; + TPtrBackdoorKey(const TKey* x) : Data(x) {} + TPtrBackdoorKey(TPtrBackdoorKey&&) = default; + TPtrBackdoorKey(const TPtrBackdoorKey&) = default; + + const TKey& operator*() const { + return *Data; + } + operator bool() const { + return !!Data; + } + }; struct THashOperationsForPtr : public THash<TKey>, public TEqualTo<TKey> { using THashBase = THash<TKey>; using TEqualBase = TEqualTo<TKey>; template<class TKeyTransparency> - inline size_t operator()(const TKeyTransparency* ptr) const noexcept { + inline size_t operator()(const TKeyTransparency& ptr) const noexcept { Y_DEBUG_ABORT_UNLESS(ptr); return this->THashBase::operator()(*ptr); } template<class TKeyTransparencyA, class TKeyTransparencyB> - inline bool operator()(const TKeyTransparencyA* a, const TKeyTransparencyB* b) const noexcept { + inline bool operator()(const TKeyTransparencyA& a, const TKeyTransparencyB& b) const noexcept { Y_DEBUG_ABORT_UNLESS(a); Y_DEBUG_ABORT_UNLESS(b); return this->TEqualBase::operator()(*a, *b); } }; - THashMap<const TKey*, typename TBase::iterator, THashOperationsForPtr, THashOperationsForPtr> Map_{}; + THashMap<TPtrBackdoorKey, typename TBase::iterator, THashOperationsForPtr, THashOperationsForPtr> Map_{}; }; template <class TKey, class TValue> |
