y_unqid.inc 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // y_textid
  2. enum E_UNIQUE_ID
  3. {
  4. E_UNIQUE_EXTERNAL,
  5. E_UNIQUE_REVISION,
  6. E_UNIQUE_INTERNAL,
  7. E_UNIQUE_FLAGS
  8. // Search using a hash map with 256 entries.
  9. E_UNIQUE_HASH_NEXT,
  10. // Combine multiple items in to a single initial ID.
  11. E_UNIQUE_LINK_NEXT
  12. }
  13. #define UniqueID:%0<%1> %0@ENTRIES[256],%0@IDS[%1][E_UNIQUE_ID],%0@EMPTY,%0@NEXT
  14. stock _Unique_Lookup(entries[256], ids[][E_UNIQUE_ID], lookup, &id, &rev, &flags)
  15. {
  16. new
  17. cur = entries[lookup & 0xFF]; // Get entry point.
  18. while (cur != -1)
  19. {
  20. if (ids[cur][E_UNIQUE_EXTERNAL] == lookup)
  21. {
  22. id = ids[cur][E_UNIQUE_INTERNAL],
  23. rev = ids[cur][E_UNIQUE_REVISION],
  24. flags = ids[cur][E_UNIQUE_FLAGS];
  25. if (ids[cur][E_UNIQUE_LINK_NEXT] != -1) return -1; // More data.
  26. else return 1;
  27. }
  28. cur = ids[cur][E_UNIQUE_HASH_NEXT];
  29. }
  30. return 0;
  31. }
  32. #define Unique_Lookup(%0, _Unique_Lookup(%0@ENTRIES,%0@IDS,
  33. stock _Unique_InUse(entries[256], ids[][E_UNIQUE_ID], lookup)
  34. {
  35. new
  36. cur = entries[lookup & 0xFF]; // Get entry point.
  37. while (cur != -1)
  38. {
  39. if (ids[cur][E_UNIQUE_EXTERNAL] == lookup) return 1;
  40. cur = ids[cur][E_UNIQUE_HASH_NEXT];
  41. }
  42. return 0;
  43. }
  44. #define Unique_InUse(%0, _Unique_InUse(%0@ENTRIES,%0@IDS,
  45. stock _Unique_Assign(entries[256], ids[][E_UNIQUE_ID], &free, &next)
  46. {
  47. if (free == -1) return 0;
  48. do
  49. {
  50. if (++next == 0) ++next;
  51. }
  52. while (_Unique_InUse(entries, ids, next));
  53. new
  54. tmp = ids[free][E_UNIQUE_HASH_NEXT];
  55. return
  56. // Copy the data.
  57. ids[free][E_UNIQUE_EXTERNAL] = next,
  58. ++ids[free][E_UNIQUE_REVISION],
  59. // Allocate this in to the hash map.
  60. ids[free][E_UNIQUE_HASH_NEXT] = entries[next & 0xFF],
  61. entries[next & 0xFF] = free,
  62. free = tmp,
  63. // Return the ID.
  64. next;
  65. }