Binary search in structs

FreeSlave via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Apr 6 11:36:05 PDT 2015


I think I found solution using opBinaryRight

import std.range;

struct S
{
     int i;
     string s;

     int opCmp(int i) {
         return this.i - i;
     }

     int opCmp(ref const S s) {
         return this.i - s.i;
     }

     int opBinaryRight(string op)(int i) if (op == "<") {
         return i - this.i;
     }
}

void main(string [] args)
{
     S[] structs = [{1,"hello"}, {2,"world"}, {3, "!"}]; //sorted 
by i

     auto sortedRange = assumeSorted(structs);

     auto t = sortedRange.trisect(2);
}


More information about the Digitalmars-d-learn mailing list