diff --git a/hipo4/bank.cpp b/hipo4/bank.cpp index 89680e7..88175bd 100644 --- a/hipo4/bank.cpp +++ b/hipo4/bank.cpp @@ -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(); @@ -407,33 +407,62 @@ hipo::iterator iterator::link(hipo::bank &bank, int row, int column){ } return blink; } - -hipo::iterator iterator::reduce(std::function 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 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 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 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(){ diff --git a/hipo4/bank.h b/hipo4/bank.h index 65ebd4d..7322dc3 100644 --- a/hipo4/bank.h +++ b/hipo4/bank.h @@ -201,11 +201,31 @@ namespace hipo { }; //typedef std::auto_ptr node_pointer; +class iterator { + private: + std::vector 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: @@ -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; @@ -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 func, bool doReset); + void reduce(const char *expression, bool doReset); void show() override; void reset(); //virtual void notify(){ }; @@ -307,6 +328,7 @@ namespace hipo { }; +/* class iterator { private: hipo::bank &ib; @@ -339,6 +361,7 @@ namespace hipo { static hipo::iterator reduce(std::function func, hipo::bank& bank); static hipo::iterator reduce(hipo::bank &bank, const char *expression); }; + */ ///////////////////////////////////// //inlined getters