Scippy

SCIP

Solving Constraint Integer Programs

global_functions.h
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program PolySCIP */
4 /* */
5 /* Copyright (C) 2012-2019 Konrad-Zuse-Zentrum */
6 /* fuer Informationstechnik Berlin */
7 /* */
8 /* PolySCIP is distributed under the terms of the ZIB Academic License. */
9 /* */
10 /* You should have received a copy of the ZIB Academic License */
11 /* along with PolySCIP; see the file LICENCE. */
12 /* */
13 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
14 
15 /**
16  * @file global_functions.h
17  * @brief Global helper functions
18  * @author Sebastian Schenker
19  *
20  */
21 
22 #ifndef POLYSCIP_SRC_GLOBAL_FUNCTIONS_H_INCLUDED
23 #define POLYSCIP_SRC_GLOBAL_FUNCTIONS_H_INCLUDED
24 
25 #include <functional>
26 #include <iomanip>
27 #include <iostream>
28 #include <ostream>
29 #include <memory>
30 #include <stdexcept>
31 #include <type_traits>
32 #include <utility>
33 
34 namespace polyscip {
35 
36  namespace global {
37 
38  /**
39  * Based on code by Stephan T. Lavavej at http://channel9.msdn.com/Series/
40  * C9-Lectures-Stephan-T-Lavavej-Core-C-/STLCCSeries6
41  */
42  namespace impl_own_stl {
43  /**
44  * Helper function
45  * @tparam T
46  * @tparam Args
47  * @param args
48  * @return
49  */
50  template<typename T, typename ... Args>
51  std::unique_ptr<T> make_unique_helper(std::false_type, Args&&... args) {
52  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
53  }
54 
55  /**
56  * Helper function
57  * @tparam T
58  * @tparam Args
59  * @param args
60  * @return
61  */
62  template<typename T, typename ... Args>
63  std::unique_ptr<T> make_unique_helper(std::true_type, Args&&... args) {
64  static_assert(std::extent<T>::value == 0,
65  "make_unique<T[N]>() is forbidden, please use make_unique<T[]>(),");
66  typedef typename std::remove_extent<T>::type U;
67  return std::unique_ptr<T>(new U[sizeof...(Args)]{std::forward<Args>(args)...});
68  }
69  }
70 
71  /**
72  * @details std::make_unique did not get into the C++11 standard, so we provide it ourselves
73  * until installed compiler can be expected to fully support C++14
74  * @tparam T
75  * @tparam Args
76  * @param args
77  * @return
78  */
79  template<typename T, typename ... Args>
80  std::unique_ptr<T> make_unique(Args&&... args) {
81  return impl_own_stl::make_unique_helper<T>(
82  std::is_array<T>(),std::forward<Args>(args)... );
83  }
84 
85  /**
86  * Print function
87  * @param container Container to be printed
88  * @param prefix String printed before Container
89  * @param suffix String printed after Container
90  * @param os Output stream to write to
91  * @param negate Indicates whether elements in Container shall be negated
92  * @param prec Used precision for output stream
93  */
94  template<typename Container>
95  void print(const Container &container,
96  const std::string& prefix = "",
97  const std::string& suffix = "",
98  std::ostream &os = std::cout,
99  bool negate = false,
100  int prec = 6) {
101  os << std::setprecision(prec) << prefix;
102  for (const auto &elem : container)
103  if (negate) {
104  os << -elem << " ";
105  }
106  else {
107  os << elem << " ";
108  }
109  os << suffix;
110  }
111 
112  /**
113  * For conversion between two scalar numeric types where a value might be narrowed.
114  * Taken from Stroustroup - "The C++ programming language" 4th edition, page 299
115  * @param v Value to convert
116  * @return Value in narrowed type
117  */
118  template<typename Target, typename Source>
119  Target narrow_cast(Source v) {
120  auto r = static_cast<Target>(v); // convert the value to the target type
121  if (static_cast<Source>(r)!=v)
122  throw std::runtime_error("narrow_cast<>() failed\n");
123  return r;
124  }
125 
126  }
127 }
128 #endif //POLYSCIP_SRC_GLOBAL_FUNCTIONS_H_INCLUDED
static SCIP_Real negate(SCIP_Real x)
Target narrow_cast(Source v)
std::unique_ptr< T > make_unique(Args &&... args)
std::unique_ptr< T > make_unique_helper(std::false_type, Args &&... args)
void print(const Container &container, const std::string &prefix="", const std::string &suffix="", std::ostream &os=std::cout, bool negate=false, int prec=6)
SCIP_Real * r
Definition: circlepacking.c:50