string_id.vala 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /*
  2. * Copyright (c) 2012-2025 Daniele Bartolini et al.
  3. * SPDX-License-Identifier: GPL-3.0-or-later
  4. */
  5. namespace Crown
  6. {
  7. [Compact]
  8. public struct StringId32
  9. {
  10. uint32 _id;
  11. public StringId32(string str)
  12. {
  13. hash(str.data, str.length);
  14. }
  15. public void hash(uint8[] str, uint len)
  16. {
  17. _id = (uint32)(murmur64(str, str.length, 0) & 0xffffffffu);
  18. }
  19. public static uint hash_func(StringId32? id)
  20. {
  21. return (uint)id._id;
  22. }
  23. public static bool equal_func(StringId32? a, StringId32? b)
  24. {
  25. return a._id == b._id;
  26. }
  27. public string to_string()
  28. {
  29. return "%.8x".printf(_id);
  30. }
  31. }
  32. [Compact]
  33. public struct StringId64
  34. {
  35. uint64 _id;
  36. public StringId64(string str)
  37. {
  38. hash(str.data, str.length);
  39. }
  40. public void hash(uint8[] str, uint len)
  41. {
  42. _id = murmur64(str, str.length, 0);
  43. }
  44. public static uint hash_func(StringId64? id)
  45. {
  46. return (uint)id._id;
  47. }
  48. public static bool equal_func(StringId64? a, StringId64? b)
  49. {
  50. return a._id == b._id;
  51. }
  52. public string to_string()
  53. {
  54. return "%.8x%.8x".printf((uint32)((_id & 0xffffffff00000000u) >> 32)
  55. , (uint32)((_id & 0x00000000ffffffffu))
  56. );
  57. }
  58. }
  59. } /* namespace Crown */