🏗 Add xdelta3 dependency
This commit is contained in:
parent
80f2439239
commit
ae59ce2256
97 changed files with 45332 additions and 1 deletions
40
lib/xdelta3/cpp-btree/CMakeLists.txt
Normal file
40
lib/xdelta3/cpp-btree/CMakeLists.txt
Normal file
|
@ -0,0 +1,40 @@
|
|||
# Copyright 2013 Google Inc. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
|
||||
project(cppbtree CXX)
|
||||
|
||||
option(build_tests "Build B-tree tests" OFF)
|
||||
add_definitions(-std=c++11)
|
||||
set(CMAKE_CXX_FLAGS "-g -O2")
|
||||
|
||||
# CMake doesn't have a way to pure template library,
|
||||
# add_library(cppbtree btree.h btree_map.h btree_set.h
|
||||
# safe_btree.h safe_btree_map.h safe_btree_set.h)
|
||||
# set_target_properties(cppbtree PROPERTIES LINKER_LANGUAGE CXX)
|
||||
|
||||
if(build_tests)
|
||||
enable_testing()
|
||||
include_directories($ENV{GTEST_ROOT}/include)
|
||||
link_directories($ENV{GTEST_ROOT})
|
||||
include_directories($ENV{GFLAGS_ROOT}/include)
|
||||
link_directories($ENV{GFLAGS_ROOT}/lib)
|
||||
add_executable(btree_test btree_test.cc btree_test_flags.cc)
|
||||
add_executable(safe_btree_test safe_btree_test.cc btree_test_flags.cc)
|
||||
add_executable(btree_bench btree_bench.cc btree_test_flags.cc)
|
||||
target_link_libraries(btree_test gtest_main gtest gflags)
|
||||
target_link_libraries(safe_btree_test gtest_main gtest gflags)
|
||||
target_link_libraries(btree_bench gflags gtest)
|
||||
endif()
|
202
lib/xdelta3/cpp-btree/COPYING
Normal file
202
lib/xdelta3/cpp-btree/COPYING
Normal file
|
@ -0,0 +1,202 @@
|
|||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
31
lib/xdelta3/cpp-btree/README
Normal file
31
lib/xdelta3/cpp-btree/README
Normal file
|
@ -0,0 +1,31 @@
|
|||
This library is a C++ template library and, as such, there is no
|
||||
library to build and install. Copy the .h files and use them!
|
||||
|
||||
See http://code.google.com/p/cpp-btree/wiki/UsageInstructions for
|
||||
details.
|
||||
|
||||
----
|
||||
|
||||
To build and run the provided tests, however, you will need to install
|
||||
CMake, the Google C++ Test framework, and the Google flags package.
|
||||
|
||||
Download and install CMake from http://www.cmake.org
|
||||
|
||||
Download and build the GoogleTest framework from
|
||||
http://code.google.com/p/googletest
|
||||
|
||||
Download and install gflags from https://code.google.com/p/gflags
|
||||
|
||||
Set GTEST_ROOT to the directory where GTEST was built.
|
||||
Set GFLAGS_ROOT to the directory prefix where GFLAGS is installed.
|
||||
|
||||
export GTEST_ROOT=/path/for/gtest-x.y
|
||||
export GFLAGS_ROOT=/opt
|
||||
|
||||
cmake . -Dbuild_tests=ON
|
||||
|
||||
For example, to build on a Unix system with the clang++ compiler,
|
||||
|
||||
export GTEST_ROOT=$(HOME)/src/googletest
|
||||
export GFLAGS_ROOT=/opt
|
||||
cmake . -G "Unix Makefiles" -Dbuild_tests=ON -DCMAKE_CXX_COMPILER=clang++
|
2394
lib/xdelta3/cpp-btree/btree.h
Normal file
2394
lib/xdelta3/cpp-btree/btree.h
Normal file
File diff suppressed because it is too large
Load diff
593
lib/xdelta3/cpp-btree/btree_bench.cc
Normal file
593
lib/xdelta3/cpp-btree/btree_bench.cc
Normal file
|
@ -0,0 +1,593 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <sys/time.h>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "gflags/gflags.h"
|
||||
#include "btree_map.h"
|
||||
#include "btree_set.h"
|
||||
#include "btree_test.h"
|
||||
|
||||
DEFINE_int32(test_random_seed, 123456789, "Seed for srand()");
|
||||
DEFINE_int32(benchmark_max_iters, 10000000, "Maximum test iterations");
|
||||
DEFINE_int32(benchmark_min_iters, 100, "Minimum test iterations");
|
||||
DEFINE_int32(benchmark_target_seconds, 1,
|
||||
"Attempt to benchmark for this many seconds");
|
||||
|
||||
using std::allocator;
|
||||
using std::less;
|
||||
using std::map;
|
||||
using std::max;
|
||||
using std::min;
|
||||
using std::multimap;
|
||||
using std::multiset;
|
||||
using std::set;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
namespace btree {
|
||||
namespace {
|
||||
|
||||
struct RandGen {
|
||||
typedef ptrdiff_t result_type;
|
||||
RandGen(result_type seed) {
|
||||
srand(seed);
|
||||
}
|
||||
result_type operator()(result_type l) {
|
||||
return rand() % l;
|
||||
}
|
||||
};
|
||||
|
||||
struct BenchmarkRun {
|
||||
BenchmarkRun(const char *name, void (*func)(int));
|
||||
void Run();
|
||||
void Stop();
|
||||
void Start();
|
||||
void Reset();
|
||||
|
||||
BenchmarkRun *next_benchmark;
|
||||
const char *benchmark_name;
|
||||
void (*benchmark_func)(int);
|
||||
int64_t accum_micros;
|
||||
int64_t last_started;
|
||||
};
|
||||
|
||||
BenchmarkRun *first_benchmark;
|
||||
BenchmarkRun *current_benchmark;
|
||||
|
||||
int64_t get_micros () {
|
||||
timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
return tv.tv_sec * 1000000 + tv.tv_usec;
|
||||
}
|
||||
|
||||
BenchmarkRun::BenchmarkRun(const char *name, void (*func)(int))
|
||||
: next_benchmark(first_benchmark),
|
||||
benchmark_name(name),
|
||||
benchmark_func(func),
|
||||
accum_micros(0),
|
||||
last_started(0) {
|
||||
first_benchmark = this;
|
||||
}
|
||||
|
||||
#define BTREE_BENCHMARK(name) \
|
||||
BTREE_BENCHMARK2(#name, name, __COUNTER__)
|
||||
#define BTREE_BENCHMARK2(name, func, counter) \
|
||||
BTREE_BENCHMARK3(name, func, counter)
|
||||
#define BTREE_BENCHMARK3(name, func, counter) \
|
||||
BenchmarkRun bench ## counter (name, func)
|
||||
|
||||
void StopBenchmarkTiming() {
|
||||
current_benchmark->Stop();
|
||||
}
|
||||
|
||||
void StartBenchmarkTiming() {
|
||||
current_benchmark->Start();
|
||||
}
|
||||
|
||||
void RunBenchmarks() {
|
||||
for (BenchmarkRun *bench = first_benchmark; bench;
|
||||
bench = bench->next_benchmark) {
|
||||
bench->Run();
|
||||
}
|
||||
}
|
||||
|
||||
void BenchmarkRun::Start() {
|
||||
assert(!last_started);
|
||||
last_started = get_micros();
|
||||
}
|
||||
|
||||
void BenchmarkRun::Stop() {
|
||||
if (last_started == 0) {
|
||||
return;
|
||||
}
|
||||
accum_micros += get_micros() - last_started;
|
||||
last_started = 0;
|
||||
}
|
||||
|
||||
void BenchmarkRun::Reset() {
|
||||
last_started = 0;
|
||||
accum_micros = 0;
|
||||
}
|
||||
|
||||
void BenchmarkRun::Run() {
|
||||
assert(current_benchmark == NULL);
|
||||
current_benchmark = this;
|
||||
int iters = FLAGS_benchmark_min_iters;
|
||||
for (;;) {
|
||||
Reset();
|
||||
Start();
|
||||
benchmark_func(iters);
|
||||
Stop();
|
||||
if (accum_micros > FLAGS_benchmark_target_seconds * 1000000 ||
|
||||
iters >= FLAGS_benchmark_max_iters) {
|
||||
break;
|
||||
} else if (accum_micros == 0) {
|
||||
iters *= 100;
|
||||
} else {
|
||||
int64_t target_micros = FLAGS_benchmark_target_seconds * 1000000;
|
||||
iters = target_micros * iters / accum_micros;
|
||||
}
|
||||
iters = min(iters, FLAGS_benchmark_max_iters);
|
||||
}
|
||||
std::cout << benchmark_name << "\t"
|
||||
<< accum_micros * 1000 / iters << "\t"
|
||||
<< iters;
|
||||
current_benchmark = NULL;
|
||||
}
|
||||
|
||||
// Used to avoid compiler optimizations for these benchmarks.
|
||||
template <typename T>
|
||||
void sink(const T& t0) {
|
||||
volatile T t = t0;
|
||||
}
|
||||
|
||||
// Benchmark insertion of values into a container.
|
||||
template <typename T>
|
||||
void BM_Insert(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
|
||||
T container;
|
||||
vector<V> values = GenerateValues<V>(FLAGS_benchmark_values);
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
container.insert(values[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; ) {
|
||||
// Remove and re-insert 10% of the keys
|
||||
int m = min(n - i, FLAGS_benchmark_values / 10);
|
||||
|
||||
for (int j = i; j < i + m; j++) {
|
||||
int x = j % FLAGS_benchmark_values;
|
||||
container.erase(key_of_value(values[x]));
|
||||
}
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int j = i; j < i + m; j++) {
|
||||
int x = j % FLAGS_benchmark_values;
|
||||
container.insert(values[x]);
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
|
||||
i += m;
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark lookup of values in a container.
|
||||
template <typename T>
|
||||
void BM_Lookup(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
|
||||
T container;
|
||||
vector<V> values = GenerateValues<V>(FLAGS_benchmark_values);
|
||||
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
container.insert(values[i]);
|
||||
}
|
||||
|
||||
V r = V();
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int m = i % values.size();
|
||||
r = *container.find(key_of_value(values[m]));
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
|
||||
sink(r); // Keep compiler from optimizing away r.
|
||||
}
|
||||
|
||||
// Benchmark lookup of values in a full container, meaning that values
|
||||
// are inserted in-order to take advantage of biased insertion, which
|
||||
// yields a full tree.
|
||||
template <typename T>
|
||||
void BM_FullLookup(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
|
||||
T container;
|
||||
vector<V> values = GenerateValues<V>(FLAGS_benchmark_values);
|
||||
vector<V> sorted(values);
|
||||
sort(sorted.begin(), sorted.end());
|
||||
|
||||
for (int i = 0; i < sorted.size(); i++) {
|
||||
container.insert(sorted[i]);
|
||||
}
|
||||
|
||||
V r = V();
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int m = i % values.size();
|
||||
r = *container.find(key_of_value(values[m]));
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
|
||||
sink(r); // Keep compiler from optimizing away r.
|
||||
}
|
||||
|
||||
// Benchmark deletion of values from a container.
|
||||
template <typename T>
|
||||
void BM_Delete(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
|
||||
T container;
|
||||
vector<V> values = GenerateValues<V>(FLAGS_benchmark_values);
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
container.insert(values[i]);
|
||||
}
|
||||
|
||||
for (int i = 0; i < n; ) {
|
||||
// Remove and re-insert 10% of the keys
|
||||
int m = min(n - i, FLAGS_benchmark_values / 10);
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int j = i; j < i + m; j++) {
|
||||
int x = j % FLAGS_benchmark_values;
|
||||
container.erase(key_of_value(values[x]));
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
|
||||
for (int j = i; j < i + m; j++) {
|
||||
int x = j % FLAGS_benchmark_values;
|
||||
container.insert(values[x]);
|
||||
}
|
||||
|
||||
i += m;
|
||||
}
|
||||
}
|
||||
|
||||
// Benchmark steady-state insert (into first half of range) and remove
|
||||
// (from second second half of range), treating the container
|
||||
// approximately like a queue with log-time access for all elements.
|
||||
// This benchmark does not test the case where insertion and removal
|
||||
// happen in the same region of the tree. This benchmark counts two
|
||||
// value constructors.
|
||||
template <typename T>
|
||||
void BM_QueueAddRem(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
assert(FLAGS_benchmark_values % 2 == 0);
|
||||
|
||||
T container;
|
||||
|
||||
const int half = FLAGS_benchmark_values / 2;
|
||||
vector<int> remove_keys(half);
|
||||
vector<int> add_keys(half);
|
||||
|
||||
for (int i = 0; i < half; i++) {
|
||||
remove_keys[i] = i;
|
||||
add_keys[i] = i;
|
||||
}
|
||||
|
||||
RandGen rand(FLAGS_test_random_seed);
|
||||
|
||||
random_shuffle(remove_keys.begin(), remove_keys.end(), rand);
|
||||
random_shuffle(add_keys.begin(), add_keys.end(), rand);
|
||||
|
||||
Generator<V> g(FLAGS_benchmark_values + FLAGS_benchmark_max_iters);
|
||||
|
||||
for (int i = 0; i < half; i++) {
|
||||
container.insert(g(add_keys[i]));
|
||||
container.insert(g(half + remove_keys[i]));
|
||||
}
|
||||
|
||||
// There are three parts each of size "half":
|
||||
// 1 is being deleted from [offset - half, offset)
|
||||
// 2 is standing [offset, offset + half)
|
||||
// 3 is being inserted into [offset + half, offset + 2 * half)
|
||||
int offset = 0;
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int idx = i % half;
|
||||
|
||||
if (idx == 0) {
|
||||
StopBenchmarkTiming();
|
||||
random_shuffle(remove_keys.begin(), remove_keys.end(), rand);
|
||||
random_shuffle(add_keys.begin(), add_keys.end(), rand);
|
||||
offset += half;
|
||||
StartBenchmarkTiming();
|
||||
}
|
||||
|
||||
int e = container.erase(key_of_value(g(offset - half + remove_keys[idx])));
|
||||
assert(e == 1);
|
||||
container.insert(g(offset + half + add_keys[idx]));
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
}
|
||||
|
||||
// Mixed insertion and deletion in the same range using pre-constructed values.
|
||||
template <typename T>
|
||||
void BM_MixedAddRem(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
assert(FLAGS_benchmark_values % 2 == 0);
|
||||
|
||||
T container;
|
||||
RandGen rand(FLAGS_test_random_seed);
|
||||
|
||||
vector<V> values = GenerateValues<V>(FLAGS_benchmark_values * 2);
|
||||
|
||||
// Create two random shuffles
|
||||
vector<int> remove_keys(FLAGS_benchmark_values);
|
||||
vector<int> add_keys(FLAGS_benchmark_values);
|
||||
|
||||
// Insert the first half of the values (already in random order)
|
||||
for (int i = 0; i < FLAGS_benchmark_values; i++) {
|
||||
container.insert(values[i]);
|
||||
|
||||
// remove_keys and add_keys will be swapped before each round,
|
||||
// therefore fill add_keys here w/ the keys being inserted, so
|
||||
// they'll be the first to be removed.
|
||||
remove_keys[i] = i + FLAGS_benchmark_values;
|
||||
add_keys[i] = i;
|
||||
}
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int idx = i % FLAGS_benchmark_values;
|
||||
|
||||
if (idx == 0) {
|
||||
StopBenchmarkTiming();
|
||||
remove_keys.swap(add_keys);
|
||||
random_shuffle(remove_keys.begin(), remove_keys.end(), rand);
|
||||
random_shuffle(add_keys.begin(), add_keys.end(), rand);
|
||||
StartBenchmarkTiming();
|
||||
}
|
||||
|
||||
int e = container.erase(key_of_value(values[remove_keys[idx]]));
|
||||
assert(e == 1);
|
||||
container.insert(values[add_keys[idx]]);
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
}
|
||||
|
||||
// Insertion at end, removal from the beginning. This benchmark
|
||||
// counts two value constructors.
|
||||
template <typename T>
|
||||
void BM_Fifo(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
|
||||
T container;
|
||||
Generator<V> g(FLAGS_benchmark_values + FLAGS_benchmark_max_iters);
|
||||
|
||||
for (int i = 0; i < FLAGS_benchmark_values; i++) {
|
||||
container.insert(g(i));
|
||||
}
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
container.erase(container.begin());
|
||||
container.insert(container.end(), g(i + FLAGS_benchmark_values));
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
}
|
||||
|
||||
// Iteration (forward) through the tree
|
||||
template <typename T>
|
||||
void BM_FwdIter(int n) {
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
|
||||
// Disable timing while we perform some initialization.
|
||||
StopBenchmarkTiming();
|
||||
|
||||
T container;
|
||||
vector<V> values = GenerateValues<V>(FLAGS_benchmark_values);
|
||||
|
||||
for (int i = 0; i < FLAGS_benchmark_values; i++) {
|
||||
container.insert(values[i]);
|
||||
}
|
||||
|
||||
typename T::iterator iter;
|
||||
|
||||
V r = V();
|
||||
|
||||
StartBenchmarkTiming();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int idx = i % FLAGS_benchmark_values;
|
||||
|
||||
if (idx == 0) {
|
||||
iter = container.begin();
|
||||
}
|
||||
r = *iter;
|
||||
++iter;
|
||||
}
|
||||
|
||||
StopBenchmarkTiming();
|
||||
|
||||
sink(r); // Keep compiler from optimizing away r.
|
||||
}
|
||||
|
||||
typedef set<int32_t> stl_set_int32;
|
||||
typedef set<int64_t> stl_set_int64;
|
||||
typedef set<string> stl_set_string;
|
||||
|
||||
typedef map<int32_t, intptr_t> stl_map_int32;
|
||||
typedef map<int64_t, intptr_t> stl_map_int64;
|
||||
typedef map<string, intptr_t> stl_map_string;
|
||||
|
||||
typedef multiset<int32_t> stl_multiset_int32;
|
||||
typedef multiset<int64_t> stl_multiset_int64;
|
||||
typedef multiset<string> stl_multiset_string;
|
||||
|
||||
typedef multimap<int32_t, intptr_t> stl_multimap_int32;
|
||||
typedef multimap<int64_t, intptr_t> stl_multimap_int64;
|
||||
typedef multimap<string, intptr_t> stl_multimap_string;
|
||||
|
||||
#define MY_BENCHMARK_TYPES2(value, name, size) \
|
||||
typedef btree ## _set<value, less<value>, allocator<value>, size> \
|
||||
btree ## _ ## size ## _set_ ## name; \
|
||||
typedef btree ## _map<value, int, less<value>, allocator<value>, size> \
|
||||
btree ## _ ## size ## _map_ ## name; \
|
||||
typedef btree ## _multiset<value, less<value>, allocator<value>, size> \
|
||||
btree ## _ ## size ## _multiset_ ## name; \
|
||||
typedef btree ## _multimap<value, int, less<value>, allocator<value>, size> \
|
||||
btree ## _ ## size ## _multimap_ ## name
|
||||
|
||||
#define MY_BENCHMARK_TYPES(value, name) \
|
||||
MY_BENCHMARK_TYPES2(value, name, 128); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 160); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 192); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 224); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 256); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 288); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 320); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 352); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 384); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 416); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 448); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 480); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 512); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 1024); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 1536); \
|
||||
MY_BENCHMARK_TYPES2(value, name, 2048)
|
||||
|
||||
MY_BENCHMARK_TYPES(int32_t, int32);
|
||||
MY_BENCHMARK_TYPES(int64_t, int64);
|
||||
MY_BENCHMARK_TYPES(string, string);
|
||||
|
||||
#define MY_BENCHMARK4(type, name, func) \
|
||||
void BM_ ## type ## _ ## name(int n) { BM_ ## func <type>(n); } \
|
||||
BTREE_BENCHMARK(BM_ ## type ## _ ## name)
|
||||
|
||||
// Define NODESIZE_TESTING when running btree_perf.py.
|
||||
|
||||
#ifdef NODESIZE_TESTING
|
||||
#define MY_BENCHMARK3(tree, type, name, func) \
|
||||
MY_BENCHMARK4(tree ## _128_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _160_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _192_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _224_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _256_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _288_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _320_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _352_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _384_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _416_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _448_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _480_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _512_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _1024_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _1536_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _2048_ ## type, name, func)
|
||||
#else
|
||||
#define MY_BENCHMARK3(tree, type, name, func) \
|
||||
MY_BENCHMARK4(tree ## _256_ ## type, name, func); \
|
||||
MY_BENCHMARK4(tree ## _2048_ ## type, name, func)
|
||||
#endif
|
||||
|
||||
#define MY_BENCHMARK2(type, name, func) \
|
||||
MY_BENCHMARK4(stl_ ## type, name, func); \
|
||||
MY_BENCHMARK3(btree, type, name, func)
|
||||
|
||||
#define MY_BENCHMARK(type) \
|
||||
MY_BENCHMARK2(type, insert, Insert); \
|
||||
MY_BENCHMARK2(type, lookup, Lookup); \
|
||||
MY_BENCHMARK2(type, fulllookup, FullLookup); \
|
||||
MY_BENCHMARK2(type, delete, Delete); \
|
||||
MY_BENCHMARK2(type, queueaddrem, QueueAddRem); \
|
||||
MY_BENCHMARK2(type, mixedaddrem, MixedAddRem); \
|
||||
MY_BENCHMARK2(type, fifo, Fifo); \
|
||||
MY_BENCHMARK2(type, fwditer, FwdIter)
|
||||
|
||||
MY_BENCHMARK(set_int32);
|
||||
MY_BENCHMARK(map_int32);
|
||||
MY_BENCHMARK(set_int64);
|
||||
MY_BENCHMARK(map_int64);
|
||||
MY_BENCHMARK(set_string);
|
||||
MY_BENCHMARK(map_string);
|
||||
|
||||
MY_BENCHMARK(multiset_int32);
|
||||
MY_BENCHMARK(multimap_int32);
|
||||
MY_BENCHMARK(multiset_int64);
|
||||
MY_BENCHMARK(multimap_int64);
|
||||
MY_BENCHMARK(multiset_string);
|
||||
MY_BENCHMARK(multimap_string);
|
||||
|
||||
} // namespace
|
||||
} // namespace btree
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
btree::RunBenchmarks();
|
||||
return 0;
|
||||
}
|
349
lib/xdelta3/cpp-btree/btree_container.h
Normal file
349
lib/xdelta3/cpp-btree/btree_container.h
Normal file
|
@ -0,0 +1,349 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef UTIL_BTREE_BTREE_CONTAINER_H__
|
||||
#define UTIL_BTREE_BTREE_CONTAINER_H__
|
||||
|
||||
#include <iosfwd>
|
||||
#include <utility>
|
||||
|
||||
#include "btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// A common base class for btree_set, btree_map, btree_multiset and
|
||||
// btree_multimap.
|
||||
template <typename Tree>
|
||||
class btree_container {
|
||||
typedef btree_container<Tree> self_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::params_type params_type;
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
typedef typename Tree::pointer pointer;
|
||||
typedef typename Tree::const_pointer const_pointer;
|
||||
typedef typename Tree::reference reference;
|
||||
typedef typename Tree::const_reference const_reference;
|
||||
typedef typename Tree::size_type size_type;
|
||||
typedef typename Tree::difference_type difference_type;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
typedef typename Tree::reverse_iterator reverse_iterator;
|
||||
typedef typename Tree::const_reverse_iterator const_reverse_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_container(const key_compare &comp, const allocator_type &alloc)
|
||||
: tree_(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_container(const self_type &x)
|
||||
: tree_(x.tree_) {
|
||||
}
|
||||
|
||||
// Iterator routines.
|
||||
iterator begin() { return tree_.begin(); }
|
||||
const_iterator begin() const { return tree_.begin(); }
|
||||
iterator end() { return tree_.end(); }
|
||||
const_iterator end() const { return tree_.end(); }
|
||||
reverse_iterator rbegin() { return tree_.rbegin(); }
|
||||
const_reverse_iterator rbegin() const { return tree_.rbegin(); }
|
||||
reverse_iterator rend() { return tree_.rend(); }
|
||||
const_reverse_iterator rend() const { return tree_.rend(); }
|
||||
|
||||
// Lookup routines.
|
||||
iterator lower_bound(const key_type &key) {
|
||||
return tree_.lower_bound(key);
|
||||
}
|
||||
const_iterator lower_bound(const key_type &key) const {
|
||||
return tree_.lower_bound(key);
|
||||
}
|
||||
iterator upper_bound(const key_type &key) {
|
||||
return tree_.upper_bound(key);
|
||||
}
|
||||
const_iterator upper_bound(const key_type &key) const {
|
||||
return tree_.upper_bound(key);
|
||||
}
|
||||
std::pair<iterator,iterator> equal_range(const key_type &key) {
|
||||
return tree_.equal_range(key);
|
||||
}
|
||||
std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const {
|
||||
return tree_.equal_range(key);
|
||||
}
|
||||
|
||||
// Utility routines.
|
||||
void clear() {
|
||||
tree_.clear();
|
||||
}
|
||||
void swap(self_type &x) {
|
||||
tree_.swap(x.tree_);
|
||||
}
|
||||
void dump(std::ostream &os) const {
|
||||
tree_.dump(os);
|
||||
}
|
||||
void verify() const {
|
||||
tree_.verify();
|
||||
}
|
||||
|
||||
// Size routines.
|
||||
size_type size() const { return tree_.size(); }
|
||||
size_type max_size() const { return tree_.max_size(); }
|
||||
bool empty() const { return tree_.empty(); }
|
||||
size_type height() const { return tree_.height(); }
|
||||
size_type internal_nodes() const { return tree_.internal_nodes(); }
|
||||
size_type leaf_nodes() const { return tree_.leaf_nodes(); }
|
||||
size_type nodes() const { return tree_.nodes(); }
|
||||
size_type bytes_used() const { return tree_.bytes_used(); }
|
||||
static double average_bytes_per_value() {
|
||||
return Tree::average_bytes_per_value();
|
||||
}
|
||||
double fullness() const { return tree_.fullness(); }
|
||||
double overhead() const { return tree_.overhead(); }
|
||||
|
||||
bool operator==(const self_type& x) const {
|
||||
if (size() != x.size()) {
|
||||
return false;
|
||||
}
|
||||
for (const_iterator i = begin(), xi = x.begin(); i != end(); ++i, ++xi) {
|
||||
if (*i != *xi) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator!=(const self_type& other) const {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
|
||||
protected:
|
||||
Tree tree_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline std::ostream& operator<<(std::ostream &os, const btree_container<T> &b) {
|
||||
b.dump(os);
|
||||
return os;
|
||||
}
|
||||
|
||||
// A common base class for btree_set and safe_btree_set.
|
||||
template <typename Tree>
|
||||
class btree_unique_container : public btree_container<Tree> {
|
||||
typedef btree_unique_container<Tree> self_type;
|
||||
typedef btree_container<Tree> super_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::size_type size_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_unique_container(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_unique_container(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_unique_container(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
insert(b, e);
|
||||
}
|
||||
|
||||
// Lookup routines.
|
||||
iterator find(const key_type &key) {
|
||||
return this->tree_.find_unique(key);
|
||||
}
|
||||
const_iterator find(const key_type &key) const {
|
||||
return this->tree_.find_unique(key);
|
||||
}
|
||||
size_type count(const key_type &key) const {
|
||||
return this->tree_.count_unique(key);
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
std::pair<iterator,bool> insert(const value_type &x) {
|
||||
return this->tree_.insert_unique(x);
|
||||
}
|
||||
iterator insert(iterator position, const value_type &x) {
|
||||
return this->tree_.insert_unique(position, x);
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert(InputIterator b, InputIterator e) {
|
||||
this->tree_.insert_unique(b, e);
|
||||
}
|
||||
|
||||
// Deletion routines.
|
||||
int erase(const key_type &key) {
|
||||
return this->tree_.erase_unique(key);
|
||||
}
|
||||
// Erase the specified iterator from the btree. The iterator must be valid
|
||||
// (i.e. not equal to end()). Return an iterator pointing to the node after
|
||||
// the one that was erased (or end() if none exists).
|
||||
iterator erase(const iterator &iter) {
|
||||
return this->tree_.erase(iter);
|
||||
}
|
||||
void erase(const iterator &first, const iterator &last) {
|
||||
this->tree_.erase(first, last);
|
||||
}
|
||||
};
|
||||
|
||||
// A common base class for btree_map and safe_btree_map.
|
||||
template <typename Tree>
|
||||
class btree_map_container : public btree_unique_container<Tree> {
|
||||
typedef btree_map_container<Tree> self_type;
|
||||
typedef btree_unique_container<Tree> super_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::data_type data_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::mapped_type mapped_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
|
||||
private:
|
||||
// A pointer-like object which only generates its value when
|
||||
// dereferenced. Used by operator[] to avoid constructing an empty data_type
|
||||
// if the key already exists in the map.
|
||||
struct generate_value {
|
||||
generate_value(const key_type &k)
|
||||
: key(k) {
|
||||
}
|
||||
value_type operator*() const {
|
||||
return std::make_pair(key, data_type());
|
||||
}
|
||||
const key_type &key;
|
||||
};
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_map_container(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_map_container(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_map_container(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
data_type& operator[](const key_type &key) {
|
||||
return this->tree_.insert_unique(key, generate_value(key)).first->second;
|
||||
}
|
||||
};
|
||||
|
||||
// A common base class for btree_multiset and btree_multimap.
|
||||
template <typename Tree>
|
||||
class btree_multi_container : public btree_container<Tree> {
|
||||
typedef btree_multi_container<Tree> self_type;
|
||||
typedef btree_container<Tree> super_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::size_type size_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_multi_container(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_multi_container(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_multi_container(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
insert(b, e);
|
||||
}
|
||||
|
||||
// Lookup routines.
|
||||
iterator find(const key_type &key) {
|
||||
return this->tree_.find_multi(key);
|
||||
}
|
||||
const_iterator find(const key_type &key) const {
|
||||
return this->tree_.find_multi(key);
|
||||
}
|
||||
size_type count(const key_type &key) const {
|
||||
return this->tree_.count_multi(key);
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
iterator insert(const value_type &x) {
|
||||
return this->tree_.insert_multi(x);
|
||||
}
|
||||
iterator insert(iterator position, const value_type &x) {
|
||||
return this->tree_.insert_multi(position, x);
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert(InputIterator b, InputIterator e) {
|
||||
this->tree_.insert_multi(b, e);
|
||||
}
|
||||
|
||||
// Deletion routines.
|
||||
int erase(const key_type &key) {
|
||||
return this->tree_.erase_multi(key);
|
||||
}
|
||||
// Erase the specified iterator from the btree. The iterator must be valid
|
||||
// (i.e. not equal to end()). Return an iterator pointing to the node after
|
||||
// the one that was erased (or end() if none exists).
|
||||
iterator erase(const iterator &iter) {
|
||||
return this->tree_.erase(iter);
|
||||
}
|
||||
void erase(const iterator &first, const iterator &last) {
|
||||
this->tree_.erase(first, last);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_BTREE_CONTAINER_H__
|
130
lib/xdelta3/cpp-btree/btree_map.h
Normal file
130
lib/xdelta3/cpp-btree/btree_map.h
Normal file
|
@ -0,0 +1,130 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// A btree_map<> implements the STL unique sorted associative container
|
||||
// interface and the pair associative container interface (a.k.a map<>) using a
|
||||
// btree. A btree_multimap<> implements the STL multiple sorted associative
|
||||
// container interface and the pair associtive container interface (a.k.a
|
||||
// multimap<>) using a btree. See btree.h for details of the btree
|
||||
// implementation and caveats.
|
||||
|
||||
#ifndef UTIL_BTREE_BTREE_MAP_H__
|
||||
#define UTIL_BTREE_BTREE_MAP_H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "btree.h"
|
||||
#include "btree_container.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The btree_map class is needed mainly for its constructors.
|
||||
template <typename Key, typename Value,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<std::pair<const Key, Value> >,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_map : public btree_map_container<
|
||||
btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_map<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_map_params<
|
||||
Key, Value, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_map_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_map(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_map(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_map(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename V, typename C, typename A, int N>
|
||||
inline void swap(btree_map<K, V, C, A, N> &x,
|
||||
btree_map<K, V, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
// The btree_multimap class is needed mainly for its constructors.
|
||||
template <typename Key, typename Value,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<std::pair<const Key, Value> >,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_multimap : public btree_multi_container<
|
||||
btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_multimap<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_map_params<
|
||||
Key, Value, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_multi_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
typedef typename btree_type::data_type data_type;
|
||||
typedef typename btree_type::mapped_type mapped_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_multimap(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_multimap(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_multimap(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename V, typename C, typename A, int N>
|
||||
inline void swap(btree_multimap<K, V, C, A, N> &x,
|
||||
btree_multimap<K, V, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_BTREE_MAP_H__
|
121
lib/xdelta3/cpp-btree/btree_set.h
Normal file
121
lib/xdelta3/cpp-btree/btree_set.h
Normal file
|
@ -0,0 +1,121 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// A btree_set<> implements the STL unique sorted associative container
|
||||
// interface (a.k.a set<>) using a btree. A btree_multiset<> implements the STL
|
||||
// multiple sorted associative container interface (a.k.a multiset<>) using a
|
||||
// btree. See btree.h for details of the btree implementation and caveats.
|
||||
|
||||
#ifndef UTIL_BTREE_BTREE_SET_H__
|
||||
#define UTIL_BTREE_BTREE_SET_H__
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "btree.h"
|
||||
#include "btree_container.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The btree_set class is needed mainly for its constructors.
|
||||
template <typename Key,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<Key>,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_set : public btree_unique_container<
|
||||
btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_set<Key, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_unique_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_set(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_set(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_set(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename C, typename A, int N>
|
||||
inline void swap(btree_set<K, C, A, N> &x, btree_set<K, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
// The btree_multiset class is needed mainly for its constructors.
|
||||
template <typename Key,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<Key>,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_multiset : public btree_multi_container<
|
||||
btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_multiset<Key, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_multi_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_multiset(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_multiset(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_multiset(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename C, typename A, int N>
|
||||
inline void swap(btree_multiset<K, C, A, N> &x,
|
||||
btree_multiset<K, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_BTREE_SET_H__
|
270
lib/xdelta3/cpp-btree/btree_test.cc
Normal file
270
lib/xdelta3/cpp-btree/btree_test.cc
Normal file
|
@ -0,0 +1,270 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "btree_map.h"
|
||||
#include "btree_set.h"
|
||||
#include "btree_test.h"
|
||||
|
||||
namespace btree {
|
||||
namespace {
|
||||
|
||||
template <typename K, int N>
|
||||
void SetTest() {
|
||||
typedef TestAllocator<K> TestAlloc;
|
||||
ASSERT_EQ(sizeof(btree_set<K>), sizeof(void*));
|
||||
BtreeTest<btree_set<K, std::less<K>, std::allocator<K>, N>, std::set<K> >();
|
||||
BtreeAllocatorTest<btree_set<K, std::less<K>, TestAlloc, N> >();
|
||||
}
|
||||
|
||||
template <typename K, int N>
|
||||
void MapTest() {
|
||||
typedef TestAllocator<K> TestAlloc;
|
||||
ASSERT_EQ(sizeof(btree_map<K, K>), sizeof(void*));
|
||||
BtreeTest<btree_map<K, K, std::less<K>, std::allocator<K>, N>, std::map<K, K> >();
|
||||
BtreeAllocatorTest<btree_map<K, K, std::less<K>, TestAlloc, N> >();
|
||||
BtreeMapTest<btree_map<K, K, std::less<K>, std::allocator<K>, N> >();
|
||||
}
|
||||
|
||||
TEST(Btree, set_int32_32) { SetTest<int32_t, 32>(); }
|
||||
TEST(Btree, set_int32_64) { SetTest<int32_t, 64>(); }
|
||||
TEST(Btree, set_int32_128) { SetTest<int32_t, 128>(); }
|
||||
TEST(Btree, set_int32_256) { SetTest<int32_t, 256>(); }
|
||||
TEST(Btree, set_int64_256) { SetTest<int64_t, 256>(); }
|
||||
TEST(Btree, set_string_256) { SetTest<std::string, 256>(); }
|
||||
TEST(Btree, set_pair_256) { SetTest<std::pair<int, int>, 256>(); }
|
||||
TEST(Btree, map_int32_256) { MapTest<int32_t, 256>(); }
|
||||
TEST(Btree, map_int64_256) { MapTest<int64_t, 256>(); }
|
||||
TEST(Btree, map_string_256) { MapTest<std::string, 256>(); }
|
||||
TEST(Btree, map_pair_256) { MapTest<std::pair<int, int>, 256>(); }
|
||||
|
||||
// Large-node tests
|
||||
TEST(Btree, map_int32_1024) { MapTest<int32_t, 1024>(); }
|
||||
TEST(Btree, map_int32_1032) { MapTest<int32_t, 1032>(); }
|
||||
TEST(Btree, map_int32_1040) { MapTest<int32_t, 1040>(); }
|
||||
TEST(Btree, map_int32_1048) { MapTest<int32_t, 1048>(); }
|
||||
TEST(Btree, map_int32_1056) { MapTest<int32_t, 1056>(); }
|
||||
|
||||
TEST(Btree, map_int32_2048) { MapTest<int32_t, 2048>(); }
|
||||
TEST(Btree, map_int32_4096) { MapTest<int32_t, 4096>(); }
|
||||
TEST(Btree, set_int32_1024) { SetTest<int32_t, 1024>(); }
|
||||
TEST(Btree, set_int32_2048) { SetTest<int32_t, 2048>(); }
|
||||
TEST(Btree, set_int32_4096) { SetTest<int32_t, 4096>(); }
|
||||
TEST(Btree, map_string_1024) { MapTest<std::string, 1024>(); }
|
||||
TEST(Btree, map_string_2048) { MapTest<std::string, 2048>(); }
|
||||
TEST(Btree, map_string_4096) { MapTest<std::string, 4096>(); }
|
||||
TEST(Btree, set_string_1024) { SetTest<std::string, 1024>(); }
|
||||
TEST(Btree, set_string_2048) { SetTest<std::string, 2048>(); }
|
||||
TEST(Btree, set_string_4096) { SetTest<std::string, 4096>(); }
|
||||
|
||||
template <typename K, int N>
|
||||
void MultiSetTest() {
|
||||
typedef TestAllocator<K> TestAlloc;
|
||||
ASSERT_EQ(sizeof(btree_multiset<K>), sizeof(void*));
|
||||
BtreeMultiTest<btree_multiset<K, std::less<K>, std::allocator<K>, N>,
|
||||
std::multiset<K> >();
|
||||
BtreeAllocatorTest<btree_multiset<K, std::less<K>, TestAlloc, N> >();
|
||||
}
|
||||
|
||||
template <typename K, int N>
|
||||
void MultiMapTest() {
|
||||
typedef TestAllocator<K> TestAlloc;
|
||||
ASSERT_EQ(sizeof(btree_multimap<K, K>), sizeof(void*));
|
||||
BtreeMultiTest<btree_multimap<K, K, std::less<K>, std::allocator<K>, N>,
|
||||
std::multimap<K, K> >();
|
||||
BtreeMultiMapTest<btree_multimap<K, K, std::less<K>, std::allocator<K>, N> >();
|
||||
BtreeAllocatorTest<btree_multimap<K, K, std::less<K>, TestAlloc, N> >();
|
||||
}
|
||||
|
||||
TEST(Btree, multiset_int32_256) { MultiSetTest<int32_t, 256>(); }
|
||||
TEST(Btree, multiset_int64_256) { MultiSetTest<int64_t, 256>(); }
|
||||
TEST(Btree, multiset_string_256) { MultiSetTest<std::string, 256>(); }
|
||||
TEST(Btree, multiset_pair_256) { MultiSetTest<std::pair<int, int>, 256>(); }
|
||||
TEST(Btree, multimap_int32_256) { MultiMapTest<int32_t, 256>(); }
|
||||
TEST(Btree, multimap_int64_256) { MultiMapTest<int64_t, 256>(); }
|
||||
TEST(Btree, multimap_string_256) { MultiMapTest<std::string, 256>(); }
|
||||
TEST(Btree, multimap_pair_256) { MultiMapTest<std::pair<int, int>, 256>(); }
|
||||
|
||||
// Large-node tests
|
||||
TEST(Btree, multimap_int32_1024) { MultiMapTest<int32_t, 1024>(); }
|
||||
TEST(Btree, multimap_int32_2048) { MultiMapTest<int32_t, 2048>(); }
|
||||
TEST(Btree, multimap_int32_4096) { MultiMapTest<int32_t, 4096>(); }
|
||||
TEST(Btree, multiset_int32_1024) { MultiSetTest<int32_t, 1024>(); }
|
||||
TEST(Btree, multiset_int32_2048) { MultiSetTest<int32_t, 2048>(); }
|
||||
TEST(Btree, multiset_int32_4096) { MultiSetTest<int32_t, 4096>(); }
|
||||
TEST(Btree, multimap_string_1024) { MultiMapTest<std::string, 1024>(); }
|
||||
TEST(Btree, multimap_string_2048) { MultiMapTest<std::string, 2048>(); }
|
||||
TEST(Btree, multimap_string_4096) { MultiMapTest<std::string, 4096>(); }
|
||||
TEST(Btree, multiset_string_1024) { MultiSetTest<std::string, 1024>(); }
|
||||
TEST(Btree, multiset_string_2048) { MultiSetTest<std::string, 2048>(); }
|
||||
TEST(Btree, multiset_string_4096) { MultiSetTest<std::string, 4096>(); }
|
||||
|
||||
// Verify that swapping btrees swaps the key comparision functors.
|
||||
struct SubstringLess {
|
||||
SubstringLess() : n(2) {}
|
||||
SubstringLess(size_t length)
|
||||
: n(length) {
|
||||
}
|
||||
bool operator()(const std::string &a, const std::string &b) const {
|
||||
std::string as(a.data(), std::min(n, a.size()));
|
||||
std::string bs(b.data(), std::min(n, b.size()));
|
||||
return as < bs;
|
||||
}
|
||||
size_t n;
|
||||
};
|
||||
|
||||
TEST(Btree, SwapKeyCompare) {
|
||||
typedef btree_set<std::string, SubstringLess> SubstringSet;
|
||||
SubstringSet s1(SubstringLess(1), SubstringSet::allocator_type());
|
||||
SubstringSet s2(SubstringLess(2), SubstringSet::allocator_type());
|
||||
|
||||
ASSERT_TRUE(s1.insert("a").second);
|
||||
ASSERT_FALSE(s1.insert("aa").second);
|
||||
|
||||
ASSERT_TRUE(s2.insert("a").second);
|
||||
ASSERT_TRUE(s2.insert("aa").second);
|
||||
ASSERT_FALSE(s2.insert("aaa").second);
|
||||
|
||||
swap(s1, s2);
|
||||
|
||||
ASSERT_TRUE(s1.insert("b").second);
|
||||
ASSERT_TRUE(s1.insert("bb").second);
|
||||
ASSERT_FALSE(s1.insert("bbb").second);
|
||||
|
||||
ASSERT_TRUE(s2.insert("b").second);
|
||||
ASSERT_FALSE(s2.insert("bb").second);
|
||||
}
|
||||
|
||||
TEST(Btree, UpperBoundRegression) {
|
||||
// Regress a bug where upper_bound would default-construct a new key_compare
|
||||
// instead of copying the existing one.
|
||||
typedef btree_set<std::string, SubstringLess> SubstringSet;
|
||||
SubstringSet my_set(SubstringLess(3));
|
||||
my_set.insert("aab");
|
||||
my_set.insert("abb");
|
||||
// We call upper_bound("aaa"). If this correctly uses the length 3
|
||||
// comparator, aaa < aab < abb, so we should get aab as the result.
|
||||
// If it instead uses the default-constructed length 2 comparator,
|
||||
// aa == aa < ab, so we'll get abb as our result.
|
||||
SubstringSet::iterator it = my_set.upper_bound("aaa");
|
||||
ASSERT_TRUE(it != my_set.end());
|
||||
EXPECT_EQ("aab", *it);
|
||||
}
|
||||
|
||||
|
||||
TEST(Btree, IteratorIncrementBy) {
|
||||
// Test that increment_by returns the same position as increment.
|
||||
const int kSetSize = 2341;
|
||||
btree_set<int32_t> my_set;
|
||||
for (int i = 0; i < kSetSize; ++i) {
|
||||
my_set.insert(i);
|
||||
}
|
||||
|
||||
{
|
||||
// Simple increment vs. increment by.
|
||||
btree_set<int32_t>::iterator a = my_set.begin();
|
||||
btree_set<int32_t>::iterator b = my_set.begin();
|
||||
a.increment();
|
||||
b.increment_by(1);
|
||||
EXPECT_EQ(*a, *b);
|
||||
}
|
||||
|
||||
btree_set<int32_t>::iterator a = my_set.begin();
|
||||
for (int i = 1; i < kSetSize; ++i) {
|
||||
++a;
|
||||
// increment_by
|
||||
btree_set<int32_t>::iterator b = my_set.begin();
|
||||
b.increment_by(i);
|
||||
EXPECT_EQ(*a, *b) << ": i=" << i;
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Btree, Comparison) {
|
||||
const int kSetSize = 1201;
|
||||
btree_set<int64_t> my_set;
|
||||
for (int i = 0; i < kSetSize; ++i) {
|
||||
my_set.insert(i);
|
||||
}
|
||||
btree_set<int64_t> my_set_copy(my_set);
|
||||
EXPECT_TRUE(my_set_copy == my_set);
|
||||
EXPECT_TRUE(my_set == my_set_copy);
|
||||
EXPECT_FALSE(my_set_copy != my_set);
|
||||
EXPECT_FALSE(my_set != my_set_copy);
|
||||
|
||||
my_set.insert(kSetSize);
|
||||
EXPECT_FALSE(my_set_copy == my_set);
|
||||
EXPECT_FALSE(my_set == my_set_copy);
|
||||
EXPECT_TRUE(my_set_copy != my_set);
|
||||
EXPECT_TRUE(my_set != my_set_copy);
|
||||
|
||||
my_set.erase(kSetSize - 1);
|
||||
EXPECT_FALSE(my_set_copy == my_set);
|
||||
EXPECT_FALSE(my_set == my_set_copy);
|
||||
EXPECT_TRUE(my_set_copy != my_set);
|
||||
EXPECT_TRUE(my_set != my_set_copy);
|
||||
|
||||
btree_map<std::string, int64_t> my_map;
|
||||
for (int i = 0; i < kSetSize; ++i) {
|
||||
my_map[std::string(i, 'a')] = i;
|
||||
}
|
||||
btree_map<std::string, int64_t> my_map_copy(my_map);
|
||||
EXPECT_TRUE(my_map_copy == my_map);
|
||||
EXPECT_TRUE(my_map == my_map_copy);
|
||||
EXPECT_FALSE(my_map_copy != my_map);
|
||||
EXPECT_FALSE(my_map != my_map_copy);
|
||||
|
||||
++my_map_copy[std::string(7, 'a')];
|
||||
EXPECT_FALSE(my_map_copy == my_map);
|
||||
EXPECT_FALSE(my_map == my_map_copy);
|
||||
EXPECT_TRUE(my_map_copy != my_map);
|
||||
EXPECT_TRUE(my_map != my_map_copy);
|
||||
|
||||
my_map_copy = my_map;
|
||||
my_map["hello"] = kSetSize;
|
||||
EXPECT_FALSE(my_map_copy == my_map);
|
||||
EXPECT_FALSE(my_map == my_map_copy);
|
||||
EXPECT_TRUE(my_map_copy != my_map);
|
||||
EXPECT_TRUE(my_map != my_map_copy);
|
||||
|
||||
my_map.erase(std::string(kSetSize - 1, 'a'));
|
||||
EXPECT_FALSE(my_map_copy == my_map);
|
||||
EXPECT_FALSE(my_map == my_map_copy);
|
||||
EXPECT_TRUE(my_map_copy != my_map);
|
||||
EXPECT_TRUE(my_map != my_map_copy);
|
||||
}
|
||||
|
||||
TEST(Btree, RangeCtorSanity) {
|
||||
typedef btree_set<int, std::less<int>, std::allocator<int>, 256> test_set;
|
||||
typedef btree_map<int, int, std::less<int>, std::allocator<int>, 256>
|
||||
test_map;
|
||||
typedef btree_multiset<int, std::less<int>, std::allocator<int>, 256>
|
||||
test_mset;
|
||||
typedef btree_multimap<int, int, std::less<int>, std::allocator<int>, 256>
|
||||
test_mmap;
|
||||
std::vector<int> ivec;
|
||||
ivec.push_back(1);
|
||||
std::map<int, int> imap;
|
||||
imap.insert(std::make_pair(1, 2));
|
||||
test_mset tmset(ivec.begin(), ivec.end());
|
||||
test_mmap tmmap(imap.begin(), imap.end());
|
||||
test_set tset(ivec.begin(), ivec.end());
|
||||
test_map tmap(imap.begin(), imap.end());
|
||||
EXPECT_EQ(1, tmset.size());
|
||||
EXPECT_EQ(1, tmmap.size());
|
||||
EXPECT_EQ(1, tset.size());
|
||||
EXPECT_EQ(1, tmap.size());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace btree
|
940
lib/xdelta3/cpp-btree/btree_test.h
Normal file
940
lib/xdelta3/cpp-btree/btree_test.h
Normal file
|
@ -0,0 +1,940 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef UTIL_BTREE_BTREE_TEST_H__
|
||||
#define UTIL_BTREE_BTREE_TEST_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <iosfwd>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "gflags/gflags.h"
|
||||
#include "btree_container.h"
|
||||
|
||||
DECLARE_int32(test_values);
|
||||
DECLARE_int32(benchmark_values);
|
||||
|
||||
namespace std {
|
||||
|
||||
// Provide operator<< support for std::pair<T, U>.
|
||||
template <typename T, typename U>
|
||||
ostream& operator<<(ostream &os, const std::pair<T, U> &p) {
|
||||
os << "(" << p.first << "," << p.second << ")";
|
||||
return os;
|
||||
}
|
||||
|
||||
// Provide pair equality testing that works as long as x.first is comparable to
|
||||
// y.first and x.second is comparable to y.second. Needed in the test for
|
||||
// comparing std::pair<T, U> to std::pair<const T, U>.
|
||||
template <typename T, typename U, typename V, typename W>
|
||||
bool operator==(const std::pair<T, U> &x, const std::pair<V, W> &y) {
|
||||
return x.first == y.first && x.second == y.second;
|
||||
}
|
||||
|
||||
// Partial specialization of remove_const that propagates the removal through
|
||||
// std::pair.
|
||||
template <typename T, typename U>
|
||||
struct remove_const<pair<T, U> > {
|
||||
typedef pair<typename remove_const<T>::type,
|
||||
typename remove_const<U>::type> type;
|
||||
};
|
||||
|
||||
} // namespace std
|
||||
|
||||
namespace btree {
|
||||
|
||||
// Select the first member of a pair.
|
||||
template <class _Pair>
|
||||
struct select1st : public std::unary_function<_Pair, typename _Pair::first_type> {
|
||||
const typename _Pair::first_type& operator()(const _Pair& __x) const {
|
||||
return __x.first;
|
||||
}
|
||||
};
|
||||
|
||||
// Utility class to provide an accessor for a key given a value. The default
|
||||
// behavior is to treat the value as a pair and return the first element.
|
||||
template <typename K, typename V>
|
||||
struct KeyOfValue {
|
||||
typedef select1st<V> type;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct identity {
|
||||
inline const T& operator()(const T& t) const { return t; }
|
||||
};
|
||||
|
||||
// Partial specialization of KeyOfValue class for when the key and value are
|
||||
// the same type such as in set<> and btree_set<>.
|
||||
template <typename K>
|
||||
struct KeyOfValue<K, K> {
|
||||
typedef identity<K> type;
|
||||
};
|
||||
|
||||
// Counts the number of occurances of "c" in a buffer.
|
||||
inline ptrdiff_t strcount(const char* buf_begin, const char* buf_end, char c) {
|
||||
if (buf_begin == NULL)
|
||||
return 0;
|
||||
if (buf_end <= buf_begin)
|
||||
return 0;
|
||||
ptrdiff_t num = 0;
|
||||
for (const char* bp = buf_begin; bp != buf_end; bp++) {
|
||||
if (*bp == c)
|
||||
num++;
|
||||
}
|
||||
return num;
|
||||
}
|
||||
|
||||
// for when the string is not null-terminated.
|
||||
inline ptrdiff_t strcount(const char* buf, size_t len, char c) {
|
||||
return strcount(buf, buf + len, c);
|
||||
}
|
||||
|
||||
inline ptrdiff_t strcount(const std::string& buf, char c) {
|
||||
return strcount(buf.c_str(), buf.size(), c);
|
||||
}
|
||||
|
||||
// The base class for a sorted associative container checker. TreeType is the
|
||||
// container type to check and CheckerType is the container type to check
|
||||
// against. TreeType is expected to be btree_{set,map,multiset,multimap} and
|
||||
// CheckerType is expected to be {set,map,multiset,multimap}.
|
||||
template <typename TreeType, typename CheckerType>
|
||||
class base_checker {
|
||||
typedef base_checker<TreeType, CheckerType> self_type;
|
||||
|
||||
public:
|
||||
typedef typename TreeType::key_type key_type;
|
||||
typedef typename TreeType::value_type value_type;
|
||||
typedef typename TreeType::key_compare key_compare;
|
||||
typedef typename TreeType::pointer pointer;
|
||||
typedef typename TreeType::const_pointer const_pointer;
|
||||
typedef typename TreeType::reference reference;
|
||||
typedef typename TreeType::const_reference const_reference;
|
||||
typedef typename TreeType::size_type size_type;
|
||||
typedef typename TreeType::difference_type difference_type;
|
||||
typedef typename TreeType::iterator iterator;
|
||||
typedef typename TreeType::const_iterator const_iterator;
|
||||
typedef typename TreeType::reverse_iterator reverse_iterator;
|
||||
typedef typename TreeType::const_reverse_iterator const_reverse_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
base_checker()
|
||||
: const_tree_(tree_) {
|
||||
}
|
||||
// Copy constructor.
|
||||
base_checker(const self_type &x)
|
||||
: tree_(x.tree_),
|
||||
const_tree_(tree_),
|
||||
checker_(x.checker_) {
|
||||
}
|
||||
// Range constructor.
|
||||
template <typename InputIterator>
|
||||
base_checker(InputIterator b, InputIterator e)
|
||||
: tree_(b, e),
|
||||
const_tree_(tree_),
|
||||
checker_(b, e) {
|
||||
}
|
||||
|
||||
// Iterator routines.
|
||||
iterator begin() { return tree_.begin(); }
|
||||
const_iterator begin() const { return tree_.begin(); }
|
||||
iterator end() { return tree_.end(); }
|
||||
const_iterator end() const { return tree_.end(); }
|
||||
reverse_iterator rbegin() { return tree_.rbegin(); }
|
||||
const_reverse_iterator rbegin() const { return tree_.rbegin(); }
|
||||
reverse_iterator rend() { return tree_.rend(); }
|
||||
const_reverse_iterator rend() const { return tree_.rend(); }
|
||||
|
||||
// Helper routines.
|
||||
template <typename IterType, typename CheckerIterType>
|
||||
IterType iter_check(
|
||||
IterType tree_iter, CheckerIterType checker_iter) const {
|
||||
if (tree_iter == tree_.end()) {
|
||||
EXPECT_EQ(checker_iter, checker_.end());
|
||||
} else {
|
||||
EXPECT_EQ(*tree_iter, *checker_iter);
|
||||
}
|
||||
return tree_iter;
|
||||
}
|
||||
template <typename IterType, typename CheckerIterType>
|
||||
IterType riter_check(
|
||||
IterType tree_iter, CheckerIterType checker_iter) const {
|
||||
if (tree_iter == tree_.rend()) {
|
||||
EXPECT_EQ(checker_iter, checker_.rend());
|
||||
} else {
|
||||
EXPECT_EQ(*tree_iter, *checker_iter);
|
||||
}
|
||||
return tree_iter;
|
||||
}
|
||||
void value_check(const value_type &x) {
|
||||
typename KeyOfValue<typename TreeType::key_type,
|
||||
typename TreeType::value_type>::type key_of_value;
|
||||
const key_type &key = key_of_value(x);
|
||||
EXPECT_EQ(*find(key), x);
|
||||
lower_bound(key);
|
||||
upper_bound(key);
|
||||
equal_range(key);
|
||||
count(key);
|
||||
}
|
||||
void erase_check(const key_type &key) {
|
||||
EXPECT_TRUE(tree_.find(key) == const_tree_.end());
|
||||
EXPECT_TRUE(const_tree_.find(key) == tree_.end());
|
||||
EXPECT_TRUE(tree_.equal_range(key).first ==
|
||||
const_tree_.equal_range(key).second);
|
||||
}
|
||||
|
||||
// Lookup routines.
|
||||
iterator lower_bound(const key_type &key) {
|
||||
return iter_check(tree_.lower_bound(key), checker_.lower_bound(key));
|
||||
}
|
||||
const_iterator lower_bound(const key_type &key) const {
|
||||
return iter_check(tree_.lower_bound(key), checker_.lower_bound(key));
|
||||
}
|
||||
iterator upper_bound(const key_type &key) {
|
||||
return iter_check(tree_.upper_bound(key), checker_.upper_bound(key));
|
||||
}
|
||||
const_iterator upper_bound(const key_type &key) const {
|
||||
return iter_check(tree_.upper_bound(key), checker_.upper_bound(key));
|
||||
}
|
||||
std::pair<iterator,iterator> equal_range(const key_type &key) {
|
||||
std::pair<typename CheckerType::iterator,
|
||||
typename CheckerType::iterator> checker_res =
|
||||
checker_.equal_range(key);
|
||||
std::pair<iterator, iterator> tree_res = tree_.equal_range(key);
|
||||
iter_check(tree_res.first, checker_res.first);
|
||||
iter_check(tree_res.second, checker_res.second);
|
||||
return tree_res;
|
||||
}
|
||||
std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const {
|
||||
std::pair<typename CheckerType::const_iterator,
|
||||
typename CheckerType::const_iterator> checker_res =
|
||||
checker_.equal_range(key);
|
||||
std::pair<const_iterator, const_iterator> tree_res = tree_.equal_range(key);
|
||||
iter_check(tree_res.first, checker_res.first);
|
||||
iter_check(tree_res.second, checker_res.second);
|
||||
return tree_res;
|
||||
}
|
||||
iterator find(const key_type &key) {
|
||||
return iter_check(tree_.find(key), checker_.find(key));
|
||||
}
|
||||
const_iterator find(const key_type &key) const {
|
||||
return iter_check(tree_.find(key), checker_.find(key));
|
||||
}
|
||||
size_type count(const key_type &key) const {
|
||||
size_type res = checker_.count(key);
|
||||
EXPECT_EQ(res, tree_.count(key));
|
||||
return res;
|
||||
}
|
||||
|
||||
// Assignment operator.
|
||||
self_type& operator=(const self_type &x) {
|
||||
tree_ = x.tree_;
|
||||
checker_ = x.checker_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Deletion routines.
|
||||
int erase(const key_type &key) {
|
||||
int size = tree_.size();
|
||||
int res = checker_.erase(key);
|
||||
EXPECT_EQ(res, tree_.count(key));
|
||||
EXPECT_EQ(res, tree_.erase(key));
|
||||
EXPECT_EQ(tree_.count(key), 0);
|
||||
EXPECT_EQ(tree_.size(), size - res);
|
||||
erase_check(key);
|
||||
return res;
|
||||
}
|
||||
iterator erase(iterator iter) {
|
||||
key_type key = iter.key();
|
||||
int size = tree_.size();
|
||||
int count = tree_.count(key);
|
||||
typename CheckerType::iterator checker_iter = checker_.find(key);
|
||||
for (iterator tmp(tree_.find(key)); tmp != iter; ++tmp) {
|
||||
++checker_iter;
|
||||
}
|
||||
typename CheckerType::iterator checker_next = checker_iter;
|
||||
++checker_next;
|
||||
checker_.erase(checker_iter);
|
||||
iter = tree_.erase(iter);
|
||||
EXPECT_EQ(tree_.size(), checker_.size());
|
||||
EXPECT_EQ(tree_.size(), size - 1);
|
||||
EXPECT_EQ(tree_.count(key), count - 1);
|
||||
if (count == 1) {
|
||||
erase_check(key);
|
||||
}
|
||||
return iter_check(iter, checker_next);
|
||||
}
|
||||
|
||||
void erase(iterator begin, iterator end) {
|
||||
int size = tree_.size();
|
||||
int count = distance(begin, end);
|
||||
typename CheckerType::iterator checker_begin = checker_.find(begin.key());
|
||||
for (iterator tmp(tree_.find(begin.key())); tmp != begin; ++tmp) {
|
||||
++checker_begin;
|
||||
}
|
||||
typename CheckerType::iterator checker_end =
|
||||
end == tree_.end() ? checker_.end() : checker_.find(end.key());
|
||||
if (end != tree_.end()) {
|
||||
for (iterator tmp(tree_.find(end.key())); tmp != end; ++tmp) {
|
||||
++checker_end;
|
||||
}
|
||||
}
|
||||
checker_.erase(checker_begin, checker_end);
|
||||
tree_.erase(begin, end);
|
||||
EXPECT_EQ(tree_.size(), checker_.size());
|
||||
EXPECT_EQ(tree_.size(), size - count);
|
||||
}
|
||||
|
||||
// Utility routines.
|
||||
void clear() {
|
||||
tree_.clear();
|
||||
checker_.clear();
|
||||
}
|
||||
void swap(self_type &x) {
|
||||
tree_.swap(x.tree_);
|
||||
checker_.swap(x.checker_);
|
||||
}
|
||||
|
||||
void verify() const {
|
||||
tree_.verify();
|
||||
EXPECT_EQ(tree_.size(), checker_.size());
|
||||
|
||||
// Move through the forward iterators using increment.
|
||||
typename CheckerType::const_iterator
|
||||
checker_iter(checker_.begin());
|
||||
const_iterator tree_iter(tree_.begin());
|
||||
for (; tree_iter != tree_.end();
|
||||
++tree_iter, ++checker_iter) {
|
||||
EXPECT_EQ(*tree_iter, *checker_iter);
|
||||
}
|
||||
|
||||
// Move through the forward iterators using decrement.
|
||||
for (int n = tree_.size() - 1; n >= 0; --n) {
|
||||
iter_check(tree_iter, checker_iter);
|
||||
--tree_iter;
|
||||
--checker_iter;
|
||||
}
|
||||
EXPECT_TRUE(tree_iter == tree_.begin());
|
||||
EXPECT_TRUE(checker_iter == checker_.begin());
|
||||
|
||||
// Move through the reverse iterators using increment.
|
||||
typename CheckerType::const_reverse_iterator
|
||||
checker_riter(checker_.rbegin());
|
||||
const_reverse_iterator tree_riter(tree_.rbegin());
|
||||
for (; tree_riter != tree_.rend();
|
||||
++tree_riter, ++checker_riter) {
|
||||
EXPECT_EQ(*tree_riter, *checker_riter);
|
||||
}
|
||||
|
||||
// Move through the reverse iterators using decrement.
|
||||
for (int n = tree_.size() - 1; n >= 0; --n) {
|
||||
riter_check(tree_riter, checker_riter);
|
||||
--tree_riter;
|
||||
--checker_riter;
|
||||
}
|
||||
EXPECT_EQ(tree_riter, tree_.rbegin());
|
||||
EXPECT_EQ(checker_riter, checker_.rbegin());
|
||||
}
|
||||
|
||||
// Access to the underlying btree.
|
||||
const TreeType& tree() const { return tree_; }
|
||||
|
||||
// Size routines.
|
||||
size_type size() const {
|
||||
EXPECT_EQ(tree_.size(), checker_.size());
|
||||
return tree_.size();
|
||||
}
|
||||
size_type max_size() const { return tree_.max_size(); }
|
||||
bool empty() const {
|
||||
EXPECT_EQ(tree_.empty(), checker_.empty());
|
||||
return tree_.empty();
|
||||
}
|
||||
size_type height() const { return tree_.height(); }
|
||||
size_type internal_nodes() const { return tree_.internal_nodes(); }
|
||||
size_type leaf_nodes() const { return tree_.leaf_nodes(); }
|
||||
size_type nodes() const { return tree_.nodes(); }
|
||||
size_type bytes_used() const { return tree_.bytes_used(); }
|
||||
double fullness() const { return tree_.fullness(); }
|
||||
double overhead() const { return tree_.overhead(); }
|
||||
|
||||
protected:
|
||||
TreeType tree_;
|
||||
const TreeType &const_tree_;
|
||||
CheckerType checker_;
|
||||
};
|
||||
|
||||
// A checker for unique sorted associative containers. TreeType is expected to
|
||||
// be btree_{set,map} and CheckerType is expected to be {set,map}.
|
||||
template <typename TreeType, typename CheckerType>
|
||||
class unique_checker : public base_checker<TreeType, CheckerType> {
|
||||
typedef base_checker<TreeType, CheckerType> super_type;
|
||||
typedef unique_checker<TreeType, CheckerType> self_type;
|
||||
|
||||
public:
|
||||
typedef typename super_type::iterator iterator;
|
||||
typedef typename super_type::value_type value_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
unique_checker()
|
||||
: super_type() {
|
||||
}
|
||||
// Copy constructor.
|
||||
unique_checker(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
unique_checker(InputIterator b, InputIterator e)
|
||||
: super_type(b, e) {
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
std::pair<iterator,bool> insert(const value_type &x) {
|
||||
int size = this->tree_.size();
|
||||
std::pair<typename CheckerType::iterator,bool> checker_res =
|
||||
this->checker_.insert(x);
|
||||
std::pair<iterator,bool> tree_res = this->tree_.insert(x);
|
||||
EXPECT_EQ(*tree_res.first, *checker_res.first);
|
||||
EXPECT_EQ(tree_res.second, checker_res.second);
|
||||
EXPECT_EQ(this->tree_.size(), this->checker_.size());
|
||||
EXPECT_EQ(this->tree_.size(), size + tree_res.second);
|
||||
return tree_res;
|
||||
}
|
||||
iterator insert(iterator position, const value_type &x) {
|
||||
int size = this->tree_.size();
|
||||
std::pair<typename CheckerType::iterator,bool> checker_res =
|
||||
this->checker_.insert(x);
|
||||
iterator tree_res = this->tree_.insert(position, x);
|
||||
EXPECT_EQ(*tree_res, *checker_res.first);
|
||||
EXPECT_EQ(this->tree_.size(), this->checker_.size());
|
||||
EXPECT_EQ(this->tree_.size(), size + checker_res.second);
|
||||
return tree_res;
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert(InputIterator b, InputIterator e) {
|
||||
for (; b != e; ++b) {
|
||||
insert(*b);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// A checker for multiple sorted associative containers. TreeType is expected
|
||||
// to be btree_{multiset,multimap} and CheckerType is expected to be
|
||||
// {multiset,multimap}.
|
||||
template <typename TreeType, typename CheckerType>
|
||||
class multi_checker : public base_checker<TreeType, CheckerType> {
|
||||
typedef base_checker<TreeType, CheckerType> super_type;
|
||||
typedef multi_checker<TreeType, CheckerType> self_type;
|
||||
|
||||
public:
|
||||
typedef typename super_type::iterator iterator;
|
||||
typedef typename super_type::value_type value_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
multi_checker()
|
||||
: super_type() {
|
||||
}
|
||||
// Copy constructor.
|
||||
multi_checker(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
multi_checker(InputIterator b, InputIterator e)
|
||||
: super_type(b, e) {
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
iterator insert(const value_type &x) {
|
||||
int size = this->tree_.size();
|
||||
typename CheckerType::iterator checker_res = this->checker_.insert(x);
|
||||
iterator tree_res = this->tree_.insert(x);
|
||||
EXPECT_EQ(*tree_res, *checker_res);
|
||||
EXPECT_EQ(this->tree_.size(), this->checker_.size());
|
||||
EXPECT_EQ(this->tree_.size(), size + 1);
|
||||
return tree_res;
|
||||
}
|
||||
iterator insert(iterator position, const value_type &x) {
|
||||
int size = this->tree_.size();
|
||||
typename CheckerType::iterator checker_res = this->checker_.insert(x);
|
||||
iterator tree_res = this->tree_.insert(position, x);
|
||||
EXPECT_EQ(*tree_res, *checker_res);
|
||||
EXPECT_EQ(this->tree_.size(), this->checker_.size());
|
||||
EXPECT_EQ(this->tree_.size(), size + 1);
|
||||
return tree_res;
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert(InputIterator b, InputIterator e) {
|
||||
for (; b != e; ++b) {
|
||||
insert(*b);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
char* GenerateDigits(char buf[16], int val, int maxval) {
|
||||
EXPECT_LE(val, maxval);
|
||||
int p = 15;
|
||||
buf[p--] = 0;
|
||||
while (maxval > 0) {
|
||||
buf[p--] = '0' + (val % 10);
|
||||
val /= 10;
|
||||
maxval /= 10;
|
||||
}
|
||||
return buf + p + 1;
|
||||
}
|
||||
|
||||
template <typename K>
|
||||
struct Generator {
|
||||
int maxval;
|
||||
Generator(int m)
|
||||
: maxval(m) {
|
||||
}
|
||||
K operator()(int i) const {
|
||||
EXPECT_LE(i, maxval);
|
||||
return i;
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Generator<std::string> {
|
||||
int maxval;
|
||||
Generator(int m)
|
||||
: maxval(m) {
|
||||
}
|
||||
std::string operator()(int i) const {
|
||||
char buf[16];
|
||||
return GenerateDigits(buf, i, maxval);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename U>
|
||||
struct Generator<std::pair<T, U> > {
|
||||
Generator<typename std::remove_const<T>::type> tgen;
|
||||
Generator<typename std::remove_const<U>::type> ugen;
|
||||
|
||||
Generator(int m)
|
||||
: tgen(m),
|
||||
ugen(m) {
|
||||
}
|
||||
std::pair<T, U> operator()(int i) const {
|
||||
return std::make_pair(tgen(i), ugen(i));
|
||||
}
|
||||
};
|
||||
|
||||
// Generate values for our tests and benchmarks. Value range is [0, maxval].
|
||||
const std::vector<int>& GenerateNumbers(int n, int maxval) {
|
||||
static std::vector<int> values;
|
||||
static std::set<int> unique_values;
|
||||
|
||||
if (values.size() < n) {
|
||||
|
||||
for (int i = values.size(); i < n; i++) {
|
||||
int value;
|
||||
do {
|
||||
value = rand() % (maxval + 1);
|
||||
} while (unique_values.find(value) != unique_values.end());
|
||||
|
||||
values.push_back(value);
|
||||
unique_values.insert(value);
|
||||
}
|
||||
}
|
||||
|
||||
return values;
|
||||
}
|
||||
|
||||
// Generates values in the range
|
||||
// [0, 4 * min(FLAGS_benchmark_values, FLAGS_test_values)]
|
||||
template <typename V>
|
||||
std::vector<V> GenerateValues(int n) {
|
||||
int two_times_max = 2 * std::max(FLAGS_benchmark_values, FLAGS_test_values);
|
||||
int four_times_max = 2 * two_times_max;
|
||||
EXPECT_LE(n, two_times_max);
|
||||
const std::vector<int> &nums = GenerateNumbers(n, four_times_max);
|
||||
Generator<V> gen(four_times_max);
|
||||
std::vector<V> vec;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
vec.push_back(gen(nums[i]));
|
||||
}
|
||||
|
||||
return vec;
|
||||
}
|
||||
|
||||
template <typename T, typename V>
|
||||
void DoTest(const char *name, T *b, const std::vector<V> &values) {
|
||||
typename KeyOfValue<typename T::key_type, V>::type key_of_value;
|
||||
|
||||
T &mutable_b = *b;
|
||||
const T &const_b = *b;
|
||||
|
||||
// Test insert.
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
mutable_b.insert(values[i]);
|
||||
mutable_b.value_check(values[i]);
|
||||
}
|
||||
assert(mutable_b.size() == values.size());
|
||||
|
||||
const_b.verify();
|
||||
printf(" %s fullness=%0.2f overhead=%0.2f bytes-per-value=%0.2f\n",
|
||||
name, const_b.fullness(), const_b.overhead(),
|
||||
double(const_b.bytes_used()) / const_b.size());
|
||||
|
||||
// Test copy constructor.
|
||||
T b_copy(const_b);
|
||||
EXPECT_EQ(b_copy.size(), const_b.size());
|
||||
EXPECT_LE(b_copy.height(), const_b.height());
|
||||
EXPECT_LE(b_copy.internal_nodes(), const_b.internal_nodes());
|
||||
EXPECT_LE(b_copy.leaf_nodes(), const_b.leaf_nodes());
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
EXPECT_EQ(*b_copy.find(key_of_value(values[i])), values[i]);
|
||||
}
|
||||
|
||||
// Test range constructor.
|
||||
T b_range(const_b.begin(), const_b.end());
|
||||
EXPECT_EQ(b_range.size(), const_b.size());
|
||||
EXPECT_LE(b_range.height(), const_b.height());
|
||||
EXPECT_LE(b_range.internal_nodes(), const_b.internal_nodes());
|
||||
EXPECT_LE(b_range.leaf_nodes(), const_b.leaf_nodes());
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
EXPECT_EQ(*b_range.find(key_of_value(values[i])), values[i]);
|
||||
}
|
||||
|
||||
// Test range insertion for values that already exist.
|
||||
b_range.insert(b_copy.begin(), b_copy.end());
|
||||
b_range.verify();
|
||||
|
||||
// Test range insertion for new values.
|
||||
b_range.clear();
|
||||
b_range.insert(b_copy.begin(), b_copy.end());
|
||||
EXPECT_EQ(b_range.size(), b_copy.size());
|
||||
EXPECT_EQ(b_range.height(), b_copy.height());
|
||||
EXPECT_EQ(b_range.internal_nodes(), b_copy.internal_nodes());
|
||||
EXPECT_EQ(b_range.leaf_nodes(), b_copy.leaf_nodes());
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
EXPECT_EQ(*b_range.find(key_of_value(values[i])), values[i]);
|
||||
}
|
||||
|
||||
// Test assignment to self. Nothing should change.
|
||||
b_range.operator=(b_range);
|
||||
EXPECT_EQ(b_range.size(), b_copy.size());
|
||||
EXPECT_EQ(b_range.height(), b_copy.height());
|
||||
EXPECT_EQ(b_range.internal_nodes(), b_copy.internal_nodes());
|
||||
EXPECT_EQ(b_range.leaf_nodes(), b_copy.leaf_nodes());
|
||||
|
||||
// Test assignment of new values.
|
||||
b_range.clear();
|
||||
b_range = b_copy;
|
||||
EXPECT_EQ(b_range.size(), b_copy.size());
|
||||
EXPECT_EQ(b_range.height(), b_copy.height());
|
||||
EXPECT_EQ(b_range.internal_nodes(), b_copy.internal_nodes());
|
||||
EXPECT_EQ(b_range.leaf_nodes(), b_copy.leaf_nodes());
|
||||
|
||||
// Test swap.
|
||||
b_range.clear();
|
||||
b_range.swap(b_copy);
|
||||
EXPECT_EQ(b_copy.size(), 0);
|
||||
EXPECT_EQ(b_range.size(), const_b.size());
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
EXPECT_EQ(*b_range.find(key_of_value(values[i])), values[i]);
|
||||
}
|
||||
b_range.swap(b_copy);
|
||||
|
||||
// Test erase via values.
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
mutable_b.erase(key_of_value(values[i]));
|
||||
// Erasing a non-existent key should have no effect.
|
||||
EXPECT_EQ(mutable_b.erase(key_of_value(values[i])), 0);
|
||||
}
|
||||
|
||||
const_b.verify();
|
||||
EXPECT_EQ(const_b.internal_nodes(), 0);
|
||||
EXPECT_EQ(const_b.leaf_nodes(), 0);
|
||||
EXPECT_EQ(const_b.size(), 0);
|
||||
|
||||
// Test erase via iterators.
|
||||
mutable_b = b_copy;
|
||||
for (int i = 0; i < values.size(); ++i) {
|
||||
mutable_b.erase(mutable_b.find(key_of_value(values[i])));
|
||||
}
|
||||
|
||||
const_b.verify();
|
||||
EXPECT_EQ(const_b.internal_nodes(), 0);
|
||||
EXPECT_EQ(const_b.leaf_nodes(), 0);
|
||||
EXPECT_EQ(const_b.size(), 0);
|
||||
|
||||
// Test insert with hint.
|
||||
for (int i = 0; i < values.size(); i++) {
|
||||
mutable_b.insert(mutable_b.upper_bound(key_of_value(values[i])), values[i]);
|
||||
}
|
||||
|
||||
const_b.verify();
|
||||
|
||||
// Test dumping of the btree to an ostream. There should be 1 line for each
|
||||
// value.
|
||||
std::stringstream strm;
|
||||
strm << mutable_b.tree();
|
||||
EXPECT_EQ(mutable_b.size(), strcount(strm.str(), '\n'));
|
||||
|
||||
// Test range erase.
|
||||
mutable_b.erase(mutable_b.begin(), mutable_b.end());
|
||||
EXPECT_EQ(mutable_b.size(), 0);
|
||||
const_b.verify();
|
||||
|
||||
// First half.
|
||||
mutable_b = b_copy;
|
||||
typename T::iterator mutable_iter_end = mutable_b.begin();
|
||||
for (int i = 0; i < values.size() / 2; ++i) ++mutable_iter_end;
|
||||
mutable_b.erase(mutable_b.begin(), mutable_iter_end);
|
||||
EXPECT_EQ(mutable_b.size(), values.size() - values.size() / 2);
|
||||
const_b.verify();
|
||||
|
||||
// Second half.
|
||||
mutable_b = b_copy;
|
||||
typename T::iterator mutable_iter_begin = mutable_b.begin();
|
||||
for (int i = 0; i < values.size() / 2; ++i) ++mutable_iter_begin;
|
||||
mutable_b.erase(mutable_iter_begin, mutable_b.end());
|
||||
EXPECT_EQ(mutable_b.size(), values.size() / 2);
|
||||
const_b.verify();
|
||||
|
||||
// Second quarter.
|
||||
mutable_b = b_copy;
|
||||
mutable_iter_begin = mutable_b.begin();
|
||||
for (int i = 0; i < values.size() / 4; ++i) ++mutable_iter_begin;
|
||||
mutable_iter_end = mutable_iter_begin;
|
||||
for (int i = 0; i < values.size() / 4; ++i) ++mutable_iter_end;
|
||||
mutable_b.erase(mutable_iter_begin, mutable_iter_end);
|
||||
EXPECT_EQ(mutable_b.size(), values.size() - values.size() / 4);
|
||||
const_b.verify();
|
||||
|
||||
mutable_b.clear();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void ConstTest() {
|
||||
typedef typename T::value_type value_type;
|
||||
typename KeyOfValue<typename T::key_type, value_type>::type key_of_value;
|
||||
|
||||
T mutable_b;
|
||||
const T &const_b = mutable_b;
|
||||
|
||||
// Insert a single value into the container and test looking it up.
|
||||
value_type value = Generator<value_type>(2)(2);
|
||||
mutable_b.insert(value);
|
||||
EXPECT_TRUE(mutable_b.find(key_of_value(value)) != const_b.end());
|
||||
EXPECT_TRUE(const_b.find(key_of_value(value)) != mutable_b.end());
|
||||
EXPECT_EQ(*const_b.lower_bound(key_of_value(value)), value);
|
||||
EXPECT_TRUE(const_b.upper_bound(key_of_value(value)) == const_b.end());
|
||||
EXPECT_EQ(*const_b.equal_range(key_of_value(value)).first, value);
|
||||
|
||||
// We can only create a non-const iterator from a non-const container.
|
||||
typename T::iterator mutable_iter(mutable_b.begin());
|
||||
EXPECT_TRUE(mutable_iter == const_b.begin());
|
||||
EXPECT_TRUE(mutable_iter != const_b.end());
|
||||
EXPECT_TRUE(const_b.begin() == mutable_iter);
|
||||
EXPECT_TRUE(const_b.end() != mutable_iter);
|
||||
typename T::reverse_iterator mutable_riter(mutable_b.rbegin());
|
||||
EXPECT_TRUE(mutable_riter == const_b.rbegin());
|
||||
EXPECT_TRUE(mutable_riter != const_b.rend());
|
||||
EXPECT_TRUE(const_b.rbegin() == mutable_riter);
|
||||
EXPECT_TRUE(const_b.rend() != mutable_riter);
|
||||
|
||||
// We can create a const iterator from a non-const iterator.
|
||||
typename T::const_iterator const_iter(mutable_iter);
|
||||
EXPECT_TRUE(const_iter == mutable_b.begin());
|
||||
EXPECT_TRUE(const_iter != mutable_b.end());
|
||||
EXPECT_TRUE(mutable_b.begin() == const_iter);
|
||||
EXPECT_TRUE(mutable_b.end() != const_iter);
|
||||
typename T::const_reverse_iterator const_riter(mutable_riter);
|
||||
EXPECT_EQ(const_riter, mutable_b.rbegin());
|
||||
EXPECT_TRUE(const_riter != mutable_b.rend());
|
||||
EXPECT_EQ(mutable_b.rbegin(), const_riter);
|
||||
EXPECT_TRUE(mutable_b.rend() != const_riter);
|
||||
|
||||
// Make sure various methods can be invoked on a const container.
|
||||
const_b.verify();
|
||||
EXPECT_FALSE(const_b.empty());
|
||||
EXPECT_EQ(const_b.size(), 1);
|
||||
EXPECT_GT(const_b.max_size(), 0);
|
||||
EXPECT_EQ(const_b.height(), 1);
|
||||
EXPECT_EQ(const_b.count(key_of_value(value)), 1);
|
||||
EXPECT_EQ(const_b.internal_nodes(), 0);
|
||||
EXPECT_EQ(const_b.leaf_nodes(), 1);
|
||||
EXPECT_EQ(const_b.nodes(), 1);
|
||||
EXPECT_GT(const_b.bytes_used(), 0);
|
||||
EXPECT_GT(const_b.fullness(), 0);
|
||||
EXPECT_GT(const_b.overhead(), 0);
|
||||
}
|
||||
|
||||
template <typename T, typename C>
|
||||
void BtreeTest() {
|
||||
ConstTest<T>();
|
||||
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
std::vector<V> random_values = GenerateValues<V>(FLAGS_test_values);
|
||||
|
||||
unique_checker<T, C> container;
|
||||
|
||||
// Test key insertion/deletion in sorted order.
|
||||
std::vector<V> sorted_values(random_values);
|
||||
sort(sorted_values.begin(), sorted_values.end());
|
||||
DoTest("sorted: ", &container, sorted_values);
|
||||
|
||||
// Test key insertion/deletion in reverse sorted order.
|
||||
reverse(sorted_values.begin(), sorted_values.end());
|
||||
DoTest("rsorted: ", &container, sorted_values);
|
||||
|
||||
// Test key insertion/deletion in random order.
|
||||
DoTest("random: ", &container, random_values);
|
||||
}
|
||||
|
||||
template <typename T, typename C>
|
||||
void BtreeMultiTest() {
|
||||
ConstTest<T>();
|
||||
|
||||
typedef typename std::remove_const<typename T::value_type>::type V;
|
||||
const std::vector<V>& random_values = GenerateValues<V>(FLAGS_test_values);
|
||||
|
||||
multi_checker<T, C> container;
|
||||
|
||||
// Test keys in sorted order.
|
||||
std::vector<V> sorted_values(random_values);
|
||||
sort(sorted_values.begin(), sorted_values.end());
|
||||
DoTest("sorted: ", &container, sorted_values);
|
||||
|
||||
// Test keys in reverse sorted order.
|
||||
reverse(sorted_values.begin(), sorted_values.end());
|
||||
DoTest("rsorted: ", &container, sorted_values);
|
||||
|
||||
// Test keys in random order.
|
||||
DoTest("random: ", &container, random_values);
|
||||
|
||||
// Test keys in random order w/ duplicates.
|
||||
std::vector<V> duplicate_values(random_values);
|
||||
duplicate_values.insert(
|
||||
duplicate_values.end(), random_values.begin(), random_values.end());
|
||||
DoTest("duplicates:", &container, duplicate_values);
|
||||
|
||||
// Test all identical keys.
|
||||
std::vector<V> identical_values(100);
|
||||
fill(identical_values.begin(), identical_values.end(), Generator<V>(2)(2));
|
||||
DoTest("identical: ", &container, identical_values);
|
||||
}
|
||||
|
||||
template <typename T, typename Alloc = std::allocator<T> >
|
||||
class TestAllocator : public Alloc {
|
||||
public:
|
||||
typedef typename Alloc::pointer pointer;
|
||||
typedef typename Alloc::size_type size_type;
|
||||
|
||||
TestAllocator() : bytes_used_(NULL) { }
|
||||
TestAllocator(int64_t *bytes_used) : bytes_used_(bytes_used) { }
|
||||
|
||||
// Constructor used for rebinding
|
||||
template <class U>
|
||||
TestAllocator(const TestAllocator<U>& x)
|
||||
: Alloc(x),
|
||||
bytes_used_(x.bytes_used()) {
|
||||
}
|
||||
|
||||
pointer allocate(size_type n, std::allocator<void>::const_pointer hint = 0) {
|
||||
EXPECT_TRUE(bytes_used_ != NULL);
|
||||
*bytes_used_ += n * sizeof(T);
|
||||
return Alloc::allocate(n, hint);
|
||||
}
|
||||
|
||||
void deallocate(pointer p, size_type n) {
|
||||
Alloc::deallocate(p, n);
|
||||
EXPECT_TRUE(bytes_used_ != NULL);
|
||||
*bytes_used_ -= n * sizeof(T);
|
||||
}
|
||||
|
||||
// Rebind allows an allocator<T> to be used for a different type
|
||||
template <class U> struct rebind {
|
||||
typedef TestAllocator<U, typename Alloc::template rebind<U>::other> other;
|
||||
};
|
||||
|
||||
int64_t* bytes_used() const { return bytes_used_; }
|
||||
|
||||
private:
|
||||
int64_t *bytes_used_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
void BtreeAllocatorTest() {
|
||||
typedef typename T::value_type value_type;
|
||||
|
||||
int64_t alloc1 = 0;
|
||||
int64_t alloc2 = 0;
|
||||
T b1(typename T::key_compare(), &alloc1);
|
||||
T b2(typename T::key_compare(), &alloc2);
|
||||
|
||||
// This should swap the allocators!
|
||||
swap(b1, b2);
|
||||
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
b1.insert(Generator<value_type>(1000)(i));
|
||||
}
|
||||
|
||||
// We should have allocated out of alloc2!
|
||||
EXPECT_LE(b1.bytes_used(), alloc2 + sizeof(b1));
|
||||
EXPECT_GT(alloc2, alloc1);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void BtreeMapTest() {
|
||||
typedef typename T::value_type value_type;
|
||||
typedef typename T::mapped_type mapped_type;
|
||||
|
||||
mapped_type m = Generator<mapped_type>(0)(0);
|
||||
(void) m;
|
||||
|
||||
T b;
|
||||
|
||||
// Verify we can insert using operator[].
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
value_type v = Generator<value_type>(1000)(i);
|
||||
b[v.first] = v.second;
|
||||
}
|
||||
EXPECT_EQ(b.size(), 1000);
|
||||
|
||||
// Test whether we can use the "->" operator on iterators and
|
||||
// reverse_iterators. This stresses the btree_map_params::pair_pointer
|
||||
// mechanism.
|
||||
EXPECT_EQ(b.begin()->first, Generator<value_type>(1000)(0).first);
|
||||
EXPECT_EQ(b.begin()->second, Generator<value_type>(1000)(0).second);
|
||||
EXPECT_EQ(b.rbegin()->first, Generator<value_type>(1000)(999).first);
|
||||
EXPECT_EQ(b.rbegin()->second, Generator<value_type>(1000)(999).second);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void BtreeMultiMapTest() {
|
||||
typedef typename T::mapped_type mapped_type;
|
||||
mapped_type m = Generator<mapped_type>(0)(0);
|
||||
(void) m;
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_BTREE_TEST_H__
|
20
lib/xdelta3/cpp-btree/btree_test_flags.cc
Normal file
20
lib/xdelta3/cpp-btree/btree_test_flags.cc
Normal file
|
@ -0,0 +1,20 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include "gflags/gflags.h"
|
||||
|
||||
DEFINE_int32(test_values, 10000,
|
||||
"The number of values to use for tests.");
|
||||
DEFINE_int32(benchmark_values, 1000000,
|
||||
"The number of values to use for benchmarks.");
|
395
lib/xdelta3/cpp-btree/safe_btree.h
Normal file
395
lib/xdelta3/cpp-btree/safe_btree.h
Normal file
|
@ -0,0 +1,395 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// A safe_btree<> wraps around a btree<> and removes the caveat that insertion
|
||||
// and deletion invalidate iterators. A safe_btree<> maintains a generation
|
||||
// number that is incremented on every mutation. A safe_btree<>::iterator keeps
|
||||
// a pointer to the safe_btree<> it came from, the generation of the tree when
|
||||
// it was last validated and the key the underlying btree<>::iterator points
|
||||
// to. If an iterator is accessed and its generation differs from the tree
|
||||
// generation it is revalidated.
|
||||
//
|
||||
// References and pointers returned by safe_btree iterators are not safe.
|
||||
//
|
||||
// See the incorrect usage examples mentioned in safe_btree_set.h and
|
||||
// safe_btree_map.h.
|
||||
|
||||
#ifndef UTIL_BTREE_SAFE_BTREE_H__
|
||||
#define UTIL_BTREE_SAFE_BTREE_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <iosfwd>
|
||||
#include <utility>
|
||||
|
||||
#include "btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
template <typename Tree, typename Iterator>
|
||||
class safe_btree_iterator {
|
||||
public:
|
||||
typedef typename Iterator::key_type key_type;
|
||||
typedef typename Iterator::value_type value_type;
|
||||
typedef typename Iterator::size_type size_type;
|
||||
typedef typename Iterator::difference_type difference_type;
|
||||
typedef typename Iterator::pointer pointer;
|
||||
typedef typename Iterator::reference reference;
|
||||
typedef typename Iterator::const_pointer const_pointer;
|
||||
typedef typename Iterator::const_reference const_reference;
|
||||
typedef typename Iterator::iterator_category iterator_category;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
typedef safe_btree_iterator<Tree, Iterator> self_type;
|
||||
|
||||
void update() const {
|
||||
if (iter_ != tree_->internal_btree()->end()) {
|
||||
// A positive generation indicates a valid key.
|
||||
generation_ = tree_->generation();
|
||||
key_ = iter_.key();
|
||||
} else {
|
||||
// Use a negative generation to indicate iter_ points to end().
|
||||
generation_ = -tree_->generation();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
safe_btree_iterator()
|
||||
: generation_(0),
|
||||
key_(),
|
||||
iter_(),
|
||||
tree_(NULL) {
|
||||
}
|
||||
safe_btree_iterator(const iterator &x)
|
||||
: generation_(x.generation()),
|
||||
key_(x.key()),
|
||||
iter_(x.iter()),
|
||||
tree_(x.tree()) {
|
||||
}
|
||||
safe_btree_iterator(Tree *tree, const Iterator &iter)
|
||||
: generation_(),
|
||||
key_(),
|
||||
iter_(iter),
|
||||
tree_(tree) {
|
||||
update();
|
||||
}
|
||||
|
||||
Tree* tree() const { return tree_; }
|
||||
int64_t generation() const { return generation_; }
|
||||
|
||||
Iterator* mutable_iter() const {
|
||||
if (generation_ != tree_->generation()) {
|
||||
if (generation_ > 0) {
|
||||
// This does the wrong thing for a multi{set,map}. If my iter was
|
||||
// pointing to the 2nd of 2 values with the same key, then this will
|
||||
// reset it to point to the first. This is why we don't provide a
|
||||
// safe_btree_multi{set,map}.
|
||||
iter_ = tree_->internal_btree()->lower_bound(key_);
|
||||
update();
|
||||
} else if (-generation_ != tree_->generation()) {
|
||||
iter_ = tree_->internal_btree()->end();
|
||||
generation_ = -tree_->generation();
|
||||
}
|
||||
}
|
||||
return &iter_;
|
||||
}
|
||||
const Iterator& iter() const {
|
||||
return *mutable_iter();
|
||||
}
|
||||
|
||||
// Equality/inequality operators.
|
||||
bool operator==(const const_iterator &x) const {
|
||||
return iter() == x.iter();
|
||||
}
|
||||
bool operator!=(const const_iterator &x) const {
|
||||
return iter() != x.iter();
|
||||
}
|
||||
|
||||
// Accessors for the key/value the iterator is pointing at.
|
||||
const key_type& key() const {
|
||||
return key_;
|
||||
}
|
||||
// This reference value is potentially invalidated by any non-const
|
||||
// method on the tree; it is NOT safe.
|
||||
reference operator*() const {
|
||||
assert(generation_ > 0);
|
||||
return iter().operator*();
|
||||
}
|
||||
// This pointer value is potentially invalidated by any non-const
|
||||
// method on the tree; it is NOT safe.
|
||||
pointer operator->() const {
|
||||
assert(generation_ > 0);
|
||||
return iter().operator->();
|
||||
}
|
||||
|
||||
// Increment/decrement operators.
|
||||
self_type& operator++() {
|
||||
++(*mutable_iter());
|
||||
update();
|
||||
return *this;
|
||||
}
|
||||
self_type& operator--() {
|
||||
--(*mutable_iter());
|
||||
update();
|
||||
return *this;
|
||||
}
|
||||
self_type operator++(int) {
|
||||
self_type tmp = *this;
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
self_type operator--(int) {
|
||||
self_type tmp = *this;
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
private:
|
||||
// The generation of the tree when "iter" was updated.
|
||||
mutable int64_t generation_;
|
||||
// The key the iterator points to.
|
||||
mutable key_type key_;
|
||||
// The underlying iterator.
|
||||
mutable Iterator iter_;
|
||||
// The tree the iterator is associated with.
|
||||
Tree *tree_;
|
||||
};
|
||||
|
||||
template <typename Params>
|
||||
class safe_btree {
|
||||
typedef safe_btree<Params> self_type;
|
||||
|
||||
typedef btree<Params> btree_type;
|
||||
typedef typename btree_type::iterator tree_iterator;
|
||||
typedef typename btree_type::const_iterator tree_const_iterator;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::params_type params_type;
|
||||
typedef typename btree_type::key_type key_type;
|
||||
typedef typename btree_type::data_type data_type;
|
||||
typedef typename btree_type::mapped_type mapped_type;
|
||||
typedef typename btree_type::value_type value_type;
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
typedef typename btree_type::pointer pointer;
|
||||
typedef typename btree_type::const_pointer const_pointer;
|
||||
typedef typename btree_type::reference reference;
|
||||
typedef typename btree_type::const_reference const_reference;
|
||||
typedef typename btree_type::size_type size_type;
|
||||
typedef typename btree_type::difference_type difference_type;
|
||||
typedef safe_btree_iterator<self_type, tree_iterator> iterator;
|
||||
typedef safe_btree_iterator<
|
||||
const self_type, tree_const_iterator> const_iterator;
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
safe_btree(const key_compare &comp, const allocator_type &alloc)
|
||||
: tree_(comp, alloc),
|
||||
generation_(1) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
safe_btree(const self_type &x)
|
||||
: tree_(x.tree_),
|
||||
generation_(1) {
|
||||
}
|
||||
|
||||
iterator begin() {
|
||||
return iterator(this, tree_.begin());
|
||||
}
|
||||
const_iterator begin() const {
|
||||
return const_iterator(this, tree_.begin());
|
||||
}
|
||||
iterator end() {
|
||||
return iterator(this, tree_.end());
|
||||
}
|
||||
const_iterator end() const {
|
||||
return const_iterator(this, tree_.end());
|
||||
}
|
||||
reverse_iterator rbegin() {
|
||||
return reverse_iterator(end());
|
||||
}
|
||||
const_reverse_iterator rbegin() const {
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
reverse_iterator rend() {
|
||||
return reverse_iterator(begin());
|
||||
}
|
||||
const_reverse_iterator rend() const {
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
|
||||
// Lookup routines.
|
||||
iterator lower_bound(const key_type &key) {
|
||||
return iterator(this, tree_.lower_bound(key));
|
||||
}
|
||||
const_iterator lower_bound(const key_type &key) const {
|
||||
return const_iterator(this, tree_.lower_bound(key));
|
||||
}
|
||||
iterator upper_bound(const key_type &key) {
|
||||
return iterator(this, tree_.upper_bound(key));
|
||||
}
|
||||
const_iterator upper_bound(const key_type &key) const {
|
||||
return const_iterator(this, tree_.upper_bound(key));
|
||||
}
|
||||
std::pair<iterator, iterator> equal_range(const key_type &key) {
|
||||
std::pair<tree_iterator, tree_iterator> p = tree_.equal_range(key);
|
||||
return std::make_pair(iterator(this, p.first),
|
||||
iterator(this, p.second));
|
||||
}
|
||||
std::pair<const_iterator, const_iterator> equal_range(const key_type &key) const {
|
||||
std::pair<tree_const_iterator, tree_const_iterator> p = tree_.equal_range(key);
|
||||
return std::make_pair(const_iterator(this, p.first),
|
||||
const_iterator(this, p.second));
|
||||
}
|
||||
iterator find_unique(const key_type &key) {
|
||||
return iterator(this, tree_.find_unique(key));
|
||||
}
|
||||
const_iterator find_unique(const key_type &key) const {
|
||||
return const_iterator(this, tree_.find_unique(key));
|
||||
}
|
||||
iterator find_multi(const key_type &key) {
|
||||
return iterator(this, tree_.find_multi(key));
|
||||
}
|
||||
const_iterator find_multi(const key_type &key) const {
|
||||
return const_iterator(this, tree_.find_multi(key));
|
||||
}
|
||||
size_type count_unique(const key_type &key) const {
|
||||
return tree_.count_unique(key);
|
||||
}
|
||||
size_type count_multi(const key_type &key) const {
|
||||
return tree_.count_multi(key);
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
template <typename ValuePointer>
|
||||
std::pair<iterator, bool> insert_unique(const key_type &key, ValuePointer value) {
|
||||
std::pair<tree_iterator, bool> p = tree_.insert_unique(key, value);
|
||||
generation_ += p.second;
|
||||
return std::make_pair(iterator(this, p.first), p.second);
|
||||
}
|
||||
std::pair<iterator, bool> insert_unique(const value_type &v) {
|
||||
std::pair<tree_iterator, bool> p = tree_.insert_unique(v);
|
||||
generation_ += p.second;
|
||||
return std::make_pair(iterator(this, p.first), p.second);
|
||||
}
|
||||
iterator insert_unique(iterator position, const value_type &v) {
|
||||
tree_iterator tree_pos = position.iter();
|
||||
++generation_;
|
||||
return iterator(this, tree_.insert_unique(tree_pos, v));
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert_unique(InputIterator b, InputIterator e) {
|
||||
for (; b != e; ++b) {
|
||||
insert_unique(*b);
|
||||
}
|
||||
}
|
||||
iterator insert_multi(const value_type &v) {
|
||||
++generation_;
|
||||
return iterator(this, tree_.insert_multi(v));
|
||||
}
|
||||
iterator insert_multi(iterator position, const value_type &v) {
|
||||
tree_iterator tree_pos = position.iter();
|
||||
++generation_;
|
||||
return iterator(this, tree_.insert_multi(tree_pos, v));
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert_multi(InputIterator b, InputIterator e) {
|
||||
for (; b != e; ++b) {
|
||||
insert_multi(*b);
|
||||
}
|
||||
}
|
||||
self_type& operator=(const self_type &x) {
|
||||
if (&x == this) {
|
||||
// Don't copy onto ourselves.
|
||||
return *this;
|
||||
}
|
||||
++generation_;
|
||||
tree_ = x.tree_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Deletion routines.
|
||||
void erase(const iterator &begin, const iterator &end) {
|
||||
tree_.erase(begin.iter(), end.iter());
|
||||
++generation_;
|
||||
}
|
||||
// Erase the specified iterator from the btree. The iterator must be valid
|
||||
// (i.e. not equal to end()). Return an iterator pointing to the node after
|
||||
// the one that was erased (or end() if none exists).
|
||||
iterator erase(iterator iter) {
|
||||
tree_iterator res = tree_.erase(iter.iter());
|
||||
++generation_;
|
||||
return iterator(this, res);
|
||||
}
|
||||
int erase_unique(const key_type &key) {
|
||||
int res = tree_.erase_unique(key);
|
||||
generation_ += res;
|
||||
return res;
|
||||
}
|
||||
int erase_multi(const key_type &key) {
|
||||
int res = tree_.erase_multi(key);
|
||||
generation_ += res;
|
||||
return res;
|
||||
}
|
||||
|
||||
// Access to the underlying btree.
|
||||
btree_type* internal_btree() { return &tree_; }
|
||||
const btree_type* internal_btree() const { return &tree_; }
|
||||
|
||||
// Utility routines.
|
||||
void clear() {
|
||||
++generation_;
|
||||
tree_.clear();
|
||||
}
|
||||
void swap(self_type &x) {
|
||||
++generation_;
|
||||
++x.generation_;
|
||||
tree_.swap(x.tree_);
|
||||
}
|
||||
void dump(std::ostream &os) const {
|
||||
tree_.dump(os);
|
||||
}
|
||||
void verify() const {
|
||||
tree_.verify();
|
||||
}
|
||||
int64_t generation() const {
|
||||
return generation_;
|
||||
}
|
||||
key_compare key_comp() const { return tree_.key_comp(); }
|
||||
|
||||
// Size routines.
|
||||
size_type size() const { return tree_.size(); }
|
||||
size_type max_size() const { return tree_.max_size(); }
|
||||
bool empty() const { return tree_.empty(); }
|
||||
size_type height() const { return tree_.height(); }
|
||||
size_type internal_nodes() const { return tree_.internal_nodes(); }
|
||||
size_type leaf_nodes() const { return tree_.leaf_nodes(); }
|
||||
size_type nodes() const { return tree_.nodes(); }
|
||||
size_type bytes_used() const { return tree_.bytes_used(); }
|
||||
static double average_bytes_per_value() {
|
||||
return btree_type::average_bytes_per_value();
|
||||
}
|
||||
double fullness() const { return tree_.fullness(); }
|
||||
double overhead() const { return tree_.overhead(); }
|
||||
|
||||
private:
|
||||
btree_type tree_;
|
||||
int64_t generation_;
|
||||
};
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_SAFE_BTREE_H__
|
89
lib/xdelta3/cpp-btree/safe_btree_map.h
Normal file
89
lib/xdelta3/cpp-btree/safe_btree_map.h
Normal file
|
@ -0,0 +1,89 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// The safe_btree_map<> is like btree_map<> except that it removes the caveat
|
||||
// about insertion and deletion invalidating existing iterators at a small cost
|
||||
// in making iterators larger and slower.
|
||||
//
|
||||
// Revalidation occurs whenever an iterator is accessed. References
|
||||
// and pointers returned by safe_btree_map<> iterators are not stable,
|
||||
// they are potentially invalidated by any non-const method on the map.
|
||||
//
|
||||
// BEGIN INCORRECT EXAMPLE
|
||||
// for (auto i = safe_map->begin(); i != safe_map->end(); ++i) {
|
||||
// const T *value = &i->second; // DO NOT DO THIS
|
||||
// [code that modifies safe_map and uses value];
|
||||
// }
|
||||
// END INCORRECT EXAMPLE
|
||||
#ifndef UTIL_BTREE_SAFE_BTREE_MAP_H__
|
||||
#define UTIL_BTREE_SAFE_BTREE_MAP_H__
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "btree_container.h"
|
||||
#include "btree_map.h"
|
||||
#include "safe_btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The safe_btree_map class is needed mainly for its constructors.
|
||||
template <typename Key, typename Value,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<std::pair<const Key, Value> >,
|
||||
int TargetNodeSize = 256>
|
||||
class safe_btree_map : public btree_map_container<
|
||||
safe_btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef safe_btree_map<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_map_params<
|
||||
Key, Value, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef safe_btree<params_type> btree_type;
|
||||
typedef btree_map_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
safe_btree_map(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
safe_btree_map(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
safe_btree_map(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename V, typename C, typename A, int N>
|
||||
inline void swap(safe_btree_map<K, V, C, A, N> &x,
|
||||
safe_btree_map<K, V, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_SAFE_BTREE_MAP_H__
|
88
lib/xdelta3/cpp-btree/safe_btree_set.h
Normal file
88
lib/xdelta3/cpp-btree/safe_btree_set.h
Normal file
|
@ -0,0 +1,88 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// The safe_btree_set<> is like btree_set<> except that it removes the caveat
|
||||
// about insertion and deletion invalidating existing iterators at a small cost
|
||||
// in making iterators larger and slower.
|
||||
//
|
||||
// Revalidation occurs whenever an iterator is accessed. References
|
||||
// and pointers returned by safe_btree_map<> iterators are not stable,
|
||||
// they are potentially invalidated by any non-const method on the set.
|
||||
//
|
||||
// BEGIN INCORRECT EXAMPLE
|
||||
// for (auto i = safe_set->begin(); i != safe_set->end(); ++i) {
|
||||
// const T &value = *i; // DO NOT DO THIS
|
||||
// [code that modifies safe_set and uses value];
|
||||
// }
|
||||
// END INCORRECT EXAMPLE
|
||||
|
||||
#ifndef UTIL_BTREE_SAFE_BTREE_SET_H__
|
||||
#define UTIL_BTREE_SAFE_BTREE_SET_H__
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "btree_container.h"
|
||||
#include "btree_set.h"
|
||||
#include "safe_btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The safe_btree_set class is needed mainly for its constructors.
|
||||
template <typename Key,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<Key>,
|
||||
int TargetNodeSize = 256>
|
||||
class safe_btree_set : public btree_unique_container<
|
||||
safe_btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef safe_btree_set<Key, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef safe_btree<params_type> btree_type;
|
||||
typedef btree_unique_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
safe_btree_set(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
safe_btree_set(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
safe_btree_set(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename C, typename A, int N>
|
||||
inline void swap(safe_btree_set<K, C, A, N> &x,
|
||||
safe_btree_set<K, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_SAFE_BTREE_SET_H__
|
116
lib/xdelta3/cpp-btree/safe_btree_test.cc
Normal file
116
lib/xdelta3/cpp-btree/safe_btree_test.cc
Normal file
|
@ -0,0 +1,116 @@
|
|||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
// TODO(pmattis): Add some tests that iterators are not invalidated by
|
||||
// insertion and deletion.
|
||||
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <set>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "btree_test.h"
|
||||
#include "safe_btree_map.h"
|
||||
#include "safe_btree_set.h"
|
||||
|
||||
class UnsafeArena;
|
||||
|
||||
namespace btree {
|
||||
namespace {
|
||||
|
||||
template <typename K, int N>
|
||||
void SetTest() {
|
||||
typedef TestAllocator<K> TestAlloc;
|
||||
BtreeTest<safe_btree_set<K, std::less<K>, std::allocator<K>, N>, std::set<K> >();
|
||||
BtreeAllocatorTest<safe_btree_set<K, std::less<K>, TestAlloc, N> >();
|
||||
}
|
||||
|
||||
template <typename K, int N>
|
||||
void MapTest() {
|
||||
typedef TestAllocator<K> TestAlloc;
|
||||
BtreeTest<safe_btree_map<K, K, std::less<K>, std::allocator<K>, N>, std::map<K, K> >();
|
||||
BtreeAllocatorTest<safe_btree_map<K, K, std::less<K>, TestAlloc, N> >();
|
||||
BtreeMapTest<safe_btree_map<K, K, std::less<K>, std::allocator<K>, N> >();
|
||||
}
|
||||
|
||||
TEST(SafeBtree, set_int32_32) { SetTest<int32_t, 32>(); }
|
||||
TEST(SafeBtree, set_int32_64) { SetTest<int32_t, 64>(); }
|
||||
TEST(SafeBtree, set_int32_128) { SetTest<int32_t, 128>(); }
|
||||
TEST(SafeBtree, set_int32_256) { SetTest<int32_t, 256>(); }
|
||||
TEST(SafeBtree, set_int64_256) { SetTest<int64_t, 256>(); }
|
||||
TEST(SafeBtree, set_string_256) { SetTest<std::string, 256>(); }
|
||||
TEST(SafeBtree, set_pair_256) { SetTest<std::pair<int, int>, 256>(); }
|
||||
TEST(SafeBtree, map_int32_256) { MapTest<int32_t, 256>(); }
|
||||
TEST(SafeBtree, map_int64_256) { MapTest<int64_t, 256>(); }
|
||||
TEST(SafeBtree, map_string_256) { MapTest<std::string, 256>(); }
|
||||
TEST(SafeBtree, map_pair_256) { MapTest<std::pair<int, int>, 256>(); }
|
||||
|
||||
TEST(SafeBtree, Comparison) {
|
||||
const int kSetSize = 1201;
|
||||
safe_btree_set<int64_t> my_set;
|
||||
for (int i = 0; i < kSetSize; ++i) {
|
||||
my_set.insert(i);
|
||||
}
|
||||
safe_btree_set<int64_t> my_set_copy(my_set);
|
||||
EXPECT_TRUE(my_set_copy == my_set);
|
||||
EXPECT_TRUE(my_set == my_set_copy);
|
||||
EXPECT_FALSE(my_set_copy != my_set);
|
||||
EXPECT_FALSE(my_set != my_set_copy);
|
||||
|
||||
my_set.insert(kSetSize);
|
||||
EXPECT_FALSE(my_set_copy == my_set);
|
||||
EXPECT_FALSE(my_set == my_set_copy);
|
||||
EXPECT_TRUE(my_set_copy != my_set);
|
||||
EXPECT_TRUE(my_set != my_set_copy);
|
||||
|
||||
my_set.erase(kSetSize - 1);
|
||||
EXPECT_FALSE(my_set_copy == my_set);
|
||||
EXPECT_FALSE(my_set == my_set_copy);
|
||||
EXPECT_TRUE(my_set_copy != my_set);
|
||||
EXPECT_TRUE(my_set != my_set_copy);
|
||||
|
||||
safe_btree_map<std::string, int64_t> my_map;
|
||||
for (int i = 0; i < kSetSize; ++i) {
|
||||
my_map[std::string(i, 'a')] = i;
|
||||
}
|
||||
safe_btree_map<std::string, int64_t> my_map_copy(my_map);
|
||||
EXPECT_TRUE(my_map_copy == my_map);
|
||||
EXPECT_TRUE(my_map == my_map_copy);
|
||||
EXPECT_FALSE(my_map_copy != my_map);
|
||||
EXPECT_FALSE(my_map != my_map_copy);
|
||||
|
||||
++my_map_copy[std::string(7, 'a')];
|
||||
EXPECT_FALSE(my_map_copy == my_map);
|
||||
EXPECT_FALSE(my_map == my_map_copy);
|
||||
EXPECT_TRUE(my_map_copy != my_map);
|
||||
EXPECT_TRUE(my_map != my_map_copy);
|
||||
|
||||
my_map_copy = my_map;
|
||||
my_map["hello"] = kSetSize;
|
||||
EXPECT_FALSE(my_map_copy == my_map);
|
||||
EXPECT_FALSE(my_map == my_map_copy);
|
||||
EXPECT_TRUE(my_map_copy != my_map);
|
||||
EXPECT_TRUE(my_map != my_map_copy);
|
||||
|
||||
my_map.erase(std::string(kSetSize - 1, 'a'));
|
||||
EXPECT_FALSE(my_map_copy == my_map);
|
||||
EXPECT_FALSE(my_map == my_map_copy);
|
||||
EXPECT_TRUE(my_map_copy != my_map);
|
||||
EXPECT_TRUE(my_map != my_map_copy);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace btree
|
Loading…
Add table
Add a link
Reference in a new issue