On Thursday, 10 January 2013 at 20:24:02 UTC, Charles Hixson wrote:
On 01/10/2013 08:41 AM, Ali Çehreli wrote:
On 01/09/2013 04:14 PM, Charles Hixson wrote:
Would the following code:
for (int i = 1; i < di.count; i++)
{ assert (node.di.entry[i - 1].key < node.di.entry[i].key); }

be optimized away if compiled under -release?

It looks like you can use std.algorithm.isSorted instead:

http://dlang.org/phobos/std_algorithm.html#isSorted

assert(isSorted(di));

Or perhaps:

assert(isSorted(node.di.entry[0 .. di.count]));

That code will disappear in release mode.

Additionally, if the O(N) complexity of that check is not acceptable
even in non-release mode there is std.range.assumeSorted:

http://dlang.org/phobos/std_range.html#.assumeSorted

assumeSorted makes very few comparisons so it may miss occasional sort
issues. Also, it is not really for checking but more for range
algorithms that require or work better with a sorted range.

Ali
OTOH, if :
version(assert)
{...}
works as suggested by monarch_dodra above, that will allow me to avoid the problem.

Well, you check if something is sorted by hand, if the code to check it already exists?

Unfortunately, entry is also a struct. key isn't its only member. If there's some other reason to define a opCmp on entry, that would be a good approach.

I'm not entirely sure what you mean by that. The fact that it is a struct should pose no problem.

As for whether or you need to define opCmp, keep in mind that "isSorted" (and *anything else* related to sorted in general) take a "less" paramter

http://dlang.org/phobos/std_algorithm.html#isSorted

So you can write an external "foo" function, that takes 2 entries, and returns the "lowest" one (by any custom ordering), and then call:

isSorted!foo(myStuff)

Reply via email to