AMROC Main     Blockstructured Adaptive Mesh Refinement in object-oriented C++


Main Page   Class Hierarchy   Compound List   File List  

SimpleBucketVoid.h

Go to the documentation of this file.
00001 #ifndef _included_SimpleBucketVoid_h
00002 #define _included_SimpleBucketVoid_h
00003 
00009 #include <stdlib.h>
00010 #include <assert.h>
00011 #include <memory.h>
00012 #include <string.h>
00013 #include <iostream.h>
00014 
00015 #ifdef DEBUG_PRINT_BKT_MEMORY
00016 #include "DAGHMemoryTrace.h"
00017 #endif
00018 
00019 #include "DAGHDefaults.h"
00020 
00021 #ifndef DefBucketSize
00022 #define DefBucketSize (256)
00023 #endif
00024 
00025 #ifndef DefIncrement
00026 #define DefIncrement (128)
00027 #endif
00028 
00029 #define NullRec ((unsigned) (0))
00030 #define EmptyBkt ((unsigned) (0))
00031 #define FullBkt ((unsigned) (-1))
00032 
00033 typedef unsigned long pointer;
00034 
00035 struct rechdr 
00036   {
00037    unsigned mysize; unsigned myoff;
00038    unsigned noff; unsigned poff;
00039    pointer head;
00040   };
00041 
00042 struct reclink 
00043   {
00044    unsigned mysize; unsigned myoff;
00045    unsigned noff; unsigned poff;
00046    pointer head;
00047    unsigned next;
00048   };
00049 
00050 struct recdata 
00051   {
00052    unsigned mysize; unsigned myoff;
00053    unsigned noff; unsigned poff;
00054    pointer head;
00055    unsigned data[2];
00056   };
00057 
00058 struct bkthdr 
00059   {
00060    unsigned blksize; unsigned nblks;
00061    unsigned headoff; unsigned tailoff;
00062    unsigned freeoff; unsigned cnt;
00063   };
00064 
00065 union record 
00066   {
00067    struct bkthdr bhdr;
00068    struct rechdr hdr;
00069    struct reclink link;
00070    struct recdata rec;
00071    unsigned data[1];
00072  
00073    inline int isempty(void) { return (hdr.mysize == 0); }
00074    friend ostream& operator<<(ostream&, union record&);
00075   };
00076    
00077 struct bkt
00078   {
00079    union record *head;
00080 
00081    inline bkt(void) : head(0) {}
00082    bkt(unsigned const blksize, unsigned const nblks);
00083    bkt(struct bkt const &other);
00084    bkt(union record const *hdr);
00085    bkt(union record *hdr);
00086    inline ~bkt(void) 
00087      { 
00088 
00089 #ifdef DEBUG_PRINT_BKT_MEMORY
00090       DAGHMemoryTrace::free(sizeof(char)*head->bhdr.blksize*head->bhdr.nblks);
00091 #endif
00092 
00093       delete [] ((char *)head); 
00094      }
00095 
00096    friend ostream& operator<<(ostream&, struct bkt&);
00097   };
00098 
00099 #define recordNULL ((union record *) 0)
00100 #define bkthdrNULL ((struct bkthdr *) 0)
00101 #define bktNULL ((struct bkt *) 0)
00102 #define SimpleBucketVoidNULL ((class SimpleBucketVoid *) 0)
00103 
00110 class SimpleBucketVoid 
00111   {
00112    friend ostream& operator<<(ostream&, SimpleBucketVoid&);
00113 
00114    unsigned blksize;
00115    unsigned nblks;
00116 
00117    struct bkt *bktptr;
00118    unsigned freeoff;
00119 
00120    void operator = (const SimpleBucketVoid&);
00121 
00122 public:
00123    inline SimpleBucketVoid(void)
00124         : blksize(0), nblks(0), bktptr(0), freeoff(0) {}
00125 
00126    SimpleBucketVoid(unsigned const bktsize, unsigned const bktnum);
00127    SimpleBucketVoid(union record *hdr);
00128    SimpleBucketVoid(union record const *hdr);
00129    SimpleBucketVoid(union record const *hdr, unsigned const size, int const n);
00130    SimpleBucketVoid(const SimpleBucketVoid&);   
00131 
00132    inline ~SimpleBucketVoid(void) { delete bktptr; }
00133 
00134 private:
00135    void allocbkt(int const incr = DefIncrement);
00136    
00137    inline union record *copydata(union record *to, union record const *from)
00138         {
00139           memcpy((void *)(((char *)to)+sizeof(struct rechdr)),
00140                  (void *)(((char const *)from)+sizeof(struct rechdr)),
00141                  blksize-sizeof(struct rechdr));
00142           //for (register int i=sizeof(struct rechdr);i<blksize;i++)
00143           //  ((char *) to)[i] = ((char *) from)[i];
00144           return (to);
00145         }
00146 
00147    /* Operation on the bkt header */
00148 
00149    inline union record *bktGetHeadRec(union record const *bhead) const
00150         { return ((union record *)
00151                 ((char const *)(bhead)+(blksize*bhead->bhdr.headoff))); }
00152 
00153    inline union record *bktGetTailRec(union record const *bhead) const
00154         { return ((union record *)
00155                 ((char const *)(bhead)+(blksize*bhead->bhdr.tailoff))); }
00156 
00157    inline union record *bktGetFreeRec(union record const *bhead) const
00158         { return ((union record *)
00159                 ((char const *)(bhead)+(blksize*bhead->bhdr.freeoff))); }
00160 
00161    inline union record *bktGetAtRec(union record const *bhead,
00162                 unsigned const off) const
00163         { return ((union record *)
00164                 ((char const *)(bhead)+(blksize*off))); }
00165   
00166    inline unsigned bktGetheadoff(union record const *bhead) const
00167         { return (bhead->bhdr.headoff); }
00168    inline void bktSetheadoff(union record *bhead, unsigned const off) 
00169         { bhead->bhdr.headoff = off; }
00170 
00171    inline unsigned bktGettailoff(union record const *bhead) const
00172         { return (bhead->bhdr.tailoff); }
00173    inline void bktSettailoff(union record *bhead, unsigned const off) 
00174         { bhead->bhdr.tailoff = off; }
00175 
00176    inline unsigned bktGetfreeoff(union record const *bhead) const
00177         { return (bhead->bhdr.freeoff); }
00178    inline void bktSetfreeoff(union record *bhead, unsigned const off) 
00179         { bhead->bhdr.freeoff = off; }
00180 
00181    inline unsigned bktGetcnt(union record const *bhead) const
00182         { return (bhead->bhdr.cnt); }
00183    inline void bktSetcnt(union record *bhead, unsigned const cnt) 
00184         { bhead->bhdr.cnt = cnt; }
00185    inline void bktIncrcnt(union record *bhead)
00186         { bhead->bhdr.cnt++; }
00187    inline void bktDecrcnt(union record *bhead)
00188         { bhead->bhdr.cnt--; }
00189 
00190    /* Operations on a record */
00191   
00192    inline union record *recPrevRec(union record const *rec) const
00193         {
00194          return ( (rec->hdr.poff != NullRec) ?
00195          (union record *) ((char *)(rec->hdr.head)+(blksize*rec->hdr.poff)) :
00196          (union record *) (0) );
00197         }
00198 
00199    inline union record *recNextRec(union record const *rec) const
00200         {
00201          return ( (rec->hdr.noff != NullRec) ?
00202          (union record *) ((char *)(rec->hdr.head)+(blksize*rec->hdr.noff)) :
00203          (union record *) (0) );
00204         }
00205   
00206    inline union record *recAtRec(union record const *rec,
00207                 unsigned const off) const
00208         {
00209          return ( (off != NullRec) ?
00210          (union record *) ((char *)(rec->hdr.head)+(blksize*off)) :
00211          (union record *) (0) );
00212         }
00213 
00214    union record *bktaddrec(union record *bhead);
00215    inline union record *bktaddrec(union record *bhead, union record const *rec)
00216         {
00217          //union record *tmprec = bktaddrec(bhead);
00218          //copydata(tmprec,rec);
00219          //return (tmprec);
00220          return ( copydata(bktaddrec(bhead),rec) );
00221         }
00222 
00223    void bktaddfree(union record *bhead, union record *rec);
00224 
00225 protected:
00226    inline int blks(void) { return (nblks-1);}
00227    inline int bsize(void) { return (blksize);}
00228 
00229    union record *addrec(void);
00230    inline union record *addrec(union record const *rec)
00231         {
00232          //union record *tmprec = addrec();
00233          //copydata(tmprec,rec);
00234          //return (tmprec);
00235          return ( copydata(addrec(),rec) );
00236         }
00237 
00238    union record *insertrec(union record *after);
00239    inline union record *insertrec(union record const *rec, union record *after)
00240         {
00241          //union record *tmprec = insertrec(after);
00242          //copydata(tmprec,rec);
00243          //return (tmprec);
00244          return ( copydata(insertrec(after),rec) );
00245         }
00246 
00247    void removerec(union record *rec);
00248 
00249    /* Walk the bucket */
00250    inline union record *nextrec(union record const *rec) const
00251         { 
00252          return ( 
00253          (rec->hdr.myoff != bktGettailoff((union record *)rec->hdr.head)) ?
00254          (union record *) ((char *)(rec->hdr.head)+(blksize*rec->hdr.noff)) :
00255          (union record *) (0) );
00256         } 
00257 
00258    inline union record *prevrec(union record const *rec) const
00259         { 
00260          return (
00261          (rec->hdr.myoff != bktGetheadoff((union record *)rec->hdr.head)) ?
00262          (union record *) ((char *)(rec->hdr.head)+(blksize*rec->hdr.poff)) :
00263          (union record *) (0) );
00264         } 
00265 
00266    inline union record *headrec(void) const
00267         {
00268          return ( (bktptr->head->bhdr.cnt > 0) ?
00269          bktGetHeadRec(bktptr->head) : recordNULL );
00270         } 
00271 
00272    inline union record *tailrec(void) const
00273         {
00274          return ( (bktptr->head->bhdr.cnt > 0) ?
00275          bktGetTailRec(bktptr->head) : recordNULL );
00276         } 
00277 
00278    inline unsigned count(void) const
00279         { return (bktGetcnt(bktptr->head)); }
00280 
00281 public:
00282    /* Utility routines */
00283    inline int isemptybkt(void) { return (bktptr->head->bhdr.cnt == 0); }
00284    void emptybkt(void);
00285 
00286    void splitbkt(SimpleBucketVoid &sbv, union record *atrec);
00287 
00288    void *pack(unsigned &size);
00289    void pack(void *&package, unsigned &size);
00290    void *pack(int &size);
00291    void pack(void *&package, int &size);
00292   };
00293 
00294 #endif


Quickstart     Users Guide     Programmers Reference     Installation      Examples     Download



AMROC Main      Home      Contact
last update: 06/01/04