Ver Fonte

Improve Array constexpr behavior

Kajetan Johannes Hammerle há 3 meses atrás
pai
commit
6fa6bc34d7

+ 10 - 10
include/core/data/Array.hpp

@@ -10,43 +10,43 @@ namespace Core {
         T data[static_cast<unsigned int>(N)];
 
     public:
-        Array() = default;
+        constexpr Array() = default;
 
-        Array(const T& t) {
+        constexpr Array(const T& t) {
             fill(t);
         }
 
-        void fill(const T& t) {
+        constexpr void fill(const T& t) {
             for(int i = 0; i < N; i++) {
                 data[i] = t;
             }
         }
 
-        T& operator[](int index) {
+        constexpr T& operator[](int index) {
             return data[index];
         }
 
-        const T& operator[](int index) const {
+        constexpr const T& operator[](int index) const {
             return data[index];
         }
 
-        T* begin() {
+        constexpr T* begin() {
             return data;
         }
 
-        T* end() {
+        constexpr T* end() {
             return data + N;
         }
 
-        const T* begin() const {
+        constexpr const T* begin() const {
             return data;
         }
 
-        const T* end() const {
+        constexpr const T* end() const {
             return data + N;
         }
 
-        constexpr int getLength() const {
+        static consteval int getLength() {
             return N;
         }
 

+ 1 - 3
include/core/utils/Clock.hpp

@@ -10,12 +10,10 @@ namespace Core {
         using Nanos = i64;
 
     private:
-        static constexpr int BITS = 7;
-        static constexpr int LENGTH = 1 << BITS;
         int index;
         Nanos last;
         Nanos sum;
-        Array<Nanos, LENGTH> time;
+        Array<Nanos, 1 << 7> time;
 
     public:
         Clock();

+ 2 - 4
include/core/utils/Random.hpp

@@ -1,6 +1,7 @@
 #ifndef CORE_RANDOM_HPP
 #define CORE_RANDOM_HPP
 
+#include "core/data/Array.hpp"
 #include "core/utils/Types.hpp"
 
 namespace Core {
@@ -8,10 +9,7 @@ namespace Core {
         using Seed = u32;
 
     private:
-        constexpr static int N = 25;
-        constexpr static int M = 7;
-
-        Seed data[N];
+        Array<Seed, 25> data;
         int index;
 
     public:

+ 2 - 2
src/Clock.cpp

@@ -14,7 +14,7 @@ Core::Error Core::Clock::update(Clock::Nanos& n) {
         n = 0;
         return Error::NONE;
     }
-    index = (index + 1) & (LENGTH - 1);
+    index = (index + 1) & (time.getLength() - 1);
     sum -= time[index];
     time[index] = current - last;
     sum += time[index];
@@ -24,7 +24,7 @@ Core::Error Core::Clock::update(Clock::Nanos& n) {
 }
 
 float Core::Clock::getUpdatesPerSecond() const {
-    return (LENGTH * 1000000000.0f) / static_cast<float>(sum);
+    return (time.getLength() * 1000000000.0f) / static_cast<float>(sum);
 }
 
 Core::Error Core::Clock::getNanos(Clock::Nanos& n) {

+ 11 - 6
src/Random.cpp

@@ -1,7 +1,11 @@
 #include "core/utils/Random.hpp"
 
-Core::Random::Random(Seed seed) : index(0) {
-    for(int i = 0; i < N; i++) {
+#include <stdio.h>
+
+constexpr static int M = 7;
+
+Core::Random::Random(Seed seed) : data(), index(0) {
+    for(int i = 0; i < data.getLength(); i++) {
         data[i] = seed;
         seed = seed * 7 + 31;
     }
@@ -9,17 +13,18 @@ Core::Random::Random(Seed seed) : index(0) {
 
 void Core::Random::update() {
     static const Seed map[2] = {0, 0x8EBFD028};
-    for(int i = 0; i < N - M; i++) {
+    for(int i = 0; i < data.getLength() - M; i++) {
         data[i] = data[i + M] ^ (data[i] >> 1) ^ map[data[i] & 1];
     }
-    for(int i = N - M; i < N; i++) {
-        data[i] = data[i + (M - N)] ^ (data[i] >> 1) ^ map[data[i] & 1];
+    for(int i = data.getLength() - M; i < data.getLength(); i++) {
+        data[i] = data[i + (M - data.getLength())] ^ (data[i] >> 1) ^
+                  map[data[i] & 1];
     }
     index = 0;
 }
 
 int Core::Random::next() {
-    if(index >= N) {
+    if(index >= data.getLength()) {
         update();
     }
     Seed r = data[index++];