Skip to content

Commit

Permalink
fix: make owner bank immutable
Browse files Browse the repository at this point in the history
  • Loading branch information
c-dilks committed Mar 28, 2024
1 parent e2ac91f commit 3403856
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 45 deletions.
50 changes: 23 additions & 27 deletions hipo4/bank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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<double(hipo::bank&, int)> func) {
if(unknownOwnerBank("reduce"))
if(ownerBankIsUnknown("reduce"))
return;
auto indx = m_list;
m_list.clear();
Expand All @@ -384,7 +383,7 @@ void bank::rowlist::reduce(std::function<double(hipo::bank&, int)> 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();
Expand All @@ -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 {
Expand All @@ -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;
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -515,7 +512,6 @@ bank::rowlist::list_t const& bank::getRowList() const {
}

bank::rowlist& bank::getMutableRowList() {
bankRowList.setBank(this);
return bankRowList;
}

Expand Down
30 changes: 12 additions & 18 deletions hipo4/bank.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,27 +215,20 @@ namespace hipo {
using list_t = std::vector<int>;

/// 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
list_t const& getList() const;
/// @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
Expand All @@ -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 = "");

};

Expand All @@ -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;}
Expand Down

0 comments on commit 3403856

Please sign in to comment.