Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Jordan Bayles | a26582d | 2019-07-10 14:44:58 -0700 | [diff] [blame] | 5 | #ifndef UTIL_STD_UTIL_H_ |
| 6 | #define UTIL_STD_UTIL_H_ |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame] | 7 | |
mark a. foltz | 044f254 | 2021-09-22 17:29:43 -0700 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
Ryan Keane | 03e7503 | 2020-06-17 17:04:34 -0700 | [diff] [blame] | 10 | #include <algorithm> |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame] | 11 | #include <map> |
Jordan Bayles | 067ccaf | 2019-06-07 10:03:02 -0700 | [diff] [blame] | 12 | #include <string> |
Ryan Keane | 03e7503 | 2020-06-17 17:04:34 -0700 | [diff] [blame] | 13 | #include <utility> |
Jordan Bayles | 05ce6b6 | 2019-09-06 14:09:10 -0700 | [diff] [blame] | 14 | #include <vector> |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame] | 15 | |
Yuri Wiitala | c5b9017 | 2019-04-15 15:42:30 -0700 | [diff] [blame] | 16 | #include "absl/algorithm/container.h" |
Jordan Bayles | 12b95e7 | 2020-12-03 09:26:47 -0800 | [diff] [blame] | 17 | #include "util/stringprintf.h" |
Yuri Wiitala | c5b9017 | 2019-04-15 15:42:30 -0700 | [diff] [blame] | 18 | |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame] | 19 | namespace openscreen { |
| 20 | |
Max Yakimakha | 349c2aa | 2019-09-09 11:01:34 -0700 | [diff] [blame] | 21 | template <typename T, size_t N> |
| 22 | constexpr size_t countof(T (&array)[N]) { |
| 23 | return N; |
| 24 | } |
| 25 | |
Jordan Bayles | 067ccaf | 2019-06-07 10:03:02 -0700 | [diff] [blame] | 26 | // std::basic_string::data() has no mutable overload prior to C++17 [1]. |
| 27 | // Hence this overload is provided. |
| 28 | // Note: str[0] is safe even for empty strings, as they are guaranteed to be |
| 29 | // null-terminated [2]. |
| 30 | // |
| 31 | // [1] http://en.cppreference.com/w/cpp/string/basic_string/data |
| 32 | // [2] http://en.cppreference.com/w/cpp/string/basic_string/operator_at |
| 33 | template <typename CharT, typename Traits, typename Allocator> |
| 34 | CharT* data(std::basic_string<CharT, Traits, Allocator>& str) { |
| 35 | return std::addressof(str[0]); |
| 36 | } |
| 37 | |
Jordan Bayles | 12b95e7 | 2020-12-03 09:26:47 -0800 | [diff] [blame] | 38 | std::string Join(const std::vector<std::string>& strings, |
| 39 | const char* delimiter); |
| 40 | |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame] | 41 | template <typename Key, typename Value> |
| 42 | void RemoveValueFromMap(std::map<Key, Value*>* map, Value* value) { |
| 43 | for (auto it = map->begin(); it != map->end();) { |
| 44 | if (it->second == value) { |
| 45 | it = map->erase(it); |
| 46 | } else { |
| 47 | ++it; |
| 48 | } |
| 49 | } |
| 50 | } |
| 51 | |
Yuri Wiitala | c5b9017 | 2019-04-15 15:42:30 -0700 | [diff] [blame] | 52 | template <typename ForwardIteratingContainer> |
| 53 | bool AreElementsSortedAndUnique(const ForwardIteratingContainer& c) { |
| 54 | return absl::c_is_sorted(c) && (absl::c_adjacent_find(c) == c.end()); |
| 55 | } |
| 56 | |
| 57 | template <typename RandomAccessContainer> |
| 58 | void SortAndDedupeElements(RandomAccessContainer* c) { |
| 59 | std::sort(c->begin(), c->end()); |
| 60 | const auto new_end = std::unique(c->begin(), c->end()); |
| 61 | c->erase(new_end, c->end()); |
| 62 | } |
| 63 | |
Ryan Keane | 03e7503 | 2020-06-17 17:04:34 -0700 | [diff] [blame] | 64 | // Append the provided elements together into a single vector. This can be |
| 65 | // useful when creating a vector of variadic templates in the ctor. |
| 66 | // |
| 67 | // This is the base case for the recursion |
| 68 | template <typename T> |
| 69 | std::vector<T>&& Append(std::vector<T>&& so_far) { |
| 70 | return std::move(so_far); |
| 71 | } |
| 72 | |
| 73 | // This is the recursive call. Depending on the number of remaining elements, it |
| 74 | // either calls into itself or into the above base case. |
| 75 | template <typename T, typename TFirst, typename... TOthers> |
| 76 | std::vector<T>&& Append(std::vector<T>&& so_far, |
| 77 | TFirst&& new_element, |
| 78 | TOthers&&... new_elements) { |
| 79 | so_far.push_back(std::move(new_element)); |
| 80 | return Append(std::move(so_far), std::move(new_elements)...); |
| 81 | } |
| 82 | |
| 83 | // Creates an empty vector with |size| elements reserved. Intended to be used as |
| 84 | // GetEmptyVectorOfSize<T>(sizeof...(variadic_input)) |
| 85 | template <typename T> |
| 86 | std::vector<T> GetVectorWithCapacity(size_t size) { |
| 87 | std::vector<T> results; |
| 88 | results.reserve(size); |
| 89 | return results; |
| 90 | } |
| 91 | |
mark a. foltz | 044f254 | 2021-09-22 17:29:43 -0700 | [diff] [blame] | 92 | // Returns true if an element equal to |element| is found in |container|. |
| 93 | // C.begin() must return an iterator to the beginning of C and C.end() must |
| 94 | // return an iterator to the end. |
| 95 | template <typename C, typename E> |
| 96 | bool Contains(const C& container, const E& element) { |
| 97 | return std::find(container.begin(), container.end(), element) != |
| 98 | container.end(); |
| 99 | } |
| 100 | |
| 101 | // Returns true if any element in |container| returns true for |predicate|. |
| 102 | // C.begin() must return an iterator to the beginning of C and C.end() must |
| 103 | // return an iterator to the end. |
| 104 | template <typename C, typename P> |
| 105 | bool ContainsIf(const C& container, P predicate) { |
| 106 | return std::find_if(container.begin(), container.end(), |
| 107 | std::move(predicate)) != container.end(); |
| 108 | } |
| 109 | |
Jordan Bayles | 8ccb485 | 2019-01-16 18:51:27 -0800 | [diff] [blame] | 110 | } // namespace openscreen |
| 111 | |
Jordan Bayles | a26582d | 2019-07-10 14:44:58 -0700 | [diff] [blame] | 112 | #endif // UTIL_STD_UTIL_H_ |