misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1 | // Copyright 2017 The Abseil Authors. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | // |
| 15 | // ----------------------------------------------------------------------------- |
| 16 | // File: container.h |
| 17 | // ----------------------------------------------------------------------------- |
| 18 | // |
| 19 | // This header file provides Container-based versions of algorithmic functions |
| 20 | // within the C++ standard library. The following standard library sets of |
| 21 | // functions are covered within this file: |
| 22 | // |
| 23 | // * Algorithmic <iterator> functions |
| 24 | // * Algorithmic <numeric> functions |
| 25 | // * <algorithm> functions |
| 26 | // |
| 27 | // The standard library functions operate on iterator ranges; the functions |
| 28 | // within this API operate on containers, though many return iterator ranges. |
| 29 | // |
| 30 | // All functions within this API are named with a `c_` prefix. Calls such as |
| 31 | // `absl::c_xx(container, ...) are equivalent to std:: functions such as |
| 32 | // `std::xx(std::begin(cont), std::end(cont), ...)`. Functions that act on |
| 33 | // iterators but not conceptually on iterator ranges (e.g. `std::iter_swap`) |
| 34 | // have no equivalent here. |
| 35 | // |
| 36 | // For template parameter and variable naming, `C` indicates the container type |
| 37 | // to which the function is applied, `Pred` indicates the predicate object type |
| 38 | // to be used by the function and `T` indicates the applicable element type. |
| 39 | // |
| 40 | |
| 41 | #ifndef ABSL_ALGORITHM_CONTAINER_H_ |
| 42 | #define ABSL_ALGORITHM_CONTAINER_H_ |
| 43 | |
| 44 | #include <algorithm> |
| 45 | #include <cassert> |
| 46 | #include <iterator> |
| 47 | #include <numeric> |
| 48 | #include <type_traits> |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 49 | #include <unordered_map> |
| 50 | #include <unordered_set> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 51 | #include <utility> |
| 52 | #include <vector> |
| 53 | |
| 54 | #include "absl/algorithm/algorithm.h" |
| 55 | #include "absl/base/macros.h" |
| 56 | #include "absl/meta/type_traits.h" |
| 57 | |
| 58 | namespace absl { |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 59 | inline namespace lts_2018_12_18 { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 60 | namespace container_algorithm_internal { |
| 61 | |
| 62 | // NOTE: it is important to defer to ADL lookup for building with C++ modules, |
| 63 | // especially for headers like <valarray> which are not visible from this file |
| 64 | // but specialize std::begin and std::end. |
| 65 | using std::begin; |
| 66 | using std::end; |
| 67 | |
| 68 | // The type of the iterator given by begin(c) (possibly std::begin(c)). |
| 69 | // ContainerIter<const vector<T>> gives vector<T>::const_iterator, |
| 70 | // while ContainerIter<vector<T>> gives vector<T>::iterator. |
| 71 | template <typename C> |
| 72 | using ContainerIter = decltype(begin(std::declval<C&>())); |
| 73 | |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 74 | // An MSVC bug involving template parameter substitution requires us to use |
| 75 | // decltype() here instead of just std::pair. |
| 76 | template <typename C1, typename C2> |
| 77 | using ContainerIterPairType = |
| 78 | decltype(std::make_pair(ContainerIter<C1>(), ContainerIter<C2>())); |
| 79 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 80 | template <typename C> |
| 81 | using ContainerDifferenceType = |
| 82 | decltype(std::distance(std::declval<ContainerIter<C>>(), |
| 83 | std::declval<ContainerIter<C>>())); |
| 84 | |
| 85 | template <typename C> |
| 86 | using ContainerPointerType = |
| 87 | typename std::iterator_traits<ContainerIter<C>>::pointer; |
| 88 | |
| 89 | // container_algorithm_internal::c_begin and |
| 90 | // container_algorithm_internal::c_end are abbreviations for proper ADL |
| 91 | // lookup of std::begin and std::end, i.e. |
| 92 | // using std::begin; |
| 93 | // using std::end; |
| 94 | // std::foo(begin(c), end(c); |
| 95 | // becomes |
| 96 | // std::foo(container_algorithm_internal::begin(c), |
| 97 | // container_algorithm_internal::end(c)); |
| 98 | // These are meant for internal use only. |
| 99 | |
| 100 | template <typename C> |
| 101 | ContainerIter<C> c_begin(C& c) { return begin(c); } |
| 102 | |
| 103 | template <typename C> |
| 104 | ContainerIter<C> c_end(C& c) { return end(c); } |
| 105 | |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 106 | template <typename T> |
| 107 | struct IsUnorderedContainer : std::false_type {}; |
| 108 | |
| 109 | template <class Key, class T, class Hash, class KeyEqual, class Allocator> |
| 110 | struct IsUnorderedContainer< |
| 111 | std::unordered_map<Key, T, Hash, KeyEqual, Allocator>> : std::true_type {}; |
| 112 | |
| 113 | template <class Key, class Hash, class KeyEqual, class Allocator> |
| 114 | struct IsUnorderedContainer<std::unordered_set<Key, Hash, KeyEqual, Allocator>> |
| 115 | : std::true_type {}; |
| 116 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 117 | } // namespace container_algorithm_internal |
| 118 | |
| 119 | // PUBLIC API |
| 120 | |
| 121 | //------------------------------------------------------------------------------ |
| 122 | // Abseil algorithm.h functions |
| 123 | //------------------------------------------------------------------------------ |
| 124 | |
| 125 | // c_linear_search() |
| 126 | // |
| 127 | // Container-based version of absl::linear_search() for performing a linear |
| 128 | // search within a container. |
| 129 | template <typename C, typename EqualityComparable> |
| 130 | bool c_linear_search(const C& c, EqualityComparable&& value) { |
| 131 | return linear_search(container_algorithm_internal::c_begin(c), |
| 132 | container_algorithm_internal::c_end(c), |
| 133 | std::forward<EqualityComparable>(value)); |
| 134 | } |
| 135 | |
| 136 | //------------------------------------------------------------------------------ |
| 137 | // <iterator> algorithms |
| 138 | //------------------------------------------------------------------------------ |
| 139 | |
| 140 | // c_distance() |
| 141 | // |
| 142 | // Container-based version of the <iterator> `std::distance()` function to |
| 143 | // return the number of elements within a container. |
| 144 | template <typename C> |
| 145 | container_algorithm_internal::ContainerDifferenceType<const C> c_distance( |
| 146 | const C& c) { |
| 147 | return std::distance(container_algorithm_internal::c_begin(c), |
| 148 | container_algorithm_internal::c_end(c)); |
| 149 | } |
| 150 | |
| 151 | //------------------------------------------------------------------------------ |
| 152 | // <algorithm> Non-modifying sequence operations |
| 153 | //------------------------------------------------------------------------------ |
| 154 | |
| 155 | // c_all_of() |
| 156 | // |
| 157 | // Container-based version of the <algorithm> `std::all_of()` function to |
| 158 | // test a condition on all elements within a container. |
| 159 | template <typename C, typename Pred> |
| 160 | bool c_all_of(const C& c, Pred&& pred) { |
| 161 | return std::all_of(container_algorithm_internal::c_begin(c), |
| 162 | container_algorithm_internal::c_end(c), |
| 163 | std::forward<Pred>(pred)); |
| 164 | } |
| 165 | |
| 166 | // c_any_of() |
| 167 | // |
| 168 | // Container-based version of the <algorithm> `std::any_of()` function to |
| 169 | // test if any element in a container fulfills a condition. |
| 170 | template <typename C, typename Pred> |
| 171 | bool c_any_of(const C& c, Pred&& pred) { |
| 172 | return std::any_of(container_algorithm_internal::c_begin(c), |
| 173 | container_algorithm_internal::c_end(c), |
| 174 | std::forward<Pred>(pred)); |
| 175 | } |
| 176 | |
| 177 | // c_none_of() |
| 178 | // |
| 179 | // Container-based version of the <algorithm> `std::none_of()` function to |
| 180 | // test if no elements in a container fulfil a condition. |
| 181 | template <typename C, typename Pred> |
| 182 | bool c_none_of(const C& c, Pred&& pred) { |
| 183 | return std::none_of(container_algorithm_internal::c_begin(c), |
| 184 | container_algorithm_internal::c_end(c), |
| 185 | std::forward<Pred>(pred)); |
| 186 | } |
| 187 | |
| 188 | // c_for_each() |
| 189 | // |
| 190 | // Container-based version of the <algorithm> `std::for_each()` function to |
| 191 | // apply a function to a container's elements. |
| 192 | template <typename C, typename Function> |
| 193 | decay_t<Function> c_for_each(C&& c, Function&& f) { |
| 194 | return std::for_each(container_algorithm_internal::c_begin(c), |
| 195 | container_algorithm_internal::c_end(c), |
| 196 | std::forward<Function>(f)); |
| 197 | } |
| 198 | |
| 199 | // c_find() |
| 200 | // |
| 201 | // Container-based version of the <algorithm> `std::find()` function to find |
| 202 | // the first element containing the passed value within a container value. |
| 203 | template <typename C, typename T> |
| 204 | container_algorithm_internal::ContainerIter<C> c_find(C& c, T&& value) { |
| 205 | return std::find(container_algorithm_internal::c_begin(c), |
| 206 | container_algorithm_internal::c_end(c), |
| 207 | std::forward<T>(value)); |
| 208 | } |
| 209 | |
| 210 | // c_find_if() |
| 211 | // |
| 212 | // Container-based version of the <algorithm> `std::find_if()` function to find |
| 213 | // the first element in a container matching the given condition. |
| 214 | template <typename C, typename Pred> |
| 215 | container_algorithm_internal::ContainerIter<C> c_find_if(C& c, Pred&& pred) { |
| 216 | return std::find_if(container_algorithm_internal::c_begin(c), |
| 217 | container_algorithm_internal::c_end(c), |
| 218 | std::forward<Pred>(pred)); |
| 219 | } |
| 220 | |
| 221 | // c_find_if_not() |
| 222 | // |
| 223 | // Container-based version of the <algorithm> `std::find_if_not()` function to |
| 224 | // find the first element in a container not matching the given condition. |
| 225 | template <typename C, typename Pred> |
| 226 | container_algorithm_internal::ContainerIter<C> c_find_if_not(C& c, |
| 227 | Pred&& pred) { |
| 228 | return std::find_if_not(container_algorithm_internal::c_begin(c), |
| 229 | container_algorithm_internal::c_end(c), |
| 230 | std::forward<Pred>(pred)); |
| 231 | } |
| 232 | |
| 233 | // c_find_end() |
| 234 | // |
| 235 | // Container-based version of the <algorithm> `std::find_end()` function to |
| 236 | // find the last subsequence within a container. |
| 237 | template <typename Sequence1, typename Sequence2> |
| 238 | container_algorithm_internal::ContainerIter<Sequence1> c_find_end( |
| 239 | Sequence1& sequence, Sequence2& subsequence) { |
| 240 | return std::find_end(container_algorithm_internal::c_begin(sequence), |
| 241 | container_algorithm_internal::c_end(sequence), |
| 242 | container_algorithm_internal::c_begin(subsequence), |
| 243 | container_algorithm_internal::c_end(subsequence)); |
| 244 | } |
| 245 | |
| 246 | // Overload of c_find_end() for using a predicate evaluation other than `==` as |
| 247 | // the function's test condition. |
| 248 | template <typename Sequence1, typename Sequence2, typename BinaryPredicate> |
| 249 | container_algorithm_internal::ContainerIter<Sequence1> c_find_end( |
| 250 | Sequence1& sequence, Sequence2& subsequence, BinaryPredicate&& pred) { |
| 251 | return std::find_end(container_algorithm_internal::c_begin(sequence), |
| 252 | container_algorithm_internal::c_end(sequence), |
| 253 | container_algorithm_internal::c_begin(subsequence), |
| 254 | container_algorithm_internal::c_end(subsequence), |
| 255 | std::forward<BinaryPredicate>(pred)); |
| 256 | } |
| 257 | |
| 258 | // c_find_first_of() |
| 259 | // |
| 260 | // Container-based version of the <algorithm> `std::find_first_of()` function to |
| 261 | // find the first elements in an ordered set within a container. |
| 262 | template <typename C1, typename C2> |
| 263 | container_algorithm_internal::ContainerIter<C1> c_find_first_of(C1& container, |
| 264 | C2& options) { |
| 265 | return std::find_first_of(container_algorithm_internal::c_begin(container), |
| 266 | container_algorithm_internal::c_end(container), |
| 267 | container_algorithm_internal::c_begin(options), |
| 268 | container_algorithm_internal::c_end(options)); |
| 269 | } |
| 270 | |
| 271 | // Overload of c_find_first_of() for using a predicate evaluation other than |
| 272 | // `==` as the function's test condition. |
| 273 | template <typename C1, typename C2, typename BinaryPredicate> |
| 274 | container_algorithm_internal::ContainerIter<C1> c_find_first_of( |
| 275 | C1& container, C2& options, BinaryPredicate&& pred) { |
| 276 | return std::find_first_of(container_algorithm_internal::c_begin(container), |
| 277 | container_algorithm_internal::c_end(container), |
| 278 | container_algorithm_internal::c_begin(options), |
| 279 | container_algorithm_internal::c_end(options), |
| 280 | std::forward<BinaryPredicate>(pred)); |
| 281 | } |
| 282 | |
| 283 | // c_adjacent_find() |
| 284 | // |
| 285 | // Container-based version of the <algorithm> `std::adjacent_find()` function to |
| 286 | // find equal adjacent elements within a container. |
| 287 | template <typename Sequence> |
| 288 | container_algorithm_internal::ContainerIter<Sequence> c_adjacent_find( |
| 289 | Sequence& sequence) { |
| 290 | return std::adjacent_find(container_algorithm_internal::c_begin(sequence), |
| 291 | container_algorithm_internal::c_end(sequence)); |
| 292 | } |
| 293 | |
| 294 | // Overload of c_adjacent_find() for using a predicate evaluation other than |
| 295 | // `==` as the function's test condition. |
| 296 | template <typename Sequence, typename BinaryPredicate> |
| 297 | container_algorithm_internal::ContainerIter<Sequence> c_adjacent_find( |
| 298 | Sequence& sequence, BinaryPredicate&& pred) { |
| 299 | return std::adjacent_find(container_algorithm_internal::c_begin(sequence), |
| 300 | container_algorithm_internal::c_end(sequence), |
| 301 | std::forward<BinaryPredicate>(pred)); |
| 302 | } |
| 303 | |
| 304 | // c_count() |
| 305 | // |
| 306 | // Container-based version of the <algorithm> `std::count()` function to count |
| 307 | // values that match within a container. |
| 308 | template <typename C, typename T> |
| 309 | container_algorithm_internal::ContainerDifferenceType<const C> c_count( |
| 310 | const C& c, T&& value) { |
| 311 | return std::count(container_algorithm_internal::c_begin(c), |
| 312 | container_algorithm_internal::c_end(c), |
| 313 | std::forward<T>(value)); |
| 314 | } |
| 315 | |
| 316 | // c_count_if() |
| 317 | // |
| 318 | // Container-based version of the <algorithm> `std::count_if()` function to |
| 319 | // count values matching a condition within a container. |
| 320 | template <typename C, typename Pred> |
| 321 | container_algorithm_internal::ContainerDifferenceType<const C> c_count_if( |
| 322 | const C& c, Pred&& pred) { |
| 323 | return std::count_if(container_algorithm_internal::c_begin(c), |
| 324 | container_algorithm_internal::c_end(c), |
| 325 | std::forward<Pred>(pred)); |
| 326 | } |
| 327 | |
| 328 | // c_mismatch() |
| 329 | // |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 330 | // Container-based version of the <algorithm> `std::mismatch()` function to |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 331 | // return the first element where two ordered containers differ. |
| 332 | template <typename C1, typename C2> |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 333 | container_algorithm_internal::ContainerIterPairType<C1, C2> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 334 | c_mismatch(C1& c1, C2& c2) { |
| 335 | return std::mismatch(container_algorithm_internal::c_begin(c1), |
| 336 | container_algorithm_internal::c_end(c1), |
| 337 | container_algorithm_internal::c_begin(c2)); |
| 338 | } |
| 339 | |
| 340 | // Overload of c_mismatch() for using a predicate evaluation other than `==` as |
| 341 | // the function's test condition. |
| 342 | template <typename C1, typename C2, typename BinaryPredicate> |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 343 | container_algorithm_internal::ContainerIterPairType<C1, C2> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 344 | c_mismatch(C1& c1, C2& c2, BinaryPredicate&& pred) { |
| 345 | return std::mismatch(container_algorithm_internal::c_begin(c1), |
| 346 | container_algorithm_internal::c_end(c1), |
| 347 | container_algorithm_internal::c_begin(c2), |
| 348 | std::forward<BinaryPredicate>(pred)); |
| 349 | } |
| 350 | |
| 351 | // c_equal() |
| 352 | // |
| 353 | // Container-based version of the <algorithm> `std::equal()` function to |
| 354 | // test whether two containers are equal. |
| 355 | // |
| 356 | // NOTE: the semantics of c_equal() are slightly different than those of |
| 357 | // equal(): while the latter iterates over the second container only up to the |
| 358 | // size of the first container, c_equal() also checks whether the container |
| 359 | // sizes are equal. This better matches expectations about c_equal() based on |
| 360 | // its signature. |
| 361 | // |
| 362 | // Example: |
| 363 | // vector v1 = <1, 2, 3>; |
| 364 | // vector v2 = <1, 2, 3, 4>; |
| 365 | // equal(std::begin(v1), std::end(v1), std::begin(v2)) returns true |
| 366 | // c_equal(v1, v2) returns false |
| 367 | |
| 368 | template <typename C1, typename C2> |
| 369 | bool c_equal(const C1& c1, const C2& c2) { |
| 370 | return ((c1.size() == c2.size()) && |
| 371 | std::equal(container_algorithm_internal::c_begin(c1), |
| 372 | container_algorithm_internal::c_end(c1), |
| 373 | container_algorithm_internal::c_begin(c2))); |
| 374 | } |
| 375 | |
| 376 | // Overload of c_equal() for using a predicate evaluation other than `==` as |
| 377 | // the function's test condition. |
| 378 | template <typename C1, typename C2, typename BinaryPredicate> |
| 379 | bool c_equal(const C1& c1, const C2& c2, BinaryPredicate&& pred) { |
| 380 | return ((c1.size() == c2.size()) && |
| 381 | std::equal(container_algorithm_internal::c_begin(c1), |
| 382 | container_algorithm_internal::c_end(c1), |
| 383 | container_algorithm_internal::c_begin(c2), |
| 384 | std::forward<BinaryPredicate>(pred))); |
| 385 | } |
| 386 | |
| 387 | // c_is_permutation() |
| 388 | // |
| 389 | // Container-based version of the <algorithm> `std::is_permutation()` function |
| 390 | // to test whether a container is a permutation of another. |
| 391 | template <typename C1, typename C2> |
| 392 | bool c_is_permutation(const C1& c1, const C2& c2) { |
| 393 | using std::begin; |
| 394 | using std::end; |
| 395 | return c1.size() == c2.size() && |
| 396 | std::is_permutation(begin(c1), end(c1), begin(c2)); |
| 397 | } |
| 398 | |
| 399 | // Overload of c_is_permutation() for using a predicate evaluation other than |
| 400 | // `==` as the function's test condition. |
| 401 | template <typename C1, typename C2, typename BinaryPredicate> |
| 402 | bool c_is_permutation(const C1& c1, const C2& c2, BinaryPredicate&& pred) { |
| 403 | using std::begin; |
| 404 | using std::end; |
| 405 | return c1.size() == c2.size() && |
| 406 | std::is_permutation(begin(c1), end(c1), begin(c2), |
| 407 | std::forward<BinaryPredicate>(pred)); |
| 408 | } |
| 409 | |
| 410 | // c_search() |
| 411 | // |
| 412 | // Container-based version of the <algorithm> `std::search()` function to search |
| 413 | // a container for a subsequence. |
| 414 | template <typename Sequence1, typename Sequence2> |
| 415 | container_algorithm_internal::ContainerIter<Sequence1> c_search( |
| 416 | Sequence1& sequence, Sequence2& subsequence) { |
| 417 | return std::search(container_algorithm_internal::c_begin(sequence), |
| 418 | container_algorithm_internal::c_end(sequence), |
| 419 | container_algorithm_internal::c_begin(subsequence), |
| 420 | container_algorithm_internal::c_end(subsequence)); |
| 421 | } |
| 422 | |
| 423 | // Overload of c_search() for using a predicate evaluation other than |
| 424 | // `==` as the function's test condition. |
| 425 | template <typename Sequence1, typename Sequence2, typename BinaryPredicate> |
| 426 | container_algorithm_internal::ContainerIter<Sequence1> c_search( |
| 427 | Sequence1& sequence, Sequence2& subsequence, BinaryPredicate&& pred) { |
| 428 | return std::search(container_algorithm_internal::c_begin(sequence), |
| 429 | container_algorithm_internal::c_end(sequence), |
| 430 | container_algorithm_internal::c_begin(subsequence), |
| 431 | container_algorithm_internal::c_end(subsequence), |
| 432 | std::forward<BinaryPredicate>(pred)); |
| 433 | } |
| 434 | |
| 435 | // c_search_n() |
| 436 | // |
| 437 | // Container-based version of the <algorithm> `std::search_n()` function to |
| 438 | // search a container for the first sequence of N elements. |
| 439 | template <typename Sequence, typename Size, typename T> |
| 440 | container_algorithm_internal::ContainerIter<Sequence> c_search_n( |
| 441 | Sequence& sequence, Size count, T&& value) { |
| 442 | return std::search_n(container_algorithm_internal::c_begin(sequence), |
| 443 | container_algorithm_internal::c_end(sequence), count, |
| 444 | std::forward<T>(value)); |
| 445 | } |
| 446 | |
| 447 | // Overload of c_search_n() for using a predicate evaluation other than |
| 448 | // `==` as the function's test condition. |
| 449 | template <typename Sequence, typename Size, typename T, |
| 450 | typename BinaryPredicate> |
| 451 | container_algorithm_internal::ContainerIter<Sequence> c_search_n( |
| 452 | Sequence& sequence, Size count, T&& value, BinaryPredicate&& pred) { |
| 453 | return std::search_n(container_algorithm_internal::c_begin(sequence), |
| 454 | container_algorithm_internal::c_end(sequence), count, |
| 455 | std::forward<T>(value), |
| 456 | std::forward<BinaryPredicate>(pred)); |
| 457 | } |
| 458 | |
| 459 | //------------------------------------------------------------------------------ |
| 460 | // <algorithm> Modifying sequence operations |
| 461 | //------------------------------------------------------------------------------ |
| 462 | |
| 463 | // c_copy() |
| 464 | // |
| 465 | // Container-based version of the <algorithm> `std::copy()` function to copy a |
| 466 | // container's elements into an iterator. |
| 467 | template <typename InputSequence, typename OutputIterator> |
| 468 | OutputIterator c_copy(const InputSequence& input, OutputIterator output) { |
| 469 | return std::copy(container_algorithm_internal::c_begin(input), |
| 470 | container_algorithm_internal::c_end(input), output); |
| 471 | } |
| 472 | |
| 473 | // c_copy_n() |
| 474 | // |
| 475 | // Container-based version of the <algorithm> `std::copy_n()` function to copy a |
| 476 | // container's first N elements into an iterator. |
| 477 | template <typename C, typename Size, typename OutputIterator> |
| 478 | OutputIterator c_copy_n(const C& input, Size n, OutputIterator output) { |
| 479 | return std::copy_n(container_algorithm_internal::c_begin(input), n, output); |
| 480 | } |
| 481 | |
| 482 | // c_copy_if() |
| 483 | // |
| 484 | // Container-based version of the <algorithm> `std::copy_if()` function to copy |
| 485 | // a container's elements satisfying some condition into an iterator. |
| 486 | template <typename InputSequence, typename OutputIterator, typename Pred> |
| 487 | OutputIterator c_copy_if(const InputSequence& input, OutputIterator output, |
| 488 | Pred&& pred) { |
| 489 | return std::copy_if(container_algorithm_internal::c_begin(input), |
| 490 | container_algorithm_internal::c_end(input), output, |
| 491 | std::forward<Pred>(pred)); |
| 492 | } |
| 493 | |
| 494 | // c_copy_backward() |
| 495 | // |
| 496 | // Container-based version of the <algorithm> `std::copy_backward()` function to |
| 497 | // copy a container's elements in reverse order into an iterator. |
| 498 | template <typename C, typename BidirectionalIterator> |
| 499 | BidirectionalIterator c_copy_backward(const C& src, |
| 500 | BidirectionalIterator dest) { |
| 501 | return std::copy_backward(container_algorithm_internal::c_begin(src), |
| 502 | container_algorithm_internal::c_end(src), dest); |
| 503 | } |
| 504 | |
| 505 | // c_move() |
| 506 | // |
| 507 | // Container-based version of the <algorithm> `std::move()` function to move |
| 508 | // a container's elements into an iterator. |
| 509 | template <typename C, typename OutputIterator> |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 510 | OutputIterator c_move(C&& src, OutputIterator dest) { |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 511 | return std::move(container_algorithm_internal::c_begin(src), |
| 512 | container_algorithm_internal::c_end(src), dest); |
| 513 | } |
| 514 | |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 515 | // c_swap_ranges() |
| 516 | // |
| 517 | // Container-based version of the <algorithm> `std::swap_ranges()` function to |
| 518 | // swap a container's elements with another container's elements. |
| 519 | template <typename C1, typename C2> |
| 520 | container_algorithm_internal::ContainerIter<C2> c_swap_ranges(C1& c1, C2& c2) { |
| 521 | return std::swap_ranges(container_algorithm_internal::c_begin(c1), |
| 522 | container_algorithm_internal::c_end(c1), |
| 523 | container_algorithm_internal::c_begin(c2)); |
| 524 | } |
| 525 | |
| 526 | // c_transform() |
| 527 | // |
| 528 | // Container-based version of the <algorithm> `std::transform()` function to |
| 529 | // transform a container's elements using the unary operation, storing the |
| 530 | // result in an iterator pointing to the last transformed element in the output |
| 531 | // range. |
| 532 | template <typename InputSequence, typename OutputIterator, typename UnaryOp> |
| 533 | OutputIterator c_transform(const InputSequence& input, OutputIterator output, |
| 534 | UnaryOp&& unary_op) { |
| 535 | return std::transform(container_algorithm_internal::c_begin(input), |
| 536 | container_algorithm_internal::c_end(input), output, |
| 537 | std::forward<UnaryOp>(unary_op)); |
| 538 | } |
| 539 | |
| 540 | // Overload of c_transform() for performing a transformation using a binary |
| 541 | // predicate. |
| 542 | template <typename InputSequence1, typename InputSequence2, |
| 543 | typename OutputIterator, typename BinaryOp> |
| 544 | OutputIterator c_transform(const InputSequence1& input1, |
| 545 | const InputSequence2& input2, OutputIterator output, |
| 546 | BinaryOp&& binary_op) { |
| 547 | return std::transform(container_algorithm_internal::c_begin(input1), |
| 548 | container_algorithm_internal::c_end(input1), |
| 549 | container_algorithm_internal::c_begin(input2), output, |
| 550 | std::forward<BinaryOp>(binary_op)); |
| 551 | } |
| 552 | |
| 553 | // c_replace() |
| 554 | // |
| 555 | // Container-based version of the <algorithm> `std::replace()` function to |
| 556 | // replace a container's elements of some value with a new value. The container |
| 557 | // is modified in place. |
| 558 | template <typename Sequence, typename T> |
| 559 | void c_replace(Sequence& sequence, const T& old_value, const T& new_value) { |
| 560 | std::replace(container_algorithm_internal::c_begin(sequence), |
| 561 | container_algorithm_internal::c_end(sequence), old_value, |
| 562 | new_value); |
| 563 | } |
| 564 | |
| 565 | // c_replace_if() |
| 566 | // |
| 567 | // Container-based version of the <algorithm> `std::replace_if()` function to |
| 568 | // replace a container's elements of some value with a new value based on some |
| 569 | // condition. The container is modified in place. |
| 570 | template <typename C, typename Pred, typename T> |
| 571 | void c_replace_if(C& c, Pred&& pred, T&& new_value) { |
| 572 | std::replace_if(container_algorithm_internal::c_begin(c), |
| 573 | container_algorithm_internal::c_end(c), |
| 574 | std::forward<Pred>(pred), std::forward<T>(new_value)); |
| 575 | } |
| 576 | |
| 577 | // c_replace_copy() |
| 578 | // |
| 579 | // Container-based version of the <algorithm> `std::replace_copy()` function to |
| 580 | // replace a container's elements of some value with a new value and return the |
| 581 | // results within an iterator. |
| 582 | template <typename C, typename OutputIterator, typename T> |
| 583 | OutputIterator c_replace_copy(const C& c, OutputIterator result, T&& old_value, |
| 584 | T&& new_value) { |
| 585 | return std::replace_copy(container_algorithm_internal::c_begin(c), |
| 586 | container_algorithm_internal::c_end(c), result, |
| 587 | std::forward<T>(old_value), |
| 588 | std::forward<T>(new_value)); |
| 589 | } |
| 590 | |
| 591 | // c_replace_copy_if() |
| 592 | // |
| 593 | // Container-based version of the <algorithm> `std::replace_copy_if()` function |
| 594 | // to replace a container's elements of some value with a new value based on |
| 595 | // some condition, and return the results within an iterator. |
| 596 | template <typename C, typename OutputIterator, typename Pred, typename T> |
| 597 | OutputIterator c_replace_copy_if(const C& c, OutputIterator result, Pred&& pred, |
| 598 | T&& new_value) { |
| 599 | return std::replace_copy_if(container_algorithm_internal::c_begin(c), |
| 600 | container_algorithm_internal::c_end(c), result, |
| 601 | std::forward<Pred>(pred), |
| 602 | std::forward<T>(new_value)); |
| 603 | } |
| 604 | |
| 605 | // c_fill() |
| 606 | // |
| 607 | // Container-based version of the <algorithm> `std::fill()` function to fill a |
| 608 | // container with some value. |
| 609 | template <typename C, typename T> |
| 610 | void c_fill(C& c, T&& value) { |
| 611 | std::fill(container_algorithm_internal::c_begin(c), |
| 612 | container_algorithm_internal::c_end(c), std::forward<T>(value)); |
| 613 | } |
| 614 | |
| 615 | // c_fill_n() |
| 616 | // |
| 617 | // Container-based version of the <algorithm> `std::fill_n()` function to fill |
| 618 | // the first N elements in a container with some value. |
| 619 | template <typename C, typename Size, typename T> |
| 620 | void c_fill_n(C& c, Size n, T&& value) { |
| 621 | std::fill_n(container_algorithm_internal::c_begin(c), n, |
| 622 | std::forward<T>(value)); |
| 623 | } |
| 624 | |
| 625 | // c_generate() |
| 626 | // |
| 627 | // Container-based version of the <algorithm> `std::generate()` function to |
| 628 | // assign a container's elements to the values provided by the given generator. |
| 629 | template <typename C, typename Generator> |
| 630 | void c_generate(C& c, Generator&& gen) { |
| 631 | std::generate(container_algorithm_internal::c_begin(c), |
| 632 | container_algorithm_internal::c_end(c), |
| 633 | std::forward<Generator>(gen)); |
| 634 | } |
| 635 | |
| 636 | // c_generate_n() |
| 637 | // |
| 638 | // Container-based version of the <algorithm> `std::generate_n()` function to |
| 639 | // assign a container's first N elements to the values provided by the given |
| 640 | // generator. |
| 641 | template <typename C, typename Size, typename Generator> |
| 642 | container_algorithm_internal::ContainerIter<C> c_generate_n(C& c, Size n, |
| 643 | Generator&& gen) { |
| 644 | return std::generate_n(container_algorithm_internal::c_begin(c), n, |
| 645 | std::forward<Generator>(gen)); |
| 646 | } |
| 647 | |
| 648 | // Note: `c_xx()` <algorithm> container versions for `remove()`, `remove_if()`, |
| 649 | // and `unique()` are omitted, because it's not clear whether or not such |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 650 | // functions should call erase on their supplied sequences afterwards. Either |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 651 | // behavior would be surprising for a different set of users. |
| 652 | // |
| 653 | |
| 654 | // c_remove_copy() |
| 655 | // |
| 656 | // Container-based version of the <algorithm> `std::remove_copy()` function to |
| 657 | // copy a container's elements while removing any elements matching the given |
| 658 | // `value`. |
| 659 | template <typename C, typename OutputIterator, typename T> |
| 660 | OutputIterator c_remove_copy(const C& c, OutputIterator result, T&& value) { |
| 661 | return std::remove_copy(container_algorithm_internal::c_begin(c), |
| 662 | container_algorithm_internal::c_end(c), result, |
| 663 | std::forward<T>(value)); |
| 664 | } |
| 665 | |
| 666 | // c_remove_copy_if() |
| 667 | // |
| 668 | // Container-based version of the <algorithm> `std::remove_copy_if()` function |
| 669 | // to copy a container's elements while removing any elements matching the given |
| 670 | // condition. |
| 671 | template <typename C, typename OutputIterator, typename Pred> |
| 672 | OutputIterator c_remove_copy_if(const C& c, OutputIterator result, |
| 673 | Pred&& pred) { |
| 674 | return std::remove_copy_if(container_algorithm_internal::c_begin(c), |
| 675 | container_algorithm_internal::c_end(c), result, |
| 676 | std::forward<Pred>(pred)); |
| 677 | } |
| 678 | |
| 679 | // c_unique_copy() |
| 680 | // |
| 681 | // Container-based version of the <algorithm> `std::unique_copy()` function to |
| 682 | // copy a container's elements while removing any elements containing duplicate |
| 683 | // values. |
| 684 | template <typename C, typename OutputIterator> |
| 685 | OutputIterator c_unique_copy(const C& c, OutputIterator result) { |
| 686 | return std::unique_copy(container_algorithm_internal::c_begin(c), |
| 687 | container_algorithm_internal::c_end(c), result); |
| 688 | } |
| 689 | |
| 690 | // Overload of c_unique_copy() for using a predicate evaluation other than |
| 691 | // `==` for comparing uniqueness of the element values. |
| 692 | template <typename C, typename OutputIterator, typename BinaryPredicate> |
| 693 | OutputIterator c_unique_copy(const C& c, OutputIterator result, |
| 694 | BinaryPredicate&& pred) { |
| 695 | return std::unique_copy(container_algorithm_internal::c_begin(c), |
| 696 | container_algorithm_internal::c_end(c), result, |
| 697 | std::forward<BinaryPredicate>(pred)); |
| 698 | } |
| 699 | |
| 700 | // c_reverse() |
| 701 | // |
| 702 | // Container-based version of the <algorithm> `std::reverse()` function to |
| 703 | // reverse a container's elements. |
| 704 | template <typename Sequence> |
| 705 | void c_reverse(Sequence& sequence) { |
| 706 | std::reverse(container_algorithm_internal::c_begin(sequence), |
| 707 | container_algorithm_internal::c_end(sequence)); |
| 708 | } |
| 709 | |
| 710 | // c_reverse_copy() |
| 711 | // |
| 712 | // Container-based version of the <algorithm> `std::reverse()` function to |
| 713 | // reverse a container's elements and write them to an iterator range. |
| 714 | template <typename C, typename OutputIterator> |
| 715 | OutputIterator c_reverse_copy(const C& sequence, OutputIterator result) { |
| 716 | return std::reverse_copy(container_algorithm_internal::c_begin(sequence), |
| 717 | container_algorithm_internal::c_end(sequence), |
| 718 | result); |
| 719 | } |
| 720 | |
| 721 | // c_rotate() |
| 722 | // |
| 723 | // Container-based version of the <algorithm> `std::rotate()` function to |
| 724 | // shift a container's elements leftward such that the `middle` element becomes |
| 725 | // the first element in the container. |
| 726 | template <typename C, |
| 727 | typename Iterator = container_algorithm_internal::ContainerIter<C>> |
| 728 | Iterator c_rotate(C& sequence, Iterator middle) { |
| 729 | return absl::rotate(container_algorithm_internal::c_begin(sequence), middle, |
| 730 | container_algorithm_internal::c_end(sequence)); |
| 731 | } |
| 732 | |
| 733 | // c_rotate_copy() |
| 734 | // |
| 735 | // Container-based version of the <algorithm> `std::rotate_copy()` function to |
| 736 | // shift a container's elements leftward such that the `middle` element becomes |
| 737 | // the first element in a new iterator range. |
| 738 | template <typename C, typename OutputIterator> |
| 739 | OutputIterator c_rotate_copy( |
| 740 | const C& sequence, |
| 741 | container_algorithm_internal::ContainerIter<const C> middle, |
| 742 | OutputIterator result) { |
| 743 | return std::rotate_copy(container_algorithm_internal::c_begin(sequence), |
| 744 | middle, container_algorithm_internal::c_end(sequence), |
| 745 | result); |
| 746 | } |
| 747 | |
| 748 | // c_shuffle() |
| 749 | // |
| 750 | // Container-based version of the <algorithm> `std::shuffle()` function to |
| 751 | // randomly shuffle elements within the container using a `gen()` uniform random |
| 752 | // number generator. |
| 753 | template <typename RandomAccessContainer, typename UniformRandomBitGenerator> |
| 754 | void c_shuffle(RandomAccessContainer& c, UniformRandomBitGenerator&& gen) { |
| 755 | std::shuffle(container_algorithm_internal::c_begin(c), |
| 756 | container_algorithm_internal::c_end(c), |
| 757 | std::forward<UniformRandomBitGenerator>(gen)); |
| 758 | } |
| 759 | |
| 760 | //------------------------------------------------------------------------------ |
| 761 | // <algorithm> Partition functions |
| 762 | //------------------------------------------------------------------------------ |
| 763 | |
| 764 | // c_is_partitioned() |
| 765 | // |
| 766 | // Container-based version of the <algorithm> `std::is_partitioned()` function |
| 767 | // to test whether all elements in the container for which `pred` returns `true` |
| 768 | // precede those for which `pred` is `false`. |
| 769 | template <typename C, typename Pred> |
| 770 | bool c_is_partitioned(const C& c, Pred&& pred) { |
| 771 | return std::is_partitioned(container_algorithm_internal::c_begin(c), |
| 772 | container_algorithm_internal::c_end(c), |
| 773 | std::forward<Pred>(pred)); |
| 774 | } |
| 775 | |
| 776 | // c_partition() |
| 777 | // |
| 778 | // Container-based version of the <algorithm> `std::partition()` function |
| 779 | // to rearrange all elements in a container in such a way that all elements for |
| 780 | // which `pred` returns `true` precede all those for which it returns `false`, |
| 781 | // returning an iterator to the first element of the second group. |
| 782 | template <typename C, typename Pred> |
| 783 | container_algorithm_internal::ContainerIter<C> c_partition(C& c, Pred&& pred) { |
| 784 | return std::partition(container_algorithm_internal::c_begin(c), |
| 785 | container_algorithm_internal::c_end(c), |
| 786 | std::forward<Pred>(pred)); |
| 787 | } |
| 788 | |
| 789 | // c_stable_partition() |
| 790 | // |
| 791 | // Container-based version of the <algorithm> `std::stable_partition()` function |
| 792 | // to rearrange all elements in a container in such a way that all elements for |
| 793 | // which `pred` returns `true` precede all those for which it returns `false`, |
| 794 | // preserving the relative ordering between the two groups. The function returns |
| 795 | // an iterator to the first element of the second group. |
| 796 | template <typename C, typename Pred> |
| 797 | container_algorithm_internal::ContainerIter<C> c_stable_partition(C& c, |
| 798 | Pred&& pred) { |
| 799 | return std::stable_partition(container_algorithm_internal::c_begin(c), |
| 800 | container_algorithm_internal::c_end(c), |
| 801 | std::forward<Pred>(pred)); |
| 802 | } |
| 803 | |
| 804 | // c_partition_copy() |
| 805 | // |
| 806 | // Container-based version of the <algorithm> `std::partition_copy()` function |
| 807 | // to partition a container's elements and return them into two iterators: one |
| 808 | // for which `pred` returns `true`, and one for which `pred` returns `false.` |
| 809 | |
| 810 | template <typename C, typename OutputIterator1, typename OutputIterator2, |
| 811 | typename Pred> |
| 812 | std::pair<OutputIterator1, OutputIterator2> c_partition_copy( |
| 813 | const C& c, OutputIterator1 out_true, OutputIterator2 out_false, |
| 814 | Pred&& pred) { |
| 815 | return std::partition_copy(container_algorithm_internal::c_begin(c), |
| 816 | container_algorithm_internal::c_end(c), out_true, |
| 817 | out_false, std::forward<Pred>(pred)); |
| 818 | } |
| 819 | |
| 820 | // c_partition_point() |
| 821 | // |
| 822 | // Container-based version of the <algorithm> `std::partition_point()` function |
| 823 | // to return the first element of an already partitioned container for which |
| 824 | // the given `pred` is not `true`. |
| 825 | template <typename C, typename Pred> |
| 826 | container_algorithm_internal::ContainerIter<C> c_partition_point(C& c, |
| 827 | Pred&& pred) { |
| 828 | return std::partition_point(container_algorithm_internal::c_begin(c), |
| 829 | container_algorithm_internal::c_end(c), |
| 830 | std::forward<Pred>(pred)); |
| 831 | } |
| 832 | |
| 833 | //------------------------------------------------------------------------------ |
| 834 | // <algorithm> Sorting functions |
| 835 | //------------------------------------------------------------------------------ |
| 836 | |
| 837 | // c_sort() |
| 838 | // |
| 839 | // Container-based version of the <algorithm> `std::sort()` function |
| 840 | // to sort elements in ascending order of their values. |
| 841 | template <typename C> |
| 842 | void c_sort(C& c) { |
| 843 | std::sort(container_algorithm_internal::c_begin(c), |
| 844 | container_algorithm_internal::c_end(c)); |
| 845 | } |
| 846 | |
| 847 | // Overload of c_sort() for performing a `comp` comparison other than the |
| 848 | // default `operator<`. |
| 849 | template <typename C, typename Compare> |
| 850 | void c_sort(C& c, Compare&& comp) { |
| 851 | std::sort(container_algorithm_internal::c_begin(c), |
| 852 | container_algorithm_internal::c_end(c), |
| 853 | std::forward<Compare>(comp)); |
| 854 | } |
| 855 | |
| 856 | // c_stable_sort() |
| 857 | // |
| 858 | // Container-based version of the <algorithm> `std::stable_sort()` function |
| 859 | // to sort elements in ascending order of their values, preserving the order |
| 860 | // of equivalents. |
| 861 | template <typename C> |
| 862 | void c_stable_sort(C& c) { |
| 863 | std::stable_sort(container_algorithm_internal::c_begin(c), |
| 864 | container_algorithm_internal::c_end(c)); |
| 865 | } |
| 866 | |
| 867 | // Overload of c_stable_sort() for performing a `comp` comparison other than the |
| 868 | // default `operator<`. |
| 869 | template <typename C, typename Compare> |
| 870 | void c_stable_sort(C& c, Compare&& comp) { |
| 871 | std::stable_sort(container_algorithm_internal::c_begin(c), |
| 872 | container_algorithm_internal::c_end(c), |
| 873 | std::forward<Compare>(comp)); |
| 874 | } |
| 875 | |
| 876 | // c_is_sorted() |
| 877 | // |
| 878 | // Container-based version of the <algorithm> `std::is_sorted()` function |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 879 | // to evaluate whether the given container is sorted in ascending order. |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 880 | template <typename C> |
| 881 | bool c_is_sorted(const C& c) { |
| 882 | return std::is_sorted(container_algorithm_internal::c_begin(c), |
| 883 | container_algorithm_internal::c_end(c)); |
| 884 | } |
| 885 | |
| 886 | // c_is_sorted() overload for performing a `comp` comparison other than the |
| 887 | // default `operator<`. |
| 888 | template <typename C, typename Compare> |
| 889 | bool c_is_sorted(const C& c, Compare&& comp) { |
| 890 | return std::is_sorted(container_algorithm_internal::c_begin(c), |
| 891 | container_algorithm_internal::c_end(c), |
| 892 | std::forward<Compare>(comp)); |
| 893 | } |
| 894 | |
| 895 | // c_partial_sort() |
| 896 | // |
| 897 | // Container-based version of the <algorithm> `std::partial_sort()` function |
| 898 | // to rearrange elements within a container such that elements before `middle` |
| 899 | // are sorted in ascending order. |
| 900 | template <typename RandomAccessContainer> |
| 901 | void c_partial_sort( |
| 902 | RandomAccessContainer& sequence, |
| 903 | container_algorithm_internal::ContainerIter<RandomAccessContainer> middle) { |
| 904 | std::partial_sort(container_algorithm_internal::c_begin(sequence), middle, |
| 905 | container_algorithm_internal::c_end(sequence)); |
| 906 | } |
| 907 | |
| 908 | // Overload of c_partial_sort() for performing a `comp` comparison other than |
| 909 | // the default `operator<`. |
| 910 | template <typename RandomAccessContainer, typename Compare> |
| 911 | void c_partial_sort( |
| 912 | RandomAccessContainer& sequence, |
| 913 | container_algorithm_internal::ContainerIter<RandomAccessContainer> middle, |
| 914 | Compare&& comp) { |
| 915 | std::partial_sort(container_algorithm_internal::c_begin(sequence), middle, |
| 916 | container_algorithm_internal::c_end(sequence), |
| 917 | std::forward<Compare>(comp)); |
| 918 | } |
| 919 | |
| 920 | // c_partial_sort_copy() |
| 921 | // |
| 922 | // Container-based version of the <algorithm> `std::partial_sort_copy()` |
| 923 | // function to sort elements within a container such that elements before |
| 924 | // `middle` are sorted in ascending order, and return the result within an |
| 925 | // iterator. |
| 926 | template <typename C, typename RandomAccessContainer> |
| 927 | container_algorithm_internal::ContainerIter<RandomAccessContainer> |
| 928 | c_partial_sort_copy(const C& sequence, RandomAccessContainer& result) { |
| 929 | return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence), |
| 930 | container_algorithm_internal::c_end(sequence), |
| 931 | container_algorithm_internal::c_begin(result), |
| 932 | container_algorithm_internal::c_end(result)); |
| 933 | } |
| 934 | |
| 935 | // Overload of c_partial_sort_copy() for performing a `comp` comparison other |
| 936 | // than the default `operator<`. |
| 937 | template <typename C, typename RandomAccessContainer, typename Compare> |
| 938 | container_algorithm_internal::ContainerIter<RandomAccessContainer> |
| 939 | c_partial_sort_copy(const C& sequence, RandomAccessContainer& result, |
| 940 | Compare&& comp) { |
| 941 | return std::partial_sort_copy(container_algorithm_internal::c_begin(sequence), |
| 942 | container_algorithm_internal::c_end(sequence), |
| 943 | container_algorithm_internal::c_begin(result), |
| 944 | container_algorithm_internal::c_end(result), |
| 945 | std::forward<Compare>(comp)); |
| 946 | } |
| 947 | |
| 948 | // c_is_sorted_until() |
| 949 | // |
| 950 | // Container-based version of the <algorithm> `std::is_sorted_until()` function |
| 951 | // to return the first element within a container that is not sorted in |
| 952 | // ascending order as an iterator. |
| 953 | template <typename C> |
| 954 | container_algorithm_internal::ContainerIter<C> c_is_sorted_until(C& c) { |
| 955 | return std::is_sorted_until(container_algorithm_internal::c_begin(c), |
| 956 | container_algorithm_internal::c_end(c)); |
| 957 | } |
| 958 | |
| 959 | // Overload of c_is_sorted_until() for performing a `comp` comparison other than |
| 960 | // the default `operator<`. |
| 961 | template <typename C, typename Compare> |
| 962 | container_algorithm_internal::ContainerIter<C> c_is_sorted_until( |
| 963 | C& c, Compare&& comp) { |
| 964 | return std::is_sorted_until(container_algorithm_internal::c_begin(c), |
| 965 | container_algorithm_internal::c_end(c), |
| 966 | std::forward<Compare>(comp)); |
| 967 | } |
| 968 | |
| 969 | // c_nth_element() |
| 970 | // |
| 971 | // Container-based version of the <algorithm> `std::nth_element()` function |
| 972 | // to rearrange the elements within a container such that the `nth` element |
| 973 | // would be in that position in an ordered sequence; other elements may be in |
| 974 | // any order, except that all preceding `nth` will be less than that element, |
| 975 | // and all following `nth` will be greater than that element. |
| 976 | template <typename RandomAccessContainer> |
| 977 | void c_nth_element( |
| 978 | RandomAccessContainer& sequence, |
| 979 | container_algorithm_internal::ContainerIter<RandomAccessContainer> nth) { |
| 980 | std::nth_element(container_algorithm_internal::c_begin(sequence), nth, |
| 981 | container_algorithm_internal::c_end(sequence)); |
| 982 | } |
| 983 | |
| 984 | // Overload of c_nth_element() for performing a `comp` comparison other than |
| 985 | // the default `operator<`. |
| 986 | template <typename RandomAccessContainer, typename Compare> |
| 987 | void c_nth_element( |
| 988 | RandomAccessContainer& sequence, |
| 989 | container_algorithm_internal::ContainerIter<RandomAccessContainer> nth, |
| 990 | Compare&& comp) { |
| 991 | std::nth_element(container_algorithm_internal::c_begin(sequence), nth, |
| 992 | container_algorithm_internal::c_end(sequence), |
| 993 | std::forward<Compare>(comp)); |
| 994 | } |
| 995 | |
| 996 | //------------------------------------------------------------------------------ |
| 997 | // <algorithm> Binary Search |
| 998 | //------------------------------------------------------------------------------ |
| 999 | |
| 1000 | // c_lower_bound() |
| 1001 | // |
| 1002 | // Container-based version of the <algorithm> `std::lower_bound()` function |
| 1003 | // to return an iterator pointing to the first element in a sorted container |
| 1004 | // which does not compare less than `value`. |
| 1005 | template <typename Sequence, typename T> |
| 1006 | container_algorithm_internal::ContainerIter<Sequence> c_lower_bound( |
| 1007 | Sequence& sequence, T&& value) { |
| 1008 | return std::lower_bound(container_algorithm_internal::c_begin(sequence), |
| 1009 | container_algorithm_internal::c_end(sequence), |
| 1010 | std::forward<T>(value)); |
| 1011 | } |
| 1012 | |
| 1013 | // Overload of c_lower_bound() for performing a `comp` comparison other than |
| 1014 | // the default `operator<`. |
| 1015 | template <typename Sequence, typename T, typename Compare> |
| 1016 | container_algorithm_internal::ContainerIter<Sequence> c_lower_bound( |
| 1017 | Sequence& sequence, T&& value, Compare&& comp) { |
| 1018 | return std::lower_bound(container_algorithm_internal::c_begin(sequence), |
| 1019 | container_algorithm_internal::c_end(sequence), |
| 1020 | std::forward<T>(value), std::forward<Compare>(comp)); |
| 1021 | } |
| 1022 | |
| 1023 | // c_upper_bound() |
| 1024 | // |
| 1025 | // Container-based version of the <algorithm> `std::upper_bound()` function |
| 1026 | // to return an iterator pointing to the first element in a sorted container |
| 1027 | // which is greater than `value`. |
| 1028 | template <typename Sequence, typename T> |
| 1029 | container_algorithm_internal::ContainerIter<Sequence> c_upper_bound( |
| 1030 | Sequence& sequence, T&& value) { |
| 1031 | return std::upper_bound(container_algorithm_internal::c_begin(sequence), |
| 1032 | container_algorithm_internal::c_end(sequence), |
| 1033 | std::forward<T>(value)); |
| 1034 | } |
| 1035 | |
| 1036 | // Overload of c_upper_bound() for performing a `comp` comparison other than |
| 1037 | // the default `operator<`. |
| 1038 | template <typename Sequence, typename T, typename Compare> |
| 1039 | container_algorithm_internal::ContainerIter<Sequence> c_upper_bound( |
| 1040 | Sequence& sequence, T&& value, Compare&& comp) { |
| 1041 | return std::upper_bound(container_algorithm_internal::c_begin(sequence), |
| 1042 | container_algorithm_internal::c_end(sequence), |
| 1043 | std::forward<T>(value), std::forward<Compare>(comp)); |
| 1044 | } |
| 1045 | |
| 1046 | // c_equal_range() |
| 1047 | // |
| 1048 | // Container-based version of the <algorithm> `std::equal_range()` function |
| 1049 | // to return an iterator pair pointing to the first and last elements in a |
| 1050 | // sorted container which compare equal to `value`. |
| 1051 | template <typename Sequence, typename T> |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 1052 | container_algorithm_internal::ContainerIterPairType<Sequence, Sequence> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1053 | c_equal_range(Sequence& sequence, T&& value) { |
| 1054 | return std::equal_range(container_algorithm_internal::c_begin(sequence), |
| 1055 | container_algorithm_internal::c_end(sequence), |
| 1056 | std::forward<T>(value)); |
| 1057 | } |
| 1058 | |
| 1059 | // Overload of c_equal_range() for performing a `comp` comparison other than |
| 1060 | // the default `operator<`. |
| 1061 | template <typename Sequence, typename T, typename Compare> |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 1062 | container_algorithm_internal::ContainerIterPairType<Sequence, Sequence> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1063 | c_equal_range(Sequence& sequence, T&& value, Compare&& comp) { |
| 1064 | return std::equal_range(container_algorithm_internal::c_begin(sequence), |
| 1065 | container_algorithm_internal::c_end(sequence), |
| 1066 | std::forward<T>(value), std::forward<Compare>(comp)); |
| 1067 | } |
| 1068 | |
| 1069 | // c_binary_search() |
| 1070 | // |
| 1071 | // Container-based version of the <algorithm> `std::binary_search()` function |
| 1072 | // to test if any element in the sorted container contains a value equivalent to |
| 1073 | // 'value'. |
| 1074 | template <typename Sequence, typename T> |
| 1075 | bool c_binary_search(Sequence&& sequence, T&& value) { |
| 1076 | return std::binary_search(container_algorithm_internal::c_begin(sequence), |
| 1077 | container_algorithm_internal::c_end(sequence), |
| 1078 | std::forward<T>(value)); |
| 1079 | } |
| 1080 | |
| 1081 | // Overload of c_binary_search() for performing a `comp` comparison other than |
| 1082 | // the default `operator<`. |
| 1083 | template <typename Sequence, typename T, typename Compare> |
| 1084 | bool c_binary_search(Sequence&& sequence, T&& value, Compare&& comp) { |
| 1085 | return std::binary_search(container_algorithm_internal::c_begin(sequence), |
| 1086 | container_algorithm_internal::c_end(sequence), |
| 1087 | std::forward<T>(value), |
| 1088 | std::forward<Compare>(comp)); |
| 1089 | } |
| 1090 | |
| 1091 | //------------------------------------------------------------------------------ |
| 1092 | // <algorithm> Merge functions |
| 1093 | //------------------------------------------------------------------------------ |
| 1094 | |
| 1095 | // c_merge() |
| 1096 | // |
| 1097 | // Container-based version of the <algorithm> `std::merge()` function |
| 1098 | // to merge two sorted containers into a single sorted iterator. |
| 1099 | template <typename C1, typename C2, typename OutputIterator> |
| 1100 | OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result) { |
| 1101 | return std::merge(container_algorithm_internal::c_begin(c1), |
| 1102 | container_algorithm_internal::c_end(c1), |
| 1103 | container_algorithm_internal::c_begin(c2), |
| 1104 | container_algorithm_internal::c_end(c2), result); |
| 1105 | } |
| 1106 | |
| 1107 | // Overload of c_merge() for performing a `comp` comparison other than |
| 1108 | // the default `operator<`. |
| 1109 | template <typename C1, typename C2, typename OutputIterator, typename Compare> |
| 1110 | OutputIterator c_merge(const C1& c1, const C2& c2, OutputIterator result, |
| 1111 | Compare&& comp) { |
| 1112 | return std::merge(container_algorithm_internal::c_begin(c1), |
| 1113 | container_algorithm_internal::c_end(c1), |
| 1114 | container_algorithm_internal::c_begin(c2), |
| 1115 | container_algorithm_internal::c_end(c2), result, |
| 1116 | std::forward<Compare>(comp)); |
| 1117 | } |
| 1118 | |
| 1119 | // c_inplace_merge() |
| 1120 | // |
| 1121 | // Container-based version of the <algorithm> `std::inplace_merge()` function |
| 1122 | // to merge a supplied iterator `middle` into a container. |
| 1123 | template <typename C> |
| 1124 | void c_inplace_merge(C& c, |
| 1125 | container_algorithm_internal::ContainerIter<C> middle) { |
| 1126 | std::inplace_merge(container_algorithm_internal::c_begin(c), middle, |
| 1127 | container_algorithm_internal::c_end(c)); |
| 1128 | } |
| 1129 | |
| 1130 | // Overload of c_inplace_merge() for performing a merge using a `comp` other |
| 1131 | // than `operator<`. |
| 1132 | template <typename C, typename Compare> |
| 1133 | void c_inplace_merge(C& c, |
| 1134 | container_algorithm_internal::ContainerIter<C> middle, |
| 1135 | Compare&& comp) { |
| 1136 | std::inplace_merge(container_algorithm_internal::c_begin(c), middle, |
| 1137 | container_algorithm_internal::c_end(c), |
| 1138 | std::forward<Compare>(comp)); |
| 1139 | } |
| 1140 | |
| 1141 | // c_includes() |
| 1142 | // |
| 1143 | // Container-based version of the <algorithm> `std::includes()` function |
| 1144 | // to test whether a sorted container `c1` entirely contains another sorted |
| 1145 | // container `c2`. |
| 1146 | template <typename C1, typename C2> |
| 1147 | bool c_includes(const C1& c1, const C2& c2) { |
| 1148 | return std::includes(container_algorithm_internal::c_begin(c1), |
| 1149 | container_algorithm_internal::c_end(c1), |
| 1150 | container_algorithm_internal::c_begin(c2), |
| 1151 | container_algorithm_internal::c_end(c2)); |
| 1152 | } |
| 1153 | |
| 1154 | // Overload of c_includes() for performing a merge using a `comp` other than |
| 1155 | // `operator<`. |
| 1156 | template <typename C1, typename C2, typename Compare> |
| 1157 | bool c_includes(const C1& c1, const C2& c2, Compare&& comp) { |
| 1158 | return std::includes(container_algorithm_internal::c_begin(c1), |
| 1159 | container_algorithm_internal::c_end(c1), |
| 1160 | container_algorithm_internal::c_begin(c2), |
| 1161 | container_algorithm_internal::c_end(c2), |
| 1162 | std::forward<Compare>(comp)); |
| 1163 | } |
| 1164 | |
| 1165 | // c_set_union() |
| 1166 | // |
| 1167 | // Container-based version of the <algorithm> `std::set_union()` function |
| 1168 | // to return an iterator containing the union of two containers; duplicate |
| 1169 | // values are not copied into the output. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1170 | template <typename C1, typename C2, typename OutputIterator, |
| 1171 | typename = typename std::enable_if< |
| 1172 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1173 | void>::type, |
| 1174 | typename = typename std::enable_if< |
| 1175 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1176 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1177 | OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output) { |
| 1178 | return std::set_union(container_algorithm_internal::c_begin(c1), |
| 1179 | container_algorithm_internal::c_end(c1), |
| 1180 | container_algorithm_internal::c_begin(c2), |
| 1181 | container_algorithm_internal::c_end(c2), output); |
| 1182 | } |
| 1183 | |
| 1184 | // Overload of c_set_union() for performing a merge using a `comp` other than |
| 1185 | // `operator<`. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1186 | template <typename C1, typename C2, typename OutputIterator, typename Compare, |
| 1187 | typename = typename std::enable_if< |
| 1188 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1189 | void>::type, |
| 1190 | typename = typename std::enable_if< |
| 1191 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1192 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1193 | OutputIterator c_set_union(const C1& c1, const C2& c2, OutputIterator output, |
| 1194 | Compare&& comp) { |
| 1195 | return std::set_union(container_algorithm_internal::c_begin(c1), |
| 1196 | container_algorithm_internal::c_end(c1), |
| 1197 | container_algorithm_internal::c_begin(c2), |
| 1198 | container_algorithm_internal::c_end(c2), output, |
| 1199 | std::forward<Compare>(comp)); |
| 1200 | } |
| 1201 | |
| 1202 | // c_set_intersection() |
| 1203 | // |
| 1204 | // Container-based version of the <algorithm> `std::set_intersection()` function |
| 1205 | // to return an iterator containing the intersection of two containers. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1206 | template <typename C1, typename C2, typename OutputIterator, |
| 1207 | typename = typename std::enable_if< |
| 1208 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1209 | void>::type, |
| 1210 | typename = typename std::enable_if< |
| 1211 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1212 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1213 | OutputIterator c_set_intersection(const C1& c1, const C2& c2, |
| 1214 | OutputIterator output) { |
| 1215 | return std::set_intersection(container_algorithm_internal::c_begin(c1), |
| 1216 | container_algorithm_internal::c_end(c1), |
| 1217 | container_algorithm_internal::c_begin(c2), |
| 1218 | container_algorithm_internal::c_end(c2), output); |
| 1219 | } |
| 1220 | |
| 1221 | // Overload of c_set_intersection() for performing a merge using a `comp` other |
| 1222 | // than `operator<`. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1223 | template <typename C1, typename C2, typename OutputIterator, typename Compare, |
| 1224 | typename = typename std::enable_if< |
| 1225 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1226 | void>::type, |
| 1227 | typename = typename std::enable_if< |
| 1228 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1229 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1230 | OutputIterator c_set_intersection(const C1& c1, const C2& c2, |
| 1231 | OutputIterator output, Compare&& comp) { |
| 1232 | return std::set_intersection(container_algorithm_internal::c_begin(c1), |
| 1233 | container_algorithm_internal::c_end(c1), |
| 1234 | container_algorithm_internal::c_begin(c2), |
| 1235 | container_algorithm_internal::c_end(c2), output, |
| 1236 | std::forward<Compare>(comp)); |
| 1237 | } |
| 1238 | |
| 1239 | // c_set_difference() |
| 1240 | // |
| 1241 | // Container-based version of the <algorithm> `std::set_difference()` function |
| 1242 | // to return an iterator containing elements present in the first container but |
| 1243 | // not in the second. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1244 | template <typename C1, typename C2, typename OutputIterator, |
| 1245 | typename = typename std::enable_if< |
| 1246 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1247 | void>::type, |
| 1248 | typename = typename std::enable_if< |
| 1249 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1250 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1251 | OutputIterator c_set_difference(const C1& c1, const C2& c2, |
| 1252 | OutputIterator output) { |
| 1253 | return std::set_difference(container_algorithm_internal::c_begin(c1), |
| 1254 | container_algorithm_internal::c_end(c1), |
| 1255 | container_algorithm_internal::c_begin(c2), |
| 1256 | container_algorithm_internal::c_end(c2), output); |
| 1257 | } |
| 1258 | |
| 1259 | // Overload of c_set_difference() for performing a merge using a `comp` other |
| 1260 | // than `operator<`. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1261 | template <typename C1, typename C2, typename OutputIterator, typename Compare, |
| 1262 | typename = typename std::enable_if< |
| 1263 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1264 | void>::type, |
| 1265 | typename = typename std::enable_if< |
| 1266 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1267 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1268 | OutputIterator c_set_difference(const C1& c1, const C2& c2, |
| 1269 | OutputIterator output, Compare&& comp) { |
| 1270 | return std::set_difference(container_algorithm_internal::c_begin(c1), |
| 1271 | container_algorithm_internal::c_end(c1), |
| 1272 | container_algorithm_internal::c_begin(c2), |
| 1273 | container_algorithm_internal::c_end(c2), output, |
| 1274 | std::forward<Compare>(comp)); |
| 1275 | } |
| 1276 | |
| 1277 | // c_set_symmetric_difference() |
| 1278 | // |
| 1279 | // Container-based version of the <algorithm> `std::set_symmetric_difference()` |
| 1280 | // function to return an iterator containing elements present in either one |
| 1281 | // container or the other, but not both. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1282 | template <typename C1, typename C2, typename OutputIterator, |
| 1283 | typename = typename std::enable_if< |
| 1284 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1285 | void>::type, |
| 1286 | typename = typename std::enable_if< |
| 1287 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1288 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1289 | OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2, |
| 1290 | OutputIterator output) { |
| 1291 | return std::set_symmetric_difference( |
| 1292 | container_algorithm_internal::c_begin(c1), |
| 1293 | container_algorithm_internal::c_end(c1), |
| 1294 | container_algorithm_internal::c_begin(c2), |
| 1295 | container_algorithm_internal::c_end(c2), output); |
| 1296 | } |
| 1297 | |
| 1298 | // Overload of c_set_symmetric_difference() for performing a merge using a |
| 1299 | // `comp` other than `operator<`. |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1300 | template <typename C1, typename C2, typename OutputIterator, typename Compare, |
| 1301 | typename = typename std::enable_if< |
| 1302 | !container_algorithm_internal::IsUnorderedContainer<C1>::value, |
| 1303 | void>::type, |
| 1304 | typename = typename std::enable_if< |
| 1305 | !container_algorithm_internal::IsUnorderedContainer<C2>::value, |
| 1306 | void>::type> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1307 | OutputIterator c_set_symmetric_difference(const C1& c1, const C2& c2, |
| 1308 | OutputIterator output, |
| 1309 | Compare&& comp) { |
| 1310 | return std::set_symmetric_difference( |
| 1311 | container_algorithm_internal::c_begin(c1), |
| 1312 | container_algorithm_internal::c_end(c1), |
| 1313 | container_algorithm_internal::c_begin(c2), |
| 1314 | container_algorithm_internal::c_end(c2), output, |
| 1315 | std::forward<Compare>(comp)); |
| 1316 | } |
| 1317 | |
| 1318 | //------------------------------------------------------------------------------ |
| 1319 | // <algorithm> Heap functions |
| 1320 | //------------------------------------------------------------------------------ |
| 1321 | |
| 1322 | // c_push_heap() |
| 1323 | // |
| 1324 | // Container-based version of the <algorithm> `std::push_heap()` function |
| 1325 | // to push a value onto a container heap. |
| 1326 | template <typename RandomAccessContainer> |
| 1327 | void c_push_heap(RandomAccessContainer& sequence) { |
| 1328 | std::push_heap(container_algorithm_internal::c_begin(sequence), |
| 1329 | container_algorithm_internal::c_end(sequence)); |
| 1330 | } |
| 1331 | |
| 1332 | // Overload of c_push_heap() for performing a push operation on a heap using a |
| 1333 | // `comp` other than `operator<`. |
| 1334 | template <typename RandomAccessContainer, typename Compare> |
| 1335 | void c_push_heap(RandomAccessContainer& sequence, Compare&& comp) { |
| 1336 | std::push_heap(container_algorithm_internal::c_begin(sequence), |
| 1337 | container_algorithm_internal::c_end(sequence), |
| 1338 | std::forward<Compare>(comp)); |
| 1339 | } |
| 1340 | |
| 1341 | // c_pop_heap() |
| 1342 | // |
| 1343 | // Container-based version of the <algorithm> `std::pop_heap()` function |
| 1344 | // to pop a value from a heap container. |
| 1345 | template <typename RandomAccessContainer> |
| 1346 | void c_pop_heap(RandomAccessContainer& sequence) { |
| 1347 | std::pop_heap(container_algorithm_internal::c_begin(sequence), |
| 1348 | container_algorithm_internal::c_end(sequence)); |
| 1349 | } |
| 1350 | |
| 1351 | // Overload of c_pop_heap() for performing a pop operation on a heap using a |
| 1352 | // `comp` other than `operator<`. |
| 1353 | template <typename RandomAccessContainer, typename Compare> |
| 1354 | void c_pop_heap(RandomAccessContainer& sequence, Compare&& comp) { |
| 1355 | std::pop_heap(container_algorithm_internal::c_begin(sequence), |
| 1356 | container_algorithm_internal::c_end(sequence), |
| 1357 | std::forward<Compare>(comp)); |
| 1358 | } |
| 1359 | |
| 1360 | // c_make_heap() |
| 1361 | // |
| 1362 | // Container-based version of the <algorithm> `std::make_heap()` function |
| 1363 | // to make a container a heap. |
| 1364 | template <typename RandomAccessContainer> |
| 1365 | void c_make_heap(RandomAccessContainer& sequence) { |
| 1366 | std::make_heap(container_algorithm_internal::c_begin(sequence), |
| 1367 | container_algorithm_internal::c_end(sequence)); |
| 1368 | } |
| 1369 | |
| 1370 | // Overload of c_make_heap() for performing heap comparisons using a |
| 1371 | // `comp` other than `operator<` |
| 1372 | template <typename RandomAccessContainer, typename Compare> |
| 1373 | void c_make_heap(RandomAccessContainer& sequence, Compare&& comp) { |
| 1374 | std::make_heap(container_algorithm_internal::c_begin(sequence), |
| 1375 | container_algorithm_internal::c_end(sequence), |
| 1376 | std::forward<Compare>(comp)); |
| 1377 | } |
| 1378 | |
| 1379 | // c_sort_heap() |
| 1380 | // |
| 1381 | // Container-based version of the <algorithm> `std::sort_heap()` function |
| 1382 | // to sort a heap into ascending order (after which it is no longer a heap). |
| 1383 | template <typename RandomAccessContainer> |
| 1384 | void c_sort_heap(RandomAccessContainer& sequence) { |
| 1385 | std::sort_heap(container_algorithm_internal::c_begin(sequence), |
| 1386 | container_algorithm_internal::c_end(sequence)); |
| 1387 | } |
| 1388 | |
| 1389 | // Overload of c_sort_heap() for performing heap comparisons using a |
| 1390 | // `comp` other than `operator<` |
| 1391 | template <typename RandomAccessContainer, typename Compare> |
| 1392 | void c_sort_heap(RandomAccessContainer& sequence, Compare&& comp) { |
| 1393 | std::sort_heap(container_algorithm_internal::c_begin(sequence), |
| 1394 | container_algorithm_internal::c_end(sequence), |
| 1395 | std::forward<Compare>(comp)); |
| 1396 | } |
| 1397 | |
| 1398 | // c_is_heap() |
| 1399 | // |
| 1400 | // Container-based version of the <algorithm> `std::is_heap()` function |
| 1401 | // to check whether the given container is a heap. |
| 1402 | template <typename RandomAccessContainer> |
| 1403 | bool c_is_heap(const RandomAccessContainer& sequence) { |
| 1404 | return std::is_heap(container_algorithm_internal::c_begin(sequence), |
| 1405 | container_algorithm_internal::c_end(sequence)); |
| 1406 | } |
| 1407 | |
| 1408 | // Overload of c_is_heap() for performing heap comparisons using a |
| 1409 | // `comp` other than `operator<` |
| 1410 | template <typename RandomAccessContainer, typename Compare> |
| 1411 | bool c_is_heap(const RandomAccessContainer& sequence, Compare&& comp) { |
| 1412 | return std::is_heap(container_algorithm_internal::c_begin(sequence), |
| 1413 | container_algorithm_internal::c_end(sequence), |
| 1414 | std::forward<Compare>(comp)); |
| 1415 | } |
| 1416 | |
| 1417 | // c_is_heap_until() |
| 1418 | // |
| 1419 | // Container-based version of the <algorithm> `std::is_heap_until()` function |
| 1420 | // to find the first element in a given container which is not in heap order. |
| 1421 | template <typename RandomAccessContainer> |
| 1422 | container_algorithm_internal::ContainerIter<RandomAccessContainer> |
| 1423 | c_is_heap_until(RandomAccessContainer& sequence) { |
| 1424 | return std::is_heap_until(container_algorithm_internal::c_begin(sequence), |
| 1425 | container_algorithm_internal::c_end(sequence)); |
| 1426 | } |
| 1427 | |
| 1428 | // Overload of c_is_heap_until() for performing heap comparisons using a |
| 1429 | // `comp` other than `operator<` |
| 1430 | template <typename RandomAccessContainer, typename Compare> |
| 1431 | container_algorithm_internal::ContainerIter<RandomAccessContainer> |
| 1432 | c_is_heap_until(RandomAccessContainer& sequence, Compare&& comp) { |
| 1433 | return std::is_heap_until(container_algorithm_internal::c_begin(sequence), |
| 1434 | container_algorithm_internal::c_end(sequence), |
| 1435 | std::forward<Compare>(comp)); |
| 1436 | } |
| 1437 | |
| 1438 | //------------------------------------------------------------------------------ |
| 1439 | // <algorithm> Min/max |
| 1440 | //------------------------------------------------------------------------------ |
| 1441 | |
| 1442 | // c_min_element() |
| 1443 | // |
| 1444 | // Container-based version of the <algorithm> `std::min_element()` function |
| 1445 | // to return an iterator pointing to the element with the smallest value, using |
| 1446 | // `operator<` to make the comparisons. |
| 1447 | template <typename Sequence> |
| 1448 | container_algorithm_internal::ContainerIter<Sequence> c_min_element( |
| 1449 | Sequence& sequence) { |
| 1450 | return std::min_element(container_algorithm_internal::c_begin(sequence), |
| 1451 | container_algorithm_internal::c_end(sequence)); |
| 1452 | } |
| 1453 | |
| 1454 | // Overload of c_min_element() for performing a `comp` comparison other than |
| 1455 | // `operator<`. |
| 1456 | template <typename Sequence, typename Compare> |
| 1457 | container_algorithm_internal::ContainerIter<Sequence> c_min_element( |
| 1458 | Sequence& sequence, Compare&& comp) { |
| 1459 | return std::min_element(container_algorithm_internal::c_begin(sequence), |
| 1460 | container_algorithm_internal::c_end(sequence), |
| 1461 | std::forward<Compare>(comp)); |
| 1462 | } |
| 1463 | |
| 1464 | // c_max_element() |
| 1465 | // |
| 1466 | // Container-based version of the <algorithm> `std::max_element()` function |
| 1467 | // to return an iterator pointing to the element with the largest value, using |
| 1468 | // `operator<` to make the comparisons. |
| 1469 | template <typename Sequence> |
| 1470 | container_algorithm_internal::ContainerIter<Sequence> c_max_element( |
| 1471 | Sequence& sequence) { |
| 1472 | return std::max_element(container_algorithm_internal::c_begin(sequence), |
| 1473 | container_algorithm_internal::c_end(sequence)); |
| 1474 | } |
| 1475 | |
| 1476 | // Overload of c_max_element() for performing a `comp` comparison other than |
| 1477 | // `operator<`. |
| 1478 | template <typename Sequence, typename Compare> |
| 1479 | container_algorithm_internal::ContainerIter<Sequence> c_max_element( |
| 1480 | Sequence& sequence, Compare&& comp) { |
| 1481 | return std::max_element(container_algorithm_internal::c_begin(sequence), |
| 1482 | container_algorithm_internal::c_end(sequence), |
| 1483 | std::forward<Compare>(comp)); |
| 1484 | } |
| 1485 | |
| 1486 | // c_minmax_element() |
| 1487 | // |
| 1488 | // Container-based version of the <algorithm> `std::minmax_element()` function |
| 1489 | // to return a pair of iterators pointing to the elements containing the |
| 1490 | // smallest and largest values, respectively, using `operator<` to make the |
| 1491 | // comparisons. |
| 1492 | template <typename C> |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 1493 | container_algorithm_internal::ContainerIterPairType<C, C> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1494 | c_minmax_element(C& c) { |
| 1495 | return std::minmax_element(container_algorithm_internal::c_begin(c), |
| 1496 | container_algorithm_internal::c_end(c)); |
| 1497 | } |
| 1498 | |
| 1499 | // Overload of c_minmax_element() for performing `comp` comparisons other than |
| 1500 | // `operator<`. |
| 1501 | template <typename C, typename Compare> |
Abseil Team | 95ddf85 | 2017-11-13 10:04:29 -0800 | [diff] [blame] | 1502 | container_algorithm_internal::ContainerIterPairType<C, C> |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1503 | c_minmax_element(C& c, Compare&& comp) { |
| 1504 | return std::minmax_element(container_algorithm_internal::c_begin(c), |
| 1505 | container_algorithm_internal::c_end(c), |
| 1506 | std::forward<Compare>(comp)); |
| 1507 | } |
| 1508 | |
| 1509 | //------------------------------------------------------------------------------ |
| 1510 | // <algorithm> Lexicographical Comparisons |
| 1511 | //------------------------------------------------------------------------------ |
| 1512 | |
| 1513 | // c_lexicographical_compare() |
| 1514 | // |
| 1515 | // Container-based version of the <algorithm> `std::lexicographical_compare()` |
| 1516 | // function to lexicographically compare (e.g. sort words alphabetically) two |
| 1517 | // container sequences. The comparison is performed using `operator<`. Note |
| 1518 | // that capital letters ("A-Z") have ASCII values less than lowercase letters |
| 1519 | // ("a-z"). |
| 1520 | template <typename Sequence1, typename Sequence2> |
| 1521 | bool c_lexicographical_compare(Sequence1&& sequence1, Sequence2&& sequence2) { |
| 1522 | return std::lexicographical_compare( |
| 1523 | container_algorithm_internal::c_begin(sequence1), |
| 1524 | container_algorithm_internal::c_end(sequence1), |
| 1525 | container_algorithm_internal::c_begin(sequence2), |
| 1526 | container_algorithm_internal::c_end(sequence2)); |
| 1527 | } |
| 1528 | |
| 1529 | // Overload of c_lexicographical_compare() for performing a lexicographical |
| 1530 | // comparison using a `comp` operator instead of `operator<`. |
| 1531 | template <typename Sequence1, typename Sequence2, typename Compare> |
| 1532 | bool c_lexicographical_compare(Sequence1&& sequence1, Sequence2&& sequence2, |
| 1533 | Compare&& comp) { |
| 1534 | return std::lexicographical_compare( |
| 1535 | container_algorithm_internal::c_begin(sequence1), |
| 1536 | container_algorithm_internal::c_end(sequence1), |
| 1537 | container_algorithm_internal::c_begin(sequence2), |
| 1538 | container_algorithm_internal::c_end(sequence2), |
| 1539 | std::forward<Compare>(comp)); |
| 1540 | } |
| 1541 | |
| 1542 | // c_next_permutation() |
| 1543 | // |
| 1544 | // Container-based version of the <algorithm> `std::next_permutation()` function |
| 1545 | // to rearrange a container's elements into the next lexicographically greater |
| 1546 | // permutation. |
| 1547 | template <typename C> |
| 1548 | bool c_next_permutation(C& c) { |
| 1549 | return std::next_permutation(container_algorithm_internal::c_begin(c), |
| 1550 | container_algorithm_internal::c_end(c)); |
| 1551 | } |
| 1552 | |
| 1553 | // Overload of c_next_permutation() for performing a lexicographical |
| 1554 | // comparison using a `comp` operator instead of `operator<`. |
| 1555 | template <typename C, typename Compare> |
| 1556 | bool c_next_permutation(C& c, Compare&& comp) { |
| 1557 | return std::next_permutation(container_algorithm_internal::c_begin(c), |
| 1558 | container_algorithm_internal::c_end(c), |
| 1559 | std::forward<Compare>(comp)); |
| 1560 | } |
| 1561 | |
| 1562 | // c_prev_permutation() |
| 1563 | // |
| 1564 | // Container-based version of the <algorithm> `std::prev_permutation()` function |
| 1565 | // to rearrange a container's elements into the next lexicographically lesser |
| 1566 | // permutation. |
| 1567 | template <typename C> |
| 1568 | bool c_prev_permutation(C& c) { |
| 1569 | return std::prev_permutation(container_algorithm_internal::c_begin(c), |
| 1570 | container_algorithm_internal::c_end(c)); |
| 1571 | } |
| 1572 | |
| 1573 | // Overload of c_prev_permutation() for performing a lexicographical |
| 1574 | // comparison using a `comp` operator instead of `operator<`. |
| 1575 | template <typename C, typename Compare> |
| 1576 | bool c_prev_permutation(C& c, Compare&& comp) { |
| 1577 | return std::prev_permutation(container_algorithm_internal::c_begin(c), |
| 1578 | container_algorithm_internal::c_end(c), |
| 1579 | std::forward<Compare>(comp)); |
| 1580 | } |
| 1581 | |
| 1582 | //------------------------------------------------------------------------------ |
| 1583 | // <numeric> algorithms |
| 1584 | //------------------------------------------------------------------------------ |
| 1585 | |
| 1586 | // c_iota() |
| 1587 | // |
| 1588 | // Container-based version of the <algorithm> `std::iota()` function |
| 1589 | // to compute successive values of `value`, as if incremented with `++value` |
| 1590 | // after each element is written. and write them to the container. |
| 1591 | template <typename Sequence, typename T> |
| 1592 | void c_iota(Sequence& sequence, T&& value) { |
| 1593 | std::iota(container_algorithm_internal::c_begin(sequence), |
| 1594 | container_algorithm_internal::c_end(sequence), |
| 1595 | std::forward<T>(value)); |
| 1596 | } |
| 1597 | // c_accumulate() |
| 1598 | // |
| 1599 | // Container-based version of the <algorithm> `std::accumulate()` function |
| 1600 | // to accumulate the element values of a container to `init` and return that |
| 1601 | // accumulation by value. |
| 1602 | // |
| 1603 | // Note: Due to a language technicality this function has return type |
| 1604 | // absl::decay_t<T>. As a user of this function you can casually read |
| 1605 | // this as "returns T by value" and assume it does the right thing. |
| 1606 | template <typename Sequence, typename T> |
| 1607 | decay_t<T> c_accumulate(const Sequence& sequence, T&& init) { |
| 1608 | return std::accumulate(container_algorithm_internal::c_begin(sequence), |
| 1609 | container_algorithm_internal::c_end(sequence), |
| 1610 | std::forward<T>(init)); |
| 1611 | } |
| 1612 | |
| 1613 | // Overload of c_accumulate() for using a binary operations other than |
| 1614 | // addition for computing the accumulation. |
| 1615 | template <typename Sequence, typename T, typename BinaryOp> |
| 1616 | decay_t<T> c_accumulate(const Sequence& sequence, T&& init, |
| 1617 | BinaryOp&& binary_op) { |
| 1618 | return std::accumulate(container_algorithm_internal::c_begin(sequence), |
| 1619 | container_algorithm_internal::c_end(sequence), |
| 1620 | std::forward<T>(init), |
| 1621 | std::forward<BinaryOp>(binary_op)); |
| 1622 | } |
| 1623 | |
| 1624 | // c_inner_product() |
| 1625 | // |
| 1626 | // Container-based version of the <algorithm> `std::inner_product()` function |
| 1627 | // to compute the cumulative inner product of container element pairs. |
| 1628 | // |
| 1629 | // Note: Due to a language technicality this function has return type |
| 1630 | // absl::decay_t<T>. As a user of this function you can casually read |
| 1631 | // this as "returns T by value" and assume it does the right thing. |
| 1632 | template <typename Sequence1, typename Sequence2, typename T> |
| 1633 | decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2, |
| 1634 | T&& sum) { |
| 1635 | return std::inner_product(container_algorithm_internal::c_begin(factors1), |
| 1636 | container_algorithm_internal::c_end(factors1), |
| 1637 | container_algorithm_internal::c_begin(factors2), |
| 1638 | std::forward<T>(sum)); |
| 1639 | } |
| 1640 | |
| 1641 | // Overload of c_inner_product() for using binary operations other than |
Bruce Mitchener | 08760ad | 2018-04-20 01:11:44 +0700 | [diff] [blame] | 1642 | // `operator+` (for computing the accumulation) and `operator*` (for computing |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1643 | // the product between the two container's element pair). |
| 1644 | template <typename Sequence1, typename Sequence2, typename T, |
| 1645 | typename BinaryOp1, typename BinaryOp2> |
| 1646 | decay_t<T> c_inner_product(const Sequence1& factors1, const Sequence2& factors2, |
| 1647 | T&& sum, BinaryOp1&& op1, BinaryOp2&& op2) { |
| 1648 | return std::inner_product(container_algorithm_internal::c_begin(factors1), |
| 1649 | container_algorithm_internal::c_end(factors1), |
| 1650 | container_algorithm_internal::c_begin(factors2), |
| 1651 | std::forward<T>(sum), std::forward<BinaryOp1>(op1), |
| 1652 | std::forward<BinaryOp2>(op2)); |
| 1653 | } |
| 1654 | |
| 1655 | // c_adjacent_difference() |
| 1656 | // |
| 1657 | // Container-based version of the <algorithm> `std::adjacent_difference()` |
| 1658 | // function to compute the difference between each element and the one preceding |
| 1659 | // it and write it to an iterator. |
| 1660 | template <typename InputSequence, typename OutputIt> |
| 1661 | OutputIt c_adjacent_difference(const InputSequence& input, |
| 1662 | OutputIt output_first) { |
| 1663 | return std::adjacent_difference(container_algorithm_internal::c_begin(input), |
| 1664 | container_algorithm_internal::c_end(input), |
| 1665 | output_first); |
| 1666 | } |
| 1667 | |
| 1668 | // Overload of c_adjacent_difference() for using a binary operation other than |
| 1669 | // subtraction to compute the adjacent difference. |
| 1670 | template <typename InputSequence, typename OutputIt, typename BinaryOp> |
| 1671 | OutputIt c_adjacent_difference(const InputSequence& input, |
| 1672 | OutputIt output_first, BinaryOp&& op) { |
| 1673 | return std::adjacent_difference(container_algorithm_internal::c_begin(input), |
| 1674 | container_algorithm_internal::c_end(input), |
| 1675 | output_first, std::forward<BinaryOp>(op)); |
| 1676 | } |
| 1677 | |
| 1678 | // c_partial_sum() |
| 1679 | // |
| 1680 | // Container-based version of the <algorithm> `std::partial_sum()` function |
| 1681 | // to compute the partial sum of the elements in a sequence and write them |
| 1682 | // to an iterator. The partial sum is the sum of all element values so far in |
| 1683 | // the sequence. |
| 1684 | template <typename InputSequence, typename OutputIt> |
| 1685 | OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first) { |
| 1686 | return std::partial_sum(container_algorithm_internal::c_begin(input), |
| 1687 | container_algorithm_internal::c_end(input), |
| 1688 | output_first); |
| 1689 | } |
| 1690 | |
| 1691 | // Overload of c_partial_sum() for using a binary operation other than addition |
| 1692 | // to compute the "partial sum". |
| 1693 | template <typename InputSequence, typename OutputIt, typename BinaryOp> |
| 1694 | OutputIt c_partial_sum(const InputSequence& input, OutputIt output_first, |
| 1695 | BinaryOp&& op) { |
| 1696 | return std::partial_sum(container_algorithm_internal::c_begin(input), |
| 1697 | container_algorithm_internal::c_end(input), |
| 1698 | output_first, std::forward<BinaryOp>(op)); |
| 1699 | } |
| 1700 | |
Abseil Team | fcb1045 | 2018-12-04 11:01:12 -0800 | [diff] [blame] | 1701 | } // inline namespace lts_2018_12_18 |
misterg | c2e7548 | 2017-09-19 16:54:40 -0400 | [diff] [blame] | 1702 | } // namespace absl |
| 1703 | |
| 1704 | #endif // ABSL_ALGORITHM_CONTAINER_H_ |