Utility.hpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #ifndef CORE_UTILITY_HPP
  2. #define CORE_UTILITY_HPP
  3. #include "core/utils/Check.hpp"
  4. #include "core/utils/Error.hpp"
  5. #include "core/utils/Types.hpp"
  6. #define CORE_SIZE(t) static_cast<i64>(sizeof(t))
  7. namespace Core {
  8. template<typename T, typename C = int>
  9. C popCount(const T& t) {
  10. static constexpr C map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
  11. 1, 2, 2, 3, 2, 3, 3, 4};
  12. C sum = 0;
  13. for(size_t i = 0; i < sizeof(T) * 8; i += 4) {
  14. sum += map[(t >> i) & 0xF];
  15. }
  16. return sum;
  17. }
  18. using ExitHandler = void (*)(int, void*);
  19. void exitWithHandler(const char* file, int line, int value);
  20. void setExitHandler(ExitHandler eh, void* data);
  21. #define CORE_EXIT(exitValue) \
  22. Core::exitWithHandler(__FILE__, __LINE__, exitValue)
  23. CError toString(signed short s, char* buffer, size_t size);
  24. CError toString(unsigned short s, char* buffer, size_t size);
  25. CError toString(signed int i, char* buffer, size_t size);
  26. CError toString(unsigned int i, char* buffer, size_t size);
  27. CError toString(signed long l, char* buffer, size_t size);
  28. CError toString(unsigned long l, char* buffer, size_t size);
  29. CError toString(signed long long ll, char* buffer, size_t size);
  30. CError toString(unsigned long long ll, char* buffer, size_t size);
  31. CError toString(float f, char* buffer, size_t size);
  32. CError toString(double d, char* buffer, size_t size);
  33. CError toString(long double ld, char* buffer, size_t size);
  34. void print(int c);
  35. void print(const char* s);
  36. void printLine(const char* s);
  37. CError reallocate(char*& p, size_t n);
  38. }
  39. #endif