Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Ticket #514 (closed enhancement: invalid)

Opened 12 years ago

Last modified 12 years ago

More contains functions in tango.core.Array

Reported by: Deewiant Assigned to: sean
Priority: normal Milestone: 1.0
Component: Core Functionality Version: trunk
Keywords: Cc:


Currently tango.core.Array has only:

bool bsearch (Elem[] buf, Elem pat, Pred2E pred = null); // no size_t-returning version?
bool contains (Elem[] setA, Elem[] setB, Pred2E pred = null);

I humbly request that there be a contains function to complement each find function. I'm fairly sure that this could be done relatively easily with a mixin to generate the code. Something like (pseudocode):

enum {
	PREDICATE, // for the findIf functions
template Contains(char[] prefix, char[] postfix, int argType) {
	static if (argType == PREDICATE)
		const Contains =
			"bool " ~ prefix ~ "contains" ~ postfix ~ "(...) {"
				"return " ~ prefix ~ "find" ~ postfix ~ "(...) != length;"

mixin (Contains!("kr", "", ELEM_AND_ARRAY)); // krfind
mixin (Contains!("", "If", PREDICATE));      // findIf

Change History

06/17/07 14:59:30 changed by larsivi

  • milestone set to 1.0.

06/17/07 18:51:53 changed by sean

  • status changed from new to closed.
  • resolution set to invalid.

'contains' is for sets, not normal ranges. It sounds like you want either 'find' or 'lbound', which operate on unsorted and on sorted ranges, respectively. If you have a sorted array containing duplicates, an "equal range" algorithm can be implemented via:

myArray[myArray.lbound( key ) .. myArray.ubound( key )];

I've considered adding this as an algorithm as well, but it's so trivial I haven't seen a need for it yet.

Rejecting this for now unless another search routine proves necessary.