Advertisement
Guest User

MikelSV Core / OList

a guest
Jun 17th, 2015
284
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. // NEW OBJECT MATRIX -- updated to -> Object List
  2.  
  3. // Options: Constructor, Destructor, List
  4. #define OLIST_OPT_CON   1
  5. #define OLIST_OPT_DES   2
  6. #define OLIST_OPT_LIST  4
  7.  
  8. template <class PROCSTRUCTCN, const int options=OLIST_OPT_CON|OLIST_OPT_DES>
  9. class OList : public TLock{
  10.     OMatrixT<OmatrixBlock> data; int datasz;
  11.     OMatrixT<PROCSTRUCTCN> use;//, free;
  12.  
  13.     int maxsz, newsz; // info
  14.     int usesz, allsz;
  15.     unsigned int ltime;
  16.  
  17.     // fast
  18.     OmatrixBlock *fre;
  19.  
  20. public:
  21.     OList(int max=S1M, int nsz=S16K/sizeof(PROCSTRUCTCN)){
  22.         memset(this, 0, sizeof(OList)); maxsz=max; newsz=nsz;
  23.     }
  24.  
  25.     ~OList(){ Clear(); }
  26.  
  27.     void Clear(){
  28.         UGLOCK(this);
  29.         // delete elements
  30.         while(PROCSTRUCTCN *el = Next(0)){
  31.             Del(el);
  32.         }
  33.  
  34.         // delete blocks
  35.         OmatrixBlock*p=data._a, *d;
  36.         while(p){
  37.             d=p; p=p->_n;
  38.             free(d);
  39.         }
  40.         use.OMClearS();
  41.         data.OMClearS();
  42.         usesz=0; allsz=0; datasz=0;
  43.         fre=0;
  44.     }
  45.  
  46.     PROCSTRUCTCN*GetFirst(){
  47.         return use._a;
  48.     }
  49.  
  50.     int size(){ return usesz; }
  51.  
  52.     PROCSTRUCTCN*New(){
  53.         UGLOCK(this);
  54.  
  55.         if(!fre || fre->u==fre->a){
  56.             OmatrixBlock*nw=data._a, *d=0;
  57.             unsigned int s=newsz+1; fre=0;
  58.             for(nw; nw; nw=nw->_n){
  59.                 if(s>nw->a-nw->u && nw->u!=nw->a){ fre=nw; s=nw->a-nw->u; }
  60.                 if(!nw->u) d=nw;
  61.             }
  62.  
  63.             if(d && d!=fre && ltime+30<time()){
  64.                 datasz--; allsz-=d->a;         
  65.                 data.OMDel(d); free(d);        
  66.                 ltime=time();
  67.             }
  68.  
  69.             if(!fre){
  70.                 if(allsz>=maxsz || newsz<1)
  71.                     return 0;
  72.  
  73.                 nw=(OmatrixBlock*)malloc(sizeof(OmatrixBlock)+sizeof(PROCSTRUCTCN)*newsz+((newsz+7)/8));
  74.                 if(!nw)
  75.                     return 0;
  76.  
  77.                 memset(nw, 0, sizeof(OmatrixBlock)+sizeof(PROCSTRUCTCN)*newsz+((newsz+7)/8));
  78.  
  79.                 nw->u=0; nw->a=newsz; nw->sz=sizeof(PROCSTRUCTCN)*newsz;
  80.                 data.OMAdd(nw); datasz++; allsz+=newsz;
  81.                 fre=nw; ltime=time();
  82.             }
  83.         }
  84.  
  85.         // free to lists
  86.         if(!fre){ globalerror("OMATRIX2 !fre"); return 0; }
  87.         int num=fre->getfree(); usesz++; if(num<0 || num>=newsz){ globalerror("OMATRIX2 num>maxsz"); return 0; }
  88.         PROCSTRUCTCN *el=((PROCSTRUCTCN*)(fre+1))+num;
  89.         fre->u++; fre->set(num, 1);
  90.         new(el)PROCSTRUCTCN;
  91.    
  92.         //AddToList<options&OLIST_OPT_LIST>(el);
  93.         //   // list
  94.         //}
  95.  
  96.         return el;
  97.     }
  98.  
  99.     //template <int option> void AddToList(PROCSTRUCTCN *el);
  100.  
  101.     void AddToList(PROCSTRUCTCN *el){}
  102.     //template<> void AddToList<0>(PROCSTRUCTCN *el){}
  103.     //template<> void AddToList<OLIST_OPT_LIST>(PROCSTRUCTCN *el){ use.OMAdd(el); }
  104.  
  105.  
  106.     //template<> void AddToList<0>(){}
  107.     //template <int option> void AddToList() {
  108.     //  AddToList<option> ();
  109.     //}
  110.  
  111.  
  112.     bool Del(PROCSTRUCTCN*el){
  113.         if(!el) return 0; // out lists
  114.         UGLOCK(this);
  115.  
  116.         OmatrixBlock*nw=data._a; int num;
  117.         for(nw; nw; nw=nw->_n){
  118.             if(nw->ismy(el)){ break; }
  119.         } if(!nw) return 0;
  120.  
  121.         num=nw->getn(el); if(!nw->get(num)) globalerror("OMATRIX2 DEL FREE");
  122.         nw->set(num, 0); nw->u--; usesz--;
  123.         el->~PROCSTRUCTCN();
  124.  
  125.         if(nw->u>=nw->a) globalerror("OMATRIX2");
  126.  
  127.         if(options&OLIST_OPT_LIST){
  128.         //  use.OMDel(el); // if list
  129.         }
  130.     return 1;
  131.     }
  132.  
  133.     PROCSTRUCTCN* Next(PROCSTRUCTCN*el){
  134.         UGLOCK(this);
  135.         if(!el){
  136.             if(!data._a) return 0;
  137.             el=((PROCSTRUCTCN*)(data._a+1));
  138.         }
  139.         else
  140.             el++;
  141.  
  142.         OmatrixBlock*nw=data._a; unsigned int num;
  143.         for(nw; nw; nw=nw->_n){
  144.             if(nw->ismy(el)){ break; }
  145.         } if(!nw) return 0;
  146.  
  147.         num=nw->getn(el);
  148.         for(num; num<nw->a; num++){
  149.             if(nw->get(num)){
  150.                 el=((PROCSTRUCTCN*)(nw+1))+num;
  151.                 return el;
  152.             }
  153.         }
  154.  
  155.         if(nw->_n)
  156.             return Next(((PROCSTRUCTCN*)(data._a+1)));
  157.  
  158.         return 0;
  159.     }
  160.  
  161. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement