From 3403856d075a2b2738d149b1751afa2d6b287944 Mon Sep 17 00:00:00 2001 From: Christopher Dilks Date: Thu, 28 Mar 2024 12:03:56 -0400 Subject: [PATCH] fix: make owner bank immutable --- hipo4/bank.cpp | 50 +++++++++++++++++++++++--------------------------- hipo4/bank.h | 30 ++++++++++++------------------ 2 files changed, 35 insertions(+), 45 deletions(-) diff --git a/hipo4/bank.cpp b/hipo4/bank.cpp index f84b996..212bf9b 100644 --- a/hipo4/bank.cpp +++ b/hipo4/bank.cpp @@ -338,20 +338,23 @@ void composite::print(){ // hipo::bank::rowlist ////////////////////////////////////////////////////////////////////////////////// -bank::rowlist::rowlist(int numRows) : m_list({}), m_init(false) { - if(numRows >= 0) - initialize(numRows); -} +bank::rowlist::rowlist(bank* const ownerBank) + : m_list({}) + , m_init(false) + , m_ownerBank(ownerBank) +{} -void bank::rowlist::initialize(int numRows) { - uninitialize(); - m_list = copy_number_list(numRows); - m_init = true; -} - -void bank::rowlist::uninitialize() { +void bank::rowlist::reset(int numRows) { m_list.clear(); m_init = false; + if(numRows >= 0) + m_list = copy_number_list(numRows); + else { + if(ownerBankIsUnknown("reset")) + return; + m_list = copy_number_list(m_owner_bank->getRows()); + } + m_init = true; } bool const bank::rowlist::isInitialized() const { @@ -369,12 +372,8 @@ void bank::rowlist::setList(list_t list) { m_init = true; } -void bank::rowlist::setBank(bank* ownerBank) { - m_owner_bank = ownerBank; -} - void bank::rowlist::reduce(std::function func) { - if(unknownOwnerBank("reduce")) + if(ownerBankIsUnknown("reduce")) return; auto indx = m_list; m_list.clear(); @@ -384,7 +383,7 @@ void bank::rowlist::reduce(std::function func) { } void bank::rowlist::reduce(const char *expression) { - if(unknownOwnerBank("reduce")) + if(ownerBankIsUnknown("reduce")) return; hipo::Parser p(expression); int nitems = m_owner_bank->getSchema().getEntries(); @@ -409,6 +408,8 @@ bank::rowlist::list_t bank::rowlist::generate_number_list(list_t::size_type num) bank::rowlist::list_t bank::rowlist::s_number_list = bank::rowlist::generate_number_list(); bank::rowlist::list_t bank::rowlist::copy_number_list(list_t::size_type num) { + if(num < 0) + return {} if(num <= s_number_list.size()) return list_t(s_number_list.begin(), s_number_list.begin() + num); else { @@ -419,12 +420,12 @@ bank::rowlist::list_t bank::rowlist::copy_number_list(list_t::size_type num) { } } -bool bank::rowlist::unknownOwnerBank(std::string_view caller) { +bool bank::rowlist::ownerBankIsUnknown(std::string_view caller) { if(m_owner_bank == nullptr) { std::cerr << "ERROR: attempted to call hipo::bank::rowlist " << (caller=="" ? "method" : caller) << - ", but no bank is owned (please call setBank())" << + ", but no bank is associated to this rowlist" << std::endl; return true; } @@ -436,28 +437,24 @@ bool bank::rowlist::unknownOwnerBank(std::string_view caller) { // hipo::bank ////////////////////////////////////////////////////////////////////////////////// -bank::bank()= default; - -bank::~bank()= default; - void bank::setRows(int rows){ bankRows = rows; int size = bankSchema.getSizeForRows(bankRows); initStructureBySize(bankSchema.getGroup(),bankSchema.getItem(), 11, size); - bankRowList.initialize(bankRows); + bankRowList.reset(); //allocate(size+12); } void bank::reset(){ setSize(0); bankRows = 0; - bankRowList.initialize(bankRows); + bankRowList.reset(); } void bank::notify(){ int size = bankSchema.getRowLength(); bankRows = getSize()/size; - bankRowList.initialize(bankRows); + bankRowList.reset(); //printf("---> bank notify called structure size = %8d (size = %5d) rows = %d\n", // getSize(),size, bankRows); } @@ -515,7 +512,6 @@ bank::rowlist::list_t const& bank::getRowList() const { } bank::rowlist& bank::getMutableRowList() { - bankRowList.setBank(this); return bankRowList; } diff --git a/hipo4/bank.h b/hipo4/bank.h index 0d5d416..2ddf93b 100644 --- a/hipo4/bank.h +++ b/hipo4/bank.h @@ -215,16 +215,13 @@ namespace hipo { using list_t = std::vector; /// constructor - /// @param numRows if non-negative, initialize with `numRows` rows, otherwise do not initialize and start with an empty list - rowlist(int numRows = -1); + /// @param ownerBank if set, associate this `rowlist` with bank `ownerBank` + rowlist(bank* const ownerBank = nullptr); ~rowlist() {} /// initialize with a full list with specified number of rows - /// @param numRows the number of rows - void initialize(int numRows); - /// clear the stored list - void uninitialize(); - /// @returns true if the list has been initialized + /// @param numRows if negative, use the owner `bank` to set the number of rows, otherwise use `numRows` + void reset(int numRows = -1); bool const isInitialized() const; /// @returns reference to the immutable list @@ -232,10 +229,6 @@ namespace hipo { /// @param list set the list to this list void setList(list_t list); - /// @param ownerBank set the pointer to the owner `hipo::bank`; this is required for mutation methods which need - /// information from the owner bank, such as `hipo::bank::rowlist::reduce` - void setBank(bank* ownerBank); - /// 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 @@ -247,12 +240,13 @@ namespace hipo { private: bool m_init; list_t m_list; - bank* m_owner_bank; + bank* const m_owner_bank; static list_t generate_number_list(list_t::size_type num = 500); static list_t copy_number_list(list_t::size_type num); static list_t s_number_list; - bool unknownOwnerBank(std::string_view caller = ""); + + bool ownerBankIsUnknown(std::string_view caller = ""); }; @@ -264,19 +258,19 @@ namespace hipo { public: - bank(); + bank() : bankRowList(rowlist(this)) = default; // constructor initializes the nodes in the bank // and they will be filled automatically by reader.next() // method. - bank(const hipo::schema& __schema) : bankSchema(__schema) {} + bank(const hipo::schema& __schema) : bankSchema(__schema), bankRowList(rowlist(this)) {} - bank(const hipo::schema& __schema, int __rows) : bankSchema(__schema), bankRows(__rows) { + bank(const hipo::schema& __schema, int __rows) : bankSchema(__schema), bankRows(__rows), bankRowList(rowlist(this)) { int size = bankSchema.getSizeForRows(bankRows); initStructureBySize(bankSchema.getGroup(),bankSchema.getItem(), 11, size); - bankRowList.initialize(bankRows); + bankRowList.reset(); } - ~bank() override; + ~bank() = default; hipo::schema &getSchema() { return bankSchema;} int getRows() const noexcept{ return bankRows;}