123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- /*
- * Seven Kingdoms: Ancient Adversaries
- *
- * Copyright 1997,1998 Enlight Software Ltd.
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- //Filename :: ODYNARR.H
- //Description :: Dynamic Array Object
- #ifndef __ODYNARR_H
- #define __ODYNARR_H
- #ifndef __ALL_H
- #include <ALL.h>
- #endif
- #ifndef __STRING_H
- #include <string.h>
- #endif
- //--------- Define constant ------------//
- #define DEF_DYNARRAY_BLOCK_SIZE 30 // default allocation block size (no. of unities each block has)
- //---------- Define sort types --------//
- enum { SORT_INT=1,
- SORT_SHORT,
- SORT_CHAR,
- SORT_CHAR_PTR,
- SORT_CHAR_STR };
- //-------- BEGIN OF CLASS DynArrary ---------//
- #pragma pack(1)
- class DynArray
- {
- public :
- int ele_num; // the size of the whole array
- int block_num; // the number of element of each block
- int cur_pos; // current position
- int last_ele; // last element position ( the array is not fully used )
- int ele_size; // the size of each element
- int sort_offset;
- char sort_type;
- char* body_buf; // cur_pos and last_ele are start from 1 (not 0)
- //----------------------------------------------//
- public :
- DynArray(int,int=DEF_DYNARRAY_BLOCK_SIZE);
- ~DynArray();
- void resize(int);
- void linkin(void*);
- void linkin_unique(void*);
- void linkout(int= -1);
- void update(void*, int= -1);
- void insert(void*);
- void insert_at(int,void*);
- void add_blank(int);
- void init_sort(int,char);
- void linkin_sort_scan_from_bottom(void*);
- // void resort(int);
- void* get();
- void* get(int);
- void* get_ptr();
- void* get_ptr(int);
- void read(void*);
- int check_pos();
- void push(void*);
- void pop(void* =0);
- void start();
- void end();
- int fwd();
- int bkwd();
- void jump(int);
- void go(int);
- int recno();
- int size();
- int is_start();
- int is_end();
- int scan_whole(void*);
- int scan(void*,int,char,int=0);
- int compare(void*,int,char);
- void quick_sort( int(*cmpFun)(const void*, const void*) );
- void clean_up(int* =0);
- void free_ptr(void*,int*);
- void zap(int resizeFlag=1);
- int write_file(File*); // Write current dynamic array to file
- int read_file(File*); // Read dynamic array from file
- };
- #pragma pack()
- //--------- END OF CLASS DynArray ---------//
- //--------- BEGIN OF FUNCTION DynArray::get -----------//
- //
- // Return : the memory pointer to the body_buf of current element
- // NULL if the record no. is invalid.
- //
- inline void* DynArray::get()
- {
- if( cur_pos == 0 )
- return NULL;
- return (void*) (body_buf+(cur_pos-1)*ele_size);
- }
- inline void* DynArray::get(int specRec)
- {
- if( specRec<1 || specRec>last_ele )
- return NULL;
- return (void*) (body_buf+(specRec-1)*ele_size);
- }
- //--------- END OF FUNCTION DynArray::get -----------//
- //--------- BEGIN OF FUNCTION DynArray::get_ptr -----------//
- //
- // The content of the entry is a pointer, return the content
- // is a pointer
- //
- // Return : the pointer
- // NULL if the record no. is invalid.
- inline void* DynArray::get_ptr()
- {
- if( cur_pos == 0 )
- return NULL;
- return (void*) *((char**)(body_buf+(cur_pos-1)*ele_size));
- }
- inline void* DynArray::get_ptr(int specRec)
- {
- if( specRec < 1 || specRec > last_ele )
- return NULL;
- return (void*) *((char**)(body_buf+(specRec-1)*ele_size));
- }
- //--------- END OF FUNCTION DynArray::get_ptr -----------//
- //--------- BEGIN OF FUNCTION DynArray::read -----------//
- //
- // Read current record into the given buffer
- //
- inline void DynArray::read(void* ent)
- {
- if( ent )
- memcpy(ent, get(), ele_size );
- }
- //---------- END OF FUNCTION DynArray::read -----------//
- //--------- BEGIN OF FUNCTIONO DynArray::push,pop -----------//
- // <void*> ent = the address of the entity to be linked into the array
- inline void DynArray::push(void* ent)
- {
- linkin(ent);
- }
- // [void*] ent = the address of the entity to be overwritten by current element
- inline void DynArray::pop(void* ent)
- {
- end();
- read(ent);
- linkout();
- }
- //----------- END OF FUNCTION DynArray::push,pop ----------//
- //-------- BEGIN OF FUNCTIONO DynArray::start,end,fwd,bkwd -------------//
- //
- inline void DynArray::start()
- {
- cur_pos = min(1,last_ele);
- }
- inline void DynArray::end()
- {
- cur_pos = last_ele;
- }
- inline int DynArray::fwd()
- {
- if (cur_pos < last_ele )
- {
- cur_pos++;
- return 1;
- }
- else
- return 0;
- }
- inline int DynArray::bkwd()
- {
- if (cur_pos > 1)
- {
- cur_pos--;
- return 1;
- }
- else
- return 0;
- }
- //---------- END OF FUNCTION DynArray::start,end,fwd,bkwd ---------//
- //--------- BEGIN OF FUNCTION DynArray::jump,go,pos,size ----------//
- inline void DynArray::jump(int step)
- {
- cur_pos+=step;
- if ( cur_pos < 0 )
- cur_pos = min(1,last_ele) ;
- if ( cur_pos > last_ele )
- cur_pos = last_ele;
- }
- inline void DynArray::go(int desPos)
- {
- if ( desPos >= 1 && desPos <= last_ele )
- cur_pos = desPos;
- }
- inline int DynArray::recno()
- {
- return cur_pos;
- }
- inline int DynArray::size()
- {
- return last_ele;
- }
- //----------- END OF FUNCTION DynArray::jump,go,pos,size ---------//
- //-------- BEGIN OF FUNCTION DynArray::isstart,isend ------//
- inline int DynArray::is_start()
- {
- return( cur_pos <= 1 );
- }
- inline int DynArray::is_end()
- {
- return( cur_pos >= last_ele );
- }
- //-------- END OF FUNCTION DynArray::isstart,isend --------//
- #endif
|