containers, iteration, and removal

Ellery Newcomer ellery-newcomer at utulsa.edu
Mon Aug 27 13:12:02 PDT 2012


On 08/27/2012 12:46 PM, Ellery Newcomer wrote:
> On 08/24/2012 12:23 PM, JN wrote:
>> I feel kinda stupid here, what's wrong with C++ remove_if (
>> http://www.cplusplus.com/reference/algorithm/remove_if/ )?
>>
>
> you mean something like
>
> c.erase(remove_if(c.begin(), c.end(), predicate), c.end())
>
> ?
>
> That actually is the sort of thing one could accomplish with position
> ranges:
>
> c.remove(filter!(anti_predicate)(c.position_range()));

Doh.

import std.stdio;
import std.container;

void main(){
     auto sl = SList!int([1,2,3,4,5,6,7,8]);
     auto rng = sl[];
     writeln(sl[]);
     sl.linearRemove(remove_if!"a % 2 == 1"(sl[]));
     writeln(sl[]);
}

auto remove_if(alias _pred, Rng)(Rng rng) {
     import std.functional;
     alias unaryFun!_pred pred;
     Rng res = rng.save();
     while(!rng.empty) {
         if(!pred(rng.front)) {
             res.front = rng.moveFront();
             res.popFront();
         }
         rng.popFront();
     }
     return res;
}


Hmm. In multi_index,

  res.front = rng.moveFront();

has some slightly nasty ramifications which I am not sure how to get 
around (without access to the node pointer of the element being 
assigned, multi_index has to remove the element and then add it back 
in). I wonder what boost::multi_index does?


More information about the Digitalmars-d mailing list