14 #ifndef EMP_TYPE_TRACKER_H 15 #define EMP_TYPE_TRACKER_H 17 #include <unordered_map> 19 #include "../base/array.h" 20 #include "../base/assert.h" 21 #include "../meta/meta.h" 38 template <
typename REAL_T,
size_t ID>
54 template <
typename... TYPES>
57 template <
typename REAL_T>
61 constexpr
static size_t GetNumTypes() {
return sizeof...(TYPES); }
66 for (
size_t v = 0; v < vals; v++) result *= GetNumTypes();
73 size_t cum_result = 1;
74 for (
size_t v = 0; v < vals; v++) {
75 cur_result *= GetNumTypes();
86 template <
typename T1,
typename T2,
typename... Ts>
87 constexpr
static size_t GetID() {
return GetID<T1>() + GetID<T2,Ts...>() * GetNumTypes(); }
90 template <
typename... Ts>
92 return GetCumCombos(
sizeof...(Ts)-1) + GetID<Ts...>();
97 template <
typename... Ts>
108 template <
typename... Ts>
110 return tt1->
GetTypeTrackerID() + GetTrackedID(tt2, ARGS...) * GetNumTypes();
115 template <
typename... Ts>
117 return GetCumCombos(
sizeof...(Ts)-1) + GetTrackedID(ARGS...);
121 std::unordered_map<size_t, emp::GenericFunction *>
fun_map;
132 for (
auto x : fun_map)
delete x.second;
154 template <
typename TEST_T>
160 template <
typename TEST_T>
164 template <
typename REAL_T>
171 template <
typename REAL_T>
175 template <
typename OUT_T>
179 template <
typename OUT_T>
184 template <
typename... Ts>
186 constexpr
size_t ID = GetComboID<Ts...>();
207 template <
typename... Ts>
209 return AddFunction( std::function<
void(Ts...)>(fun) );
213 template <
typename... Ts>
215 const size_t pos = GetTrackedComboID(args...);
216 if (
Has(fun_map, pos)) {
223 template <
typename... Ts>
~TypeTracker()
Definition: TypeTracker.h:131
A set of simple functions to manipulate maps.
void operator()(Ts...args)
Call TypeTracker as a function (refers call to RunFunction)
Definition: TypeTracker.h:224
static size_t GetTrackedID(const TrackedType &tt)
A Tracked ID is simply the unique ID of the type being tracked.
Definition: TypeTracker.h:96
The base class of any type to be tracked.
Definition: TypeTracker.h:30
virtual size_t GetTypeTrackerID() const noexcept
Definition: TypeTracker.h:49
static constexpr size_t GetID()
Each set of types should have an ID unique within that number of types.
Definition: TypeTracker.h:87
static constexpr size_t GetID()
Each type should have a unique ID.
Definition: TypeTracker.h:83
TypeTracker_Class(REAL_T &&in)
Definition: TypeTracker.h:44
REAL_T real_t
Definition: TypeTracker.h:40
REAL_TYPE type_decoy
Definition: meta.h:94
this_t & AddFunction(std::function< void(Ts...)> fun)
Definition: TypeTracker.h:185
static size_t GetTrackedID(TrackedType *tt1, TrackedType *tt2, Ts *...ARGS)
A set of pointers to access tracked IDs.
Definition: TypeTracker.h:109
Definition: BitVector.h:785
auto Call(Ts &&...args)
A generic form of the function call operator; use arg types to determine derived form.
Definition: GenericFunction.h:69
REAL_T value
Definition: TypeTracker.h:41
bool IsType(TrackedType *tt)
Test if the tracked type points to TEST_T.
Definition: TypeTracker.h:161
bool IsType(TrackedType &tt)
Test if the tracked type is TEST_T.
Definition: TypeTracker.h:155
ID
Definition: struct.h:26
virtual ~TrackedType()
Definition: TypeTracker.h:32
Definition: TypeTracker.h:39
TypeTracker_Class(const REAL_T &in)
Definition: TypeTracker.h:43
Definition: GenericFunction.h:29
static size_t GetTrackedID(TrackedType *tt)
We should also about able to use a pointer to access tracked IDs.
Definition: TypeTracker.h:105
OUT_T Cast(TrackedType &tt)
Cast the tracked type to OUT_T. Try to do so even if NOT original type!
Definition: TypeTracker.h:176
std::unordered_map< size_t, emp::GenericFunction * > fun_map
fun_map is a hash table that maps a set of inputs to the appropriate function.
Definition: TypeTracker.h:121
OUT_T Cast(TrackedType *tt)
Cast the tracked type pointer to OUT_T. Try to do so even if NOT original type!
Definition: TypeTracker.h:180
static constexpr size_t GetCumCombos(size_t vals=2)
How many combinations are the of the given number of types OR FEWER?
Definition: TypeTracker.h:71
static constexpr size_t GetComboID()
A ComboID should be unique across all size combinations.
Definition: TypeTracker.h:91
wrap_t< REAL_T > * New(REAL_T &&val)
Create an input value in a TypeTracker_Class maintaining the value (move version) ...
Definition: TypeTracker.h:148
bool Has(const MAP_T &in_map, const KEY_T &key)
Take any map type, and run find to determine if a key is present.
Definition: map_utils.h:21
wrap_t< REAL_T > * New(REAL_T &val)
Create an input value in a TypeTracker_Class maintaining the value (reference version) ...
Definition: TypeTracker.h:142
Based on std::function, but with a common base class.
TypeTracker()
Definition: TypeTracker.h:124
If we are in emscripten, make sure to include the header.
Definition: array.h:37
Definition: GenericFunction.h:47
virtual size_t GetTypeTrackerID() const noexcept=0
#define emp_assert(...)
Definition: assert.h:199
wrap_t< REAL_T > Wrap(REAL_T &&val)
Convert an input value into a TypeTracker_Class maintaining the value (universal version) ...
Definition: TypeTracker.h:136
constexpr int get_type_index()
Definition: meta.h:70
REAL_T ToType(TrackedType &tt)
Convert the tracked type back to REAL_T. Assert that this is type safe!
Definition: TypeTracker.h:165
static size_t GetTrackedID(const TrackedType &tt1, const TrackedType &tt2, const Ts &...ARGS)
Or set of types being tracked...
Definition: TypeTracker.h:100
static constexpr bool cur_result
Definition: TypePack.h:110
Definition: TypeTracker.h:55
this_t & AddFunction(void(*fun)(Ts...))
Definition: TypeTracker.h:208
static constexpr size_t GetTrackedComboID(Ts...ARGS)
Definition: TypeTracker.h:116
void RunFunction(Ts...args)
Run the appropriate function based on the argument types received.
Definition: TypeTracker.h:214
static constexpr size_t GetNumCombos(size_t vals=2)
How many combinations of V types are there?
Definition: TypeTracker.h:64
REAL_T ToType(TrackedType *tt)
Convert the tracked type pointer back to REAL_T. Assert that this is type safe!
Definition: TypeTracker.h:172
static constexpr size_t GetNumTypes()
How many types are we working with?
Definition: TypeTracker.h:61
constexpr bool AllTrue()
Combine bools to AND them all together.
Definition: functions.h:52