Skip to content

Commit

Permalink
Added common math operators to Datum.
Browse files Browse the repository at this point in the history
  • Loading branch information
mercere99 committed May 24, 2022
1 parent 390789f commit e9efd8c
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions include/emp/data/Datum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "../base/assert.hpp"
#include "../base/notify.hpp"
#include "../math/math.hpp"

namespace emp {

Expand Down Expand Up @@ -133,6 +134,22 @@ namespace emp {
template<typename T> bool operator> (T && rhs) const { return Compare(std::forward<T>(rhs)) == 1; }
template<typename T> bool operator<=(T && rhs) const { return Compare(std::forward<T>(rhs)) != 1; }

Datum operator+(const Datum & in) const {
if (IsDouble()) return NativeDouble() + in.AsDouble();
return NativeString() + in.AsString();
}
Datum operator*(const Datum & in) const {
if (IsDouble()) return NativeDouble() * in.AsDouble();
std::string out_string;
size_t count = static_cast<size_t>(in.AsDouble());
out_string.reserve(NativeString().size() * count);
for (size_t i = 0; i < count; i++) out_string += NativeString();
return out_string;
}
Datum operator-(const Datum & in) const { return AsDouble() - in.AsDouble(); }
Datum operator/(const Datum & in) const { return AsDouble() / in.AsDouble(); }
Datum operator%(const Datum & in) const { return emp::Mod(AsDouble(), in.AsDouble()); }

};

std::ostream & operator<<(std::ostream & out, const emp::Datum & d) {
Expand Down

0 comments on commit e9efd8c

Please sign in to comment.