Browse Source

unsigned ints very overflow is needed

Kajetan Johannes Hammerle 3 years ago
parent
commit
94bb22f042
7 changed files with 30 additions and 16 deletions
  1. 4 2
      utils/Clock.cpp
  2. 3 2
      utils/HashMap.h
  3. 6 5
      utils/Random.cpp
  4. 8 3
      utils/Random.h
  5. 5 3
      utils/StringBuffer.h
  6. 2 0
      utils/Types.h
  7. 2 1
      utils/Utils.h

+ 4 - 2
utils/Clock.cpp

@@ -17,11 +17,13 @@ Clock::Nanos Clock::update() {
 }
 
 float Clock::getUpdatesPerSecond() const {
-    return LENGTH * (1000000000.0f / sum);
+    return (LENGTH * 1000000000.0f) / sum;
 }
 
 Clock::Nanos Clock::getNanos() const {
-    return std::chrono::high_resolution_clock::now().time_since_epoch().count();
+    return std::chrono::duration_cast<std::chrono::nanoseconds>(
+               std::chrono::high_resolution_clock::now().time_since_epoch())
+        .count();
 }
 
 void Clock::wait(Nanos nanos) const {

+ 3 - 2
utils/HashMap.h

@@ -4,6 +4,7 @@
 #include "utils/Array.h"
 #include "utils/List.h"
 #include "utils/StringBuffer.h"
+#include "utils/Types.h"
 #include "utils/Utils.h"
 
 template<typename K, typename V, int N_MIN>
@@ -39,11 +40,11 @@ class HashMap final {
     }
 
     template<typename H>
-    static int hash(const H& key) {
+    static Hash hash(const H& key) {
         return key.hashCode();
     }
 
-    static int hash(int key) {
+    static Hash hash(int key) {
         return key;
     }
 

+ 6 - 5
utils/Random.cpp

@@ -3,18 +3,19 @@
 
 #include "utils/Random.h"
 
-Random::Random(int seed) : index(0) {
+Random::Random(Seed seed) : index(0) {
     for(int i = 0; i < N; i++) {
         data[i] = seed;
         seed = seed * 7 + 31;
     }
 }
 
-Random::Random() : Random(std::chrono::steady_clock::now().time_since_epoch().count()) {
+Random::Random()
+    : Random(std::chrono::steady_clock::now().time_since_epoch().count()) {
 }
 
 void Random::update() {
-    static const int map[2] = {0, -1900031960};
+    static const Seed map[2] = {0, 0x8EBFD028};
     for(int i = 0; i < N - M; i++) {
         data[i] = data[i + M] ^ (data[i] >> 1) ^ map[data[i] & 1];
     }
@@ -28,11 +29,11 @@ int Random::next() {
     if(index >= N) {
         update();
     }
-    int r = data[index++];
+    Seed r = data[index++];
     r ^= (r << 7) & 0x2B5B2500;
     r ^= (r << 15) & 0xDB8B0000;
     r ^= (r >> 16);
-    return r & static_cast<int>(-1u >> 1);
+    return static_cast<int>(r >> 1);
 }
 
 int Random::next(int min, int inclusiveMax) {

+ 8 - 3
utils/Random.h

@@ -1,17 +1,22 @@
 #ifndef RANDOM_H
 #define RANDOM_H
 
-class Random final {
+#include "utils/Types.h"
+
+struct Random final {
+    typedef uint32 Seed;
+
+private:
     constexpr static int N = 25;
     constexpr static int M = 7;
 
-    int data[N];
+    Seed data[N];
     int index;
 
     void update();
 
 public:
-    Random(int seed);
+    Random(Seed seed);
     Random();
 
     int next();

+ 5 - 3
utils/StringBuffer.h

@@ -4,14 +4,16 @@
 #include <cstring>
 #include <iostream>
 
+#include "utils/Types.h"
+
 template<int N>
 class StringBuffer final {
     int length;
-    int hash;
+    Hash hash;
     char data[N];
 
     void addToHash(char c) {
-        hash = 136671 * hash + c;
+        hash = 2120251889 * hash + c;
     }
 
     void addToHash(int from, int to) {
@@ -119,7 +121,7 @@ public:
         return *this;
     }
 
-    int hashCode() const {
+    Hash hashCode() const {
         return hash;
     }
 

+ 2 - 0
utils/Types.h

@@ -12,4 +12,6 @@ typedef uint32_t uint32;
 typedef uint16_t uint16;
 typedef uint8_t uint8;
 
+typedef uint32_t Hash;
+
 #endif

+ 2 - 1
utils/Utils.h

@@ -10,7 +10,8 @@ namespace Utils {
 
     template<typename T>
     int popCount(const T& t) {
-        static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
+        static constexpr int map[16] = {0, 1, 1, 2, 1, 2, 2, 3,
+                                        1, 2, 2, 3, 2, 3, 3, 4};
         int sum = 0;
         for(int i = 0; i < static_cast<int>(sizeof(T) * 8); i += 4) {
             sum += map[(t >> i) & 0xF];