85 #include "../submodules/CoreLibrary/CoreLibrary/utils.h"
91 using namespace std::chrono;
95 Timestamp Utils::TimeReference = Timestamp(microseconds(0));
96 microseconds Utils::BasePeriod = microseconds(0);
97 float32 Utils::FloatTolerance = 0;
98 microseconds Utils::TimeTolerance = microseconds(0);
100 Timestamp Utils::GetTimeReference() {
return TimeReference; }
101 microseconds Utils::GetBasePeriod() {
return BasePeriod; }
102 uint32 Utils::GetFloatTolerance() {
return FloatTolerance; }
103 microseconds Utils::GetTimeTolerance() {
return TimeTolerance; }
105 void Utils::SetReferenceValues(microseconds base_period, float32 float_tolerance, microseconds time_tolerance) {
107 BasePeriod = base_period;
108 FloatTolerance = float_tolerance;
109 TimeTolerance = time_tolerance;
112 void Utils::SetTimeReference(Timestamp time_reference) {
114 TimeReference = time_reference;
117 bool Utils::Equal(float32 l, float32 r) {
121 return fabs(l - r) < FloatTolerance;
124 bool Utils::Synchronous(Timestamp l, Timestamp r) {
126 return abs(l - r) < TimeTolerance;
129 Timestamp Utils::GetTimestamp(
const Atom *iptr) {
131 return Timestamp(microseconds(GetInt64(iptr, 1)));
134 void Utils::SetTimestamp(
Atom *iptr, Timestamp timestamp) {
136 iptr[0] = Atom::Timestamp();
137 SetInt64(iptr, 1, duration_cast<microseconds>(timestamp.time_since_epoch()).count());
140 void Utils::SetTimestampStruct(
Code *
object, uint16 index, Timestamp timestamp) {
142 object->code(index) = Atom::Timestamp();
144 object->code(index + 2) = 0;
145 SetInt64(&object->code(0), index + 1, duration_cast<microseconds>(timestamp.time_since_epoch()).count());
148 string Utils::ToString_s_ms_us(Timestamp timestamp, Timestamp time_reference) {
149 auto duration = timestamp - time_reference;
150 uint64 t = abs(duration_cast<microseconds>(duration).count());
152 uint64 us = t % 1000;
153 uint64 ms = t / 1000;
154 uint64 s = ms / 1000;
157 std::string result = (duration < microseconds(0) ?
"-" :
"");
158 result += std::to_string(s);
160 result += std::to_string(ms);
162 result += std::to_string(us);
168 void Utils::SetDurationStruct(
Code *
object, uint16 index, microseconds duration) {
169 object->resize_code(index + 3);
170 object->code(index) = Atom::Duration();
171 SetInt64(&object->code(0), index + 1, duration.count());
174 string Utils::ToString_us(microseconds duration) {
175 uint64 us = abs(duration_cast<microseconds>(duration).count());
177 std::string sign = (duration < microseconds(0) ?
"-" :
"");
179 return sign + std::to_string(us) +
"us";
181 uint64 ms = us / 1000;
183 return sign + std::to_string(ms) +
"ms";
185 uint64 s = ms / 1000;
186 return sign + std::to_string(s) +
"s";
191 std::string Utils::GetString(
const Atom *iptr) {
195 uint8 char_count = (iptr[0].atom_ & 0x000000FF);
196 memcpy(buffer, iptr + 1, char_count);
197 buffer[char_count] = 0;
202 void Utils::SetString(Atom *iptr,
const std::string &s) {
204 uint8 l = (uint8)s.length();
206 iptr[index] = Atom::String(l);
209 for (uint8 i = 0; i < l; ++i) {
224 int32 Utils::GetResilience(Timestamp now, microseconds time_to_live, uint64 upr) {
226 if (time_to_live.count() == 0 || upr == 0)
228 auto deadline = now + time_to_live;
229 uint64 last_upr = duration_cast<microseconds>(now - TimeReference).count() / upr;
230 uint64 next_upr = duration_cast<microseconds>(deadline - TimeReference).count() / upr;
231 if (duration_cast<microseconds>(deadline - TimeReference).count() % upr > 0)
233 return next_upr - last_upr;
236 int32 Utils::GetResilience(float32 resilience, float32 origin_upr, float32 destination_upr) {
240 if (destination_upr <= origin_upr)
242 float32 r = origin_upr / destination_upr;
243 float32 res = resilience * r;
249 std::string Utils::RelativeTime(Timestamp t) {
251 return ToString_s_ms_us(t, TimeReference);
254 bool Utils::has_reference(
const Atom* code, uint16 index) {
255 Atom atom = code[index];
257 switch (atom.getDescriptor()) {
261 return has_reference(code, atom.asIndex());
268 case Atom::TIMESTAMP:
271 uint16 count = atom.getAtomCount();
272 for (uint16 i = 1; i <= count; ++i) {
273 if (has_reference(code, index + i))