Changeset 153 for cpp/frams/util/list.h


Ignore:
Timestamp:
03/01/14 22:20:08 (8 years ago)
Author:
sz
Message:

SList container iteration: FOREACH() macro now creates local variable for maintaining the loop, removed the obsolete SList iteration implementation, replaced some unnecessarily complex for() with FOREACH()

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/util/list.h

    r121 r153  
    8787        {return mem[i];}
    8888bool hasMore() {return pos<size();}
    89 T& operator()() ///< return next element
    90         { return mem[pos++];}
    9189int operator==(const SListTempl<T>& l) const ///< compare list
    9290 {
     
    9795int find(const T& e) const ///< return position of element, or -1 if not found
    9896        { for (int i=0;i<size();i++) if (mem[i]==e) return i;   return -1; }
    99 int start() {return pos=0;} ///< start iteration throgh list
    10097void append(const T& data) ///< add 1 element
    10198        {needSize(size()+1); mem[used++]=data;}
     
    128125        { needSize(s); used=s;}
    129126T& get(int i) const {return operator()(i);}
    130 T& get() {return operator()();}
    131127void clear() {used=0;} ///< remove all elements
    132128void hardclear() {resize(0); used=0;} ///< remove all elements and free mem
     
    148144};
    149145
    150 #define FOREACH(type,var,list) for(type var=(type)list.start();var=(type)list();)
     146#define FOREACH(type,var,list) for(int _ ## var ## _i_=0,_ ## var ## _tmp_=0; _ ## var ## _i_==0;_ ## var ## _i_++) \
     147                for(type var;((_ ## var ## _tmp_=(_ ## var ## _i_<list.size()))?(var=(type)list(_ ## var ## _i_)):0),_ ## var ## _tmp_;_ ## var ## _i_++)
    151148
    152149// usage example: FOREACH(char*,t,thelist) printf("t=%s\n",t);
     
    155152{
    156153   public:
    157 T operator()() ///< return next element
    158      {if (this->pos>=this->size()) return 0; else return
    159 this->mem[this->pos++];}
    160 
    161154T operator()(int i) const ///< return element at position i
    162155     {if (i>=this->size()) return 0; else return this->mem[i];}
    163156
    164157T get(int i) const {return operator()(i);}
    165 T get() {return operator()();}
    166158T& getref(int i) const {return SListTempl<T>::get(i);}
    167 T& getref() {return SListTempl<T>::get();}
    168159};
    169160
Note: See TracChangeset for help on using the changeset viewer.