Skip to content

Commit

Permalink
fix: getFullList caused computer to crash if negative number is used
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Mar 29, 2024
1 parent 72b7d75 commit 6340673
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 18 deletions.
32 changes: 17 additions & 15 deletions hipo4/bank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,23 +362,23 @@ void bank::rowlist::setList(list_t list) {
m_init = true;
}

void bank::rowlist::reduce(std::function<double(bank&, int)> func) {
void bank::rowlist::reduce(std::function<bool(bank&, int)> func) {
if(ownerBankIsUnknown("reduce"))
return;
auto indx = m_list;
m_list.clear();
for(auto const& r : indx)
if(func(*m_owner_bank, r) > 0.5)
if(func(*m_owner_bank, r) == true)
m_list.push_back(r);
}

void bank::rowlist::reduce(const char *expression) {
if(ownerBankIsUnknown("reduce"))
return;
Parser p(expression);
int nitems = m_owner_bank->getSchema().getEntries();
int nitems = m_owner_bank->getSchema().getEntries();
schema &schema = m_owner_bank->getSchema();
auto indx = m_list;
auto indx = m_list;
m_list.clear();
for(auto const& r : indx){
for(int i = 0; i < nitems; i++)
Expand All @@ -388,16 +388,11 @@ void bank::rowlist::reduce(const char *expression) {
}
}

bank::rowlist::list_t bank::rowlist::s_number_list_init(list_t::size_type num) {
list_t result;
for(list_t::size_type i = 0; i < num; i++)
result.push_back(i);
return result;
}

bank::rowlist::list_t bank::rowlist::getFullList(list_t::size_type num) {
if(num < 0)
bank::rowlist::list_t bank::rowlist::getFullList(int num) {
if(num < 0) {
std::cerr << "ERROR: attempted to call rowlist::getFullRowList with a negative size" << std::endl;
return {};
}
if(num <= s_number_list.size())
return list_t(s_number_list.begin(), s_number_list.begin() + num);
else {
Expand All @@ -408,8 +403,6 @@ bank::rowlist::list_t bank::rowlist::getFullList(list_t::size_type num) {
}
}

bank::rowlist::list_t bank::rowlist::s_number_list = bank::rowlist::s_number_list_init();

bool bank::rowlist::ownerBankIsUnknown(std::string_view caller) {
if(m_owner_bank == nullptr) {
std::cerr <<
Expand All @@ -422,6 +415,15 @@ bool bank::rowlist::ownerBankIsUnknown(std::string_view caller) {
return false;
}

bank::rowlist::list_t bank::rowlist::s_number_list_init(list_t::size_type num) {
list_t result;
for(list_t::size_type i = 0; i < num; i++)
result.push_back(i);
return result;
}

bank::rowlist::list_t bank::rowlist::s_number_list = bank::rowlist::s_number_list_init();


//////////////////////////////////////////////////////////////////////////////////
// hipo::bank
Expand Down
6 changes: 3 additions & 3 deletions hipo4/bank.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,15 @@ namespace hipo {

/// filter the list according to a function
/// @param func a function which takes a `hipo::bank` reference and an `int` row number and returns a `double`;
/// if the returned `double` is larger than 0.5, the row is accepted
void reduce(std::function<double(bank&, int)> func);
/// if the returned `bool` is `true`, the row is accepted
void reduce(std::function<bool(bank&, int)> func);
/// filter the list according to an expression
/// @param expression the filter expression
void reduce(char const* expression);

/// @returns a list of numbers from 0 to `num`
/// @param num the size of the list
static list_t getFullList(list_t::size_type num);
static list_t getFullList(int num);

/// @param ownerBank set the owner bank
void setOwnerBank(bank* const ownerBank) { m_owner_bank = ownerBank; }
Expand Down

0 comments on commit 6340673

Please sign in to comment.