partition(range, leftsubrange) or partition(range, rightsubrange)

Sean Kelly sean at invisibleduck.org
Wed Sep 10 19:05:07 PDT 2008


Benji Smith wrote:
> Sean Kelly wrote:
>> superdan wrote:
>>> got a question on this range stuff. in stl partition is 
>>> partition(begin, mid, end). neat. in std.algorithm partition is 
>>> partition(range, mid). so-so. never like it mucho. in the new stuff 
>>> with ranges n all there are two choices partition(range, 
>>> leftsubrange) or partition(range, rightsubrange). question is, is 
>>> there a better choice between the two or are they just the same. 
>>> would be cool to have a clear rule with some advantage. and that's 
>>> easy to remember too.
>>>
>>> like steve i think ranges are cool n all but fraid iterators are 
>>> still good at sumthin'. either-way choices ain't a good sign.
>>
>> To throw another version into the mix, Tango's partition routine is 
>> declared as partition(range, pred) and uses the result of pred to 
>> determine what to do with each element.  In std.algorithm parlance, 
>> that would be equivalent to partition!("a < 5")(range), for example.
> 
> I was looking at that the other day, and it made me wonder...
> 
> Why does tango's partition use a bool predicate instead of an int 
> predicate (returning -1, 0, or 1 like opCmp does)?

Simply because it's more natural.  I don't really like having to store 
the result of a compare and then switch off it.

> Using the int predicate would enable a qsort routine that avoids 
> pointlessly swapping adjacent elements if they have equal values. It's 
> very handy for collections with lots of duplicate entries.

Tango's sort routine already optimizes for duplicate entries.  Partition 
and whatnot don't though.


Sean


More information about the Digitalmars-d-announce mailing list