Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

139 rader
3.3 KiB

  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include "./types.hpp"
  5. u32 hm_hash(u32 u);
  6. inline bool hm_objects_match(u32 a, u32 b);
  7. struct Key;
  8. u32 hm_hash(Key u);
  9. inline bool hm_objects_match(Key a, Key b);
  10. #define ZoneScoped
  11. #define ZoneScopedN(name)
  12. #include "./error.hpp"
  13. #include "./hooks.hpp"
  14. #include "./hashmap.hpp"
  15. #include "./error.hpp"
  16. #include "./print.hpp"
  17. u32 hm_hash(u32 u) {
  18. return ((u64)u * 2654435761) % 4294967296;
  19. }
  20. inline bool hm_objects_match(u32 a, u32 b) {
  21. return a == b;
  22. }
  23. struct Key {
  24. int x;
  25. int y;
  26. int z;
  27. };
  28. u32 hm_hash(Key u) {
  29. return ((u.y ^ (u.x << 1)) >> 1) ^ u.z;
  30. }
  31. inline bool hm_objects_match(Key a, Key b) {
  32. return a.x == b.x
  33. && a.y == b.y
  34. && a.z == b.z;
  35. }
  36. auto print_dots(FILE* f) -> u32 {
  37. return print_to_file(f, "...");
  38. }
  39. auto test_printer() -> void {
  40. u32 arr[] = {1,2,3,4,1,1,3};
  41. f32 f_arr[] = {1.1,2.1,3.2};
  42. register_printer("dots", print_dots, Printer_Function_Type::_void);
  43. u32 u1 = -1;
  44. u64 u2 = -1;
  45. char* str;
  46. print_to_string(&str, " - %{dots[5]} %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  47. print("---> %{->char}", str);
  48. print(" - %{dots[3]}\n");
  49. print(" - %{u32} %{u64}\n", u1, u2);
  50. print(" - %{u32} %{u32} %{u32}\n", 2, 5, 7);
  51. print(" - %{f32} %{f32} %{f32}\n", 2.0, 5.0, 7.0);
  52. print(" - %{u32} %{bool} %{->char}\n", 2, true, "hello");
  53. print(" - %{f32[3]}\n", f_arr);
  54. print(" - %{f32,3}\n", 44.9, 55.1, 66.2);
  55. print(" - %{u32[5]}\n", arr);
  56. print(" - %{u32[*]}\n", arr, 4);
  57. print(" - %{u32,5}\n", 1,2,3,4,1,2);
  58. print(" - %{unknown%d}\n", 1);
  59. print(" - %{s32,3}\n", -1,200,-300);
  60. print(" - %{->} <> %{->,2}\n", &u1, &arr, nullptr);
  61. print("%{->char}%{->char}%{->char}",
  62. true ? "general " : "",
  63. false ? "validation " : "",
  64. false ? "performance " : "");
  65. // print("%{->char}%{->char}\n\n", "hallo","");
  66. }
  67. auto test_hm() -> void {
  68. Hash_Map<u32, u32> h1;
  69. h1.alloc();
  70. defer { h1.dealloc(); };
  71. h1.set_object(1, 2);
  72. h1.set_object(2, 4);
  73. h1.set_object(3, 6);
  74. h1.set_object(4, 8);
  75. assert(h1.key_exists(1), "key shoud exist");
  76. assert(h1.key_exists(2), "key shoud exist");
  77. assert(h1.key_exists(3), "key shoud exist");
  78. assert(h1.key_exists(4), "key shoud exist");
  79. assert(!h1.key_exists(5), "key shoud not exist");
  80. assert(h1.get_object(1) == 2, "value should be correct");
  81. assert(h1.get_object(2) == 4, "value should be correct");
  82. assert(h1.get_object(3) == 6, "value should be correct");
  83. assert(h1.get_object(4) == 8, "value should be correct");
  84. Hash_Map<Key, u32> h2;
  85. h2.alloc();
  86. defer { h2.dealloc(); };
  87. h2.set_object({.x = 1, .y = 2, .z = 3}, 1);
  88. h2.set_object({.x = 3, .y = 3, .z = 3}, 3);
  89. assert(h2.key_exists({.x = 1, .y = 2, .z = 3}), "key shoud exist");
  90. assert(h2.key_exists({.x = 3, .y = 3, .z = 3}), "key shoud exist");
  91. assert(h2.get_object({.x = 1, .y = 2, .z = 3}) == 1, "value should be correct");
  92. assert(h2.get_object({.x = 3, .y = 3, .z = 3}) == 3, "value should be correct");
  93. h2.for_each([] (Key k, u32 v, u32 i) {
  94. print("%{s32} %{u32} %{u32}\n", k.x, v, i);
  95. });
  96. }
  97. s32 main(s32 argc, char* argv[]) {
  98. init_printer();
  99. test_hm();
  100. print("done.");
  101. return 0;
  102. }