Re: sortUniq

2015-01-24 Thread Andrei Alexandrescu via Digitalmars-d
On 1/24/15 7:17 AM, zeljkog wrote: private struct FilterResult(alias predicate, Range) { static if (is(predicate == struct)) predicate pred; else alias pred = predicate; ... The problem is here - the creation of an empty object is unlikely to be of use to the

Re: sortUniq

2015-01-24 Thread Andrei Alexandrescu via Digitalmars-d
On 1/24/15 9:34 AM, zeljkog wrote: On 24.01.15 18:26, zeljkog wrote: Until now I concluded: 1. Resulting range must be output range. Right? Should be input range. For filter it's a forward range if input is forward or better. -- Andrei

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 24.01.15 19:47, Andrei Alexandrescu wrote: But here there is potential problem with shared state after save? It's not a problem - save saves the state of the iteration, not the data being iterated. -- Andrei If you pass stateful object with opCall or closure it becomes part of state

Re: sortUniq

2015-01-24 Thread Andrei Alexandrescu via Digitalmars-d
On 1/24/15 11:00 AM, H. S. Teoh via Digitalmars-d wrote: On Sat, Jan 24, 2015 at 10:35:13AM -0800, Andrei Alexandrescu via Digitalmars-d wrote: On 1/24/15 9:34 AM, zeljkog wrote: On 24.01.15 18:26, zeljkog wrote: Until now I concluded: 1. Resulting range must be output range. Right? Should

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 24.01.15 18:26, zeljkog wrote: Until now I concluded: 1. Resulting range must be output range. Right? Should be input range.

Re: sortUniq

2015-01-24 Thread H. S. Teoh via Digitalmars-d
On Sat, Jan 24, 2015 at 10:35:13AM -0800, Andrei Alexandrescu via Digitalmars-d wrote: On 1/24/15 9:34 AM, zeljkog wrote: On 24.01.15 18:26, zeljkog wrote: Until now I concluded: 1. Resulting range must be output range. Right? Should be input range. For filter it's a forward range if

Re: sortUniq

2015-01-24 Thread H. S. Teoh via Digitalmars-d
On Sat, Jan 24, 2015 at 11:20:55AM -0800, Andrei Alexandrescu via Digitalmars-d wrote: On 1/24/15 11:00 AM, H. S. Teoh via Digitalmars-d wrote: On Sat, Jan 24, 2015 at 10:35:13AM -0800, Andrei Alexandrescu via Digitalmars-d wrote: [...] For filter it's a forward range if input is forward or

Re: sortUniq

2015-01-24 Thread Peter Alexander via Digitalmars-d
On Saturday, 24 January 2015 at 14:53:02 UTC, Andrei Alexandrescu wrote: On 1/24/15 3:50 AM, Peter Alexander wrote: On Friday, 23 January 2015 at 18:22:08 UTC, zeljkog wrote: On 23.01.15 19:13, Andrei Alexandrescu wrote: On 1/23/15 10:05 AM, zeljkog wrote: On 23.01.15 18:48, H. S. Teoh via

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 24.01.15 18:02, Andrei Alexandrescu wrote: On 1/24/15 7:17 AM, zeljkog wrote: private struct FilterResult(alias predicate, Range) { static if (is(predicate == struct)) predicate pred; else alias pred = predicate; ... The problem is here - the creation of

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 24.01.15 19:35, Andrei Alexandrescu wrote: On 1/24/15 9:34 AM, zeljkog wrote: On 24.01.15 18:26, zeljkog wrote: Until now I concluded: 1. Resulting range must be output range. Right? Should be input range. For filter it's a forward range if input is forward or better. -- Andrei But

Re: sortUniq

2015-01-24 Thread Andrei Alexandrescu via Digitalmars-d
On 1/24/15 10:41 AM, zeljkog wrote: On 24.01.15 19:35, Andrei Alexandrescu wrote: On 1/24/15 9:34 AM, zeljkog wrote: On 24.01.15 18:26, zeljkog wrote: Until now I concluded: 1. Resulting range must be output range. Right? Should be input range. For filter it's a forward range if input is

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 24.01.15 15:53, Andrei Alexandrescu wrote: On 1/24/15 4:13 AM, zeljkog wrote: On 24.01.15 12:50, Peter Alexander wrote: auto f = unique(); [1, 5, 5, 2, 1, 5, 6, 6].filter!(f).writeln; // [1, 5, 2, 6] Filter needs an alias, and you cannot alias an R-value (it has no symbol). Yes, I see

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 24.01.15 16:17, zeljkog wrote: template filter(alias predicate) if (is(typeof(unaryFun!predicate))) Should be: template filter(alias predicate) if (is(typeof(unaryFun!predicate)) || is(predicate == struct))

Re: sortUniq

2015-01-24 Thread Laeeth Isharc via Digitalmars-d
The problem with RedBlackTree is that it's cache-unfriendly due to the memory allocation and per-node pointers. One reason quicksort performs so well is because it works in-place, and the partition operation accesses elements bilinearly (i.e., two linear traversals over the same stretch of

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 23.01.15 19:05, zeljkog wrote: On 23.01.15 18:48, H. S. Teoh via Digitalmars-d wrote: I think what he's trying to do is to call a function that returns a delegate, and use that delegate to instantiate the filter template. AFAIK I've never seen code like this before, and it looks like the

Re: sortUniq

2015-01-24 Thread ixid via Digitalmars-d
Ignore me, sorry! Obviously that will not work for reuse of unique.

Re: sortUniq

2015-01-24 Thread zeljkog via Digitalmars-d
On 24.01.15 12:50, Peter Alexander wrote: auto f = unique(); [1, 5, 5, 2, 1, 5, 6, 6].filter!(f).writeln; // [1, 5, 2, 6] Filter needs an alias, and you cannot alias an R-value (it has no symbol). Yes, I see :) But I think this should be supported by std.algorithm: import std.stdio,

Re: sortUniq

2015-01-24 Thread Peter Alexander via Digitalmars-d
On Friday, 23 January 2015 at 18:22:08 UTC, zeljkog wrote: On 23.01.15 19:13, Andrei Alexandrescu wrote: On 1/23/15 10:05 AM, zeljkog wrote: On 23.01.15 18:48, H. S. Teoh via Digitalmars-d wrote: I think what he's trying to do is to call a function that returns a delegate, and use that

Re: sortUniq

2015-01-24 Thread ixid via Digitalmars-d
import std.stdio, std.algorithm; auto unique(){ bool[int] c; return (int a){ if (a in c) return false; else{ c[a] = true; return true; } }; } void main() { [1, 5, 5, 2, 1, 5, 6, 6].filter!(unique()).writeln; } Just

Re: sortUniq

2015-01-24 Thread Andrei Alexandrescu via Digitalmars-d
On 1/24/15 3:50 AM, Peter Alexander wrote: On Friday, 23 January 2015 at 18:22:08 UTC, zeljkog wrote: On 23.01.15 19:13, Andrei Alexandrescu wrote: On 1/23/15 10:05 AM, zeljkog wrote: On 23.01.15 18:48, H. S. Teoh via Digitalmars-d wrote: I think what he's trying to do is to call a function

Re: sortUniq

2015-01-24 Thread Andrei Alexandrescu via Digitalmars-d
On 1/24/15 4:13 AM, zeljkog wrote: On 24.01.15 12:50, Peter Alexander wrote: auto f = unique(); [1, 5, 5, 2, 1, 5, 6, 6].filter!(f).writeln; // [1, 5, 2, 6] Filter needs an alias, and you cannot alias an R-value (it has no symbol). Yes, I see :) But I think this should be supported by

Re: sortUniq

2015-01-23 Thread zeljkog via Digitalmars-d
On 23.01.15 19:13, Andrei Alexandrescu wrote: On 1/23/15 10:05 AM, zeljkog wrote: On 23.01.15 18:48, H. S. Teoh via Digitalmars-d wrote: I think what he's trying to do is to call a function that returns a delegate, and use that delegate to instantiate the filter template. AFAIK I've never

Re: sortUniq

2015-01-23 Thread Andrei Alexandrescu via Digitalmars-d
On 1/23/15 10:05 AM, zeljkog wrote: On 23.01.15 18:48, H. S. Teoh via Digitalmars-d wrote: I think what he's trying to do is to call a function that returns a delegate, and use that delegate to instantiate the filter template. AFAIK I've never seen code like this before, and it looks like the

Re: sortUniq

2015-01-23 Thread Andrei Alexandrescu via Digitalmars-d
On 1/23/15 7:04 AM, zeljkog wrote: On 22.01.15 22:40, Andrei Alexandrescu wrote: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. Loosely-related, compiling ... auto ret = arr.filter!(myFilter()); ... I got: Error: closures are not

Re: sortUniq

2015-01-23 Thread H. S. Teoh via Digitalmars-d
On Fri, Jan 23, 2015 at 09:40:42AM -0800, Andrei Alexandrescu via Digitalmars-d wrote: On 1/23/15 7:04 AM, zeljkog wrote: On 22.01.15 22:40, Andrei Alexandrescu wrote: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements.

Re: sortUniq

2015-01-23 Thread zeljkog via Digitalmars-d
On 23.01.15 18:48, H. S. Teoh via Digitalmars-d wrote: I think what he's trying to do is to call a function that returns a delegate, and use that delegate to instantiate the filter template. AFAIK I've never seen code like this before, and it looks like the compiler isn't prepared to handle

Re: sortUniq

2015-01-23 Thread zeljkog via Digitalmars-d
On 22.01.15 22:40, Andrei Alexandrescu wrote: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. Loosely-related, compiling ... auto ret = arr.filter!(myFilter()); ... I got: Error: closures are not yet supported in CTFE Using struct

Re: sortUniq

2015-01-23 Thread bearophile via Digitalmars-d
zeljkog: Loosely-related, compiling ... auto ret = arr.filter!(myFilter()); ... I got: Error: closures are not yet supported in CTFE Using struct with opCall directly also does not pass. Did I miss something? Is it near? Please show a minimal nonworking example in the D.learn newsgroup.

Re: sortUniq

2015-01-22 Thread Vladimir Panteleev via Digitalmars-d
On Thursday, 22 January 2015 at 21:40:57 UTC, Andrei Alexandrescu wrote: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. What would be a better integrated version - one that does sorting and uniq in one shot? I suspect the combination

Re: sortUniq

2015-01-22 Thread H. S. Teoh via Digitalmars-d
On Thu, Jan 22, 2015 at 01:40:56PM -0800, Andrei Alexandrescu via Digitalmars-d wrote: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. What would be a better integrated version - one that does sorting and uniq in one shot? I suspect

Re: sortUniq

2015-01-22 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. In Bugzilla I've asked for a hashGroup, it returns a built-in associative array. Bye, bearophile

Re: sortUniq

2015-01-22 Thread Justin Whear via Digitalmars-d
On Thu, 22 Jan 2015 13:40:56 -0800, Andrei Alexandrescu wrote: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. What would be a better integrated version - one that does sorting and uniq in one shot? I suspect the combination could

sortUniq

2015-01-22 Thread Andrei Alexandrescu via Digitalmars-d
There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. What would be a better integrated version - one that does sorting and uniq in one shot? I suspect the combination could be quite a bit better than doing the two in sequence. A few

Re: sortUniq

2015-01-22 Thread Andrei Alexandrescu via Digitalmars-d
On 1/22/15 2:06 PM, Justin Whear wrote: On Thu, 22 Jan 2015 13:40:56 -0800, Andrei Alexandrescu wrote: There's this classic patter on Unix: |sort|uniq, i.e. sort some data and only display the unique elements. What would be a better integrated version - one that does sorting and uniq in one

Re: sortUniq

2015-01-22 Thread Vladimir Panteleev via Digitalmars-d
On Thursday, 22 January 2015 at 23:35:32 UTC, H. S. Teoh via Digitalmars-d wrote: On Thu, Jan 22, 2015 at 02:51:38PM -0800, H. S. Teoh via Digitalmars-d wrote: Wait, if R1 and R3, after the recursion, are sorted and contain only unique elements, doesn't that mean that we can just collapse R2

Re: sortUniq

2015-01-22 Thread H. S. Teoh via Digitalmars-d
On Thu, Jan 22, 2015 at 11:52:18PM +, Vladimir Panteleev via Digitalmars-d wrote: On Thursday, 22 January 2015 at 23:35:32 UTC, H. S. Teoh via Digitalmars-d wrote: On Thu, Jan 22, 2015 at 02:51:38PM -0800, H. S. Teoh via Digitalmars-d wrote: Wait, if R1 and R3, after the recursion, are

Re: sortUniq

2015-01-22 Thread bearophile via Digitalmars-d
Andrei Alexandrescu: What would be a better integrated version - one that does sorting and uniq in one shot? I suspect the combination could be quite a bit better than doing the two in sequence. What I need most is a groping by hashing, since years. A unique sort can be handy, but it's less

Re: sortUniq

2015-01-22 Thread H. S. Teoh via Digitalmars-d
On Thu, Jan 22, 2015 at 02:51:38PM -0800, H. S. Teoh via Digitalmars-d wrote: On Thu, Jan 22, 2015 at 02:29:58PM -0800, Andrei Alexandrescu via Digitalmars-d wrote: [...] Thanks. I'm thinking of something based on general comparisons. Something like a modified quicksort with fat pivot:

Re: sortUniq

2015-01-22 Thread Paul O'Neil via Digitalmars-d
On 01/22/2015 07:06 PM, bearophile wrote: Andrei Alexandrescu: What would be a better integrated version - one that does sorting and uniq in one shot? I suspect the combination could be quite a bit better than doing the two in sequence. What I need most is a groping by hashing, since

Re: sortUniq

2015-01-22 Thread H. S. Teoh via Digitalmars-d
On Thu, Jan 22, 2015 at 02:29:58PM -0800, Andrei Alexandrescu via Digitalmars-d wrote: [...] Thanks. I'm thinking of something based on general comparisons. Something like a modified quicksort with fat pivot: A. Partition into three subranges: R1pivot, R2=pivot, R3pivot B. Recurse on R1

Re: sortUniq

2015-01-22 Thread Andrei Alexandrescu via Digitalmars-d
On 1/22/15 2:51 PM, H. S. Teoh via Digitalmars-d wrote: Wait, if R1 and R3, after the recursion, are sorted and contain only unique elements, doesn't that mean that we can just collapse R2 into a single element along with the endpoints of R1 and R3? So if R1[$-1] pivot, then we're done,

Re: sortUniq

2015-01-22 Thread Andrei Alexandrescu via Digitalmars-d
On 1/22/15 4:06 PM, bearophile wrote: Andrei Alexandrescu: What would be a better integrated version - one that does sorting and uniq in one shot? I suspect the combination could be quite a bit better than doing the two in sequence. What I need most is a groping by hashing, since years. A

Re: sortUniq

2015-01-22 Thread Andrei Alexandrescu via Digitalmars-d
On 1/22/15 3:52 PM, Vladimir Panteleev wrote: On Thursday, 22 January 2015 at 23:35:32 UTC, H. S. Teoh via Digitalmars-d wrote: On Thu, Jan 22, 2015 at 02:51:38PM -0800, H. S. Teoh via Digitalmars-d wrote: Wait, if R1 and R3, after the recursion, are sorted and contain only unique elements,

Re: sortUniq

2015-01-22 Thread bearophile via Digitalmars-d
Paul O'Neil: Is that different than groupBy? It works with hashing, and doesn't require a sorting, so it's different. On the other hand it's just few lines of code long, and it's quite easy to write... Bye, bearophile

Re: sortUniq

2015-01-22 Thread Xinok via Digitalmars-d
could be quite a bit better than doing the two in sequence. A few google searches didn't yield much. Ideas? Thanks, Andrei My thought is that uniq on a sorted list is only an O(n) operation, so it's not an expensive operation by any means. If there's to be any benefit to a sortUniq

Re: sortUniq

2015-01-22 Thread H. S. Teoh via Digitalmars-d
to a sortUniq, it has to be capable of reducing work incurred during the sorting process; else you're just going to end up with something less efficient. One solution may be RedBlackTree, which has an option to disallow duplicate elements. This container has three useful properties: The problem

Re: sortUniq

2015-01-22 Thread H. S. Teoh via Digitalmars-d
On Thu, Jan 22, 2015 at 04:10:45PM -0800, Andrei Alexandrescu via Digitalmars-d wrote: On 1/22/15 2:51 PM, H. S. Teoh via Digitalmars-d wrote: Wait, if R1 and R3, after the recursion, are sorted and contain only unique elements, doesn't that mean that we can just collapse R2 into a single

Re: sortUniq

2015-01-22 Thread Andrei Alexandrescu via Digitalmars-d
On 1/22/15 4:26 PM, H. S. Teoh via Digitalmars-d wrote: The question, though, is whether this hybrid recursion actually wins us anything, since heapsort is known to be somewhat slower than quicksort. In the worst case, you choose a poor pivot and get*both* the worst case of quicksort and the

Re: sortUniq

2015-01-22 Thread H. S. Teoh via Digitalmars-d
On Thu, Jan 22, 2015 at 04:33:28PM -0800, Andrei Alexandrescu via Digitalmars-d wrote: On 1/22/15 4:26 PM, H. S. Teoh via Digitalmars-d wrote: The question, though, is whether this hybrid recursion actually wins us anything, since heapsort is known to be somewhat slower than quicksort. In