Subversion Repositories Kolibri OS

Rev

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

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