www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to search for an element in the array. D2 phobos.

reply Eldar Insafutdinov <e.insafutdinov gmail.com> writes:
I am a little bit confused with a simple operation of searching in the array
with D2-phobos. With tango I used to use:
    size_t find( Elem[] buf, Elem pat, Pred2E pred = Pred2E.init );

    Performs a linear scan of buf from [0 .. buf.length), returning the index
of the first element matching pat, or buf.length if no match was found.
Comparisons will be performed using the supplied predicate or '==' if none is
supplied.

I tried std.algorithm.find:

class Node { ... }

Node[] v;
Node node;
...
int pos = find(v, node);

I get this error: /home/eldar/d/bin/../src/phobos/std/algorithm.d(1617): Error:
no property 'empty' for type 'model_d2.Node'

I think I completely misunderstood how to use it.

Many thanks in advance,
Eldar.
Jul 12 2009
parent reply bearophile <bearophileHUGS lycos.com> writes:
Eldar Insafutdinov:
 I think I completely misunderstood how to use it.
Yes, it's too much complex. It tries to do many different things in the most efficient way possible, the result is a high complexity in usage. That's one of the faults of Andrei's code, it's not tested by letting average coders use it. A solution for this problem is to offer simple functions (like you can find in Tango. In my dlibs the situation is intermediate, I think) with a simple API for the most common purposes. Bye, bearophile
Jul 12 2009
next sibling parent Bill Baxter <wbaxter gmail.com> writes:
On Sun, Jul 12, 2009 at 3:56 PM, bearophile<bearophileHUGS lycos.com> wrote=
:
 Eldar Insafutdinov:
 I think I completely misunderstood how to use it.
Yes, it's too much complex. It tries to do many different things in the m=
ost efficient way possible, the result is a high complexity in usage. That'= s one of the faults of Andrei's code, it's not tested by letting =A0average= coders use it.
 A solution for this problem is to offer simple functions (like you can fi=
nd in Tango. In my dlibs the situation is intermediate, I think) with a sim= ple API for the most common purposes. Isn't that a matter of built-in arrays not yet sporting the range interface= ? I assume if .empty is part of the range interface then arrays are supposed to support it. --bb
Jul 12 2009
prev sibling parent Daniel Keep <daniel.keep.lists gmail.com> writes:
bearophile wrote:
 Eldar Insafutdinov:
 I think I completely misunderstood how to use it.
Yes, it's too much complex. It tries to do many different things in the most efficient way possible, the result is a high complexity in usage. That's one of the faults of Andrei's code, it's not tested by letting average coders use it. A solution for this problem is to offer simple functions (like you can find in Tango. In my dlibs the situation is intermediate, I think) with a simple API for the most common purposes. Bye, bearophile
It should work. Taken from the algorithm docs:
 Example:

 int[] a = [ 1, 4, 2, 3 ];
 assert(find(a, 4) == [ 4, 2, 3 ]);
That said, find is too complex for his purposes. Amazingly, Phobos still appears to lack a basic indexOf function. Phobos2: can build a working sentient AI that can feel love up from individual atoms, but can't find the index of an array element. :P
Jul 12 2009