Skip to content

Commit

Permalink
refactor: restore some modularity
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Mar 27, 2024
1 parent cba9e30 commit 2fe77a5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 59 deletions.
126 changes: 85 additions & 41 deletions hipo4/bank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,84 @@ void composite::print(){
}


//////////////////////////////////////////////////////////////////////////////////
// hipo::bank::rowlist
//////////////////////////////////////////////////////////////////////////////////

bank::rowlist::list_t bank::rowlist::generate_number_list(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::copy_number_list(list_t::size_type num) {
if(num <= s_number_list.size())
return list_t(s_number_list.begin(), s_number_list.begin() + num);
else {
auto result = s_number_list;
for(list_t::size_type i = s_number_list.size(); i < num; i++)
result.push_back(i);
return result;
}
}

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

void bank::rowlist::initialize(int numRows) {
m_list = copy_number_list(numRows);
m_init = true;
}

void bank::rowlist::uninitialize() {
m_list.clear();
m_init = false;
}

bank::rowlist::list_t const& bank::rowlist::getList() const {
if(!m_init)
std::cerr << "WARNING: attempted to get an uninitialized bank row list" << std::endl;
return m_list;
}

void bank::rowlist::setList(list_t& list) {
m_list = list;
m_init = true;
}

void bank::rowlist::reduce(std::function<double(hipo::bank&, int)> func) {
if(m_owner_bank == nullptr) {
std::cerr << "ERROR: attempted to call rowlist::reduce() but no bank is owned (please call setBank())" << std::endl;
return;
}
auto indx = m_list;
m_list.clear();
for(auto const& r : indx)
if(func(*m_owner_bank, r) > 0.5)
m_list.push_back(r);
}

void bank::rowlist::reduce(const char *expression) {
if(m_owner_bank == nullptr) {
std::cerr << "ERROR: attempted to call rowlist::reduce() but no bank is owned (please call setBank())" << std::endl;
return;
}
hipo::Parser p(expression);
int nitems = m_owner_bank->getSchema().getEntries();
hipo::schema &schema = m_owner_bank->getSchema();
auto indx = m_list;
m_list.clear();
for(auto const& r : indx){
for(int i = 0; i < nitems; i++)
p[schema.getEntryName(i)] = m_owner_bank->get(i,r);
if(p.Evaluate() > 0.5)
m_list.push_back(r);
}
}

//////////////////////////////////////////////////////////////////////////////////
// hipo::bank
//////////////////////////////////////////////////////////////////////////////////

bank::bank()= default;

Expand All @@ -349,8 +427,7 @@ void bank::setRows(int rows){
void bank::reset(){
setSize(0);
bankRows = 0;
bankIteratorRows.clear();
bankIteratorRows.push_back(-1);
bankRowList.initialize(0);
}

void bank::notify(){
Expand All @@ -360,7 +437,6 @@ void bank::notify(){
// getSize(),size, bankRows);
}


void bank::putInt(const char *name, int index, int32_t value){
int item = bankSchema.getEntryOrder(name);
int type = bankSchema.getEntryType(item);
Expand Down Expand Up @@ -400,55 +476,23 @@ void bank::putLong(const char *name, int index, int64_t value){
putLongAt(offset,value);
}

bank::rowlist const bank::getRowList(bool const getAllRows) const {
if(getAllRows) {
bank::rowlist all_rows;
for(int row = 0; row < getRows(); row++)
all_rows.push_back(row);
return all_rows;
}
else {
if(!bankIteratorRows.empty() && bankIteratorRows.at(0) == -1)
return getRowList(true); // return full rowlist, if not set
else
return bankIteratorRows;
}
}

bank::rowlist const bank::getRowListLinked(int const row, int const column) const {
bank::rowlist linked_rows;
bank::rowlist::list_t const bank::getRowListLinked(int const row, int const column) const {
rowlist::list_t linked_rows;
for(auto const& r : getRowList()) {
if(getInt(column,r)==row)
linked_rows.push_back(r);
}
return linked_rows;
}

void bank::reduce(std::function<double(hipo::bank&, int)> func, bool doReset){
bank::rowlist indx = getRowList(doReset);
bankIteratorRows.clear();
for(auto const& r : indx)
if(func(*this,r) > 0.5)
bankIteratorRows.push_back(r);
}

void bank::reduce(const char *expression, bool doReset){
hipo::Parser p(expression);
int nitems = getSchema().getEntries();
hipo::schema &schema = getSchema();
bank::rowlist indx = getRowList(doReset);
bankIteratorRows.clear();
for(auto const& r : indx){
for(int i = 0; i < nitems; i++)
p[schema.getEntryName(i)] = get(i,r);
if(p.Evaluate() > 0.5)
bankIteratorRows.push_back(r);
}
bank::rowlist bank::getMutableRowList() {
bankRowList.setBank(this);
return bankRowList;
}

void bank::show(bool showAllRows) const {

bank::rowlist indx = getRowList(showAllRows);
auto indx = showAllRows ? rowlist::copy_number_list(getRows()) : getRowList();
if(showAllRows || indx.size() == getRows())
printf("BANK :: NAME %24s , ROWS %6d\n",bankSchema.getName().c_str(),getRows());
else
Expand Down
55 changes: 37 additions & 18 deletions hipo4/bank.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,12 +207,44 @@ namespace hipo {

class bank : public hipo::structure {

using rowlist = std::vector<int>;
public:

class rowlist {

public:
using list_t = std::vector<int>;

rowlist() : m_list({}), m_init(false) {}
rowlist(int numRows) { initialize(numRows); }

static list_t copy_number_list(list_t::size_type num);

void initialize(int numRows);
void uninitialize();

bool const isInitialized() const { return m_init; }
list_t const& getList() const;

void setList(list_t& list);
void setBank(bank* ownerBank) { m_owner_bank = ownerBank; }

void reduce(std::function<double(hipo::bank&, int)> func);
void reduce(char const* expression);

private:
bool m_init;
list_t m_list;
bank* m_owner_bank;

static list_t generate_number_list(list_t::size_type num = 500);
static list_t s_number_list;

};

private:

hipo::schema bankSchema;
rowlist bankIteratorRows{-1};
rowlist bankRowList;
int bankRows{-1};

public:
Expand Down Expand Up @@ -297,26 +329,13 @@ namespace hipo {
}
}

/// @returns a `rowlist` for this bank, to be used for looping over bank rows
/// @param getAllRows if true, gets **all** the bank rows,
/// otherwise gets the rows that have not been filtered by, _e.g._, `hipo::bank::reduce`
rowlist const getRowList(bool const getAllRows=false) const;
rowlist::list_t const& getRowList() const { return bankRowList.getList(); }
rowlist getMutableRowList();

/// @returns a `rowlist` for this bank, for rows `r` such that `getInt(column,r) == row`
/// @param row the value to check
/// @param column the column to check (must be an integer-type column, _e.g._, that of `"pindex"`)
rowlist const getRowListLinked(int const row, int const column) const;

/// reduces this bank's `rowlist` according to `func`
/// @param func a first order function which will be called on `this` bank for each `int` row in the current `rowlist`;
/// if the function call returns a value greater than `0.5`, the row will be included in the updated `rowlist`
/// @param doReset if true, initializes the `rowlist` so **all** rows are checked, otherwise just the ones in `rowlist` are checked
void reduce(std::function<double(hipo::bank&, int)> func, bool doReset=false);

/// reduces this bank's `rowlist` according to `expression`
/// @param expression an expression to decide whether to include a row in the updated `rowlist`
/// @param doReset if true, initializes the `rowlist` so **all** rows are checked, otherwise just the ones in `rowlist` are checked
void reduce(const char *expression, bool doReset=false);
rowlist::list_t const getRowListLinked(int const row, int const column) const;

/// show this bank's contents; only the rows in its `rowlist` are shown
void show() const override { show(false); }
Expand Down

0 comments on commit 2fe77a5

Please sign in to comment.