common.h 33 KB
Newer Older
1
2
3
4
/*!
 * Copyright (c) 2016 Microsoft Corporation. All rights reserved.
 * Licensed under the MIT License. See LICENSE file in the project root for license information.
 */
5
6
#ifndef LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_COMMON_H_
#define LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_COMMON_H_
Guolin Ke's avatar
Guolin Ke committed
7

8
#include <LightGBM/utils/json11.h>
9
10
11
#include <LightGBM/utils/log.h>
#include <LightGBM/utils/openmp_wrapper.h>

12
#include <limits>
Guolin Ke's avatar
Guolin Ke committed
13
#include <string>
14
#include <algorithm>
15
#include <chrono>
16
#include <cmath>
17
18
#include <cstdint>
#include <cstdio>
Chen Yufei's avatar
Chen Yufei committed
19
#include <cstdlib>
20
#include <cstring>
Guolin Ke's avatar
Guolin Ke committed
21
#include <functional>
22
#include <iomanip>
23
#include <iterator>
24
#include <map>
25
26
#include <memory>
#include <sstream>
Guolin Ke's avatar
Guolin Ke committed
27
#include <type_traits>
28
#include <unordered_map>
29
30
#include <utility>
#include <vector>
Guolin Ke's avatar
Guolin Ke committed
31

32
#define FMT_HEADER_ONLY
33
34
#include "fast_double_parser.h"
#include "fmt/format.h"
35

36
37
38
39
40
#ifdef _MSC_VER
#include <intrin.h>
#pragma intrinsic(_BitScanReverse)
#endif

41
#if defined(_MSC_VER)
42
43
44
#include <malloc.h>
#elif MM_MALLOC
#include <mm_malloc.h>
45
46
47
48
49
50
// https://gcc.gnu.org/onlinedocs/cpp/Common-Predefined-Macros.html
// https://www.oreilly.com/library/view/mac-os-x/0596003560/ch05s01s02.html
#elif defined(__GNUC__) && defined(HAVE_MALLOC_H)
  #include <malloc.h>
  #define _mm_malloc(a, b) memalign(b, a)
  #define _mm_free(a) free(a)
51
52
53
54
#else
#include <stdlib.h>
#define _mm_malloc(a, b) malloc(a)
#define _mm_free(a) free(a)
55
56
#endif

Guolin Ke's avatar
Guolin Ke committed
57
58
59
60
namespace LightGBM {

namespace Common {

61
using json11_internal_lightgbm::Json;
62

63
64
65
66
67
68
69
/*!
* Imbues the stream with the C locale.
*/
static void C_stringstream(std::stringstream &ss) {
  ss.imbue(std::locale::classic());
}

70
inline static char tolower(char in) {
Guolin Ke's avatar
Guolin Ke committed
71
72
73
74
75
  if (in <= 'Z' && in >= 'A')
    return in - ('Z' - 'z');
  return in;
}

76
inline static std::string Trim(std::string str) {
Guolin Ke's avatar
Guolin Ke committed
77
  if (str.empty()) {
Guolin Ke's avatar
Guolin Ke committed
78
79
80
81
82
83
84
    return str;
  }
  str.erase(str.find_last_not_of(" \f\n\r\t\v") + 1);
  str.erase(0, str.find_first_not_of(" \f\n\r\t\v"));
  return str;
}

85
inline static std::string RemoveQuotationSymbol(std::string str) {
Guolin Ke's avatar
Guolin Ke committed
86
  if (str.empty()) {
87
88
89
90
91
92
    return str;
  }
  str.erase(str.find_last_not_of("'\"") + 1);
  str.erase(0, str.find_first_not_of("'\""));
  return str;
}
Guolin Ke's avatar
Guolin Ke committed
93

Guolin Ke's avatar
Guolin Ke committed
94
95
96
97
98
99
100
inline static bool StartsWith(const std::string& str, const std::string prefix) {
  if (str.substr(0, prefix.size()) == prefix) {
    return true;
  } else {
    return false;
  }
}
Guolin Ke's avatar
Guolin Ke committed
101

Guolin Ke's avatar
Guolin Ke committed
102
inline static std::vector<std::string> Split(const char* c_str, char delimiter) {
Guolin Ke's avatar
Guolin Ke committed
103
  std::vector<std::string> ret;
Guolin Ke's avatar
Guolin Ke committed
104
105
  std::string str(c_str);
  size_t i = 0;
Guolin Ke's avatar
Guolin Ke committed
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  size_t pos = 0;
  while (pos < str.length()) {
    if (str[pos] == delimiter) {
      if (i < pos) {
        ret.push_back(str.substr(i, pos - i));
      }
      ++pos;
      i = pos;
    } else {
      ++pos;
    }
  }
  if (i < pos) {
    ret.push_back(str.substr(i));
  }
  return ret;
}

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
inline static std::vector<std::string> SplitBrackets(const char* c_str, char left_delimiter, char right_delimiter) {
  std::vector<std::string> ret;
  std::string str(c_str);
  size_t i = 0;
  size_t pos = 0;
  bool open = false;
  while (pos < str.length()) {
    if (str[pos] == left_delimiter) {
      open = true;
      ++pos;
      i = pos;
    } else if (str[pos] == right_delimiter && open) {
      if (i < pos) {
        ret.push_back(str.substr(i, pos - i));
      }
      open = false;
      ++pos;
    } else {
      ++pos;
    }
  }
  return ret;
}

Guolin Ke's avatar
Guolin Ke committed
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
inline static std::vector<std::string> SplitLines(const char* c_str) {
  std::vector<std::string> ret;
  std::string str(c_str);
  size_t i = 0;
  size_t pos = 0;
  while (pos < str.length()) {
    if (str[pos] == '\n' || str[pos] == '\r') {
      if (i < pos) {
        ret.push_back(str.substr(i, pos - i));
      }
      // skip the line endings
      while (str[pos] == '\n' || str[pos] == '\r') ++pos;
      // new begin
      i = pos;
    } else {
      ++pos;
    }
  }
  if (i < pos) {
    ret.push_back(str.substr(i));
Guolin Ke's avatar
Guolin Ke committed
168
169
170
171
  }
  return ret;
}

Guolin Ke's avatar
Guolin Ke committed
172
173
174
175
inline static std::vector<std::string> Split(const char* c_str, const char* delimiters) {
  std::vector<std::string> ret;
  std::string str(c_str);
  size_t i = 0;
Guolin Ke's avatar
Guolin Ke committed
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
  size_t pos = 0;
  while (pos < str.length()) {
    bool met_delimiters = false;
    for (int j = 0; delimiters[j] != '\0'; ++j) {
      if (str[pos] == delimiters[j]) {
        met_delimiters = true;
        break;
      }
    }
    if (met_delimiters) {
      if (i < pos) {
        ret.push_back(str.substr(i, pos - i));
      }
      ++pos;
      i = pos;
    } else {
      ++pos;
    }
  }
  if (i < pos) {
    ret.push_back(str.substr(i));
Guolin Ke's avatar
Guolin Ke committed
197
198
199
200
  }
  return ret;
}

201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
inline static std::string GetFromParserConfig(std::string config_str, std::string key) {
  // parser config should follow json format.
  std::string err;
  Json config_json = Json::parse(config_str, &err);
  if (!err.empty()) {
    Log::Fatal("Invalid parser config: %s. Please check if follow json format.", err.c_str());
  }
  return config_json[key].string_value();
}

inline static std::string SaveToParserConfig(std::string config_str, std::string key, std::string value) {
  std::string err;
  Json config_json = Json::parse(config_str, &err);
  if (!err.empty()) {
    Log::Fatal("Invalid parser config: %s. Please check if follow json format.", err.c_str());
  }
  CHECK(config_json.is_object());
  std::map<std::string, Json> config_map = config_json.object_items();
  config_map.insert(std::pair<std::string, Json>(key, Json(value)));
  return Json(config_map).dump();
}

223
224
225
226
template<typename T>
inline static const char* Atoi(const char* p, T* out) {
  int sign;
  T value;
Guolin Ke's avatar
Guolin Ke committed
227
228
229
230
231
232
233
  while (*p == ' ') {
    ++p;
  }
  sign = 1;
  if (*p == '-') {
    sign = -1;
    ++p;
234
  } else if (*p == '+') {
Guolin Ke's avatar
Guolin Ke committed
235
236
237
238
239
    ++p;
  }
  for (value = 0; *p >= '0' && *p <= '9'; ++p) {
    value = value * 10 + (*p - '0');
  }
240
  *out = static_cast<T>(sign * value);
Guolin Ke's avatar
Guolin Ke committed
241
242
243
244
245
246
  while (*p == ' ') {
    ++p;
  }
  return p;
}

247
template<typename T>
248
249
250
251
252
253
254
255
256
257
258
259
260
261
inline static double Pow(T base, int power) {
  if (power < 0) {
    return 1.0 / Pow(base, -power);
  } else if (power == 0) {
    return 1;
  } else if (power % 2 == 0) {
    return Pow(base*base, power / 2);
  } else if (power % 3 == 0) {
    return Pow(base*base*base, power / 3);
  } else {
    return base * Pow(base, power - 1);
  }
}

262
inline static const char* Atof(const char* p, double* out) {
Guolin Ke's avatar
Guolin Ke committed
263
  int frac;
264
  double sign, value, scale;
Guolin Ke's avatar
Guolin Ke committed
265
  *out = NAN;
Guolin Ke's avatar
Guolin Ke committed
266
267
268
269
270
  // Skip leading white space, if any.
  while (*p == ' ') {
    ++p;
  }
  // Get sign, if any.
271
  sign = 1.0;
Guolin Ke's avatar
Guolin Ke committed
272
  if (*p == '-') {
273
    sign = -1.0;
Guolin Ke's avatar
Guolin Ke committed
274
    ++p;
275
  } else if (*p == '+') {
Guolin Ke's avatar
Guolin Ke committed
276
277
278
    ++p;
  }

Guolin Ke's avatar
Guolin Ke committed
279
280
281
  // is a number
  if ((*p >= '0' && *p <= '9') || *p == '.' || *p == 'e' || *p == 'E') {
    // Get digits before decimal point or exponent, if any.
282
283
    for (value = 0.0; *p >= '0' && *p <= '9'; ++p) {
      value = value * 10.0 + (*p - '0');
Guolin Ke's avatar
Guolin Ke committed
284
    }
Guolin Ke's avatar
Guolin Ke committed
285

Guolin Ke's avatar
Guolin Ke committed
286
287
    // Get digits after decimal point, if any.
    if (*p == '.') {
288
289
      double right = 0.0;
      int nn = 0;
Guolin Ke's avatar
Guolin Ke committed
290
      ++p;
Guolin Ke's avatar
Guolin Ke committed
291
      while (*p >= '0' && *p <= '9') {
292
293
        right = (*p - '0') + right * 10.0;
        ++nn;
Guolin Ke's avatar
Guolin Ke committed
294
295
        ++p;
      }
296
      value += right / Pow(10.0, nn);
Guolin Ke's avatar
Guolin Ke committed
297
298
    }

Guolin Ke's avatar
Guolin Ke committed
299
300
    // Handle exponent, if any.
    frac = 0;
301
    scale = 1.0;
Guolin Ke's avatar
Guolin Ke committed
302
    if ((*p == 'e') || (*p == 'E')) {
Guolin Ke's avatar
Guolin Ke committed
303
      uint32_t expon;
Guolin Ke's avatar
Guolin Ke committed
304
      // Get sign of exponent, if any.
Guolin Ke's avatar
Guolin Ke committed
305
      ++p;
Guolin Ke's avatar
Guolin Ke committed
306
307
308
309
310
311
312
313
314
315
      if (*p == '-') {
        frac = 1;
        ++p;
      } else if (*p == '+') {
        ++p;
      }
      // Get digits of exponent, if any.
      for (expon = 0; *p >= '0' && *p <= '9'; ++p) {
        expon = expon * 10 + (*p - '0');
      }
316
317
      if (expon > 308) expon = 308;
      // Calculate scaling factor.
318
319
320
321
322
323
324
325
326
327
328
329
      while (expon >= 50) {
        scale *= 1E50;
        expon -= 50;
      }
      while (expon >= 8) {
        scale *= 1E8;
        expon -= 8;
      }
      while (expon > 0) {
        scale *= 10.0;
        expon -= 1;
      }
Guolin Ke's avatar
Guolin Ke committed
330
    }
Guolin Ke's avatar
Guolin Ke committed
331
332
333
    // Return signed and scaled floating point result.
    *out = sign * (frac ? (value / scale) : (value * scale));
  } else {
334
    size_t cnt = 0;
335
    while (*(p + cnt) != '\0' && *(p + cnt) != ' '
336
337
338
           && *(p + cnt) != '\t' && *(p + cnt) != ','
           && *(p + cnt) != '\n' && *(p + cnt) != '\r'
           && *(p + cnt) != ':') {
339
340
      ++cnt;
    }
341
    if (cnt > 0) {
Guolin Ke's avatar
Guolin Ke committed
342
      std::string tmp_str(p, cnt);
Guolin Ke's avatar
Guolin Ke committed
343
      std::transform(tmp_str.begin(), tmp_str.end(), tmp_str.begin(), Common::tolower);
zhangjin's avatar
zhangjin committed
344
345
      if (tmp_str == std::string("na") || tmp_str == std::string("nan") ||
          tmp_str == std::string("null")) {
Guolin Ke's avatar
Guolin Ke committed
346
        *out = NAN;
347
      } else if (tmp_str == std::string("inf") || tmp_str == std::string("infinity")) {
348
        *out = sign * 1e308;
349
      } else {
350
        Log::Fatal("Unknown token %s in data file", tmp_str.c_str());
Guolin Ke's avatar
Guolin Ke committed
351
352
      }
      p += cnt;
353
    }
Guolin Ke's avatar
Guolin Ke committed
354
  }
Guolin Ke's avatar
Guolin Ke committed
355

Guolin Ke's avatar
Guolin Ke committed
356
357
358
  while (*p == ' ') {
    ++p;
  }
Guolin Ke's avatar
Guolin Ke committed
359

Guolin Ke's avatar
Guolin Ke committed
360
361
362
  return p;
}

Chen Yufei's avatar
Chen Yufei committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Use fast_double_parse and strtod (if parse failed) to parse double.
inline static const char* AtofPrecise(const char* p, double* out) {
  const char* end = fast_double_parser::parse_number(p, out);

  if (end != nullptr) {
    return end;
  }

  // Rare path: Not in RFC 7159 format. Possible "inf", "nan", etc. Fallback to standard library:
  char* end2;
  errno = 0;  // This is Required before calling strtod.
  *out = std::strtod(p, &end2);  // strtod is locale aware.
  if (end2 == p) {
    Log::Fatal("no conversion to double for: %s", p);
  }
  if (errno == ERANGE) {
379
    Log::Warning("convert to double got underflow or overflow: %s", p);
Chen Yufei's avatar
Chen Yufei committed
380
381
382
383
  }
  return end2;
}

384
inline static bool AtoiAndCheck(const char* p, int* out) {
385
386
387
388
389
390
391
  const char* after = Atoi(p, out);
  if (*after != '\0') {
    return false;
  }
  return true;
}

392
inline static bool AtofAndCheck(const char* p, double* out) {
393
394
395
396
397
398
399
  const char* after = Atof(p, out);
  if (*after != '\0') {
    return false;
  }
  return true;
}

Guolin Ke's avatar
Guolin Ke committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
inline static const char* SkipSpaceAndTab(const char* p) {
  while (*p == ' ' || *p == '\t') {
    ++p;
  }
  return p;
}

inline static const char* SkipReturn(const char* p) {
  while (*p == '\n' || *p == '\r' || *p == ' ') {
    ++p;
  }
  return p;
}

Guolin Ke's avatar
Guolin Ke committed
414
415
template<typename T, typename T2>
inline static std::vector<T2> ArrayCast(const std::vector<T>& arr) {
416
  std::vector<T2> ret(arr.size());
Guolin Ke's avatar
Guolin Ke committed
417
  for (size_t i = 0; i < arr.size(); ++i) {
418
    ret[i] = static_cast<T2>(arr[i]);
Guolin Ke's avatar
Guolin Ke committed
419
  }
Guolin Ke's avatar
Guolin Ke committed
420
  return ret;
Guolin Ke's avatar
Guolin Ke committed
421
422
}

423
424
425
template<typename T, bool is_float>
struct __StringToTHelper {
  T operator()(const std::string& str) const {
426
427
428
    T ret = 0;
    Atoi(str.c_str(), &ret);
    return ret;
429
430
431
432
433
434
435
436
437
438
  }
};

template<typename T>
struct __StringToTHelper<T, true> {
  T operator()(const std::string& str) const {
    return static_cast<T>(std::stod(str));
  }
};

Guolin Ke's avatar
Guolin Ke committed
439
template<typename T>
440
inline static std::vector<T> StringToArray(const std::string& str, char delimiter) {
Guolin Ke's avatar
Guolin Ke committed
441
  std::vector<std::string> strs = Split(str.c_str(), delimiter);
442
443
  std::vector<T> ret;
  ret.reserve(strs.size());
444
  __StringToTHelper<T, std::is_floating_point<T>::value> helper;
445
446
  for (const auto& s : strs) {
    ret.push_back(helper(s));
Guolin Ke's avatar
Guolin Ke committed
447
448
449
450
  }
  return ret;
}

451
452
453
454
455
456
457
458
459
460
461
template<typename T>
inline static std::vector<std::vector<T>> StringToArrayofArrays(
    const std::string& str, char left_bracket, char right_bracket, char delimiter) {
  std::vector<std::string> strs = SplitBrackets(str.c_str(), left_bracket, right_bracket);
  std::vector<std::vector<T>> ret;
  for (const auto& s : strs) {
    ret.push_back(StringToArray<T>(s, delimiter));
  }
  return ret;
}

Guolin Ke's avatar
Guolin Ke committed
462
template<typename T>
463
464
465
466
467
inline static std::vector<T> StringToArray(const std::string& str, int n) {
  if (n == 0) {
    return std::vector<T>();
  }
  std::vector<std::string> strs = Split(str.c_str(), ' ');
Nikita Titov's avatar
Nikita Titov committed
468
  CHECK_EQ(strs.size(), static_cast<size_t>(n));
Guolin Ke's avatar
Guolin Ke committed
469
  std::vector<T> ret;
470
471
472
473
  ret.reserve(strs.size());
  __StringToTHelper<T, std::is_floating_point<T>::value> helper;
  for (const auto& s : strs) {
    ret.push_back(helper(s));
Guolin Ke's avatar
Guolin Ke committed
474
475
476
477
  }
  return ret;
}

478
479
480
481
482
483
484
485
486
487
488
489
template<typename T, bool is_float>
struct __StringToTHelperFast {
  const char* operator()(const char*p, T* out) const {
    return Atoi(p, out);
  }
};

template<typename T>
struct __StringToTHelperFast<T, true> {
  const char* operator()(const char*p, T* out) const {
    double tmp = 0.0f;
    auto ret = Atof(p, &tmp);
490
    *out = static_cast<T>(tmp);
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
    return ret;
  }
};

template<typename T>
inline static std::vector<T> StringToArrayFast(const std::string& str, int n) {
  if (n == 0) {
    return std::vector<T>();
  }
  auto p_str = str.c_str();
  __StringToTHelperFast<T, std::is_floating_point<T>::value> helper;
  std::vector<T> ret(n);
  for (int i = 0; i < n; ++i) {
    p_str = helper(p_str, &ret[i]);
  }
  return ret;
}

509
template<typename T>
510
inline static std::string Join(const std::vector<T>& strs, const char* delimiter, const bool force_C_locale = false) {
Guolin Ke's avatar
Guolin Ke committed
511
  if (strs.empty()) {
Guolin Ke's avatar
Guolin Ke committed
512
513
    return std::string("");
  }
514
  std::stringstream str_buf;
515
516
517
  if (force_C_locale) {
    C_stringstream(str_buf);
  }
518
  str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
519
  str_buf << strs[0];
Guolin Ke's avatar
Guolin Ke committed
520
  for (size_t i = 1; i < strs.size(); ++i) {
521
522
    str_buf << delimiter;
    str_buf << strs[i];
Guolin Ke's avatar
Guolin Ke committed
523
  }
524
  return str_buf.str();
Guolin Ke's avatar
Guolin Ke committed
525
526
}

527
template<>
528
inline std::string Join<int8_t>(const std::vector<int8_t>& strs, const char* delimiter, const bool force_C_locale) {
529
530
531
532
  if (strs.empty()) {
    return std::string("");
  }
  std::stringstream str_buf;
533
534
535
  if (force_C_locale) {
    C_stringstream(str_buf);
  }
536
537
538
539
540
541
542
543
544
  str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
  str_buf << static_cast<int16_t>(strs[0]);
  for (size_t i = 1; i < strs.size(); ++i) {
    str_buf << delimiter;
    str_buf << static_cast<int16_t>(strs[i]);
  }
  return str_buf.str();
}

545
template<typename T>
546
inline static std::string Join(const std::vector<T>& strs, size_t start, size_t end, const char* delimiter, const bool force_C_locale = false) {
Guolin Ke's avatar
Guolin Ke committed
547
548
549
  if (end - start <= 0) {
    return std::string("");
  }
Guolin Ke's avatar
Guolin Ke committed
550
551
  start = std::min(start, static_cast<size_t>(strs.size()) - 1);
  end = std::min(end, static_cast<size_t>(strs.size()));
552
  std::stringstream str_buf;
553
554
555
  if (force_C_locale) {
    C_stringstream(str_buf);
  }
556
  str_buf << std::setprecision(std::numeric_limits<double>::digits10 + 2);
557
  str_buf << strs[start];
Guolin Ke's avatar
Guolin Ke committed
558
  for (size_t i = start + 1; i < end; ++i) {
559
560
    str_buf << delimiter;
    str_buf << strs[i];
Guolin Ke's avatar
Guolin Ke committed
561
  }
562
  return str_buf.str();
Guolin Ke's avatar
Guolin Ke committed
563
564
}

565
inline static int64_t Pow2RoundUp(int64_t x) {
Guolin Ke's avatar
Guolin Ke committed
566
567
568
569
570
571
572
573
574
575
  int64_t t = 1;
  for (int i = 0; i < 64; ++i) {
    if (t >= x) {
      return t;
    }
    t <<= 1;
  }
  return 0;
}

576
/*!
577
 * \brief Do inplace softmax transformation on p_rec
578
579
 * \param p_rec The input/output vector of the values.
 */
580
inline static void Softmax(std::vector<double>* p_rec) {
581
582
  std::vector<double> &rec = *p_rec;
  double wmax = rec[0];
583
584
585
  for (size_t i = 1; i < rec.size(); ++i) {
    wmax = std::max(rec[i], wmax);
  }
586
  double wsum = 0.0f;
587
588
589
590
591
  for (size_t i = 0; i < rec.size(); ++i) {
    rec[i] = std::exp(rec[i] - wmax);
    wsum += rec[i];
  }
  for (size_t i = 0; i < rec.size(); ++i) {
592
    rec[i] /= static_cast<double>(wsum);
593
594
595
  }
}

596
inline static void Softmax(const double* input, double* output, int len) {
Guolin Ke's avatar
Guolin Ke committed
597
  double wmax = input[0];
598
  for (int i = 1; i < len; ++i) {
Guolin Ke's avatar
Guolin Ke committed
599
    wmax = std::max(input[i], wmax);
600
601
602
  }
  double wsum = 0.0f;
  for (int i = 0; i < len; ++i) {
Guolin Ke's avatar
Guolin Ke committed
603
604
    output[i] = std::exp(input[i] - wmax);
    wsum += output[i];
605
606
  }
  for (int i = 0; i < len; ++i) {
Guolin Ke's avatar
Guolin Ke committed
607
    output[i] /= static_cast<double>(wsum);
608
609
610
  }
}

Guolin Ke's avatar
Guolin Ke committed
611
612
613
template<typename T>
std::vector<const T*> ConstPtrInVectorWrapper(const std::vector<std::unique_ptr<T>>& input) {
  std::vector<const T*> ret;
Guolin Ke's avatar
Guolin Ke committed
614
615
  for (auto t = input.begin(); t !=input.end(); ++t) {
    ret.push_back(t->get());
616
  }
Guolin Ke's avatar
Guolin Ke committed
617
  return ret;
618
619
}

Guolin Ke's avatar
Guolin Ke committed
620
template<typename T1, typename T2>
Guolin Ke's avatar
Guolin Ke committed
621
inline static void SortForPair(std::vector<T1>* keys, std::vector<T2>* values, size_t start, bool is_reverse = false) {
Guolin Ke's avatar
Guolin Ke committed
622
  std::vector<std::pair<T1, T2>> arr;
Guolin Ke's avatar
Guolin Ke committed
623
624
  auto& ref_key = *keys;
  auto& ref_value = *values;
Guolin Ke's avatar
Guolin Ke committed
625
  for (size_t i = start; i < keys->size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
626
    arr.emplace_back(ref_key[i], ref_value[i]);
Guolin Ke's avatar
Guolin Ke committed
627
628
  }
  if (!is_reverse) {
629
    std::stable_sort(arr.begin(), arr.end(), [](const std::pair<T1, T2>& a, const std::pair<T1, T2>& b) {
Guolin Ke's avatar
Guolin Ke committed
630
631
632
      return a.first < b.first;
    });
  } else {
633
    std::stable_sort(arr.begin(), arr.end(), [](const std::pair<T1, T2>& a, const std::pair<T1, T2>& b) {
Guolin Ke's avatar
Guolin Ke committed
634
635
636
637
      return a.first > b.first;
    });
  }
  for (size_t i = start; i < arr.size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
638
639
    ref_key[i] = arr[i].first;
    ref_value[i] = arr[i].second;
Guolin Ke's avatar
Guolin Ke committed
640
641
642
  }
}

643
template <typename T>
Guolin Ke's avatar
Guolin Ke committed
644
645
inline static std::vector<T*> Vector2Ptr(std::vector<std::vector<T>>* data) {
  std::vector<T*> ptr(data->size());
Guolin Ke's avatar
Guolin Ke committed
646
  auto& ref_data = *data;
Guolin Ke's avatar
Guolin Ke committed
647
  for (size_t i = 0; i < data->size(); ++i) {
Guolin Ke's avatar
Guolin Ke committed
648
    ptr[i] = ref_data[i].data();
649
650
651
652
653
654
655
656
657
658
659
660
661
  }
  return ptr;
}

template <typename T>
inline static std::vector<int> VectorSize(const std::vector<std::vector<T>>& data) {
  std::vector<int> ret(data.size());
  for (size_t i = 0; i < data.size(); ++i) {
    ret[i] = static_cast<int>(data[i].size());
  }
  return ret;
}

Guolin Ke's avatar
Guolin Ke committed
662
inline static double AvoidInf(double x) {
Guolin Ke's avatar
Guolin Ke committed
663
664
665
  if (std::isnan(x)) {
    return 0.0;
  } else if (x >= 1e300) {
Guolin Ke's avatar
Guolin Ke committed
666
    return 1e300;
667
  } else if (x <= -1e300) {
Guolin Ke's avatar
Guolin Ke committed
668
    return -1e300;
Guolin Ke's avatar
Guolin Ke committed
669
670
671
672
673
  } else {
    return x;
  }
}

674
inline static float AvoidInf(float x) {
Guolin Ke's avatar
Guolin Ke committed
675
  if (std::isnan(x)) {
Guolin Ke's avatar
Guolin Ke committed
676
677
    return 0.0f;
  } else if (x >= 1e38) {
678
679
680
681
682
683
    return 1e38f;
  } else if (x <= -1e38) {
    return -1e38f;
  } else {
    return x;
  }
684
685
686
}

template<typename _Iter> inline
687
688
689
690
static typename std::iterator_traits<_Iter>::value_type* IteratorValType(_Iter) {
  return (0);
}

691
template<typename _RanIt, typename _Pr, typename _VTRanIt> inline
692
693
694
static void ParallelSort(_RanIt _First, _RanIt _Last, _Pr _Pred, _VTRanIt*) {
  size_t len = _Last - _First;
  const size_t kMinInnerLen = 1024;
695
  int num_threads = OMP_NUM_THREADS();
696
697
698
699
700
701
702
  if (len <= kMinInnerLen || num_threads <= 1) {
    std::sort(_First, _Last, _Pred);
    return;
  }
  size_t inner_size = (len + num_threads - 1) / num_threads;
  inner_size = std::max(inner_size, kMinInnerLen);
  num_threads = static_cast<int>((len + inner_size - 1) / inner_size);
703
#pragma omp parallel for num_threads(num_threads) schedule(static, 1)
704
705
706
707
708
709
710
711
712
713
714
  for (int i = 0; i < num_threads; ++i) {
    size_t left = inner_size*i;
    size_t right = left + inner_size;
    right = std::min(right, len);
    if (right > left) {
      std::sort(_First + left, _First + right, _Pred);
    }
  }
  // Buffer for merge.
  std::vector<_VTRanIt> temp_buf(len);
  _RanIt buf = temp_buf.begin();
715
  size_t s = inner_size;
716
717
718
  // Recursive merge
  while (s < len) {
    int loop_size = static_cast<int>((len + s * 2 - 1) / (s * 2));
719
    #pragma omp parallel for num_threads(num_threads) schedule(static, 1)
720
721
722
723
724
    for (int i = 0; i < loop_size; ++i) {
      size_t left = i * 2 * s;
      size_t mid = left + s;
      size_t right = mid + s;
      right = std::min(len, right);
725
726
727
      if (mid >= right) {
        continue;
      }
728
729
730
731
732
733
734
      std::copy(_First + left, _First + mid, buf + left);
      std::merge(buf + left, buf + mid, _First + mid, _First + right, _First + left, _Pred);
    }
    s *= 2;
  }
}

735
template<typename _RanIt, typename _Pr> inline
736
737
738
739
static void ParallelSort(_RanIt _First, _RanIt _Last, _Pr _Pred) {
  return ParallelSort(_First, _Last, _Pred, IteratorValType(_First));
}

740
// Check that all y[] are in interval [ymin, ymax] (end points included); throws error if not
741
template <typename T>
742
inline static void CheckElementsIntervalClosed(const T *y, T ymin, T ymax, int ny, const char *callername) {
743
  auto fatal_msg = [&y, &ymin, &ymax, &callername](int i) {
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
    std::ostringstream os;
    os << "[%s]: does not tolerate element [#%i = " << y[i] << "] outside [" << ymin << ", " << ymax << "]";
    Log::Fatal(os.str().c_str(), callername, i);
  };
  for (int i = 1; i < ny; i += 2) {
    if (y[i - 1] < y[i]) {
      if (y[i - 1] < ymin) {
        fatal_msg(i - 1);
      } else if (y[i] > ymax) {
        fatal_msg(i);
      }
    } else {
      if (y[i - 1] > ymax) {
        fatal_msg(i - 1);
      } else if (y[i] < ymin) {
        fatal_msg(i);
      }
    }
  }
763
  if (ny & 1) {  // odd
764
765
    if (y[ny - 1] < ymin || y[ny - 1] > ymax) {
      fatal_msg(ny - 1);
766
767
768
769
770
771
    }
  }
}

// One-pass scan over array w with nw elements: find min, max and sum of elements;
// this is useful for checking weight requirements.
772
template <typename T1, typename T2>
773
inline static void ObtainMinMaxSum(const T1 *w, int nw, T1 *mi, T1 *ma, T2 *su) {
774
775
776
777
  T1 minw;
  T1 maxw;
  T1 sumw;
  int i;
778
  if (nw & 1) {  // odd
779
780
781
782
    minw = w[0];
    maxw = w[0];
    sumw = w[0];
    i = 2;
783
  } else {  // even
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
    if (w[0] < w[1]) {
      minw = w[0];
      maxw = w[1];
    } else {
      minw = w[1];
      maxw = w[0];
    }
    sumw = w[0] + w[1];
    i = 3;
  }
  for (; i < nw; i += 2) {
    if (w[i - 1] < w[i]) {
      minw = std::min(minw, w[i - 1]);
      maxw = std::max(maxw, w[i]);
    } else {
      minw = std::min(minw, w[i]);
      maxw = std::max(maxw, w[i - 1]);
    }
    sumw += w[i - 1] + w[i];
  }
  if (mi != nullptr) {
    *mi = minw;
  }
  if (ma != nullptr) {
    *ma = maxw;
  }
  if (su != nullptr) {
    *su = static_cast<T2>(sumw);
  }
813
814
}

815
inline static std::vector<uint32_t> EmptyBitset(int n) {
816
  int size = n / 32;
817
  if (n % 32 != 0) ++size;
818
819
820
821
  return std::vector<uint32_t>(size);
}

template<typename T>
Guolin Ke's avatar
Guolin Ke committed
822
inline static void InsertBitset(std::vector<uint32_t>* vec, const T val) {
Guolin Ke's avatar
Guolin Ke committed
823
824
825
826
827
828
829
  auto& ref_v = *vec;
  int i1 = val / 32;
  int i2 = val % 32;
  if (static_cast<int>(vec->size()) < i1 + 1) {
    vec->resize(i1 + 1, 0);
  }
  ref_v[i1] |= (1 << i2);
830
831
}

832
833
template<typename T>
inline static std::vector<uint32_t> ConstructBitset(const T* vals, int n) {
834
835
836
837
838
839
840
841
842
843
844
845
  std::vector<uint32_t> ret;
  for (int i = 0; i < n; ++i) {
    int i1 = vals[i] / 32;
    int i2 = vals[i] % 32;
    if (static_cast<int>(ret.size()) < i1 + 1) {
      ret.resize(i1 + 1, 0);
    }
    ret[i1] |= (1 << i2);
  }
  return ret;
}

846
847
template<typename T>
inline static bool FindInBitset(const uint32_t* bits, int n, T pos) {
848
849
850
851
852
853
854
855
  int i1 = pos / 32;
  if (i1 >= n) {
    return false;
  }
  int i2 = pos % 32;
  return (bits[i1] >> i2) & 1;
}

856
857
858
859
860
861
inline static bool CheckDoubleEqualOrdered(double a, double b) {
  double upper = std::nextafter(a, INFINITY);
  return b <= upper;
}

inline static double GetDoubleUpperBound(double a) {
Hongbin Shi's avatar
Hongbin Shi committed
862
  return std::nextafter(a, INFINITY);
863
864
}

865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
inline static size_t GetLine(const char* str) {
  auto start = str;
  while (*str != '\0' && *str != '\n' && *str != '\r') {
    ++str;
  }
  return str - start;
}

inline static const char* SkipNewLine(const char* str) {
  if (*str == '\r') {
    ++str;
  }
  if (*str == '\n') {
    ++str;
  }
  return str;
}

883
884
885
886
887
template <typename T>
static int Sign(T x) {
  return (x > T(0)) - (x < T(0));
}

Guolin Ke's avatar
Guolin Ke committed
888
889
890
891
892
893
894
895
896
template <typename T>
static T SafeLog(T x) {
  if (x > 0) {
    return std::log(x);
  } else {
    return -INFINITY;
  }
}

897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
inline bool CheckAllowedJSON(const std::string& s) {
  unsigned char char_code;
  for (auto c : s) {
    char_code = static_cast<unsigned char>(c);
    if (char_code == 34      // "
        || char_code == 44   // ,
        || char_code == 58   // :
        || char_code == 91   // [
        || char_code == 93   // ]
        || char_code == 123  // {
        || char_code == 125  // }
        ) {
      return false;
    }
  }
  return true;
}

915
916
917
918
919
920
inline int RoundInt(double x) {
  return static_cast<int>(x + 0.5f);
}

template <typename T, std::size_t N = 32>
class AlignmentAllocator {
921
 public:
922
923
924
925
926
927
928
929
930
931
  typedef T value_type;
  typedef std::size_t size_type;
  typedef std::ptrdiff_t difference_type;

  typedef T* pointer;
  typedef const T* const_pointer;

  typedef T& reference;
  typedef const T& const_reference;

932
  inline AlignmentAllocator() throw() {}
933
934

  template <typename T2>
935
  inline AlignmentAllocator(const AlignmentAllocator<T2, N>&) throw() {}
936

937
  inline ~AlignmentAllocator() throw() {}
938

939
  inline pointer address(reference r) {
940
941
942
    return &r;
  }

943
  inline const_pointer address(const_reference r) const {
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
    return &r;
  }

  inline pointer allocate(size_type n) {
    return (pointer)_mm_malloc(n * sizeof(value_type), N);
  }

  inline void deallocate(pointer p, size_type) {
    _mm_free(p);
  }

  inline void construct(pointer p, const value_type& wert) {
    new (p) value_type(wert);
  }

  inline void destroy(pointer p) {
    p->~value_type();
  }

963
  inline size_type max_size() const throw() {
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
    return size_type(-1) / sizeof(value_type);
  }

  template <typename T2>
  struct rebind {
    typedef AlignmentAllocator<T2, N> other;
  };

  bool operator!=(const AlignmentAllocator<T, N>& other) const {
    return !(*this == other);
  }

  // Returns true if and only if storage allocated from *this
  // can be deallocated from other, and vice versa.
  // Always returns true for stateless allocators.
  bool operator==(const AlignmentAllocator<T, N>&) const {
    return true;
  }
};

class Timer {
 public:
Guolin Ke's avatar
Guolin Ke committed
986
987
  Timer() {
#ifdef TIMETAG
988
    int num_threads = OMP_NUM_THREADS();
Guolin Ke's avatar
Guolin Ke committed
989
990
991
    start_time_.resize(num_threads);
    stats_.resize(num_threads);
#endif  // TIMETAG
992
  }
993

Guolin Ke's avatar
Guolin Ke committed
994
995
996
  ~Timer() { Print(); }

#ifdef TIMETAG
997
  void Start(const std::string& name) {
Guolin Ke's avatar
Guolin Ke committed
998
999
    auto tid = omp_get_thread_num();
    start_time_[tid][name] = std::chrono::steady_clock::now();
1000
  }
1001

1002
  void Stop(const std::string& name) {
Guolin Ke's avatar
Guolin Ke committed
1003
1004
1005
1006
    auto cur_time = std::chrono::steady_clock::now();
    auto tid = omp_get_thread_num();
    if (stats_[tid].find(name) == stats_[tid].end()) {
      stats_[tid][name] = std::chrono::duration<double, std::milli>(0);
1007
    }
Guolin Ke's avatar
Guolin Ke committed
1008
    stats_[tid][name] += cur_time - start_time_[tid][name];
1009
  }
1010

Guolin Ke's avatar
Guolin Ke committed
1011
1012
#else
  void Start(const std::string&) {}
1013

Guolin Ke's avatar
Guolin Ke committed
1014
1015
  void Stop(const std::string&) {}
#endif  // TIMETAG
1016
1017

  void Print() const {
Guolin Ke's avatar
Guolin Ke committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
#ifdef TIMETAG
    std::unordered_map<std::string, std::chrono::duration<double, std::milli>>
        stats(stats_[0].begin(), stats_[0].end());
    for (size_t i = 1; i < stats_.size(); ++i) {
      for (auto it = stats_[i].begin(); it != stats_[i].end(); ++it) {
        if (stats.find(it->first) == stats.end()) {
          stats[it->first] = it->second;
        } else {
          stats[it->first] += it->second;
        }
      }
    }
    std::map<std::string, std::chrono::duration<double, std::milli>> ordered(
        stats.begin(), stats.end());
1032
    for (auto it = ordered.begin(); it != ordered.end(); ++it) {
1033
      Log::Info("%s costs:\t %f", it->first.c_str(), it->second * 1e-3);
1034
    }
Guolin Ke's avatar
Guolin Ke committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
#endif  // TIMETAG
  }
#ifdef TIMETAG
  std::vector<
      std::unordered_map<std::string, std::chrono::steady_clock::time_point>>
      start_time_;
  std::vector<std::unordered_map<std::string,
                                 std::chrono::duration<double, std::milli>>>
      stats_;
#endif  // TIMETAG
1045
1046
1047
1048
1049
};

// Note: this class is not thread-safe, don't use it inside omp blocks
class FunctionTimer {
 public:
1050
#ifdef TIMETAG
Guolin Ke's avatar
Guolin Ke committed
1051
  FunctionTimer(const std::string& name, Timer& timer) : timer_(timer) {
1052
1053
1054
    timer.Start(name);
    name_ = name;
  }
1055

Guolin Ke's avatar
Guolin Ke committed
1056
  ~FunctionTimer() { timer_.Stop(name_); }
1057

1058
1059
1060
 private:
  std::string name_;
  Timer& timer_;
1061
1062
1063
#else
  FunctionTimer(const std::string&, Timer&) {}
#endif  // TIMETAG
1064
1065
};

Guolin Ke's avatar
Guolin Ke committed
1066
1067
}  // namespace Common

1068
1069
extern Common::Timer global_timer;

1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134

/*!
* Provides locale-independent alternatives to Common's methods.
* Essential to make models robust to locale settings.
*/
namespace CommonC {

template<typename T>
inline static std::string Join(const std::vector<T>& strs, const char* delimiter) {
  return LightGBM::Common::Join(strs, delimiter, true);
}

template<typename T>
inline static std::string Join(const std::vector<T>& strs, size_t start, size_t end, const char* delimiter) {
  return LightGBM::Common::Join(strs, start, end, delimiter, true);
}

inline static const char* Atof(const char* p, double* out) {
  return LightGBM::Common::Atof(p, out);
}

template<typename T, bool is_float>
struct __StringToTHelperFast {
  const char* operator()(const char*p, T* out) const {
    return LightGBM::Common::Atoi(p, out);
  }
};

/*!
* \warning Beware that ``Common::Atof`` in ``__StringToTHelperFast``,
*          has **less** floating point precision than ``__StringToTHelper``.
*          Both versions are kept to maintain bit-for-bit the "legacy" LightGBM behaviour in terms of precision.
*          Check ``StringToArrayFast`` and ``StringToArray`` for more details on this.
*/
template<typename T>
struct __StringToTHelperFast<T, true> {
  const char* operator()(const char*p, T* out) const {
    double tmp = 0.0f;
    auto ret = Atof(p, &tmp);
    *out = static_cast<T>(tmp);
    return ret;
  }
};

template<typename T, bool is_float>
struct __StringToTHelper {
  T operator()(const std::string& str) const {
    T ret = 0;
    LightGBM::Common::Atoi(str.c_str(), &ret);
    return ret;
  }
};

/*!
* \warning Beware that ``Common::Atof`` in ``__StringToTHelperFast``,
*          has **less** floating point precision than ``__StringToTHelper``.
*          Both versions are kept to maintain bit-for-bit the "legacy" LightGBM behaviour in terms of precision.
*          Check ``StringToArrayFast`` and ``StringToArray`` for more details on this.
* \note It is possible that ``fast_double_parser::parse_number`` is faster than ``Common::Atof``.
*/
template<typename T>
struct __StringToTHelper<T, true> {
  T operator()(const std::string& str) const {
    double tmp;

Chen Yufei's avatar
Chen Yufei committed
1135
1136
    const char* end = Common::AtofPrecise(str.c_str(), &tmp);
    if (end == str.c_str()) {
1137
        Log::Fatal("Failed to parse double: %s", str.c_str());
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
    }

    return static_cast<T>(tmp);
  }
};


/*!
* \warning Beware that due to internal use of ``Common::Atof`` in ``__StringToTHelperFast``,
*          this method has less precision for floating point numbers than ``StringToArray``,
*          which calls ``__StringToTHelper``.
*          As such, ``StringToArrayFast`` and ``StringToArray`` are not equivalent!
*          Both versions were kept to maintain bit-for-bit the "legacy" LightGBM behaviour in terms of precision.
*/
template<typename T>
inline static std::vector<T> StringToArrayFast(const std::string& str, int n) {
  if (n == 0) {
    return std::vector<T>();
  }
  auto p_str = str.c_str();
  __StringToTHelperFast<T, std::is_floating_point<T>::value> helper;
  std::vector<T> ret(n);
  for (int i = 0; i < n; ++i) {
    p_str = helper(p_str, &ret[i]);
  }
  return ret;
}

/*!
* \warning Do not replace calls to this method by ``StringToArrayFast``.
*          This method is more precise for floating point numbers.
*          Check ``StringToArrayFast`` for more details.
*/
template<typename T>
inline static std::vector<T> StringToArray(const std::string& str, int n) {
  if (n == 0) {
    return std::vector<T>();
  }
  std::vector<std::string> strs = LightGBM::Common::Split(str.c_str(), ' ');
  CHECK_EQ(strs.size(), static_cast<size_t>(n));
  std::vector<T> ret;
  ret.reserve(strs.size());
  __StringToTHelper<T, std::is_floating_point<T>::value> helper;
  for (const auto& s : strs) {
    ret.push_back(helper(s));
  }
  return ret;
}

/*!
* \warning Do not replace calls to this method by ``StringToArrayFast``.
*          This method is more precise for floating point numbers.
*          Check ``StringToArrayFast`` for more details.
*/
template<typename T>
inline static std::vector<T> StringToArray(const std::string& str, char delimiter) {
  std::vector<std::string> strs = LightGBM::Common::Split(str.c_str(), delimiter);
  std::vector<T> ret;
  ret.reserve(strs.size());
  __StringToTHelper<T, std::is_floating_point<T>::value> helper;
  for (const auto& s : strs) {
    ret.push_back(helper(s));
  }
  return ret;
}

/*!
* Safely formats a value onto a buffer according to a format string and null-terminates it.
*
* \note It checks that the full value was written or forcefully aborts.
*       This safety check serves to prevent incorrect internal API usage.
*       Correct usage will never incur in this problem:
*         - The received buffer size shall be sufficient at all times for the input format string and value.
*/
template <typename T>
inline static void format_to_buf(char* buffer, const size_t buf_len, const char* format, const T value) {
    auto result = fmt::format_to_n(buffer, buf_len, format, value);
    if (result.size >= buf_len) {
      Log::Fatal("Numerical conversion failed. Buffer is too small.");
    }
    buffer[result.size] = '\0';
}

template<typename T, bool is_float, bool high_precision>
struct __TToStringHelper {
  void operator()(T value, char* buffer, size_t buf_len) const {
    format_to_buf(buffer, buf_len, "{}", value);
  }
};

template<typename T>
struct __TToStringHelper<T, true, false> {
  void operator()(T value, char* buffer, size_t buf_len) const {
    format_to_buf(buffer, buf_len, "{:g}", value);
  }
};

template<typename T>
struct __TToStringHelper<T, true, true> {
  void operator()(T value, char* buffer, size_t buf_len) const {
    format_to_buf(buffer, buf_len, "{:.17g}", value);
  }
};

/*!
* Converts an array to a string with with values separated by the space character.
* This method replaces Common's ``ArrayToString`` and ``ArrayToStringFast`` functionality
* and is locale-independent.
1246
*
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
* \note If ``high_precision_output`` is set to true,
*       floating point values are output with more digits of precision.
*/
template<bool high_precision_output = false, typename T>
inline static std::string ArrayToString(const std::vector<T>& arr, size_t n) {
  if (arr.empty() || n == 0) {
    return std::string("");
  }
  __TToStringHelper<T, std::is_floating_point<T>::value, high_precision_output> helper;
  const size_t buf_len = high_precision_output ? 32 : 16;
  std::vector<char> buffer(buf_len);
  std::stringstream str_buf;
  Common::C_stringstream(str_buf);
  helper(arr[0], buffer.data(), buf_len);
  str_buf << buffer.data();
  for (size_t i = 1; i < std::min(n, arr.size()); ++i) {
    helper(arr[i], buffer.data(), buf_len);
    str_buf << ' ' << buffer.data();
  }
  return str_buf.str();
}


}  // namespace CommonC


Guolin Ke's avatar
Guolin Ke committed
1273
1274
}  // namespace LightGBM

1275
#endif  // LIGHTGBM_INCLUDE_LIGHTGBM_UTILS_COMMON_H_