Skip to content

Commit

Permalink
keep track of channel initializations in an ordered set
Browse files Browse the repository at this point in the history
  • Loading branch information
rghilduta committed Nov 29, 2023
1 parent 810afe3 commit 27de289
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions lib/channel_store.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
#include <map>
#include <set>

template<typename T>
class channel_store
{
std::map<size_t, T> values_;
std::set<size_t> initialized_channels_;

public:
template <typename pred>
T set_if_not_equal(T value, size_t n, size_t max_n, pred fn)
{
if( n < max_n)
{
auto &val = values_[n];
if(val != value)
{
val = fn();
}
return val;
if( initialized_channels_.find(n) == initialized_channels_.end())
{
values_[n] = fn();
initialized_channels_.insert(n);
} else {
auto &val = values_[n];
if( val != value)
{
val = fn();
}
}
return values_[n];
}
return T{};
}
Expand Down

0 comments on commit 27de289

Please sign in to comment.