Randoms + Metaprogramming test.

Added randoms using C++ templates. Might be able to change the profiler class to a template template without the need of the config file.
v1
Brett 2023-01-04 14:35:57 -05:00
parent 4330e9940d
commit 1587f342ad
5 changed files with 63 additions and 3 deletions

View File

@ -11,6 +11,7 @@
#include <string_view>
#include <blt/config.h>
#include <mutex>
#include <queue>
#ifdef PHMAP_ENABLED
#include <parallel_hashmap/phmap.h>
@ -30,7 +31,7 @@ namespace BLT {
};
#ifdef PHMAP_ENABLED
typedef phmap::parallel_flat_hash_map<std::string_view, CaptureInterval> INTERVAL_MAP;
typedef phmap::parallel_flat_hash_map<std::string_view, CapturePoint> POINT_MAP;
typedef phmap::parallel_flat_hash_map<std::string_view, std::queue<CapturePoint>> POINT_MAP;
typedef phmap::parallel_flat_hash_map<std::string_view, POINT_MAP> POINT_HISTORY_MAP;
typedef phmap::parallel_flat_hash_map<int, std::string_view> ORDER_MAP;
#else
@ -43,8 +44,8 @@ namespace BLT {
class Profiler {
private:
INTERVAL_MAP intervals{};
POINT_MAP points{};
POINT_MAP cyclicPoints{};
std::queue<CapturePoint> points{};
std::queue<CapturePoint> cyclicPoints{};
POINT_HISTORY_MAP cyclicPointsHistory{};
ORDER_MAP order{};
@ -55,6 +56,11 @@ namespace BLT {
void finishCycle();
void startInterval(const std::string_view& name);
void endInterval(const std::string_view& name);
void profilerPoint();
/**
* Uses a separate tracking device that will be reset when finishCycle(); is called.
*/
void profilerPointCyclic();
};
}

View File

@ -30,6 +30,11 @@ namespace BLT {
int headIndex = 0;
T* data = new T[size];
/**
* Expands the internal array to the new size, copying over the data and shifting its minimal position to index 0
* and deletes the old array from memory.
* @param newSize new size of the internal array
*/
void expand(int newSize){
auto tempData = new T[newSize];
for (int i = 0; i < size - headIndex; i++)

34
include/blt/std/random.h Normal file
View File

@ -0,0 +1,34 @@
/*
* Created by Brett on 04/01/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#ifndef BLT_RANDOM_H
#define BLT_RANDOM_H
#include <random>
namespace BLT {
/**
* Creates a container class for generating random number distributions
* @tparam T numeric type
* @tparam dist std::uniform_real_distribution or std::uniform_int_distribution
*/
template<typename T, template<typename = T> typename dist = std::uniform_real_distribution>
class Random {
private:
std::random_device rd; // obtain a random number from hardware
std::mt19937 gen;
dist<T>* distribution = nullptr;
public:
explicit Random(T min = (T)0, T max = (T)1, long seed = 0): gen(std::mt19937(seed)){
distribution = new dist(min, max);
}
T get(){
return *distribution(gen);
}
};
}
#endif //BLT_RANDOM_H

View File

@ -29,4 +29,13 @@ namespace BLT {
intervals[name].end = {getCurrentTimeNanoseconds()};
order[lastOrder++] = name;
}
void Profiler::profilerPoint() {
}
void Profiler::profilerPointCyclic() {
}
}

6
src/blt/std/random.cpp Normal file
View File

@ -0,0 +1,6 @@
/*
* Created by Brett on 04/01/23.
* Licensed under GNU General Public License V3.0
* See LICENSE file for license detail
*/
#include <blt/std/random.h>