From 489059320322d36f34636a4868c606a192e10ca1 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sat, 8 Apr 2023 13:32:34 +0200 Subject: [PATCH] Add absolute array comparators Check for highest/lowest value in array (absolute). --- r5dev/public/utility/utility.h | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/r5dev/public/utility/utility.h b/r5dev/public/utility/utility.h index 7c14f9f8..20eeeb19 100644 --- a/r5dev/public/utility/utility.h +++ b/r5dev/public/utility/utility.h @@ -84,6 +84,31 @@ string PrintPercentageEscape(const string& svInput); string FormatV(const char* szFormat, va_list args); string Format(const char* szFormat, ...); +///////////////////////////////////////////////////////////////////////////// +// Array +template +Iter ExtremeElementABS(Iter first, Iter last, Compare compare) +{ + auto abs_compare = [compare](LONG a, LONG b) + { + return compare(abs(a), abs(b)); + }; + + return std::min_element(first, last, abs_compare); +} + +template // Return lowest element in array. +Iter MinElementABS(Iter first, Iter last) +{ + return ExtremeElementABS(first, last, std::less<>()); +} + +template // Return highest element in array. +Iter MaxElementABS(Iter first, Iter last) +{ + return ExtremeElementABS(first, last, std::greater<>()); +} + ///////////////////////////////////////////////////////////////////////////// // Time std::chrono::nanoseconds IntervalToDuration(const float flInterval);