Subversion Repositories Kolibri OS

Rev

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

  1. // Author: Pavel Iakovlev by. pavelyakov
  2.  
  3. #ifndef INCLUDE_ARRAY_H
  4. #define INCLUDE_ARRAY_H
  5.  
  6. // Array memory: [dword key][byte flags][dword left][dword right][dword value] -> 17 bytes = 1 position
  7. // If key don't exists then value == 0
  8. :struct Array
  9. {
  10.         dword memory;
  11.         dword offsetMemory;
  12.         dword lenInitSize;
  13.         dword recursiveIndex(dword i, address);
  14.         byte set(dword key, data);
  15.         dword get(dword key);
  16.         void reallocMemory(dword newSize);
  17.         //dword del(dword key);
  18.         byte init(dword size);
  19. };
  20.  
  21. :void Array::reallocMemory(dword newSize)
  22. {
  23.         memory = realloc(memory, newSize);
  24.         lenInitSize = newSize;
  25. }
  26.  
  27. :dword Array::recursiveIndex(dword key, address)
  28. {
  29.         dword flags = 0;
  30.         IF (DSDWORD[address] == key) RETURN address;
  31.         flags = DSBYTE[address + 4];
  32.         //IF (flags & 100b) RETURN address; // if delete
  33.         IF (flags & 010b) && (DSDWORD[address] < key) RETURN recursiveIndex(key, DSDWORD[address + 5]); // left tree
  34.         IF (flags & 001b) && (DSDWORD[address] > key) RETURN recursiveIndex(key, DSDWORD[address + 9]); // right tree
  35.         RETURN address;
  36. }
  37. :byte Array::init(dword size)
  38. {
  39.         IF(!size) RETURN 0;
  40.         IF(!memory)
  41.         {
  42.                 lenInitSize = size * 17;
  43.                 memory = malloc(lenInitSize);
  44.                 EBX = memory;
  45.                 DSDWORD[EBX] = 0;
  46.                 DSBYTE[EBX + 4] = 0;
  47.                 DSDWORD[EBX + 5] = 0;
  48.                 DSDWORD[EBX + 9] = 0;
  49.                 DSDWORD[EBX + 13] = 0;
  50.                 offsetMemory = 17;
  51.                 RETURN 0xFF;
  52.         }
  53.         IF(size > lenInitSize)
  54.         {
  55.                 reallocMemory(size * 17);
  56.                 RETURN 0xFF;
  57.         }
  58.         RETURN 0;
  59. }
  60. :byte Array::set(dword key, data)
  61. {
  62.         dword address = 0;
  63.         dword newOffset = 0;
  64.         IF(offsetMemory > lenInitSize) reallocMemory(offsetMemory << 1);
  65.         address = recursiveIndex(key, memory);
  66.         /*IF(DSBYTE[address + 4] & 100b)
  67.         {
  68.                 IF(DSDWORD[address] < key)
  69.                 {
  70.                         DSBYTE[address + 4] |= 10b;
  71.                         DSDWORD[address + 5] = newOffset;
  72.                 }
  73.                 ELSE IF(DSDWORD[address] > key)
  74.                 {
  75.                         DSBYTE[address + 4] |= 01b;
  76.                         DSDWORD[address + 9] = newOffset;
  77.                 }
  78.                 ELSE
  79.                 {
  80.                         DSDWORD[address + 13] = data;
  81.                         RETURN 0xFF;
  82.                 }
  83.         }*/
  84.         newOffset = memory + offsetMemory;
  85.         IF(DSDWORD[address] < key)
  86.         {
  87.                 DSBYTE[address + 4] |= 010b; // set flag left address
  88.                 DSDWORD[address + 5] = newOffset;
  89.         }
  90.         ELSE IF(DSDWORD[address] > key)
  91.         {
  92.                 DSBYTE[address + 4] |= 001b; // set flag right address
  93.                 DSDWORD[address + 9] = newOffset;
  94.         }
  95.         ELSE
  96.         {
  97.                 DSDWORD[address + 13] = data;
  98.                 RETURN 0xFF;
  99.         }
  100.         DSDWORD[newOffset] = key;
  101.         DSBYTE[newOffset+4] = 0;
  102.         DSDWORD[newOffset+5] = 0;
  103.         DSDWORD[newOffset+9] = 0;
  104.         DSDWORD[newOffset+13] = data;
  105.         offsetMemory += 17;
  106.         RETURN 0xFF;
  107. }
  108. :dword Array::get(dword key)
  109. {
  110.         EBX = recursiveIndex(key, memory);
  111.         IF(DSDWORD[EBX] != key) RETURN 0;
  112.         IF(DSBYTE[EBX + 4] & 100b) RETURN 0;
  113.         RETURN DSDWORD[EBX + 13];
  114. }
  115. /*:dword Array::del(dword key)
  116. {
  117.         dword address = 0;
  118.         address = recursiveIndex(key, memory);
  119.         IF(DSDWORD[address] != key) RETURN 0;
  120.         DSBYTE[address + 4] |= 100b;
  121.         RETURN 0xFF;
  122. }*/
  123.  
  124. :struct Dictionary
  125. {
  126.         Array array;
  127.         dword hash(dword text);
  128.         byte set(dword key, value);
  129.         dword get(dword key);
  130.         byte init(dword size);
  131. };
  132.  
  133. :dword Dictionary::hash(dword text) // max 255 bytes as strings => 4 byte or duble word hash
  134. {
  135.         dword checkSum1 = 1;
  136.         dword checkSum2 = 0;
  137.         dword beginAddress = 0;
  138.        
  139.         beginAddress = text;
  140.         WHILE(DSBYTE[text])
  141.         {
  142.                 checkSum1 += DSBYTE[text];
  143.                 checkSum2 += checkSum1;
  144.                 text++;
  145.         }
  146.         //IF(h1 > 0x03FFF) RETURN h1 << 8 ^ h2;
  147.         //IF(h2 > 0x3FFFF) RETURN h1 << 8 ^ h2;
  148.         EAX = text - beginAddress;
  149.         EAX <<= 23;
  150.         RETURN EAX | checkSum2;
  151. }
  152.  
  153. :byte Dictionary::set(dword key, value)
  154. {
  155.         RETURN array.set(hash(key),value);
  156. }
  157.  
  158. :dword Dictionary::get(dword key)
  159. {
  160.         RETURN array.get(hash(key));
  161. }
  162.  
  163. :byte Dictionary::init(dword size)
  164. {
  165.         RETURN array.init(size);
  166. }
  167.  
  168. #endif