File: utils.cc

package info (click to toggle)
fasttext 0.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 7,800 kB
  • sloc: javascript: 10,266; cpp: 5,458; python: 2,425; sh: 616; makefile: 102; xml: 81; perl: 43
file content (53 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * Copyright (c) 2016-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */

#include "utils.h"

#include <iomanip>
#include <ios>

namespace fasttext {

namespace utils {

int64_t size(std::ifstream& ifs) {
  ifs.seekg(std::streamoff(0), std::ios::end);
  return ifs.tellg();
}

void seek(std::ifstream& ifs, int64_t pos) {
  ifs.clear();
  ifs.seekg(std::streampos(pos));
}

double getDuration(
    const std::chrono::steady_clock::time_point& start,
    const std::chrono::steady_clock::time_point& end) {
  return std::chrono::duration_cast<std::chrono::duration<double>>(end - start)
      .count();
}

ClockPrint::ClockPrint(int32_t duration) : duration_(duration) {}

std::ostream& operator<<(std::ostream& out, const ClockPrint& me) {
  int32_t etah = me.duration_ / 3600;
  int32_t etam = (me.duration_ % 3600) / 60;
  int32_t etas = (me.duration_ % 3600) % 60;

  out << std::setw(3) << etah << "h" << std::setw(2) << etam << "m";
  out << std::setw(2) << etas << "s";
  return out;
}

bool compareFirstLess(const std::pair<double, double>& l, const double& r) {
  return l.first < r;
}

} // namespace utils

} // namespace fasttext