1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
// -*-c++-*-
// vim: set ft=cpp:
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file LICENSE.rst or https://cmake.org/licensing for details. */
#pragma once
#include <algorithm>
#include <iterator>
#include <memory>
#include <utility>
#include <cm/type_traits>
#include <cmext/iterator>
#include <cmext/type_traits>
#if defined(__SUNPRO_CC) && defined(__sparc)
# include <list>
# include <string>
# include <vector>
#endif
namespace cm {
#if defined(__SUNPRO_CC) && defined(__sparc)
// Oracle DeveloperStudio C++ compiler on Solaris/Sparc fails to compile
// templates with constraints.
// So, on this platform, use only simple templates.
# define APPEND_TWO(C1, C2) \
template <typename T, typename U> \
void append(C1<std::unique_ptr<T>>& v, C2<std::unique_ptr<U>>&& r) \
{ \
std::transform( \
r.begin(), r.end(), std::back_inserter(v), \
[](std::unique_ptr<U>& item) { return std::move(item); }); \
r.clear(); \
} \
\
template <typename T, typename U> \
void append(C1<T*>& v, C2<std::unique_ptr<U>> const& r) \
{ \
std::transform( \
r.begin(), r.end(), std::back_inserter(v), \
[](const std::unique_ptr<U>& item) { return item.get(); }); \
}
# define APPEND_ONE(C) \
template <typename T, typename InputIt, \
cm::enable_if_t<cm::is_input_iterator<InputIt>::value, int> = \
0> \
void append(C<T>& v, InputIt first, InputIt last) \
{ \
v.insert(v.end(), first, last); \
} \
\
template <typename T, typename Range, \
cm::enable_if_t<cm::is_input_range<Range>::value, int> = 0> \
void append(C<T>& v, Range const& r) \
{ \
v.insert(v.end(), r.begin(), r.end()); \
}
# define APPEND(C) \
APPEND_TWO(C, C) \
APPEND_ONE(C)
# define APPEND_MIX(C1, C2) \
APPEND_TWO(C1, C2) \
APPEND_TWO(C2, C1)
// For now, manage only support for std::vector, std::list, and
// std::basic_string. Other sequential container support can be added if
// needed.
APPEND(std::vector)
APPEND(std::list)
APPEND(std::basic_string)
APPEND_MIX(std::vector, std::list)
APPEND_MIX(std::vector, std::basic_string)
APPEND_MIX(std::list, std::basic_string)
# undef APPEND
# undef APPEND_MIX
# undef APPEND_TWO
# undef APPEND_ONE
#else
template <
typename Container1, typename Container2,
cm::enable_if_t<
cm::is_sequence_container<Container1>::value &&
cm::is_unique_ptr<typename Container1::value_type>::value &&
cm::is_unique_ptr<typename Container2::value_type>::value &&
std::is_convertible<typename Container2::value_type::pointer,
typename Container1::value_type::pointer>::value,
int> = 0>
void append(Container1& v, Container2&& r)
{
std::transform(
r.begin(), r.end(), std::back_inserter(v),
[](typename Container2::value_type& item) { return std::move(item); });
r.clear();
}
template <typename Container1, typename Container2,
cm::enable_if_t<
cm::is_sequence_container<Container1>::value &&
std::is_pointer<typename Container1::value_type>::value &&
cm::is_unique_ptr<typename Container2::value_type>::value &&
std::is_convertible<typename Container2::value_type::pointer,
typename Container1::value_type>::value,
int> = 0>
# if defined(__SUNPRO_CC)
void append(Container1& v, Container2 const& r, detail::overload_selector<0>)
# else
void append(Container1& v, Container2 const& r)
# endif
{
std::transform(
r.begin(), r.end(), std::back_inserter(v),
[](typename Container2::value_type const& item) { return item.get(); });
}
template <
typename Container, typename InputIt,
cm::enable_if_t<
cm::is_sequence_container<Container>::value &&
cm::is_input_iterator<InputIt>::value &&
std::is_convertible<typename std::iterator_traits<InputIt>::value_type,
typename Container::value_type>::value,
int> = 0>
void append(Container& v, InputIt first, InputIt last)
{
v.insert(v.end(), first, last);
}
template <typename Container, typename Range,
cm::enable_if_t<
cm::is_sequence_container<Container>::value &&
cm::is_input_range<Range>::value &&
!cm::is_unique_ptr<typename Container::value_type>::value &&
!cm::is_unique_ptr<typename Range::value_type>::value &&
std::is_convertible<typename Range::value_type,
typename Container::value_type>::value,
int> = 0>
# if defined(__SUNPRO_CC)
void append(Container& v, Range const& r, detail::overload_selector<1>)
# else
void append(Container& v, Range const& r)
# endif
{
v.insert(v.end(), r.begin(), r.end());
}
# if defined(__SUNPRO_CC)
template <typename T, typename U>
void append(T& v, U const& r)
{
cm::append(v, r, detail::overload_selector<1>{});
}
# endif
#endif
#if defined(__SUNPRO_CC)
template <typename Iterator, typename Key>
auto contains(Iterator first, Iterator last, Key const& key,
detail::overload_selector<1>) -> decltype(first->first == key)
#else
template <typename Iterator, typename Key,
cm::enable_if_t<
cm::is_input_iterator<Iterator>::value &&
std::is_convertible<Key,
typename std::iterator_traits<
Iterator>::value_type::first_type>::value,
int> = 0>
bool contains(Iterator first, Iterator last, Key const& key)
#endif
{
return std::find_if(
first, last,
[&key](
typename std::iterator_traits<Iterator>::value_type const& item) {
return item.first == key;
}) != last;
}
#if defined(__SUNPRO_CC)
template <typename Iterator, typename Key>
bool contains(Iterator first, Iterator last, Key const& key,
detail::overload_selector<0>)
#else
template <
typename Iterator, typename Key,
cm::enable_if_t<
cm::is_input_iterator<Iterator>::value &&
std::is_convertible<
Key, typename std::iterator_traits<Iterator>::value_type>::value,
int> = 0>
bool contains(Iterator first, Iterator last, Key const& key)
#endif
{
return std::find(first, last, key) != last;
}
#if defined(__SUNPRO_CC)
template <typename Iterator, typename Key>
bool contains(Iterator first, Iterator last, Key const& key)
{
return contains(first, last, key, detail::overload_selector<1>{});
}
#endif
#if defined(__SUNPRO_CC)
template <typename Range, typename Key>
auto contains(Range const& range, Key const& key,
detail::overload_selector<1>) -> decltype(range.find(key) !=
range.end())
#else
template <
typename Range, typename Key,
cm::enable_if_t<cm::is_associative_container<Range>::value ||
cm::is_unordered_associative_container<Range>::value,
int> = 0>
bool contains(Range const& range, Key const& key)
#endif
{
return range.find(key) != range.end();
}
#if defined(__SUNPRO_CC)
template <typename Range, typename Key>
bool contains(Range const& range, Key const& key, detail::overload_selector<0>)
#else
template <
typename Range, typename Key,
cm::enable_if_t<cm::is_input_range<Range>::value &&
!(cm::is_associative_container<Range>::value ||
cm::is_unordered_associative_container<Range>::value),
int> = 0>
bool contains(Range const& range, Key const& key)
#endif
{
return std::find(std::begin(range), std::end(range), key) != std::end(range);
}
#if defined(__SUNPRO_CC)
template <typename Range, typename Key>
bool contains(Range const& range, Key const& key)
{
return contains(range, key, detail::overload_selector<1>{});
}
#endif
} // namespace cm
|