Conversation
|
@JackStouffer Great! But it is a little bit more complicated.
|
Originally, there was a
I suppose you could for ranges that have a odd length, but unless I am missing something, that would require that function to throw if it received an even length function. Which is fine, as only that overload would not be marked as
Thanks for the links. |
|
Does anyone know why the documentation for |
|
private? |
|
Ha, yes, there was a |
Ok, I've run into a problem. I don't see a way of doing that algorithm without allocation. After the pivot is found, the two sub ranges can be constructed via Here is what I have so far. |
|
@JackStouffer luckily, since you are using SortedRange, you can use lowerBound and upprerBound, instead of filter. See: http://dlang.org/phobos/std_range#.SortedRange |
The last variant is for an unsorted range. |
|
Sorry, I've missed that. If you need a RandomAccessRange partition from an unsorted range, then yes, you'll need to use dynamic allocation, unless you can make a median function that doesn't need random access. |
|
I'm going to leave the median of medians algorithm out for now, as it's frowned upon to add more allocating code into Phobos when it can just wait for std.allocator to be moved out of experimental and be done properly. If this gets merged, I'll add a bugzilla report noting that it should be added in later. |
88176bc to
7b741f2
Compare
Thinking about this more, does it really make sense? What kind user defined types that don't have these operators are going to be used in a range which someone wants to take the median of? IMO I think it's reasonable to restrict this function to user types that are like numbers, and then we can assume that if someone is making a type to represent a numerical value, then they are going to have addition and division defined for them. |
|
Honest this looks a bit overkill. I don't see much use for these functions. |
|
@andralex I disagree, I believe that the standard library should have, at least, the following basic stats functions:
I wrote the functions this way because I don't believe that a median function should be mutating because the most common way to find the median is to sort the array and then find the middle result, so instead of making that implicit in the function, I made it explicit. |
|
All told I see 213 lines with a poor work/scaffolding ratio. The algorithm implemented is essentially "For empty ranges median doesn't make sense. For others, go to the kth position in the range, and if it has odd elements pick that guy, otherwise pick the average of the two adjacent guys." I think it's unusual to compute the median for a sorted non-random range in O(n). Computing the median of unsorted ranges is real work. There's algorithms, analyses, papers written about it etc. etc. Taking the median of an already sorted range is trivial - just go there and pick it up. I agree "picking up" does have a few details to be minded, but all in all this doesn't make the cut for inclusion in the standard library. |
Now that #2991 has been closed, I am reintroducing this function. I believe std.numeric to be a better fit than std.algorithm.iteration, so I moved it.
See #3680 for previous discussion