Subversion Repositories Kolibri OS

Rev

Rev 7930 | Rev 7973 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef INCLUDE_COLLECTION_H
  2. #define INCLUDE_COLLECTION_H
  3. #print "[include <collection.h>]\n"
  4.  
  5. #include "array.h"
  6.  
  7. /*========================================================
  8. =                                                        =
  9. =                       Integer                          =
  10. =                                                        =
  11. ========================================================*/
  12.  
  13. struct collection_int
  14. {
  15.         dword buf;
  16.         dword buf_size;
  17.         unsigned count;
  18.         void alloc();
  19.         void add();
  20.         dword get();
  21.         void set();
  22.         void swap();
  23.         dword len();
  24.         dword get_last();
  25.         void pop();
  26.         void drop();
  27. };
  28.  
  29. :void collection_int::alloc() {
  30.         if (!buf) {
  31.                 buf_size = 4096;
  32.                 buf = malloc(4096);
  33.         } else {
  34.                 buf_size += 4096;
  35.                 buf = realloc(buf, buf_size);
  36.         }
  37. }
  38.  
  39. :void collection_int::add(dword _in) {
  40.         if (!buf) || (count * sizeof(dword) >= buf_size) alloc();
  41.         EAX = count * sizeof(dword) + buf;
  42.         ESDWORD[EAX] = _in;
  43.         count++;
  44. }
  45.  
  46. :dword collection_int::get(dword pos) {
  47.         if (pos<0) || (pos>=count) return 0;
  48.         return ESDWORD[pos * sizeof(dword) + buf];
  49. }
  50.  
  51.  
  52. :void collection_int::set(dword pos, _in) {
  53.         while (pos >= count) add(0);
  54.         EAX = pos * sizeof(dword) + buf;
  55.         ESDWORD[EAX] = _in;
  56. }
  57.  
  58. :void collection_int::swap(dword pos1, pos2) {
  59.         while (pos1 >= count) add(0);
  60.         while (pos2 >= count) add(0);
  61.         EAX = pos1 * sizeof(dword) + buf;
  62.         EBX = pos2 * sizeof(dword) + buf;
  63.         ESDWORD[EAX] >< ESDWORD[EBX];
  64. }
  65.  
  66. :dword collection_int::len(dword pos) {
  67.         if (pos<0) || (pos+1>=count) return 0;
  68.         return get(pos+1) - get(pos);
  69. }
  70.  
  71. :dword collection_int::get_last() {
  72.         return get(count-1);
  73. }
  74.  
  75. :void collection_int::pop() {
  76.         if (count>0) count--;
  77. }
  78.  
  79. :void collection_int::drop() {
  80.         count = 0;
  81. }
  82.  
  83. /*========================================================
  84. =                                                        =
  85. =                       String                           =
  86. =                                                        =
  87. ========================================================*/
  88.  
  89. struct collection
  90. {
  91.         int realloc_size, count;
  92.         dword data_start;
  93.         dword data_size;
  94.         collection_int offset;
  95.         int add();
  96.         int addn();
  97.         dword get(); //get_name_by_pos
  98.         dword get_pos_by_name();
  99.         void drop();
  100.         void increase_data_size();
  101.         dword get_last();
  102.         bool pop();
  103. };
  104.  
  105. :void collection::increase_data_size() {
  106.         int filled_size;
  107.         if (realloc_size<4096) realloc_size = 4096;
  108.         if (!data_size) {
  109.                 data_size = realloc_size;
  110.                 data_start = malloc(realloc_size);             
  111.         }
  112.         else {
  113.                 data_size = data_size + realloc_size;
  114.                 data_start = realloc(data_start, data_size);
  115.         }
  116. }
  117.  
  118. :int collection::add(dword in) {
  119.         return addn(in, strlen(in));
  120. }
  121.  
  122. :int collection::addn(dword in, len) {
  123.         if (offset.get(count)+len+2 > data_size) {
  124.                 increase_data_size();
  125.                 addn(in, len);
  126.                 return 1;
  127.         }
  128.         strncpy(data_start+offset.get(count), in, len);
  129.         count++;
  130.         offset.set(count, offset.get(count-1) + len + 1);
  131.         return 1;
  132. }
  133.  
  134. :dword collection::get(dword pos) {
  135.         if (pos<0) || (pos>=count) return 0;
  136.         return data_start + offset.get(pos);
  137. }
  138.  
  139. :dword collection::get_last() {
  140.         return get(count-1);
  141. }
  142.  
  143. :dword collection::get_pos_by_name(dword name) {
  144.         dword i;
  145.         for (i=0; i<count; i++) {
  146.                 if (strcmp(data_start + offset.get(i), name)==0) return i;
  147.         }
  148.         return -1;
  149. }
  150.  
  151. :void collection::drop() {
  152.         if (data_start) free(data_start);
  153.         data_size = 0;
  154.         data_start = 0;
  155.         offset.drop();
  156.         count = 0;
  157. }
  158.  
  159. :bool collection::pop() {
  160.         if (count>0) count--;
  161. }
  162.  
  163. #endif