Opened 13 years ago

Closed 10 years ago

Last modified 10 years ago

#3215 closed Feature Requests (fixed)

Clamp function

Reported by: olafvdspek@… 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 Marshall Clow, 13 years ago

Owner: set to Marshall Clow
Status: newassigned

comment:2 by Marshall Clow, 13 years ago

I just added "sandbox/boost/algorithm/clamp.hpp" Tests in "sandbox/libs/algorithm/clamp/test".

Let me know if that's what you wanted.

comment:3 by OlafvdSpek@…, 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 OlafvdSpek@…, 13 years ago

return val < lo ? lo : val < hi ? val : hi;

Should be:

return val < lo ? lo : hi < val ? hi : val;

comment:5 by Marshall Clow, 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 Olaf van der Spek <OlafvdSpek@…>, 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:7 by Marshall Clow, 13 years ago

Updated in the sandbox - revision 54484.

comment:8 by olafvdspek@…, 13 years ago

Looks good.

comment:10 by olafvdspek@…, 13 years ago

Milestone: Boost 1.40.0Boost 1.41.0

To which library will this function be added?

comment:11 by Marshall Clow, 13 years ago

Boost.Algorithm; once it (the library) is reviewed and accepted.

comment:12 by anonymous, 12 years ago

Resolution: invalid
Status: assignedclosed

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.

comment:13 by Olaf van der Spek, 12 years ago

Resolution: invalid
Status: closedreopened

I propose to close this ticket

Proposing isn't the same as doing... Why don't you reassign to the algorithm lib?

in reply to:  13 comment:14 by viboes, 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:15 by anonymous, 12 years ago

It's a feature request, not a bug report.

in reply to:  15 comment:16 by viboes, 12 years ago

Replying to anonymous:

It's a feature request, not a bug report.

There is Library Submissions for that, I think.

comment:17 by anonymous, 12 years ago

No, that's for submitting a library, not for requesting a function.

comment:18 by viboes, 12 years ago

It is not useful to make a request that nobody can handle :) Please close it yourself.

comment:19 by anonymous, 12 years ago

marshall appears to be handling it. What's the problem?

comment:20 by Marshall Clow, 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:21 by sorokin@…, 12 years ago

So, will this function be included in boost?

comment:23 by Marshall Clow, 11 years ago

Component: Nonealgorithm
Milestone: Boost 1.41.0Boost 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 Marshall Clow, 10 years ago

Resolution: fixed
Status: reopenedclosed

(In [78557]) Merged changes for Boost.Algorithm to release; Fixes #6596; Fixes #6689; Fixes #3215; Fixes #6840

comment:25 by Olaf van der Spek <olafvdspek@…>, 10 years ago

Is this supposed to be available in namespace boost or just in boost::algorithm?

comment:26 by Marshall Clow, 10 years ago

boost::algorithm

All the algorithms in Boost.Algorithm are in that namespace.

comment:27 by Olaf van der Spek <olafvdspek@…>, 10 years ago

Why's that? String functions, like to_upper, are in boost itself.

Note: See TracTickets for help on using tickets.