TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
mathutils.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <algorithm>
11 #include <cassert>
12 
13 namespace common {
14 
15 template <class T, class Compare>
16 constexpr const T& clamp(const T& v, const T& lo, const T& hi, Compare comp) {
17  return assert(!comp(hi, lo)), comp(v, lo) ? lo : comp(hi, v) ? hi : v;
18 }
19 
20 template <class T>
21 constexpr const T& clamp(const T& v, const T& lo, const T& hi) {
22  return common::clamp(v, lo, hi, std::less<>());
23 }
24 
25 template <class T>
26 constexpr const T& safeClamp(const T& v1, const T& v2, const T& v3) {
27  const T& min = std::min(v2, v3);
28  const T& max = std::max(v2, v3);
29  return std::min(max, std::max(min, v1));
30 }
31 
32 } // namespace common
constexpr const T & clamp(const T &v, const T &lo, const T &hi, Compare comp)
Definition: mathutils.h:16
constexpr const T & safeClamp(const T &v1, const T &v2, const T &v3)
Definition: mathutils.h:26
General utilities.
Definition: assert.cpp:7