Skip to content

Commit

Permalink
flat_map: Avoid double-templating Pair
Browse files Browse the repository at this point in the history
As is, the type is a bit obnoxious to name, because you have to use
either FlatMap<K, V, 1>::Pair<K, V> or FlatMap<K, V, 1>::value_type.

Because FlatMap doesn't have any copy semantics, you are pretty much
required to use constructors that take the expected std::array, should a
class want to decorate this type.

Change-Id: I908fdf7d0f4502932d2c1244e8e8b5dc593267a8
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/102130
Commit-Queue: Anqi Dong <anqid@google.com>
Reviewed-by: Armando Montanez <amontanez@google.com>
  • Loading branch information
anqid-g authored and CQ Bot Account committed Jul 15, 2022
1 parent 1cab1b4 commit f4a8b04
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions pw_containers/public/pw_containers/flat_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,25 @@

namespace pw::containers {

// Define and use a custom Pair object. This is because std::pair does not
// support constexpr assignment until C++20. The assignment is needed since
// the array of pairs will be sorted in the constructor (if not already).
template <typename First, typename Second>
struct Pair {
First first;
Second second;
};

// A simple, fixed-size associative array with lookup by key or value.
//
// FlatMaps are initialized with a std::array of FlatMap::Pair objects:
// FlatMaps are initialized with a std::array of Pair<K, V> objects:
// FlatMap<int, int> map({{{1, 2}, {3, 4}}});
//
// The keys do not need to be sorted as the constructor will sort the items
// if need be.
template <typename Key, typename Value, size_t kArraySize>
class FlatMap {
public:
// Define and use a custom Pair object. This is because std::pair does not
// support constexpr assignment until C++20. The assignment is needed since
// the array of pairs will be sorted in the constructor (if not already).
template <typename First, typename Second>
struct Pair {
First first;
Second second;
};

using key_type = Key;
using mapped_type = Value;
using value_type = Pair<key_type, mapped_type>;
Expand Down

0 comments on commit f4a8b04

Please sign in to comment.