OR-Tools  8.2
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
17
18namespace operations_research {
19
21
23
24namespace {
25double 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
34void 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
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
#define CHECK(condition)
Definition: base/logging.h:495
#define CHECK_GE(val1, val2)
Definition: base/logging.h:701
#define CHECK_GT(val1, val2)
Definition: base/logging.h:702
double Log2(int64 input) const
Definition: cached_log.cc:43
void Init(int cache_size)
Definition: cached_log.cc:34
int64_t int64
The vehicle routing library lets one model and solve generic vehicle routing problems ranging from th...
static int input(yyscan_t yyscanner)