1- // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
1+ // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
22// vim: ts=8 sw=2 smarttab
33#pragma once
44/* *
2525 * appears most frequently) of the collected values.
2626 *
2727 * The template parameters are:
28- * - OBJ_ID : The type of the object ID (e.g., replica ID).
28+ * - ObjIdT : The type of the object ID (e.g., replica ID).
2929 * - K: The type of the value being collected.
30- * - HSH : The hash function for K, to be used with the unordered_map.
31- * Note: if HSH is std::identity, then K must fit in size_t.
30+ * - HshT : The hash function for K, to be used with the unordered_map.
31+ * Note: if HshT is std::identity, then K must fit in size_t.
3232 * - MAX_ELEM is used to calculate the estimated memory footprint of the
3333 * unordered_map.
3434 *
@@ -42,9 +42,9 @@ struct ModeFinder {
4242
4343 // / a 'non-templated' version of mode_status_t, to simplify usage.
4444 enum class mode_status_t {
45- no_mode_value, // /< No clear victory for any value
46- mode_value, // /< we have a winner, but it appears in less than half
47- // /< of the samples
45+ no_mode_value, // /< No clear victory for any value
46+ mode_value, // /< we have a winner, but it appears in less than half
47+ // /< of the samples
4848 authorative_value // /< more than half of the samples are of the same value
4949 };
5050};
@@ -54,44 +54,43 @@ struct ModeFinder {
5454// of the unrdered map).
5555
5656template <
57- typename OBJ_ID, // /< how to identify the object that reported a value
58- typename K, // /< the type of the value being collected
59- typename HSH = std::identity, // /< the hash function for K
57+ typename ObjIdT, // /< how to identify the object that reported a value
58+ typename K, // /< the type of the value being collected
59+ typename HshT = std::identity, // /< the hash function for K
6060 int MAX_ELEM = 12 >
6161 requires (
62- std::invocable<HSH , K> &&
63- sizeof (std::invoke_result_t <HSH , K>) <= sizeof(size_t ))
62+ std::invocable<HshT , K> &&
63+ sizeof (std::invoke_result_t <HshT , K>) <= sizeof(size_t ))
6464class ModeCollector : public ModeFinder {
65- private:
6665 struct node_type_t {
6766 size_t m_count{0 };
68- OBJ_ID m_id; // /< Stores the object ID associated with this value
67+ ObjIdT m_id; // /< Stores the object ID associated with this value
6968 };
7069
7170 // estimated (upper limit) memory footprint of the unordered_map
7271 // vvvvvvvvvvvvvvvvvvvvvvvvvvvv
7372 // Bucket array: typically 2x num_elements for good load factor
74- static const size_t bucket_array_size = (MAX_ELEM * 2 ) * sizeof (void *);
73+ static const size_t BUCKET_ARRAY_SIZE = (MAX_ELEM * 2 ) * sizeof (void *);
7574 // Node storage: each elem needs hash + next-ptr
76- static constexpr size_t node_overhead = sizeof (void *) + sizeof (size_t );
77- static constexpr size_t node_storage =
78- MAX_ELEM * (sizeof (K) + sizeof (node_type_t ) + node_overhead );
75+ static constexpr size_t NODE_OVERHEAD = sizeof (void *) + sizeof (size_t );
76+ static constexpr size_t NODE_STORAGE =
77+ MAX_ELEM * (sizeof (K) + sizeof (node_type_t ) + NODE_OVERHEAD );
7978 // PMR allocator overhead (alignment, bookkeeping)
80- static constexpr size_t pmr_overhead_per_alloc = 16 ; // typical
79+ static constexpr size_t PMR_OVERHEAD_PER_ALLOC = 16 ; // typical
8180 // bucket array + nodes
82- static constexpr size_t total_overhead = pmr_overhead_per_alloc * 2 ;
83- static constexpr size_t m_estimated_memory_footprint =
84- bucket_array_size + node_storage + total_overhead ;
81+ static constexpr size_t TOTAL_OVERHEAD = PMR_OVERHEAD_PER_ALLOC * 2 ;
82+ static constexpr size_t ESTIMATED_MEMORY_FOOTPRINT =
83+ BUCKET_ARRAY_SIZE + NODE_STORAGE + TOTAL_OVERHEAD ;
8584 // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8685
87- std::array<std::byte, m_estimated_memory_footprint > m_buffer;
86+ std::array<std::byte, ESTIMATED_MEMORY_FOOTPRINT > m_buffer;
8887 std::pmr::monotonic_buffer_resource m_mbr{m_buffer.data (), m_buffer.size ()};
8988
9089 // / Map to store the occurrence count of each value
9190 std::pmr::unordered_map<
9291 K,
9392 node_type_t ,
94- HSH ,
93+ HshT ,
9594 std::equal_to<K> >
9695 m_frequency_map;
9796
@@ -108,7 +107,7 @@ class ModeCollector : public ModeFinder {
108107 K key;
109108 // / an object ID, "arbitrary" selected from the set of objects that
110109 // / reported the mode value
111- OBJ_ID id;
110+ ObjIdT id;
112111 // / the number of times the mode value was reported
113112 size_t count;
114113 auto operator <=>(const results_t & rhs) const = default ;
@@ -120,7 +119,7 @@ class ModeCollector : public ModeFinder {
120119 }
121120
122121 // / Add a value to the collector
123- void insert (const OBJ_ID & obj, const K& value) noexcept
122+ void insert (const ObjIdT & obj, const K& value) noexcept
124123 {
125124 auto & node = m_frequency_map[value];
126125 node.m_count ++;
@@ -141,34 +140,33 @@ class ModeCollector : public ModeFinder {
141140 assert (!m_frequency_map.empty ());
142141
143142 auto max_elem = std::ranges::max_element (
144- m_frequency_map, {},
145- [](const auto & pair) { return pair.second .m_count ; });
143+ m_frequency_map, {},
144+ [](const auto & pair) { return pair.second .m_count ; });
146145
147146 // Check for clear victory
148147 if (max_elem->second .m_count > m_actual_count / 2 ) {
149148 return {
150- mode_status_t ::authorative_value, max_elem->first ,
151- max_elem->second .m_id , max_elem->second .m_count };
149+ mode_status_t ::authorative_value, max_elem->first ,
150+ max_elem->second .m_id , max_elem->second .m_count };
152151 }
153152
154153 // Check for possible ties
155154 const auto max_elem_cnt = max_elem->second .m_count ;
156155
157156 max_elem->second .m_count = 0 ; // Reset the count of the max element
158157 const auto second_best_elem = std::ranges::max_element (
159- m_frequency_map, {},
160- [](const auto & pair) { return pair.second .m_count ; });
158+ m_frequency_map, {},
159+ [](const auto & pair) { return pair.second .m_count ; });
161160 max_elem->second .m_count = max_elem_cnt; // Restore the count
162161
163162 if (second_best_elem->second .m_count == max_elem_cnt) {
164163 return {
165- mode_status_t ::no_mode_value, max_elem->first , max_elem->second .m_id ,
166- max_elem_cnt};
164+ mode_status_t ::no_mode_value, max_elem->first , max_elem->second .m_id ,
165+ max_elem_cnt};
167166 }
168167
169168 return {
170- mode_status_t ::mode_value, max_elem->first , max_elem->second .m_id ,
171- max_elem_cnt};
169+ mode_status_t ::mode_value, max_elem->first , max_elem->second .m_id ,
170+ max_elem_cnt};
172171 }
173172};
174-
0 commit comments