CMMTree v1.0
utils.hpp
Go to the documentation of this file.
1#ifndef _utils4_3_5_h_
2#define _utils4_3_5_h_
3//---------------------------------------------
4#include<iostream>
5#include<sstream>
6#include<thread>
7#include<mutex>
8#include<chrono>
9#include<vector>
10
11using namespace std::chrono_literals;
12
13#ifdef _WIN32 // Includes both 32 bit and 64 bit
14 #include <windows.h>
15 #define BLACK 0
16 #define BBLACK 8
17 #define RED 4
18 #define BRED 12
19 #define GREEN 2
20 #define BGREEN 10
21 #define YELLOW 6
22 #define BYELLOW 14
23 #define BLUE 1
24 #define BBLUE 9
25 #define MAGENTA 5
26 #define BMAGENTA 13
27 #define CYAN 3
28 #define BCYAN 11
29 #define WHITE 7
30 #define BWHITE 15
31 #define DEFAULT 7
32
33 inline
34 void SetColor(int color) {
35 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
36 SetConsoleTextAttribute(hConsole, WORD(color) );
37 }
38
39#elif defined(__linux__) || defined(__APPLE__)
40
41 #define BLACK "\033[0;30m"
42 #define BBLACK "\033[1;30m"
43 #define RED "\033[0;31m"
44 #define BRED "\033[1;31m"
45 #define GREEN "\033[0;32m"
46 #define BGREEN "\033[1;32m"
47 #define YELLOW "\033[0;33m"
48 #define BYELLOW "\033[1;33m"
49 #define BLUE "\033[0;34m"
50 #define BBLUE "\033[1;34m"
51 #define MAGENTA "\033[0;35m"
52 #define BMAGENTA "\033[1;35m"
53 #define CYAN "\033[0;36m"
54 #define BCYAN "\033[1;36m"
55 #define WHITE "\033[0;37m"
56 #define BWHITE "\033[1;37m"
57 #define DEFAULT "\033[0m"
58
59 inline
60 void SetColor(const std::string& color) { std::cout << color; }
61#endif
62
63#ifdef __APPLE__
64 class scoped_thr{
65 std::thread t;
66 public:
67 explicit scoped_thr(std::thread&& t_): t(std::move(t_)){
68 if(!t.joinable())
69 throw std::logic_error("No thread");
70 }
71 ~scoped_thr() {t.join(); }
72 scoped_thr()=delete;
73 scoped_thr(scoped_thr const&)=delete;
74 scoped_thr& operator=(scoped_thr const&)=delete;
75 };
76 template<class T>
77 void println(const T& a) { std::cout << a << std::endl; }
78 template<class T>
79 void print(const T& a) { std::cout << a; }
80#else
81 constexpr void println(auto const& a) { std::cout << a << std::endl; }
82 constexpr void print(auto const& a) { std::cout << a; }
83#endif
84
85inline
86void time_info(const long long& time, std::ostream& out){
87 out << "The time of the CMMTree calculations : " << time << " [ µs ] = " << (0.001*(double)time) << " [ ms ]\n";
88}
89
90inline
91void time_info(const std::string& msg, const unsigned long& time, std::ostream& out){
92 out << msg << time << " [ µs ] = " << (0.001*(double)time) << " [ ms ]\n";
93}
94class mt_out : public std::stringstream
95{
96 std::ostream &out;
97
98public:
99 static inline std::mutex out_mutex;
100 mt_out(std::ostream &r = std::cout) : out(r)
101 {
102 }
104 {
105 std::lock_guard<std::mutex> mt{out_mutex};
106 out << rdbuf();
107 out.flush();
108 }
109};
110
111template<class T>
113 std::vector<T> v;
114public:
118
119 ptr_vector_guard(std::vector<T>&& a):v{ std::move(a) }{}
120
121 ptr_vector_guard(ptr_vector_guard<T>&& other):v{ std::move(other.v) }{}
123 for( auto&& r : v )
124 delete r;
125 v = std::move(other.v);
126 return *this;
127 }
128
129 ~ptr_vector_guard(){ for( auto&& r : v ) delete r; }
130 std::vector<T> & vec() { return v;}
131 std::vector<T> const& vec()const { return v;}
132};
133
134//---------------------------------------------
135#endif
136
~mt_out()
Definition utils.hpp:103
static std::mutex out_mutex
Definition utils.hpp:99
mt_out(std::ostream &r=std::cout)
Definition utils.hpp:100
ptr_vector_guard()=delete
ptr_vector_guard(const ptr_vector_guard &)=delete
ptr_vector_guard(std::vector< T > &&a)
Definition utils.hpp:119
ptr_vector_guard & operator=(ptr_vector_guard< T > &&other) noexcept
Definition utils.hpp:122
ptr_vector_guard & operator=(const ptr_vector_guard &)=delete
ptr_vector_guard(ptr_vector_guard< T > &&other)
Definition utils.hpp:121
std::vector< T > const & vec() const
Definition utils.hpp:131
std::vector< T > & vec()
Definition utils.hpp:130
constexpr void print(auto const &a)
Definition utils.hpp:82
void time_info(const long long &time, std::ostream &out)
Definition utils.hpp:86
constexpr void println(auto const &a)
Definition utils.hpp:81