Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
cached_log.cc
Go to the documentation of this file.
1// Copyright 2010-2024 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_t 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
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
43double CachedLog::Log2(int64_t 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
IntegerValue size
double Log2(int64_t input) const
Returns the log2 of 'input'.
Definition cached_log.cc:43
void Init(int cache_size)
This method can only be called once, and with a cache_size > 0.
Definition cached_log.cc:34
In SWIG mode, we don't want anything besides these top-level includes.
static int input(yyscan_t yyscanner)