[OT] Algorithm question

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Mon May 1 10:03:51 PDT 2017


On Mon, May 01, 2017 at 11:32:19AM +0000, Mike B Johnson via Digitalmars-d wrote:
[...]
> Since most real world problems would require selecting elements more
> than once it may be far more efficient to sort by P(x)(filter) then
> simply select a random element.
> 
> e.g.,
> 
> a = A.filter(x->P(x))  // Creates a new set a where only the elements
> of A that satisfy P(x) are added
> 
> ...
> 
> e = a.random;
> 
> 
> this is O(1) if you only have to filter once(you can create a
> container that always "sorts" on P(x) so to speak.. like a sorted
> dictionary).
[...]

Sorry, I forgot to specify that P(x) may change each time, as may the
input A. So caching would be of little help in this case.


T

-- 
This is a tpyo.


More information about the Digitalmars-d mailing list