Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

ODYNARR.H Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ODYNARR.H

Go to the documentation of this file.
00001 //Filename    :: ODYNARR.H
00002 //Description :: Dynamic Array Object
00003 
00004 #ifndef __ODYNARR_H
00005 #define __ODYNARR_H
00006 
00007 #ifndef __ALL_H
00008 #include <ALL.H>
00009 #endif
00010 
00011 #ifndef __STRING_H
00012 #include <string.h>
00013 #endif
00014 
00015 class File;
00016 
00017 //--------- Define constant ------------//
00018 
00019 #define DEF_DYNARRAY_BLOCK_SIZE 30                // default allocation block size (no. of unities each block has)
00020 
00021 //---------- Define sort types --------//
00022 
00023 enum {
00024     SORT_INT=1,
00025     SORT_SHORT,
00026     SORT_CHAR,
00027     SORT_CHAR_PTR,
00028     SORT_CHAR_STR
00029 };
00030 
00031 //-------- BEGIN OF CLASS DynArrary ---------//
00032 
00034 class DynArray {
00035 public :
00036 
00037     int  ele_num;                                 // the size of the whole array
00038     int  block_num;                               // the number of element of each block
00039     int  cur_pos;                                 // current position
00040     int  last_ele;                                // last element position ( the array is not fully used )
00041     int  ele_size;                                // the size of each element
00042 
00043     int  sort_offset;
00044     char sort_type;
00045 
00046     char* body_buf;                               // cur_pos and last_ele are start from 1 (not 0)
00047 
00048     //----------------------------------------------//
00049 
00050 public :
00051 
00052     DynArray(int,int=DEF_DYNARRAY_BLOCK_SIZE);
00053     ~DynArray();
00054 
00055     void  deinit();
00056     void  resize(int);
00057 
00058     void  linkin(void*);
00059     void  linkin_unique(void*);
00060     void  linkout(int= -1);
00061     void  update(void*, int= -1);
00062     void  insert(void*);
00063     void  insert_at(int,void*);
00064     void  add_blank(int);
00065 
00066     void  init_sort(int,char);
00067     void  linkin_sort_scan_from_bottom(void*);
00068     // void  resort(int);
00069 
00070     void* get();
00071     void* get(int);
00072     void* get_ptr();
00073     void* get_ptr(int);
00074     void  read(void*);
00075 
00076     int   check_pos();
00077 
00078     void  push(void*);
00079     void  pop(void* =0);
00080 
00081     void  start();
00082     void  end();
00083     int   fwd();
00084     int   bkwd();
00085 
00086     void  jump(int);
00087     void  go(int);
00088     int   recno();
00089     int   size();
00090 
00091     int   is_start();
00092     int   is_end();
00093 
00094     int   scan_whole(void*);
00095     int   scan(void*,int,char,int=0);
00096     int   compare(void*,int,char);
00097 
00098     void  quick_sort( int(*cmpFun)(const void*, const void*) );
00099     void  bubble_sort( int(*cmpFun)(const void*, const void*) );
00100 
00101     void  clean_up(int* =0);
00102     void  free_ptr(void*,int*);
00103     void  zap(int resizeFlag=1);
00104 
00105     int   write_file(File*);                      // Write current dynamic array to file
00106     int   read_file(File*);                       // Read dynamic array from file
00107 };
00108 
00109 //--------- END OF CLASS DynArray ---------//
00110 
00111 //--------- BEGIN OF FUNCTION DynArray::get -----------//
00112 //
00113 // Return : the memory pointer to the body_buf of current element
00114 //          NULL if the record no. is invalid.
00115 //
00116 inline void* DynArray::get() {
00117     if( cur_pos == 0 )
00118         return NULL;
00119 
00120     return (void*) (body_buf+(cur_pos-1)*ele_size);
00121 }
00122 
00123 inline void* DynArray::get(int specRec) {
00124     if( specRec<1 || specRec>last_ele )
00125         return NULL;
00126 
00127     return (void*) (body_buf+(specRec-1)*ele_size);
00128 }
00129 
00130 //--------- END OF FUNCTION DynArray::get -----------//
00131 
00132 //--------- BEGIN OF FUNCTION DynArray::get_ptr -----------//
00133 //
00134 // The content of the entry is a pointer, return the content
00135 // is a pointer
00136 //
00137 // Return : the pointer
00138 //          NULL if the record no. is invalid.
00139 
00140 inline void* DynArray::get_ptr() {
00141     if( cur_pos == 0 )
00142         return NULL;
00143 
00144     return (void*) *((char**)(body_buf+(cur_pos-1)*ele_size));
00145 }
00146 
00147 inline void* DynArray::get_ptr(int specRec) {
00148     if( specRec < 1 || specRec > last_ele )
00149         return NULL;
00150 
00151     return (void*) *((char**)(body_buf+(specRec-1)*ele_size));
00152 }
00153 
00154 //--------- END OF FUNCTION DynArray::get_ptr -----------//
00155 
00156 //--------- BEGIN OF FUNCTION DynArray::read -----------//
00157 //
00158 // Read current record into the given buffer
00159 //
00160 inline void DynArray::read(void* ent) {
00161     if( ent )
00162         memcpy(ent, get(), ele_size );
00163 }
00164 
00165 //---------- END OF FUNCTION DynArray::read -----------//
00166 
00167 //--------- BEGIN OF FUNCTION DynArray::push,pop -----------//
00168 
00169 // <void*> ent = the address of the entity to be linked into the array
00170 
00171 inline void DynArray::push(void* ent) {
00172     linkin(ent);
00173 }
00174 
00175 // [void*] ent = the address of the entity to be overwritten by current element
00176 
00177 inline void DynArray::pop(void* ent) {
00178     end();
00179     read(ent);
00180     linkout();
00181 }
00182 
00183 //----------- END OF FUNCTION DynArray::push,pop ----------//
00184 
00185 //-------- BEGIN OF FUNCTIONO DynArray::start,end,fwd,bkwd -------------//
00186 //
00187 inline void DynArray::start() {
00188     cur_pos = min(1,last_ele);
00189 }
00190 
00191 inline void DynArray::end() {
00192     cur_pos = last_ele;
00193 }
00194 
00195 inline int DynArray::fwd() {
00196     if (cur_pos < last_ele ) {
00197         cur_pos++;
00198         return 1;
00199     }
00200     else
00201         return 0;
00202 }
00203 
00204 inline int DynArray::bkwd() {
00205     if (cur_pos > 1) {
00206         cur_pos--;
00207         return 1;
00208     }
00209     else
00210         return 0;
00211 }
00212 
00213 //---------- END OF FUNCTION DynArray::start,end,fwd,bkwd ---------//
00214 
00215 //--------- BEGIN OF FUNCTION DynArray::jump,go,pos,size ----------//
00216 
00217 inline void DynArray::jump(int step) {
00218     cur_pos+=step;
00219 
00220     if ( cur_pos < 0 )
00221         cur_pos = min(1,last_ele) ;
00222 
00223     if ( cur_pos > last_ele )
00224         cur_pos = last_ele;
00225 }
00226 
00227 inline void DynArray::go(int desPos) {
00228     if ( desPos >= 1 && desPos <= last_ele )
00229         cur_pos = desPos;
00230 }
00231 
00232 inline int DynArray::recno() {
00233     return cur_pos;
00234 }
00235 
00236 inline int DynArray::size() {
00237     return last_ele;
00238 }
00239 
00240 //----------- END OF FUNCTION DynArray::jump,go,pos,size ---------//
00241 
00242 //-------- BEGIN OF FUNCTION DynArray::isstart,isend ------//
00243 
00244 inline int DynArray::is_start() {
00245     return( cur_pos <= 1 );
00246 }
00247 
00248 inline int DynArray::is_end() {
00249     return( cur_pos >= last_ele );
00250 }
00251 
00252 //-------- END OF FUNCTION DynArray::isstart,isend --------//
00253 #endif

Generated on Fri Aug 23 01:37:26 2002 for VirtualU by doxygen1.2.17