wxMaxima
Loading...
Searching...
No Matches
clamp.hpp
1// -*- mode: c++; c-file-style: "linux"; c-basic-offset: 2; indent-tabs-mode: nil -*-
2//
3// Copyright (C) 2020 Kuba Ober <kuba@mareimbrium.org>
4//
5// Use, modification, and distribution is subject to the Boost Software
6// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7// http://www.boost.org/LICENSE_1_0.txt)
8//
9// SPDX-License-Identifier: BSL-1.0
10
11#ifndef WXMAXIMA_UTILS_CLAMP_H
12#define WXMAXIMA_UTILS_CLAMP_H
13
14#if __cplusplus >= 201703L
15
16#include <algorithm>
17
18namespace stx {
19
20using std::clamp;
21
22}
23
24#else
25
26#include <cassert>
27
28namespace stx {
29
30template <class T>
31constexpr const T &clamp(const T &val, const T &min, const T &max)
32{
33 assert(!(max < min));
34 return (val < min) ? min : (max < val) ? max : val;
35}
36
37template <class T, class Compare>
38constexpr const T &clamp(const T &val, const T &min, const T &max, Compare comp)
39{
40 assert(!comp(max, min));
41 return comp(val, min) ? min : comp(max, val) ? max : val;
42}
43
44}
45
46#endif // __cplusplus < 201703L
47
48#endif