Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
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 is currently offline  Oblivion
Messages: 1094
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


[Updated on: Mon, 07 June 2021 00:13]

Report message to a moderator

Previous Topic: Linking has failed using mitielib
Next Topic: OleCalc example can't compile
Goto Forum:
  


Current Time: Sat Jun 15 15:24:37 CEST 2024

Total time taken to generate the page: 0.02492 seconds