#3215 closed Feature Requests (fixed)
Clamp function
Reported by: | Owned by: | Marshall Clow | |
---|---|---|---|
Milestone: | Boost 1.50.0 | Component: | algorithm |
Version: | Boost 1.39.0 | Severity: | Problem |
Keywords: | Cc: |
Description
A clamp function comes in handy in certain cases. Could one be added to Boost? I'm not sure which component would be most appropriate.
The function may be something like this:
template<class T> T clamp(T v, T a, T b) { return max(a, min(v, b)); }
Change History (27)
comment:1 by , 13 years ago
Owner: | set to |
---|---|
Status: | new → assigned |
comment:2 by , 13 years ago
comment:3 by , 13 years ago
Yes, except you put the value at the back instead of at the front, I'm not sure that's preferable.
Would it be better to use separate templated types for lo and hi to avoid the issues with std::max and std::min? The return type is clear in this case, the type of the original value.
comment:4 by , 13 years ago
return val < lo ? lo : val < hi ? val : hi;
Should be:
return val < lo ? lo : hi < val ? hi : val;
comment:5 by , 13 years ago
I can see putting the value first, that's no problem.
However, I don't see the difference in the two code snippets. When hi == val, the first one returns hi, and the second one returns val. When comparing objects, the first calls val.operator< ( hi ), the second hi.operator< ( val ). So?
comment:6 by , 13 years ago
There is a difference when hi and val are equivalent but not equal. It's not an issue for simple types but it might be an issue in other cases.
comment:10 by , 13 years ago
Milestone: | Boost 1.40.0 → Boost 1.41.0 |
---|
To which library will this function be added?
comment:12 by , 12 years ago
Resolution: | → invalid |
---|---|
Status: | assigned → closed |
This kind of request must be sent to the Boost ML, not the Trac.
I propose to close this ticket as this is more related to new features to the forthcoming Algorithm library.
follow-up: 14 comment:13 by , 12 years ago
Resolution: | invalid |
---|---|
Status: | closed → reopened |
I propose to close this ticket
Proposing isn't the same as doing... Why don't you reassign to the algorithm lib?
comment:14 by , 12 years ago
Replying to Olaf van der Spek:
I propose to close this ticket
Proposing isn't the same as doing... Why don't you reassign to the algorithm lib?
There is no Algorithm library. So no need to make defects on libraries that doesn't exists. If you can not assign a component, maybe you should close the ticket yourself.
comment:16 by , 12 years ago
Replying to anonymous:
It's a feature request, not a bug report.
There is Library Submissions for that, I think.
comment:18 by , 12 years ago
It is not useful to make a request that nobody can handle :) Please close it yourself.
comment:20 by , 12 years ago
The reason that I assigned this ticket to myself is that I have implemented this in the (proposed) Boost.Algorithms library, which I will post a version 0.1 on the man list "REAL SOON NOW".
comment:23 by , 11 years ago
Component: | None → algorithm |
---|---|
Milestone: | Boost 1.41.0 → Boost 1.50.0 |
Checked into trunk in [76388]. Will close ticket when merged to release (which will be for 1.50 release)
comment:24 by , 10 years ago
Resolution: | → fixed |
---|---|
Status: | reopened → closed |
comment:25 by , 10 years ago
Is this supposed to be available in namespace boost or just in boost::algorithm?
comment:26 by , 10 years ago
boost::algorithm
All the algorithms in Boost.Algorithm are in that namespace.
I just added "sandbox/boost/algorithm/clamp.hpp" Tests in "sandbox/libs/algorithm/clamp/test".
Let me know if that's what you wanted.