Skip to content

Commit

Permalink
changed the way iterator operates, now it's part of the bank class an…
Browse files Browse the repository at this point in the history
…d bank has methods to reduce iterator given a function call
  • Loading branch information
gavalian committed Feb 29, 2024
1 parent f40da67 commit 263522f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 25 deletions.
73 changes: 51 additions & 22 deletions hipo4/bank.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void bank::putLong(const char *name, int index, int64_t value){
int offset = bankSchema.getOffset(item, index, bankRows);
putLongAt(offset,value);
}

/*
hipo::iterator iterator::link(hipo::bank &bank, int row, int column){
hipo::iterator blink(bank);
int nrows = bank.getRows();
Expand All @@ -407,33 +407,62 @@ hipo::iterator iterator::link(hipo::bank &bank, int row, int column){
}
return blink;
}

hipo::iterator iterator::reduce(std::function<double(hipo::bank&, int)> func, hipo::bank& bank){
hipo::iterator it(bank);
int nrows = bank.getRows();
for(int r = 0; r < nrows; r++){
double v = func(bank,r);
if(v>0.5) it.add(r);
*/
void bank::reduce(std::function<double(hipo::bank&, int)> func, bool doReset){

if(doReset==true){
bankIterator.reset();
int nrows = getRows();
for(int r = 0; r < nrows; r++){
double v = func(*this,r);
if(v>0.5) bankIterator.add(r);
}
} else {
std::vector<int> indx;
for(bankIterator.begin();!bankIterator.end(); bankIterator.next()){
indx.push_back(bankIterator.index());
}
bankIterator.reset();
int nrows = (int) indx.size();
for(int r = 0; r < nrows; r++){
double v = func(*this,indx[r]);
if(v>0.5) bankIterator.add(indx[r]);
}
}
return it;
//return it;
}

hipo::iterator iterator::reduce(hipo::bank &bank, const char *expression){
void bank::reduce(const char *expression, bool doReset){
hipo::Parser p(expression);

int nrows = bank.getRows();
int nitems = bank.getSchema().getEntries();
hipo::schema &schema = bank.getSchema();
hipo::iterator it(bank);
for(int k = 0; k < nrows; k++){
for(int i = 0; i < nitems; i++){
p[schema.getEntryName(i)] = bank.get(i,k);
}
double value = p.Evaluate();
int nrows = getRows();
int nitems = getSchema().getEntries();
hipo::schema &schema = getSchema();
if(doReset==true){
bankIterator.reset();
for(int k = 0; k < nrows; k++){
for(int i = 0; i < nitems; i++){
p[schema.getEntryName(i)] = get(i,k);
}
double value = p.Evaluate();
//printf(" row = %d - value %f\n",k,value);
if(value>0.5) bankIterator.add(k);
}
} else {
std::vector<int> indx;
for(bankIterator.begin();!bankIterator.end(); bankIterator.next()){
indx.push_back(bankIterator.index());
}
bankIterator.reset();
for(int k = 0; k < (int) indx.size(); k++){
for(int i = 0; i < nitems; i++){
p[schema.getEntryName(i)] = get(i,indx[k]);
}
double value = p.Evaluate();
//printf(" row = %d - value %f\n",k,value);
if(value>0.5) it.add(k);
if(value>0.5) bankIterator.add(indx[k]);
}
}
return it;
//return it;
}

void bank::show(){
Expand Down
29 changes: 26 additions & 3 deletions hipo4/bank.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,31 @@ namespace hipo {
};
//typedef std::auto_ptr<hipo::generic_node> node_pointer;

class iterator {
private:
std::vector<int> rows;
decltype(rows)::size_type current_index;
public:
iterator(){}
virtual ~iterator(){}
void reset(){ rows.clear();}
void add(int index){rows.push_back(index);}
void begin(){current_index=0;}
bool next(){
current_index++; if(current_index>rows.size()) {
current_index = rows.size(); return false;
} return true;
}
bool end(){return current_index>=rows.size();}
int index(){ return rows[current_index];}
void show(){ for(const auto& row : rows) printf("%5d ",row); printf("\n");}
};
class bank : public hipo::structure {

private:

hipo::schema bankSchema;
hipo::schema bankSchema;
hipo::iterator bankIterator;
int bankRows{-1};

protected:
Expand Down Expand Up @@ -234,7 +254,7 @@ namespace hipo {
//void show();

hipo::schema &getSchema() { return bankSchema;}

hipo::iterator &getIterator(){ return bankIterator;}
int getRows() const noexcept{ return bankRows;}
void setRows( int rows);
int getInt( int item, int index) const noexcept;
Expand Down Expand Up @@ -298,7 +318,8 @@ namespace hipo {
printf("---> error(put) : unknown type for [%s] type = %d\n", bankSchema.getEntryName(item).c_str(), type);
}
}

void reduce(std::function<double(hipo::bank&, int)> func, bool doReset);
void reduce(const char *expression, bool doReset);
void show() override;
void reset();
//virtual void notify(){ };
Expand All @@ -307,6 +328,7 @@ namespace hipo {

};

/*
class iterator {
private:
hipo::bank &ib;
Expand Down Expand Up @@ -339,6 +361,7 @@ namespace hipo {
static hipo::iterator reduce(std::function<double(hipo::bank&, int)> func, hipo::bank& bank);
static hipo::iterator reduce(hipo::bank &bank, const char *expression);
};
*/
/////////////////////////////////////
//inlined getters

Expand Down

0 comments on commit 263522f

Please sign in to comment.