Medial Code Documentation
Loading...
Searching...
No Matches
utils.h
Go to the documentation of this file.
1
7#ifndef RABIT_INTERNAL_UTILS_H_
8#define RABIT_INTERNAL_UTILS_H_
9
10#include <rabit/base.h>
11
12#include <cstdarg>
13#include <cstdio>
14#include <cstdlib>
15#include <cstring>
16#include <stdexcept>
17#include <string>
18#include <vector>
19
20#include "dmlc/io.h"
21#include "xgboost/logging.h"
22
23#if !defined(__GNUC__) || defined(__FreeBSD__)
24#define fopen64 std::fopen
25#endif // !defined(__GNUC__) || defined(__FreeBSD__)
26
27#ifndef _MSC_VER
28
29#ifdef _FILE_OFFSET_BITS
30#if _FILE_OFFSET_BITS == 32
31#pragma message("Warning: FILE OFFSET BITS defined to be 32 bit")
32#endif // _FILE_OFFSET_BITS == 32
33#endif // _FILE_OFFSET_BITS
34
35#ifdef __APPLE__
36#define off64_t off_t
37#define fopen64 std::fopen
38#endif // __APPLE__
39
40extern "C" {
41#include <sys/types.h>
42}
43#endif // _MSC_VER
44
45#include <cinttypes>
46
47namespace rabit {
49namespace utils {
50
52const int kPrintBuffer = 1 << 12;
53
54/* \brief Case-insensitive string comparison */
55inline int CompareStringsCaseInsensitive(const char* s1, const char* s2) {
56#ifdef _MSC_VER
57 return _stricmp(s1, s2);
58#else // _MSC_VER
59 return strcasecmp(s1, s2);
60#endif // _MSC_VER
61}
62
63/* \brief parse config string too bool*/
64inline bool StringToBool(const char* s) {
65 return CompareStringsCaseInsensitive(s, "true") == 0 || atoi(s) != 0;
66}
67
69inline void Printf(const char *fmt, ...) {
70 std::string msg(kPrintBuffer, '\0');
71 va_list args;
72 va_start(args, fmt);
73 vsnprintf(&msg[0], kPrintBuffer, fmt, args);
74 va_end(args);
75 LOG(CONSOLE) << msg;
76}
77
79inline void Assert(bool exp, const char *fmt, ...) {
80 if (!exp) {
81 std::string msg(kPrintBuffer, '\0');
82 va_list args;
83 va_start(args, fmt);
84 vsnprintf(&msg[0], kPrintBuffer, fmt, args);
85 va_end(args);
86 LOG(FATAL) << msg;
87 }
88}
89
91inline void Check(bool exp, const char *fmt, ...) {
92 if (!exp) {
93 std::string msg(kPrintBuffer, '\0');
94 va_list args;
95 va_start(args, fmt);
96 vsnprintf(&msg[0], kPrintBuffer, fmt, args);
97 va_end(args);
98 LOG(FATAL) << msg;
99 }
100}
101
103inline void Error(const char *fmt, ...) {
104 {
105 std::string msg(kPrintBuffer, '\0');
106 va_list args;
107 va_start(args, fmt);
108 vsnprintf(&msg[0], kPrintBuffer, fmt, args);
109 va_end(args);
110 LOG(FATAL) << msg;
111 }
112}
113} // namespace utils
114
115// Can not use std::min on Windows with msvc due to:
116// error C2589: '(': illegal token on right side of '::'
117template <typename T>
118auto Min(T const& l, T const& r) {
119 return l < r ? l : r;
120}
121// same with Min
122template <typename T>
123auto Max(T const& l, T const& r) {
124 return l > r ? l : r;
125}
126
127// easy utils that can be directly accessed in xgboost
129template<typename T>
130inline T *BeginPtr(std::vector<T> &vec) { // NOLINT(*)
131 if (vec.size() == 0) {
132 return nullptr;
133 } else {
134 return &vec[0];
135 }
136}
137inline char* BeginPtr(std::string &str) { // NOLINT(*)
138 if (str.length() == 0) return nullptr;
139 return &str[0];
140}
141inline const char* BeginPtr(const std::string &str) {
142 if (str.length() == 0) return nullptr;
143 return &str[0];
144}
145} // namespace rabit
146#endif // RABIT_INTERNAL_UTILS_H_
@ Max
"max" - take max on conflict
defines serializable interface of dmlc
defines console logging options for xgboost. Use to enforce unified print behavior.
T * BeginPtr(std::vector< T > &vec)
safely get the beginning address of a vector
Definition base.h:284
void Error(const char *fmt,...)
report error message, same as check
Definition utils.h:103
void Check(bool exp, const char *fmt,...)
same as assert, but this is intended to be used as a message for users
Definition utils.h:91
void Printf(const char *fmt,...)
printf, prints messages to the console
Definition utils.h:69
void Assert(bool exp, const char *fmt,...)
assert a condition is true, use this to handle debug information
Definition utils.h:79
const int kPrintBuffer
error message buffer length
Definition utils.h:52
namespace of rabit
Definition engine.h:18
Macros common to all headers.