OR-Tools  8.1
cached_log.cc
Go to the documentation of this file.
1 // Copyright 2010-2018 Google LLC
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
15 
16 #include "ortools/base/logging.h"
17 
18 namespace operations_research {
19 
21 
23 
24 namespace {
25 double FastLog2(int64 input) {
26 #if defined(_MSC_VER) || defined(__ANDROID__)
27  return log(static_cast<double>(input)) / log(2.0L);
28 #else
29  return log2(input);
30 #endif
31 }
32 } // namespace
33 
34 void CachedLog::Init(int size) {
35  CHECK(cache_.empty());
36  CHECK_GT(size, 0);
37  cache_.resize(size, 0.0);
38  for (int i = 0; i < size; ++i) {
39  cache_[i] = FastLog2(i + 1);
40  }
41 }
42 
43 double CachedLog::Log2(int64 input) const {
44  CHECK_GE(input, 1);
45  if (input <= cache_.size()) {
46  return cache_[input - 1];
47  } else {
48  return FastLog2(input);
49  }
50 }
51 
52 } // namespace operations_research
operations_research::CachedLog::Log2
double Log2(int64 input) const
Definition: cached_log.cc:43
CHECK_GE
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
logging.h
operations_research::CachedLog::Init
void Init(int cache_size)
Definition: cached_log.cc:34
CHECK_GT
#define CHECK_GT(val1, val2)
Definition: base/logging.h:702
operations_research
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
Definition: dense_doubly_linked_list.h:21
int64
int64_t int64
Definition: integral_types.h:34
operations_research::CachedLog::CachedLog
CachedLog()
Definition: cached_log.cc:20
cached_log.h
operations_research::CachedLog::~CachedLog
~CachedLog()
Definition: cached_log.cc:22
input
static int input(yyscan_t yyscanner)
CHECK
#define CHECK(condition)
Definition: base/logging.h:495