From 70b29fe5a5c1752158830eabc9aa273718b477af Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Thu, 6 May 2021 07:35:26 -0700 Subject: Export of internal Abseil changes -- daf5a2b9ab3507ad5fb9aebe9165933f33098b83 by Abseil Team : Absl flat containers reserve enough space even in the presence of tombstones. PiperOrigin-RevId: 372339945 -- 9a61504867ba0eccc5046d7333090fbe3439cdd9 by Abseil Team : Add benchmark for BlockingCounter PiperOrigin-RevId: 372246068 -- 91ee87e6de09fc62970667ee52654c9dcf7c478d by Evan Brown : In absl::StrSplit, support btree_multimap, and other non-std::multimap-multimaps by supporting any map type that returns iterator from insert(). Also: - Use emplace() instead of insert() when available, not just for std::(multi)map - we can potentially change some string copies to moves this way. - We no longer need the Insert class so remove it. PiperOrigin-RevId: 372209653 GitOrigin-RevId: daf5a2b9ab3507ad5fb9aebe9165933f33098b83 Change-Id: I83098fde4a722cd4b682f024d3bfa56c613f960c --- absl/strings/internal/str_split_internal.h | 68 +++++++++++++++--------------- 1 file changed, 34 insertions(+), 34 deletions(-) (limited to 'absl/strings/internal') diff --git a/absl/strings/internal/str_split_internal.h b/absl/strings/internal/str_split_internal.h index a2f41c15..17c1bfe8 100644 --- a/absl/strings/internal/str_split_internal.h +++ b/absl/strings/internal/str_split_internal.h @@ -32,7 +32,7 @@ #include #include #include -#include +#include #include #include #include @@ -182,6 +182,13 @@ template struct HasConstIterator> : std::true_type {}; +// HasEmplace::value is true iff there exists a method T::emplace(). +template +struct HasEmplace : std::false_type {}; +template +struct HasEmplace().emplace())>> + : std::true_type {}; + // IsInitializerList::value is true iff T is an std::initializer_list. More // details below in Splitter<> where this is used. std::false_type IsInitializerListDispatch(...); // default: No @@ -372,50 +379,43 @@ class Splitter { // value. template struct ConvertToContainer, true> { + using iterator = typename Container::iterator; + Container operator()(const Splitter& splitter) const { Container m; - typename Container::iterator it; + iterator it; bool insert = true; - for (const auto& sp : splitter) { + for (const absl::string_view sv : splitter) { if (insert) { - it = Inserter::Insert(&m, First(sp), Second()); + it = InsertOrEmplace(&m, sv); } else { - it->second = Second(sp); + it->second = Second(sv); } insert = !insert; } return m; } - // Inserts the key and value into the given map, returning an iterator to - // the inserted item. Specialized for std::map and std::multimap to use - // emplace() and adapt emplace()'s return value. - template - struct Inserter { - using M = Map; - template - static typename M::iterator Insert(M* m, Args&&... args) { - return m->insert(std::make_pair(std::forward(args)...)).first; - } - }; - - template - struct Inserter> { - using M = std::map; - template - static typename M::iterator Insert(M* m, Args&&... args) { - return m->emplace(std::make_pair(std::forward(args)...)).first; - } - }; - - template - struct Inserter> { - using M = std::multimap; - template - static typename M::iterator Insert(M* m, Args&&... args) { - return m->emplace(std::make_pair(std::forward(args)...)); - } - }; + // Inserts the key and an empty value into the map, returning an iterator to + // the inserted item. We use emplace() if available, otherwise insert(). + template + static absl::enable_if_t::value, iterator> InsertOrEmplace( + M* m, absl::string_view key) { + // Use piecewise_construct to support old versions of gcc in which pair + // constructor can't otherwise construct string from string_view. + return ToIter(m->emplace(std::piecewise_construct, std::make_tuple(key), + std::tuple<>())); + } + template + static absl::enable_if_t::value, iterator> InsertOrEmplace( + M* m, absl::string_view key) { + return ToIter(m->insert(std::make_pair(First(key), Second("")))); + } + + static iterator ToIter(std::pair pair) { + return pair.first; + } + static iterator ToIter(iterator iter) { return iter; } }; StringType text_; -- cgit v1.2.3