how use lowerBound with just sorting key, not complete values

Daniel Davidson nospam at spam.com
Tue Nov 12 06:36:14 PST 2013


The following code works for finding the lower bound based on 
needle. But I have to create a needle which I don't want to do. 
How can I use lowerBound with just the sortKey, date in this 
case?  So I want to do something like the following - but it 
won't work. Is there a way to search an array I know is ordered 
by date by only supplying date?

assumeSorted!q{ a.date < b.date }(sarr)
           .lowerBound(Date(2000,2,1));

Thanks
Dan
-------------------------------------------------------

import std.stdio;
import std.range;
import std.datetime;
import std.algorithm;
import std.array;

struct S {
   Date date;
   string foo;
}

void main() {
   auto sarr = [ S(Date(2000,1,1), "x"),
                 S(Date(2000,2,1), "a"),
                 S(Date(2000,3,1), "foo") ];
   assert(sort!q{ a.date < b.date }(sarr).array == sarr);

   auto needle = S(Date(2000,2,1));
   writeln(assumeSorted!q{ a.date < b.date }(sarr)
           .lowerBound(needle));

   writeln(sarr);
}




More information about the Digitalmars-d-learn mailing list