00001
00002
00003
00004 #include <OINFO.H>
00005 #include <ODYNARRB.H>
00006 #include <OFILE.H>
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #define EMPTY_ROOM_ALLOC_STEP 5
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 DynArrayB::DynArrayB(int eleSize,int blockNum,int reuseIntervalDays) : DynArray(eleSize, blockNum) {
00031 empty_room_array = NULL;
00032 empty_room_num = 0;
00033 empty_room_count = 0;
00034
00035 reuse_interval_days = reuseIntervalDays;
00036 }
00037
00038
00039
00040
00041
00042 DynArrayB::~DynArrayB() {
00043 if( empty_room_array )
00044 mem_del( empty_room_array );
00045 }
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064 void DynArrayB::linkin(void* ent) {
00065
00066
00067 int reusedFlag=0;
00068
00069 if( empty_room_count > 0 ) {
00070 if( reuse_interval_days ) {
00071
00072
00073 if( info.game_date >= empty_room_array[0].deleted_game_date + reuse_interval_days ) {
00074 cur_pos = empty_room_array[0].recno;
00075
00076 memmove( empty_room_array, empty_room_array+1, sizeof(empty_room_array[0]) * (empty_room_count-1) );
00077
00078 empty_room_count--;
00079 reusedFlag = 1;
00080 }
00081 }
00082 else {
00083
00084
00085 cur_pos = empty_room_array[empty_room_count-1].recno;
00086
00087 empty_room_count--;
00088 reusedFlag = 1;
00089 }
00090 }
00091
00092 if( !reusedFlag ) {
00093 last_ele++;
00094 cur_pos=last_ele;
00095 }
00096
00097
00098
00099 if ( last_ele > ele_num )
00100 resize( ele_num + block_num );
00101
00102 if ( ent )
00103 memcpy(body_buf+(cur_pos-1)*ele_size, ent, ele_size );
00104 else
00105 *(body_buf+(cur_pos-1)*ele_size) = NULL;
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120 void DynArrayB::linkout(int delPos) {
00121 if( delPos < 0 )
00122 delPos = cur_pos;
00123
00124 if( delPos == 0 || delPos > last_ele )
00125 return;
00126
00127
00128
00129 if( ++empty_room_count > empty_room_num ) {
00130 empty_room_array = (EmptyRoom*) mem_resize( empty_room_array,
00131 (empty_room_num+EMPTY_ROOM_ALLOC_STEP) * sizeof(*empty_room_array) );
00132
00133 empty_room_num += EMPTY_ROOM_ALLOC_STEP;
00134 }
00135
00136 empty_room_array[empty_room_count-1].recno = delPos;
00137 empty_room_array[empty_room_count-1].deleted_game_date = info.game_date;
00138
00139 memset( body_buf+(delPos-1)*ele_size, 0, ele_size );
00140 }
00141
00142
00143
00144
00154
00155 if( !filePtr->file_write( this, sizeof(DynArray) ) )
00156 return 0;
00157
00158
00159
00160 if( last_ele > 0 ) {
00161 if( !filePtr->file_write( body_buf, ele_size*last_ele ) )
00162 return 0;
00163 }
00164
00165
00166
00167 write_empty_room(filePtr);
00168
00169 return 1;
00170 }
00171
00172
00173
00174
00183
00184 char* bodyBuf = body_buf;
00185
00186 if( !filePtr->file_read( this, sizeof(DynArray) ) )
00187 return 0;
00188
00189
00190
00191 body_buf = mem_resize( bodyBuf, ele_size*ele_num );
00192
00193 if( last_ele > 0 ) {
00194 if( !filePtr->file_read( body_buf, ele_size*last_ele ) )
00195 return 0;
00196 }
00197
00198
00199
00200 read_empty_room(filePtr);
00201
00202
00203
00204 start();
00205
00206 return 1;
00207 }
00208
00209
00210
00211
00221
00222 filePtr->file_put_short( empty_room_count );
00223
00224
00225
00226 if( empty_room_count > 0 ) {
00227 if( !filePtr->file_write( empty_room_array,
00228 sizeof(EmptyRoom) * empty_room_count ) ) {
00229 return 0;
00230 }
00231 }
00232
00233 return 1;
00234 }
00235
00236
00237
00238
00247
00248
00249 empty_room_num = empty_room_count = filePtr->file_get_short();
00250
00251
00252
00253 if( empty_room_count > 0 ) {
00254 empty_room_array = (EmptyRoom*) mem_resize( empty_room_array,
00255 sizeof(EmptyRoom) * empty_room_count );
00256
00257 if( !filePtr->file_read( empty_room_array,
00258 sizeof(*empty_room_array) * empty_room_count ) ) {
00259 return 0;
00260 }
00261 }
00262 else {
00263 if( empty_room_array ) {
00264 mem_del( empty_room_array );
00265 empty_room_array = NULL;
00266 }
00267 }
00268
00269
00270
00271 return 1;
00272 }
00273
00274
00275
00276
00287
00288 int i, packedRecno = recNo;
00289
00290 for( i=0 ; i<empty_room_count ; i++ ) {
00291 if( empty_room_array[i].recno < recNo )
00292 packedRecno--;
00293 }
00294
00295 return packedRecno;
00296 }
00297
00298
00299
00300
00304
00305 DynArray::zap();
00306
00307 empty_room_count=0;
00308 }
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319 int DynArrayB::write_ptr_array(File* filePtr, int objectSize) {
00320 int i;
00321 char* elePtr;
00322
00323 filePtr->file_put_short( size() );
00324
00325 for( i=1; i<=size() ; i++ ) {
00326 elePtr = (char*) get_ptr(i);
00327
00328
00329
00330 if( !elePtr ) {
00331 filePtr->file_put_short(0);
00332 }
00333 else {
00334 filePtr->file_put_short(1);
00335
00336 if( !filePtr->file_write(elePtr, objectSize) )
00337 return 0;
00338 }
00339 }
00340
00341
00342
00343 write_empty_room(filePtr);
00344
00345 return 1;
00346 }
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359 int DynArrayB::read_ptr_array(File* filePtr, int objectSize, CreateEleFP createEleFunc) {
00360 int i;
00361 char* elePtr;
00362
00363 int eleCount = filePtr->file_get_short();
00364
00365 for( i=1 ; i<=eleCount ; i++ ) {
00366 if( filePtr->file_get_short()==0 ) {
00367 add_blank(1);
00368 }
00369 else {
00370 elePtr = (*createEleFunc)();
00371
00372 if( !filePtr->file_read(elePtr, objectSize) )
00373 return 0;
00374 }
00375 }
00376
00377
00378
00379
00380
00381 for( i=size() ; i>0 ; i-- ) {
00382 DynArrayB::go(i);
00383
00384 if( get_ptr() == NULL )
00385 linkout();
00386 }
00387
00388
00389
00390 read_empty_room(filePtr);
00391
00392 return 1;
00393 }
00394
00395