Home » Extra libraries, Code snippets, applications etc. » U++ users applications in progress and useful code snippets, including reference examples! » findarg_r, a findarg variant that works with value ranges.
findarg_r, a findarg variant that works with value ranges. [message #57186] |
Mon, 07 June 2021 00:09 |
Oblivion
Messages: 1170 Registered: August 2007
|
Senior Contributor |
|
|
Hi,
Sometimes it is useful to search a value in a value range (min, max val).
Below is the code of findarg variant that works with value ranges.
It returns the index of range (starting with 0) of arg,. If not found, -1 is returned.
#include <Core/Core.h>
using namespace Upp;
template <class T, class K>
constexpr int findarg_r(const T& x, const K& l, const K& h)
{
return x >= l && x <= h ? 0 : -1;
}
template <class T, class L, typename... R>
constexpr int findarg_r(const T& sel, const L& l, const L& h, const R& ...args)
{
if(sel >= l && sel <= h)
return 0;
int q = findarg_r(sel, args...);
return q >= 0 ? q + 1 : -1;
}
CONSOLE_APP_MAIN
{
StdLogSetup(LOG_COUT|LOG_FILE);
String s = "dcba";
RDUMP(findarg_r(s,
"aa", "bb",
"cc", "dd"));
RDUMP(findarg_r(123,
0, 1,
2, 3,
4, 100,
100, 400,
500, 1000) == 3);
}
Output:
findarg_r(s, "aa", "bb", "cc", "dd") = 1
findarg_r(123, 0, 1, 2, 3, 4, 100, 100, 400, 500, 1000) == 3 = true
A decode_r can also be implemented in a similar way.
Best regards,
Oblivion
Github page: https://github.com/ismail-yilmaz
upp-components: https://github.com/ismail-yilmaz/upp-components
Bobcat the terminal emulator: https://github.com/ismail-yilmaz/Bobcat
[Updated on: Mon, 07 June 2021 00:13] Report message to a moderator
|
|
|
Goto Forum:
Current Time: Thu Jan 23 02:10:05 CET 2025
Total time taken to generate the page: 0.04064 seconds
|