From 2fe77a59a942b90c8282945a0dfeb9bdd71e651e Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Wed, 27 Mar 2024 11:07:51 -0400 Subject: [PATCH] refactor: restore some modularity --- hipo4/bank.cpp | 126 +++++++++++++++++++++++++++++++++---------------- hipo4/bank.h | 55 ++++++++++++++------- 2 files changed, 122 insertions(+), 59 deletions(-) diff --git a/hipo4/bank.cpp b/hipo4/bank.cpp index 894c681..9da639c 100644 --- a/hipo4/bank.cpp +++ b/hipo4/bank.cpp @@ -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 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; @@ -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(){ @@ -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); @@ -400,23 +476,8 @@ 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); @@ -424,31 +485,14 @@ bank::rowlist const bank::getRowListLinked(int const row, int const column) cons return linked_rows; } -void bank::reduce(std::function 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 diff --git a/hipo4/bank.h b/hipo4/bank.h index 803a46e..0bc209a 100644 --- a/hipo4/bank.h +++ b/hipo4/bank.h @@ -207,12 +207,44 @@ namespace hipo { class bank : public hipo::structure { - using rowlist = std::vector; + public: + + class rowlist { + + public: + using list_t = std::vector; + + 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 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: @@ -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 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); }