ODYNARR.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /*
  2. * Seven Kingdoms: Ancient Adversaries
  3. *
  4. * Copyright 1997,1998 Enlight Software Ltd.
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. //Filename :: ODYNARR.H
  21. //Description :: Dynamic Array Object
  22. #ifndef __ODYNARR_H
  23. #define __ODYNARR_H
  24. #ifndef __ALL_H
  25. #include <ALL.h>
  26. #endif
  27. #ifndef __STRING_H
  28. #include <string.h>
  29. #endif
  30. //--------- Define constant ------------//
  31. #define DEF_DYNARRAY_BLOCK_SIZE 30 // default allocation block size (no. of unities each block has)
  32. //---------- Define sort types --------//
  33. enum { SORT_INT=1,
  34. SORT_SHORT,
  35. SORT_CHAR,
  36. SORT_CHAR_PTR,
  37. SORT_CHAR_STR };
  38. //-------- BEGIN OF CLASS DynArrary ---------//
  39. #pragma pack(1)
  40. class DynArray
  41. {
  42. public :
  43. int ele_num; // the size of the whole array
  44. int block_num; // the number of element of each block
  45. int cur_pos; // current position
  46. int last_ele; // last element position ( the array is not fully used )
  47. int ele_size; // the size of each element
  48. int sort_offset;
  49. char sort_type;
  50. char* body_buf; // cur_pos and last_ele are start from 1 (not 0)
  51. //----------------------------------------------//
  52. public :
  53. DynArray(int,int=DEF_DYNARRAY_BLOCK_SIZE);
  54. ~DynArray();
  55. void resize(int);
  56. void linkin(void*);
  57. void linkin_unique(void*);
  58. void linkout(int= -1);
  59. void update(void*, int= -1);
  60. void insert(void*);
  61. void insert_at(int,void*);
  62. void add_blank(int);
  63. void init_sort(int,char);
  64. void linkin_sort_scan_from_bottom(void*);
  65. // void resort(int);
  66. void* get();
  67. void* get(int);
  68. void* get_ptr();
  69. void* get_ptr(int);
  70. void read(void*);
  71. int check_pos();
  72. void push(void*);
  73. void pop(void* =0);
  74. void start();
  75. void end();
  76. int fwd();
  77. int bkwd();
  78. void jump(int);
  79. void go(int);
  80. int recno();
  81. int size();
  82. int is_start();
  83. int is_end();
  84. int scan_whole(void*);
  85. int scan(void*,int,char,int=0);
  86. int compare(void*,int,char);
  87. void quick_sort( int(*cmpFun)(const void*, const void*) );
  88. void clean_up(int* =0);
  89. void free_ptr(void*,int*);
  90. void zap(int resizeFlag=1);
  91. int write_file(File*); // Write current dynamic array to file
  92. int read_file(File*); // Read dynamic array from file
  93. };
  94. #pragma pack()
  95. //--------- END OF CLASS DynArray ---------//
  96. //--------- BEGIN OF FUNCTION DynArray::get -----------//
  97. //
  98. // Return : the memory pointer to the body_buf of current element
  99. // NULL if the record no. is invalid.
  100. //
  101. inline void* DynArray::get()
  102. {
  103. if( cur_pos == 0 )
  104. return NULL;
  105. return (void*) (body_buf+(cur_pos-1)*ele_size);
  106. }
  107. inline void* DynArray::get(int specRec)
  108. {
  109. if( specRec<1 || specRec>last_ele )
  110. return NULL;
  111. return (void*) (body_buf+(specRec-1)*ele_size);
  112. }
  113. //--------- END OF FUNCTION DynArray::get -----------//
  114. //--------- BEGIN OF FUNCTION DynArray::get_ptr -----------//
  115. //
  116. // The content of the entry is a pointer, return the content
  117. // is a pointer
  118. //
  119. // Return : the pointer
  120. // NULL if the record no. is invalid.
  121. inline void* DynArray::get_ptr()
  122. {
  123. if( cur_pos == 0 )
  124. return NULL;
  125. return (void*) *((char**)(body_buf+(cur_pos-1)*ele_size));
  126. }
  127. inline void* DynArray::get_ptr(int specRec)
  128. {
  129. if( specRec < 1 || specRec > last_ele )
  130. return NULL;
  131. return (void*) *((char**)(body_buf+(specRec-1)*ele_size));
  132. }
  133. //--------- END OF FUNCTION DynArray::get_ptr -----------//
  134. //--------- BEGIN OF FUNCTION DynArray::read -----------//
  135. //
  136. // Read current record into the given buffer
  137. //
  138. inline void DynArray::read(void* ent)
  139. {
  140. if( ent )
  141. memcpy(ent, get(), ele_size );
  142. }
  143. //---------- END OF FUNCTION DynArray::read -----------//
  144. //--------- BEGIN OF FUNCTIONO DynArray::push,pop -----------//
  145. // <void*> ent = the address of the entity to be linked into the array
  146. inline void DynArray::push(void* ent)
  147. {
  148. linkin(ent);
  149. }
  150. // [void*] ent = the address of the entity to be overwritten by current element
  151. inline void DynArray::pop(void* ent)
  152. {
  153. end();
  154. read(ent);
  155. linkout();
  156. }
  157. //----------- END OF FUNCTION DynArray::push,pop ----------//
  158. //-------- BEGIN OF FUNCTIONO DynArray::start,end,fwd,bkwd -------------//
  159. //
  160. inline void DynArray::start()
  161. {
  162. cur_pos = min(1,last_ele);
  163. }
  164. inline void DynArray::end()
  165. {
  166. cur_pos = last_ele;
  167. }
  168. inline int DynArray::fwd()
  169. {
  170. if (cur_pos < last_ele )
  171. {
  172. cur_pos++;
  173. return 1;
  174. }
  175. else
  176. return 0;
  177. }
  178. inline int DynArray::bkwd()
  179. {
  180. if (cur_pos > 1)
  181. {
  182. cur_pos--;
  183. return 1;
  184. }
  185. else
  186. return 0;
  187. }
  188. //---------- END OF FUNCTION DynArray::start,end,fwd,bkwd ---------//
  189. //--------- BEGIN OF FUNCTION DynArray::jump,go,pos,size ----------//
  190. inline void DynArray::jump(int step)
  191. {
  192. cur_pos+=step;
  193. if ( cur_pos < 0 )
  194. cur_pos = min(1,last_ele) ;
  195. if ( cur_pos > last_ele )
  196. cur_pos = last_ele;
  197. }
  198. inline void DynArray::go(int desPos)
  199. {
  200. if ( desPos >= 1 && desPos <= last_ele )
  201. cur_pos = desPos;
  202. }
  203. inline int DynArray::recno()
  204. {
  205. return cur_pos;
  206. }
  207. inline int DynArray::size()
  208. {
  209. return last_ele;
  210. }
  211. //----------- END OF FUNCTION DynArray::jump,go,pos,size ---------//
  212. //-------- BEGIN OF FUNCTION DynArray::isstart,isend ------//
  213. inline int DynArray::is_start()
  214. {
  215. return( cur_pos <= 1 );
  216. }
  217. inline int DynArray::is_end()
  218. {
  219. return( cur_pos >= last_ele );
  220. }
  221. //-------- END OF FUNCTION DynArray::isstart,isend --------//
  222. #endif