Modify

Ticket #3215 (closed Feature Requests: fixed)

Opened 5 years ago

Last modified 22 months ago

Clamp function

Reported by: olafvdspek@… Owned by: marshall
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

Change History

comment:1 Changed 5 years ago by marshall

  • Owner set to marshall
  • Status changed from new to assigned

comment:2 Changed 5 years ago by marshall

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 5 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 5 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 5 years ago by marshall

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 5 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 5 years ago by marshall

Updated in the sandbox - revision 54484.

comment:8 Changed 5 years ago by olafvdspek@…

Looks good.

comment:10 Changed 5 years ago by olafvdspek@…

  • Milestone changed from Boost 1.40.0 to Boost 1.41.0

To which library will this function be added?

comment:11 Changed 5 years ago by marshall

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

comment:12 Changed 4 years ago by anonymous

  • Status changed from assigned to closed
  • Resolution set to invalid

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 follow-up: ↓ 14 Changed 4 years ago by Olaf van der Spek

  • Status changed from closed to reopened
  • Resolution invalid deleted

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 4 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 follow-up: ↓ 16 Changed 4 years ago by anonymous

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

comment:16 in reply to: ↑ 15 Changed 4 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 4 years ago by anonymous

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

comment:18 Changed 3 years ago by viboes

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

comment:19 Changed 3 years ago by anonymous

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

comment:20 Changed 3 years ago by marshall

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

So, will this function be included in boost?

comment:23 Changed 2 years ago by marshall

  • Component changed from None to algorithm
  • Milestone changed from Boost 1.41.0 to 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 Changed 2 years ago by marshall

  • Status changed from reopened to closed
  • Resolution set to fixed

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

comment:25 Changed 22 months ago by Olaf van der Spek <olafvdspek@…>

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

comment:26 Changed 22 months ago by marshall

boost::algorithm

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

comment:27 Changed 22 months ago by Olaf van der Spek <olafvdspek@…>

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

View

Add a comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
The resolution will be deleted. Next status will be 'reopened'
Author


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

 
Note: See TracTickets for help on using tickets.