Modify

Opened 8 years ago

Closed 6 years ago

Last modified 5 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));
}

Attachments (0)

Change History (27)

comment:1 Changed 8 years ago by Marshall Clow

Owner: set to Marshall Clow
Status: newassigned

comment:2 Changed 8 years ago by Marshall Clow

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 Changed 8 years ago by OlafvdSpek@…

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 Changed 8 years ago by OlafvdSpek@…

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

Should be:

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

comment:5 Changed 8 years ago by Marshall Clow

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 Changed 8 years ago by Olaf van der Spek <OlafvdSpek@…>

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 Changed 8 years ago by Marshall Clow

Updated in the sandbox - revision 54484.

comment:8 Changed 8 years ago by olafvdspek@…

Looks good.

comment:10 Changed 8 years ago by olafvdspek@…

Milestone: Boost 1.40.0Boost 1.41.0

To which library will this function be added?

comment:11 Changed 8 years ago by Marshall Clow

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

comment:12 Changed 7 years ago by anonymous

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 Changed 7 years ago by Olaf van der Spek

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?

comment:14 in reply to:  13 Changed 7 years ago by viboes

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 Changed 7 years ago by anonymous

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

comment:16 in reply to:  15 Changed 7 years ago by viboes

Replying to anonymous:

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

There is Library Submissions for that, I think.

comment:17 Changed 7 years ago by anonymous

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

comment:18 Changed 7 years ago by viboes

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

comment:19 Changed 7 years ago by anonymous

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

comment:20 Changed 7 years ago by Marshall Clow

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 Changed 7 years ago by sorokin@…

So, will this function be included in boost?

comment:23 Changed 6 years ago by Marshall Clow

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 Changed 6 years ago by Marshall Clow

Resolution: fixed
Status: reopenedclosed

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

comment:25 Changed 5 years ago by Olaf van der Spek <olafvdspek@…>

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

comment:26 Changed 5 years ago by Marshall Clow

boost::algorithm

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

comment:27 Changed 5 years ago by Olaf van der Spek <olafvdspek@…>

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

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Marshall Clow.
The resolution will be deleted.

Add Comment


E-mail address and name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.