From 5ec7f81e6657eb2a45ef428f5b63c20cbf5986b4 Mon Sep 17 00:00:00 2001 From: Tor Brede Vekterli Date: Mon, 17 Jun 2019 15:17:47 +0000 Subject: Replace GCC-only `std::_Identity` and `std::_Select1st` with own code Put in `stllike` submodule since it's technically hoisted from _a_ STL implementation...! `vespalib::Identity` can be replaced with `std::identity` once on a C++20 compiler. --- .../src/vespa/searchlib/expression/resultvector.h | 7 ++++--- .../src/vespa/vespalib/stllike/lrucache_map.h | 3 ++- vespalib/src/tests/stllike/hashtable_test.cpp | 3 ++- vespalib/src/vespa/vespalib/stllike/hash_map.h | 3 ++- vespalib/src/vespa/vespalib/stllike/hash_map.hpp | 11 ++++++----- vespalib/src/vespa/vespalib/stllike/hash_set.h | 3 ++- vespalib/src/vespa/vespalib/stllike/hash_set.hpp | 5 +++-- vespalib/src/vespa/vespalib/stllike/identity.h | 18 ++++++++++++++++++ vespalib/src/vespa/vespalib/stllike/select.h | 17 +++++++++++++++++ 9 files changed, 56 insertions(+), 14 deletions(-) create mode 100644 vespalib/src/vespa/vespalib/stllike/identity.h create mode 100644 vespalib/src/vespa/vespalib/stllike/select.h diff --git a/searchlib/src/vespa/searchlib/expression/resultvector.h b/searchlib/src/vespa/searchlib/expression/resultvector.h index cd29178f24f..f1f863edf12 100644 --- a/searchlib/src/vespa/searchlib/expression/resultvector.h +++ b/searchlib/src/vespa/searchlib/expression/resultvector.h @@ -11,6 +11,7 @@ #include "stringbucketresultnode.h" #include "rawbucketresultnode.h" #include +#include #include namespace search::expression { @@ -214,7 +215,7 @@ struct GetString { }; template -class NumericResultNodeVectorT : public ResultNodeVectorT, std::_Identity > +class NumericResultNodeVectorT : public ResultNodeVectorT, vespalib::Identity> { public: ResultNode & flattenMultiply(ResultNode & r) const override { @@ -366,7 +367,7 @@ public: const FloatBucketResultNode& getNullBucket() const override { return FloatBucketResultNode::getNull(); } }; -class StringResultNodeVector : public ResultNodeVectorT, std::_Identity > +class StringResultNodeVector : public ResultNodeVectorT, vespalib::Identity> { public: StringResultNodeVector() { } @@ -375,7 +376,7 @@ public: const StringBucketResultNode& getNullBucket() const override { return StringBucketResultNode::getNull(); } }; -class RawResultNodeVector : public ResultNodeVectorT, std::_Identity > +class RawResultNodeVector : public ResultNodeVectorT, vespalib::Identity> { public: RawResultNodeVector() { } diff --git a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h index 128a7dc9424..07137263cf6 100644 --- a/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h +++ b/staging_vespalib/src/vespa/vespalib/stllike/lrucache_map.h @@ -3,6 +3,7 @@ #include #include +#include #include namespace vespalib { @@ -29,7 +30,7 @@ struct LruParam { typedef LinkedValue LV; typedef std::pair< K, LV > value_type; - typedef std::_Select1st< value_type > select_key; + typedef vespalib::Select1st< value_type > select_key; typedef K Key; typedef V Value; typedef H Hash; diff --git a/vespalib/src/tests/stllike/hashtable_test.cpp b/vespalib/src/tests/stllike/hashtable_test.cpp index 4948faf450f..877a5dddcb5 100644 --- a/vespalib/src/tests/stllike/hashtable_test.cpp +++ b/vespalib/src/tests/stllike/hashtable_test.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include #include @@ -63,7 +64,7 @@ TEST("require that hashtable can store pairs of ") { } template using set_hashtable = - hashtable, std::equal_to, std::_Identity>; + hashtable, std::equal_to, Identity>; TEST("require that hashtable can be copied") { set_hashtable table(100); diff --git a/vespalib/src/vespa/vespalib/stllike/hash_map.h b/vespalib/src/vespa/vespalib/stllike/hash_map.h index 0de03cb97ee..5eae4cea55e 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_map.h +++ b/vespalib/src/vespa/vespalib/stllike/hash_map.h @@ -3,6 +3,7 @@ #include "hashtable.h" #include "hash_fun.h" +#include "select.h" namespace vespalib { @@ -13,7 +14,7 @@ public: typedef std::pair value_type; typedef K key_type; typedef V mapped_type; - using HashTable = hashtable< K, value_type, H, EQ, std::_Select1st< value_type >, M >; + using HashTable = hashtable< K, value_type, H, EQ, Select1st, M >; private: HashTable _ht; public: diff --git a/vespalib/src/vespa/vespalib/stllike/hash_map.hpp b/vespalib/src/vespa/vespalib/stllike/hash_map.hpp index 2ca6b97748f..311a256be76 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_map.hpp +++ b/vespalib/src/vespa/vespalib/stllike/hash_map.hpp @@ -3,6 +3,7 @@ #include "hash_map_insert.hpp" #include "hashtable.hpp" +#include "select.h" namespace vespalib { @@ -68,11 +69,11 @@ hash_map::getMemoryUsed() const #define VESPALIB_HASH_MAP_INSTANTIATE_H_E_M(K, V, H, E, M) \ template class vespalib::hash_map; \ - template class vespalib::hashtable, H, E, std::_Select1st>, M>; \ - template vespalib::hashtable, H, E, std::_Select1st>, M>::insert_result \ - vespalib::hashtable, H, E, std::_Select1st>, M>::insert(std::pair &&); \ - template vespalib::hashtable, H, E, std::_Select1st>, M>::insert_result \ - vespalib::hashtable, H, E, std::_Select1st>, M>::insertInternal(std::pair &&); \ + template class vespalib::hashtable, H, E, vespalib::Select1st>, M>; \ + template vespalib::hashtable, H, E, vespalib::Select1st>, M>::insert_result \ + vespalib::hashtable, H, E, vespalib::Select1st>, M>::insert(std::pair &&); \ + template vespalib::hashtable, H, E, vespalib::Select1st>, M>::insert_result \ + vespalib::hashtable, H, E, vespalib::Select1st>, M>::insertInternal(std::pair &&); \ template class vespalib::Array>>; #define VESPALIB_HASH_MAP_INSTANTIATE_H_E(K, V, H, E) \ diff --git a/vespalib/src/vespa/vespalib/stllike/hash_set.h b/vespalib/src/vespa/vespalib/stllike/hash_set.h index 7a2db4735aa..919e5e8c47f 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_set.h +++ b/vespalib/src/vespa/vespalib/stllike/hash_set.h @@ -3,6 +3,7 @@ #include "hashtable.h" #include "hash_fun.h" +#include "identity.h" #include namespace vespalib { @@ -11,7 +12,7 @@ template< typename K, typename H = vespalib::hash, typename EQ = std::equal_t class hash_set { private: - using HashTable = hashtable< K, K, H, EQ, std::_Identity, M>; + using HashTable = hashtable< K, K, H, EQ, Identity, M>; HashTable _ht; public: typedef typename HashTable::iterator iterator; diff --git a/vespalib/src/vespa/vespalib/stllike/hash_set.hpp b/vespalib/src/vespa/vespalib/stllike/hash_set.hpp index 814c96a3c85..19114798806 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_set.hpp +++ b/vespalib/src/vespa/vespalib/stllike/hash_set.hpp @@ -3,6 +3,7 @@ #include "hash_set_insert.hpp" #include "hashtable.hpp" +#include "identity.h" namespace vespalib { @@ -84,11 +85,11 @@ hash_set::insert(K &&value) { #define VESPALIB_HASH_SET_INSTANTIATE(K) \ template class vespalib::hash_set; \ - template class vespalib::hashtable, std::equal_to<>, std::_Identity>; \ + template class vespalib::hashtable, std::equal_to<>, vespalib::Identity>; \ template class vespalib::Array>; #define VESPALIB_HASH_SET_INSTANTIATE_H(K, H) \ template class vespalib::hash_set; \ - template class vespalib::hashtable, std::_Identity>; \ + template class vespalib::hashtable, vespalib::Identity>; \ template class vespalib::Array>; diff --git a/vespalib/src/vespa/vespalib/stllike/identity.h b/vespalib/src/vespa/vespalib/stllike/identity.h new file mode 100644 index 00000000000..06019fb4690 --- /dev/null +++ b/vespalib/src/vespa/vespalib/stllike/identity.h @@ -0,0 +1,18 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +#include + +namespace vespalib { + +// Functor which returns its argument unchanged. +// Functionally identical to C++20's std::identity +// TODO remove and replace with std::identity once it is available. +struct Identity { + template + constexpr T&& operator()(T&& v) const noexcept { + return std::forward(v); + } +}; + +} diff --git a/vespalib/src/vespa/vespalib/stllike/select.h b/vespalib/src/vespa/vespalib/stllike/select.h new file mode 100644 index 00000000000..28bcd6a01fc --- /dev/null +++ b/vespalib/src/vespa/vespalib/stllike/select.h @@ -0,0 +1,17 @@ +// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +#pragma once + +namespace vespalib { + +// Convenience functor for extracting the first element of a std::pair (or compatible type) +template +struct Select1st { + constexpr typename Pair::first_type& operator()(Pair& p) const noexcept { + return p.first; + } + constexpr const typename Pair::first_type& operator()(const Pair& p) const noexcept { + return p.first; + } +}; + +} -- cgit v1.2.3