rcpputils
master
C++ API providing common utilities and data structures.
include
rcppmath
clamp.hpp
Go to the documentation of this file.
1
// Copyright 2020 PAL Robotics S.L.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
19
#ifndef RCPPMATH__CLAMP_HPP_
20
#define RCPPMATH__CLAMP_HPP_
21
22
#include <cassert>
23
24
namespace
rcppmath
25
{
27
// than v, returns hi; otherwise returns v. Uses operator< to compare the values
38
template
<
class
T>
39
constexpr
const
T &
clamp
(
const
T & v,
const
T & lo,
const
T & hi)
40
{
41
assert(!(hi < lo) );
42
return
(v < lo) ? lo : (hi < v) ? hi : v;
43
}
44
46
50
template
<
class
T,
class
Compare>
51
constexpr
const
T &
clamp
(
const
T & v,
const
T & lo,
const
T & hi, Compare comp)
52
{
53
assert(!comp(hi, lo) );
54
return
comp(v, lo) ? lo : comp(hi, v) ? hi : v;
55
}
56
57
}
// namespace rcppmath
58
59
#endif // RCPPMATH__CLAMP_HPP_
rcppmath
Definition:
clamp.hpp:24
rcppmath::clamp
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
If v compares less than lo, returns lo; otherwise if hi compares less.
Definition:
clamp.hpp:39
Generated by
1.8.17