Skip to content

Commit 99a75e9

Browse files
committed
Asked GitHub Copilot about the sum problem
1 parent c52f47e commit 99a75e9

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

include/CommonConfig.hpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
static_assert(__cplusplus >= 202302L, "C++23 standard or later required.");
44

55
// Imports
6+
#include <cstddef>
67
#include <cstdint>
78
#include <etl/array.h>
89
#include <etl/combinations.h>
910
#include <etl/vector.h>
11+
#include <utility>
1012

1113
// CommonConfig.hpp
1214

@@ -54,12 +56,62 @@ namespace LOAD {
5456
etl::combinations<NUM_PINS, 4>::value +
5557
etl::combinations<NUM_PINS, 5>::value +
5658
etl::combinations<NUM_PINS, 6>::value;
59+
60+
// According to GitHub Copilot, GPT-5.1, Ask mode
61+
static_assert(NUM_COMBINATIONS == (1u << NUM_PINS),
62+
"NUM_COMBINATIONS must be 2^NUM_PINS");
63+
5764
constexpr etl::array<uint_fast16_t, NUM_PINS> PIN_VALUES_mOhms = {
5865
500, 1000, 2000, 3000, 5000, 10000}; // in mOhms, in series // TODO
5966
static_assert(PIN_VALUES_mOhms.size() == NUM_PINS,
6067
"PIN_VALUES size mismatch");
6168
static_assert(PIN_VALUES_mOhms.back() < UINT16_MAX,
6269
"PIN_VALUES value too large");
70+
71+
/**
72+
* @brief Detail namespace for combination sum calculations
73+
* @author GitHub Copilot, GPT-5.1, Ask mode
74+
* @author BobSaidHi
75+
* @details Prompt: Using C++23 consteval functions and templates, I would
76+
* like to create an array of all possible combinations of values in
77+
* PIN_VALUES_mOhms. (included some relevant lines of my own attempts as
78+
* context)
79+
*/
80+
namespace detail {
81+
// Sum of selected pins for a given bitmask
82+
consteval uint_fast16_t
83+
sum_for_mask(uint_fast8_t mask,
84+
const etl::array<uint_fast16_t, NUM_PINS> &values) {
85+
86+
uint_fast16_t sum = 0;
87+
for (std::size_t i = 0; i < NUM_PINS; ++i) {
88+
if (mask & (1u << i)) {
89+
sum += values[i];
90+
}
91+
}
92+
return sum;
93+
}
94+
95+
template <std::size_t... Is>
96+
consteval etl::array<uint_fast16_t, NUM_COMBINATIONS>
97+
make_all_combinations(const etl::array<uint_fast16_t, NUM_PINS> &values,
98+
std::index_sequence<Is...>) {
99+
100+
return {sum_for_mask(static_cast<uint_fast8_t>(Is), values)...};
101+
}
102+
} // namespace detail
103+
104+
/**
105+
* @brief get all combinations of values
106+
* @author GitHub Copilot, GPT-5.1, Ask mode
107+
* @author BobSaidHi
108+
*/
109+
consteval etl::array<uint_fast16_t, NUM_COMBINATIONS>
110+
getAllCombinations(const etl::array<uint_fast16_t, NUM_PINS> &values) {
111+
return detail::make_all_combinations(
112+
values, std::make_index_sequence<NUM_COMBINATIONS>{});
113+
}
114+
63115
constexpr etl::array<uint_fast16_t, NUM_COMBINATIONS>
64116
ALL_COMBINATIONS_mOhms = getAllCombinations(PIN_VALUES_mOhms);
65117

0 commit comments

Comments
 (0)