Skip to content

Commit

Permalink
Dirty hack for single resistor solutions, spelling fix
Browse files Browse the repository at this point in the history
  • Loading branch information
platofff committed Feb 20, 2024
1 parent 73b4cba commit e02fd6e
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ node_modules
.output/*
dist
.nuxt/*
.cache/*
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
#include "__generator.hpp"
#endif

std::generator<const std::vector<size_t>&> gen_indicies(const size_t n,
std::generator<const std::vector<size_t>&> gen_indices(const size_t n,
const size_t r);

template <typename T>
std::generator<std::vector<T>&&> combinations_with_replacement(
const std::vector<T>& pool,
const size_t r) {
for (const auto& indicies : gen_indicies(pool.size(), r)) {
for (const auto& indices : gen_indices(pool.size(), r)) {
auto combination = std::vector<T>(r);

auto transform_view =
std::views::iota((size_t)0, r) |
std::views::transform(
[&indicies, &pool](const size_t i) { return pool[indicies[i]]; });
[&indices, &pool](const size_t i) { return pool[indices[i]]; });

std::ranges::copy(transform_view, combination.begin());
co_yield std::move(combination);
Expand Down
6 changes: 3 additions & 3 deletions resistor-optimization/src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
int main() {
#if !__has_include(<emscripten/bind.h>)
// some tests
int r = 5;
std::vector<double> resistors{17.56};
int r = 3;
std::vector<double> resistors{1.0, 1.7, 7.5, 3.6, 9};;

const auto ro = ResistanceOptimizer(resistors);
const auto [svg, resistance] = ro.optimize(17.56, r);
const auto [svg, resistance] = ro.optimize(1.7, r);
std::cout << resistance << std::endl;
std::cout << svg << std::endl;
#endif
Expand Down
7 changes: 7 additions & 0 deletions resistor-optimization/src/ResistanceOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ ResistanceOptimizer::generateCombinations(
const auto c = std::ranges::subrange(begin, end);

if (base == nullptr) {
for (const auto& resistor : c) {
const auto base = std::make_shared<SeriesContainer>();
base->add(resistor);
co_yield base;
}

const auto base1 = std::make_shared<SeriesContainer>();
for (const auto& res : generateCombinations(
begin, end, base1)) {
Expand Down Expand Up @@ -61,6 +67,7 @@ ResistanceOptimizer::generateCombinations(
}
} else {
auto pcont = std::dynamic_pointer_cast<ParallelContainer>(container);
assert(pcont);

const auto new_scont = std::make_shared<SeriesContainer>();
new_scont->add(c[0]);
Expand Down
10 changes: 5 additions & 5 deletions resistor-optimization/src/combinations_with_replacement.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
#include "combinations_with_replacement.hpp"

std::generator<const std::vector<size_t>&> gen_indicies(const size_t n,
std::generator<const std::vector<size_t>&> gen_indices(const size_t n,
const size_t r) {
std::vector<size_t> indicies(r);
co_yield indicies;
const auto rev = indicies | std::views::reverse;
std::vector<size_t> indices(r);
co_yield indices;
const auto rev = indices | std::views::reverse;

while (true) {
const auto found = std::ranges::find_if(
Expand All @@ -19,6 +19,6 @@ std::generator<const std::vector<size_t>&> gen_indicies(const size_t n,
el = fill;
}

co_yield indicies;
co_yield indices;
}
}

0 comments on commit e02fd6e

Please sign in to comment.