00001
00002
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
00018
00019 #define DEF_DYNARRAY_BLOCK_SIZE 30 // default allocation block size (no. of unities each block has)
00020
00021
00022
00023 enum {
00024 SORT_INT=1,
00025 SORT_SHORT,
00026 SORT_CHAR,
00027 SORT_CHAR_PTR,
00028 SORT_CHAR_STR
00029 };
00030
00031
00032
00034 class DynArray {
00035 public :
00036
00037 int ele_num;
00038 int block_num;
00039 int cur_pos;
00040 int last_ele;
00041 int ele_size;
00042
00043 int sort_offset;
00044 char sort_type;
00045
00046 char* body_buf;
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
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*);
00106 int read_file(File*);
00107 };
00108
00109
00110
00111
00112
00113
00114
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
00131
00132
00133
00134
00135
00136
00137
00138
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
00155
00156
00157
00158
00159
00160 inline void DynArray::read(void* ent) {
00161 if( ent )
00162 memcpy(ent, get(), ele_size );
00163 }
00164
00165
00166
00167
00168
00169
00170
00171 inline void DynArray::push(void* ent) {
00172 linkin(ent);
00173 }
00174
00175
00176
00177 inline void DynArray::pop(void* ent) {
00178 end();
00179 read(ent);
00180 linkout();
00181 }
00182
00183
00184
00185
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
00214
00215
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
00241
00242
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
00253 #endif