Initial checkin of C++ btree code, has some useless google3-specific

bits to be removed and needs a Makefile.
pull/5/head
Josh MacDonald 2011-12-12 13:46:12 -08:00
commit 1a14ff58c9
19 changed files with 5669 additions and 0 deletions

75
Makefile Normal file
View File

@ -0,0 +1,75 @@
# -*- mode: python; -*-
# This should be a Makefile, but it's not.
# cc_library(name = "btree",
# srcs = [ "btree.h",
# "btree_container.h",
# "btree_map.h",
# "btree_set.h",
# "safe_btree.h",
# "safe_btree_map.h",
# "safe_btree_set.h" ],
# deps = [ "//strings",
# "//strings:cord" ])
# cc_library(name = "btree_test_flags",
# srcs = [ "btree_test_flags.cc" ],
# deps = [ "//base" ])
# cc_binary(name = "btree_bench",
# srcs = [ "btree_bench.cc" ],
# deps = [ ":btree",
# ":btree_test_flags",
# "//testing/base",
# "//util/random" ])
# cc_test(name = "btree_test",
# srcs = [ "btree_test.cc", ],
# deps = [ ":btree",
# ":btree_test_flags",
# "//base:heapcheck",
# "//testing/base",
# "//util/random",
# ],
# size = "large")
# cc_test(name = "safe_btree_test",
# srcs = [ "safe_btree_test.cc", ],
# deps = [ ":btree",
# ":btree_test_flags",
# "//base:heapcheck",
# "//testing/base",
# "//util/random",
# ],
# size = "large")
# cc_fake_binary(name = "btree_nc",
# srcs = [ "btree_nc.cc" ],
# deps = [ ":btree" ],
# legacy = 0)
# py_test(name = "btree_nc_test",
# srcs = [ "btree_nc_test.py" ],
# deps = [ "//pyglib",
# "//testing/pybase" ],
# data = [ "btree_nc" ],
# size = "large")
# cc_binary(name = "btree_test_program",
# srcs = [ "btree_test_program.cc" ],
# deps = [ ":btree",
# "//devtools/gdb/component:gdb_test_utils" ],
# testonly = 1)
# # This test will only actually test the pretty-printing code if it's
# # compiled with debug information (blaze build -c dbg). The default
# # mode, fastbuild, will pass but will not catch any regressions!
# py_test(name = "btree_printer_test",
# size = "large",
# srcs = [ "btree_printer_test.py",
# "btree_printer.py" ],
# deps = [ "//devtools/gdb/component:gdbpy",
# "//testing/pybase",
# "//testing/gdb:gdb_script_test_util",
# ":btree_test_program" ])

27
benchmarks.awk Executable file
View File

@ -0,0 +1,27 @@
#!/usr/bin/gawk -f
/^Run on/ {
print $0;
printf "%-25s %5s %-20s\n",
"Benchmark", "STL(ns)", "B-Tree(ns) @ <size>"
printf "--------------------------------------------------------\n";
}
/^BM_/ {
split($1, name, "_");
if (name[2] == "stl") {
stl = $3;
stl_bytes = $5
printf "%-25s %5d ", name[1] "_" name[3] "_" name[4] "_" name[5], stl;
fflush();
} else if (name[2] == "btree") {
btree = $3
btree_size = name[3]
btree_bytes = $5
printf "%5d %+7.2f%% <%3d>", btree, 100.0 * (stl - btree) / stl, btree_size;
printf " [%4.1f, %4.1f]\n", stl_bytes, btree_bytes;
fflush();
} else {
printf "ERROR: %s unrecognized\n", $1
}
}

2420
btree.h Normal file

File diff suppressed because it is too large Load Diff

483
btree_bench.cc Normal file
View File

@ -0,0 +1,483 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
// Author: pmattis@google.com (Peter Mattis)
#include <stdint.h>
#include <algorithm>
#include <functional>
#include <map>
#include <set>
#include <string>
#include <vector>
#include "base/commandlineflags.h"
#include "base/init_google.h"
#include "base/integral_types.h"
#include "base/logging.h"
#include "base/stringprintf.h"
#include "base/type_traits.h"
#include "strings/cord.h"
#include "testing/base/public/benchmark.h"
#include "testing/base/public/googletest.h"
#include "util/btree/btree_map.h"
#include "util/btree/btree_set.h"
#include "util/btree/btree_test.h"
#include "util/random/acmrandom.h"
DECLARE_int32(benchmark_max_iters);
namespace util {
namespace btree {
namespace {
// Benchmark insertion of values into a container.
template <typename T>
void BM_Insert(int n) {
typedef typename base::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]);
}
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
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 base::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]);
}
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
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();
VLOG(4) << 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 base::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]);
}
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
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();
VLOG(4) << r; // Keep compiler from optimizing away r.
}
// Benchmark deletion of values from a container.
template <typename T>
void BM_Delete(int n) {
typedef typename base::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]);
}
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
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 base::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();
CHECK(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;
}
ACMRandom 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])));
DCHECK(e == 1);
container.insert(g(offset + half + add_keys[idx]));
}
StopBenchmarkTiming();
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
}
// Mixed insertion and deletion in the same range using pre-constructed values.
template <typename T>
void BM_MixedAddRem(int n) {
typedef typename base::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();
CHECK(FLAGS_benchmark_values % 2 == 0);
T container;
ACMRandom 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]]));
DCHECK(e == 1);
container.insert(values[add_keys[idx]]);
}
StopBenchmarkTiming();
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
}
// Insertion at end, removal from the beginning. This benchmark
// counts two value constructors.
template <typename T>
void BM_Fifo(int n) {
typedef typename base::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();
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
}
// Iteration (forward) through the tree
template <typename T>
void BM_FwdIter(int n) {
typedef typename base::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();
VLOG(4) << r; // Keep compiler from optimizing away r.
SetBenchmarkLabel(StringPrintf(" %0.2f", ContainerInfo(container)));
}
typedef set<int32> stl_set_int32;
typedef set<int64> stl_set_int64;
typedef set<string> stl_set_string;
typedef set<Cord> stl_set_cord;
typedef map<int32, intptr_t> stl_map_int32;
typedef map<int64, intptr_t> stl_map_int64;
typedef map<string, intptr_t> stl_map_string;
typedef map<Cord, intptr_t> stl_map_cord;
typedef multiset<int32> stl_multiset_int32;
typedef multiset<int64> stl_multiset_int64;
typedef multiset<string> stl_multiset_string;
typedef multiset<Cord> stl_multiset_cord;
typedef multimap<int32, intptr_t> stl_multimap_int32;
typedef multimap<int64, intptr_t> stl_multimap_int64;
typedef multimap<string, intptr_t> stl_multimap_string;
typedef multimap<Cord, intptr_t> stl_multimap_cord;
#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_TYPES(int32, int32);
MY_BENCHMARK_TYPES(int64, int64);
MY_BENCHMARK_TYPES(string, string);
MY_BENCHMARK_TYPES(Cord, cord);
#define MY_BENCHMARK4(type, name, func) \
void BM_ ## type ## _ ## name(int n) { BM_ ## func <type>(n); } \
BENCHMARK(BM_ ## type ## _ ## name)
// Define NODESIZE_TESTING when running btree_perf.py. You need to do
// a local build or raise the distcc timeout, it takes about 5 minutes
// to build:
//
// blaze build --copts=-DNODESIZE_TESTING --cc_strategy=local
// --compilation_mode=opt util/btree/btree_test
#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)
#else
#define MY_BENCHMARK3(tree, type, name, func) \
MY_BENCHMARK4(tree ## _256_ ## 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(set_cord);
MY_BENCHMARK(map_cord);
MY_BENCHMARK(multiset_int32);
MY_BENCHMARK(multimap_int32);
MY_BENCHMARK(multiset_int64);
MY_BENCHMARK(multimap_int64);
MY_BENCHMARK(multiset_string);
MY_BENCHMARK(multimap_string);
MY_BENCHMARK(multiset_cord);
MY_BENCHMARK(multimap_cord);
} // namespace
} // namespace btree
} // namespace util
int main(int argc, char **argv) {
FLAGS_logtostderr = true;
InitGoogle(argv[0], &argc, &argv, true);
RunSpecifiedBenchmarks();
return 0;
}

325
btree_container.h Normal file
View File

@ -0,0 +1,325 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
// Author: pmattis@google.com (Peter Mattis)
#ifndef UTIL_BTREE_BTREE_CONTAINER_H__
#define UTIL_BTREE_BTREE_CONTAINER_H__
#include <iosfwd>
#include <utility>
#include "util/btree/btree.h" // IWYU pragma: export
namespace util {
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);
}
pair<iterator,iterator> equal_range(const key_type &key) {
return tree_.equal_range(key);
}
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(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(); }
protected:
Tree tree_;
};
template <typename T>
inline ostream& operator<<(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.
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 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(comp, alloc) {
insert(b, e);
}
// 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(b, e, 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
} // namespace util
#endif // UTIL_BTREE_BTREE_CONTAINER_H__

122
btree_map.h Normal file
View File

@ -0,0 +1,122 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
// Author: pmattis@google.com (Peter Mattis)
//
// 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 "util/btree/btree.h" // IWYU pragma: export
#include "util/btree/btree_container.h" // IWYU pragma: export
namespace util {
namespace btree {
// The btree_map class is needed mainly for it's constructors.
template <typename Key, typename Value,
typename Compare = less<Key>,
typename Alloc = std::allocator<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(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 it's constructors.
template <typename Key, typename Value,
typename Compare = less<Key>,
typename Alloc = std::allocator<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
} // namespace util
#endif // UTIL_BTREE_BTREE_MAP_H__

50
btree_nc.cc Normal file
View File

@ -0,0 +1,50 @@
// Copyright 2009 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
#include "util/btree/btree_set.h"
namespace {
template <typename R>
struct Compare {
R operator()(int a, int b) const { return reinterpret_cast<R>(a < b); }
};
template <typename R>
struct CompareTo : public util::btree::btree_key_compare_to_tag {
R operator()(int a, int b) const { return reinterpret_cast<R>(a < b); }
};
#define TEST_COMPARE(r) \
void TestCompile() { \
util::btree::btree_set<int, Compare<r> > s; \
}
#define TEST_COMPARE_TO(r) \
void TestCompile() { \
util::btree::btree_set<int, CompareTo<r> > s; \
}
#if defined(TEST_bool)
TEST_COMPARE(bool);
#elif defined(TEST_int)
TEST_COMPARE(int);
#elif defined(TEST_float)
TEST_COMPARE(float);
#elif defined(TEST_pointer)
TEST_COMPARE(void*);
#elif defined(TEST_compare_to_bool)
TEST_COMPARE_TO(bool);
#elif defined(TEST_compare_to_int)
TEST_COMPARE_TO(int);
#elif defined(TEST_compare_to_float)
TEST_COMPARE_TO(float);
#elif defined(TEST_compare_to_pointer)
TEST_COMPARE_TO(pointer);
#endif
} // namespace
int main() {
return 1;
}

83
btree_nc_test.py Executable file
View File

@ -0,0 +1,83 @@
#!/usr/bin/python2.4
#
# Copyright 2006 Google Inc. All Rights Reserved.
"""Negative compilation unit test for btree.h.
"""
__author__ = 'pmattis@google.com (Peter Mattis)'
import os
from google3.testing.pybase import googletest
from google3.testing.pybase import fake_target_util
from google3.pyglib import flags
_FLAGS = flags.FLAGS
class BtreeNegativeUnitTest(googletest.TestCase):
"""Negative compilation tests for btree.h"""
def testCompilerErrors(self):
"""Runs a list of tests to verify that erroneous code leads to
expected compiler messages."""
# Defines a list of test specs, where each element is a tuple
# (test name, list of regexes for matching the compiler errors).
test_specs = [
# Test that bool works as a return type for key comparison.
('bool', None), # None means compilation should succeed.
# Test that int does not work as a return type for key comparison.
('int',
[r'error: creating array with negative size', # for gcc
r'', # for icc
]),
# Test that float does not work as a return type for key comparison.
('float',
[r'error: creating array with negative size', # for gcc
r'', # for icc
]),
# Test that void* does not work as a return type for key comparison.
('pointer',
[r'error: creating array with negative size', # for gcc
r'', # for icc
]),
# Test that bool does not work as a return type for compare-to
# comparison.
('compare_to_bool',
[r'error: creating array with negative size', # for gcc
r'', # for icc
]),
# Test that int works as a return type for compare-to comparison.
('compare_to_int', None), # None means compilation should succeed.
# Test that float does not work as a return type for compare-to
# comparison.
('compare_to_float',
[r'error: creating array with negative size', # for gcc
r'', # for icc
]),
# Test that void* does not work as a return type for compare-to
# comparison.
('compare_to_pointer',
[r'error: creating array with negative size', # for gcc
r'', # for icc
]),
]
# Runs the list of tests.
fake_target_util.AssertCcCompilerErrors(
self,
'google3/util/btree/btree_nc', # path to the fake target file.
'btree_nc.o', # name of the target to build.
test_specs)
if __name__ == '__main__':
googletest.main()

125
btree_printer.py Executable file
View File

@ -0,0 +1,125 @@
#!/usr/bin/python2.4
# Copyright 2011 Google Inc. All Rights Reserved.
# GDB support for pretty printing StringPiece.
"""GDB pretty-printer for btrees."""
# This is a module provided by GDB.
# Ref: http://wiki/Main/Google3GDBScripts
import gdb
from google3.devtools.gdb.component import printing
class BaseBtreePrinter(object):
"""btree pretty-printer, for util/btree."""
def __init__(self, typename, val):
self.typename = typename
self.val = val
def display_hint(self):
return 'array'
def _my_iter(self, node):
count = node['fields_']['count']
if node['fields_']['leaf']:
for i in range(count):
key = node['fields_']['values'][i]
yield ('[item]', key)
else:
# recursive generators are annoying: we can't just recurse, we need
# to expand and yield the values.
for i in range(count+1):
child = node['fields_']['children'][i]
for v in self._my_iter(child.dereference()):
yield v
def children(self):
if self.nelements() != 0:
return self._my_iter(self.val['root_']['data'].dereference())
else:
return iter([])
def nelements(self):
if self.val['root_']['data'] != 0:
root_fields = self.val['root_']['data'].dereference()['fields_']
if root_fields['leaf']:
return root_fields['count']
else:
return root_fields['size']
else:
return 0
def to_string(self): # pylint: disable-msg=C6409
"""GDB calls this to compute the pretty-printed form."""
return '%s with %d elements' % (self.typename, self.nelements())
class BtreePrinter(BaseBtreePrinter):
"""btree<> pretty-printer, for util/btree."""
def __init__(self, val):
BaseBtreePrinter.__init__(self, 'btree', val)
class BtreeSetPrinter(BaseBtreePrinter):
"""btree_set<> pretty-printer."""
def __init__(self, val):
BaseBtreePrinter.__init__(self, 'btree_set', val['tree_'])
class BtreeMultisetPrinter(BaseBtreePrinter):
"""btree_multiset<> pretty-printer."""
def __init__(self, val):
BaseBtreePrinter.__init__(self, 'btree_multiset', val['tree_'])
class BaseBtreeMapPrinter(BaseBtreePrinter):
"""btree maps pretty-printer."""
def __init__(self, typename, val):
BaseBtreePrinter.__init__(self, typename, val['tree_'])
def display_hint(self):
return 'map'
def _my_map_iter(self, g):
for (_, pair) in g:
yield ('[key]', pair['first'])
yield ('[value]', pair['second'])
def children(self):
# we need to break apart the pairs and yield them separately
if self.nelements() != 0:
return self._my_map_iter(BaseBtreePrinter.children(self))
else:
return iter([])
class BtreeMapPrinter(BaseBtreeMapPrinter):
"""btree_map<> pretty-printer."""
def __init__(self, val):
BaseBtreeMapPrinter.__init__(self, 'btree_map', val)
class BtreeMultimapPrinter(BaseBtreeMapPrinter):
"""btree_multimap<> pretty-printer."""
def __init__(self, val):
BaseBtreeMapPrinter.__init__(self, 'btree_multimap', val)
if __name__ == '__main__':
printing.RegisterGoogle3ClassPrettyPrinter('util::btree::btree<.*>',
BtreePrinter)
printing.RegisterGoogle3ClassPrettyPrinter('util::btree::btree_set<.*>',
BtreeSetPrinter)
printing.RegisterGoogle3ClassPrettyPrinter('util::btree::btree_multiset<.*>',
BtreeMultisetPrinter)
printing.RegisterGoogle3ClassPrettyPrinter('util::btree::btree_map<.*>',
BtreeMapPrinter)
printing.RegisterGoogle3ClassPrettyPrinter('util::btree::btree_multimap<.*>',
BtreeMultimapPrinter)

92
btree_printer_test.py Executable file
View File

@ -0,0 +1,92 @@
#!/usr/bin/python2.4
#
# Copyright 2011 Google Inc. All Rights Reserved.
"""Tests for btree_printer.py gdb pretty printer."""
__author__ = "leg@google.com (Lawrence Greenfield)"
from google3.pyglib import flags
from google3.testing.gdb import gdb_script_test_util
from google3.testing.pybase import googletest
FLAGS = flags.FLAGS
class BtreePrinterTest(gdb_script_test_util.TestCase):
def testBtreeSet(self):
self.InitSession("btree_set",
"util/btree/btree_test_program")
self.RunTo("StopHereForDebugger")
self.SetOption("print elements", 20)
self.TestPrintOutputMatches("*empty_set",
"""btree_set with 0 elements""")
self.TestPrintOutputMatches("*small_set",
"""btree_set with 10 elements = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}""")
self.TestPrintOutputMatches("*small_multiset",
"""btree_multiset with 10 elements = {0, 0, 1, 1, 2, 2, 3, 3, 4, 4}""")
self.TestPrintOutputMatches("*big_set",
"""btree_set with 80 elements = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19...}""")
self.RunSession()
def testBtreeMap(self):
self.InitSession("btree_set",
"util/btree/btree_test_program")
self.RunTo("StopHereForDebugger")
self.SetOption("print elements", 30)
self.TestPrintOutputMatches("*empty_map",
"""btree_map with 0 elements""")
self.TestPrintOutputMatches("*small_map",
"""btree_map with 10 elements = {
\\[0\\] = 0,
\\[1\\] = 13,
\\[2\\] = 26,
\\[3\\] = 39,
\\[4\\] = 52,
\\[5\\] = 65,
\\[6\\] = 78,
\\[7\\] = 91,
\\[8\\] = 104,
\\[9\\] = 117
}""")
self.TestPrintOutputMatches("*small_multimap",
"""btree_multimap with 10 elements = {
\\[0\\] = 0,
\\[0\\] = 1,
\\[1\\] = 2,
\\[1\\] = 3,
\\[2\\] = 4,
\\[2\\] = 5,
\\[3\\] = 6,
\\[3\\] = 7,
\\[4\\] = 8,
\\[4\\] = 9
}""")
self.TestPrintOutputMatches("*big_map",
"""btree_map with 80 elements = {
\\[0\\] = 0,
\\[1\\] = 7,
\\[2\\] = 14,
\\[3\\] = 21,
\\[4\\] = 28,
\\[5\\] = 35,
\\[6\\] = 42,
\\[7\\] = 49,
\\[8\\] = 56,
\\[9\\] = 63,
\\[10\\] = 70,
\\[11\\] = 77,
\\[12\\] = 84,
\\[13\\] = 91,
\\[14\\] = 98
...
}""")
self.RunSession()
if __name__ == "__main__":
googletest.main()

113
btree_set.h Normal file
View File

@ -0,0 +1,113 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
// Author: pmattis@google.com (Peter Mattis)
//
// 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 "util/btree/btree.h" // IWYU pragma: export
#include "util/btree/btree_container.h" // IWYU pragma: export
namespace util {
namespace btree {
// The btree_set class is needed mainly for it's constructors.
template <typename Key,
typename Compare = 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 it's constructors.
template <typename Key,
typename Compare = 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
} // namespace util
#endif // UTIL_BTREE_BTREE_SET_H__

167
btree_test.cc Normal file
View File

@ -0,0 +1,167 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
// Author: pmattis@google.com (Peter Mattis)
#include "base/arena-inl.h"
#include "base/init_google.h"
#include "base/integral_types.h"
#include "base/logging.h"
#include "strings/stringpiece.h"
#include "testing/base/public/gunit.h"
#include "util/btree/btree_map.h"
#include "util/btree/btree_set.h"
#include "util/btree/btree_test.h"
namespace util {
namespace btree {
namespace {
template <typename K, int N>
void SetTest() {
typedef ArenaAllocator<K, UnsafeArena> ArenaAlloc;
CHECK_EQ(sizeof(btree_set<K>), sizeof(void*));
BtreeTest<btree_set<K, less<K>, allocator<K>, N>, set<K> >();
BtreeArenaTest<btree_set<K, less<K>, ArenaAlloc, N> >();
}
template <typename K, int N>
void MapTest() {
typedef ArenaAllocator<K, UnsafeArena> ArenaAlloc;
CHECK_EQ(sizeof(btree_map<K, K>), sizeof(void*));
BtreeTest<btree_map<K, K, less<K>, allocator<K>, N>, map<K, K> >();
BtreeArenaTest<btree_map<K, K, less<K>, ArenaAlloc, N> >();
BtreeMapTest<btree_map<K, K, less<K>, allocator<K>, N> >();
}
TEST(Btree, set_int32_32) { SetTest<int32, 32>(); }
TEST(Btree, set_int32_64) { SetTest<int32, 64>(); }
TEST(Btree, set_int32_128) { SetTest<int32, 128>(); }
TEST(Btree, set_int32_256) { SetTest<int32, 256>(); }
TEST(Btree, set_int64_256) { SetTest<int64, 256>(); }
TEST(Btree, set_string_256) { SetTest<string, 256>(); }
TEST(Btree, set_cord_256) { SetTest<Cord, 256>(); }
TEST(Btree, set_pair_256) { SetTest<pair<int, int>, 256>(); }
TEST(Btree, map_int32_256) { MapTest<int32, 256>(); }
TEST(Btree, map_int64_256) { MapTest<int64, 256>(); }
TEST(Btree, map_string_256) { MapTest<string, 256>(); }
TEST(Btree, map_cord_256) { MapTest<Cord, 256>(); }
TEST(Btree, map_pair_256) { MapTest<pair<int, int>, 256>(); }
template <typename K, int N>
void MultiSetTest() {
typedef ArenaAllocator<K, UnsafeArena> ArenaAlloc;
CHECK_EQ(sizeof(btree_multiset<K>), sizeof(void*));
BtreeMultiTest<btree_multiset<K, less<K>, allocator<K>, N>,
multiset<K> >();
BtreeArenaTest<btree_multiset<K, less<K>, ArenaAlloc, N> >();
}
template <typename K, int N>
void MultiMapTest() {
typedef ArenaAllocator<K, UnsafeArena> ArenaAlloc;
CHECK_EQ(sizeof(btree_multimap<K, K>), sizeof(void*));
BtreeMultiTest<btree_multimap<K, K, less<K>, allocator<K>, N>,
multimap<K, K> >();
BtreeMultiMapTest<btree_multimap<K, K, less<K>, allocator<K>, N> >();
BtreeArenaTest<btree_multimap<K, K, less<K>, ArenaAlloc, N> >();
}
TEST(Btree, multiset_int32_256) { MultiSetTest<int32, 256>(); }
TEST(Btree, multiset_int64_256) { MultiSetTest<int64, 256>(); }
TEST(Btree, multiset_string_256) { MultiSetTest<string, 256>(); }
TEST(Btree, multiset_cord_256) { MultiSetTest<Cord, 256>(); }
TEST(Btree, multiset_pair_256) { MultiSetTest<pair<int, int>, 256>(); }
TEST(Btree, multimap_int32_256) { MultiMapTest<int32, 256>(); }
TEST(Btree, multimap_int64_256) { MultiMapTest<int64, 256>(); }
TEST(Btree, multimap_string_256) { MultiMapTest<string, 256>(); }
TEST(Btree, multimap_cord_256) { MultiMapTest<Cord, 256>(); }
TEST(Btree, multimap_pair_256) { MultiMapTest<pair<int, int>, 256>(); }
// Verify that swapping btrees swaps the key comparision functors.
struct SubstringLess {
SubstringLess() : n(2) {}
SubstringLess(int length)
: n(length) {
}
bool operator()(const string &a, const string &b) const {
return StringPiece(a).substr(0, n) < StringPiece(b).substr(0, n);
}
int n;
};
TEST(Btree, SwapKeyCompare) {
typedef btree_set<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<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> my_set;
for (int i = 0; i < kSetSize; ++i) {
my_set.insert(i);
}
{
// Simple increment vs. increment by.
btree_set<int32>::iterator a = my_set.begin();
btree_set<int32>::iterator b = my_set.begin();
a.increment();
b.increment_by(1);
EXPECT_EQ(*a, *b);
}
btree_set<int32>::iterator a = my_set.begin();
for (int i = 1; i < kSetSize; ++i) {
++a;
// increment_by
btree_set<int32>::iterator b = my_set.begin();
b.increment_by(i);
EXPECT_EQ(*a, *b) << ": i=" << i;
}
}
} // namespace
} // namespace btree
} // namespace util
int main(int argc, char **argv) {
FLAGS_logtostderr = true;
InitGoogle(argv[0], &argc, &argv, true);
return RUN_ALL_TESTS();
}

930
btree_test.h Normal file
View File

@ -0,0 +1,930 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
// Author: pmattis@google.com (Peter Mattis)
#ifndef UTIL_BTREE_BTREE_TEST_H__
#define UTIL_BTREE_BTREE_TEST_H__
#include <stdio.h>
#include <algorithm>
#include <functional>
#include <iosfwd>
#include <map>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "base/arena.h"
#include "base/commandlineflags.h"
#include "base/logging.h"
#include "base/type_traits.h"
#include "strings/cord.h"
#include "strings/util.h"
#include "testing/base/public/googletest.h"
#include "util/btree/btree_container.h"
#include "util/random/acmrandom.h"
DECLARE_int32(test_values);
DECLARE_int32(benchmark_values);
namespace std {
// Provide operator<< support for pair<T, U>.
template <typename T, typename U>
ostream& operator<<(ostream &os, const 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 pair<T, U> to pair<const T, U>.
template <typename T, typename U, typename V, typename W>
bool operator==(const pair<T, U> &x, const pair<V, W> &y) {
return x.first == y.first && x.second == y.second;
}
} // namespace std
namespace base {
// Partial specialization of remove_const that propagates the removal through
// std::pair.
template <typename T, typename U>
struct remove_const<std::pair<T, U> > {
typedef std::pair<typename remove_const<T>::type,
typename remove_const<U>::type> type;
};
} // namespace base
namespace util {
namespace btree {
// 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;
};
// 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;
};
// 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_) {
}
// 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()) {
CHECK(checker_iter == checker_.end());
} else {
CHECK_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()) {
CHECK(checker_iter == checker_.rend());
} else {
CHECK_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);
CHECK_EQ(*find(key), x);
lower_bound(key);
upper_bound(key);
equal_range(key);
count(key);
}
void erase_check(const key_type &key) {
CHECK(tree_.find(key) == const_tree_.end());
CHECK(const_tree_.find(key) == tree_.end());
CHECK(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));
}
pair<iterator,iterator> equal_range(const key_type &key) {
pair<typename CheckerType::iterator,
typename CheckerType::iterator> checker_res =
checker_.equal_range(key);
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;
}
pair<const_iterator,const_iterator> equal_range(const key_type &key) const {
pair<typename CheckerType::const_iterator,
typename CheckerType::const_iterator> checker_res =
checker_.equal_range(key);
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);
CHECK_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);
CHECK_EQ(res, tree_.count(key));
CHECK_EQ(res, tree_.erase(key));
CHECK_EQ(tree_.count(key), 0);
CHECK_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);
CHECK_EQ(tree_.size(), checker_.size());
CHECK_EQ(tree_.size(), size - 1);
CHECK_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);
CHECK_EQ(tree_.size(), checker_.size());
CHECK_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();
CHECK_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) {
CHECK_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;
}
CHECK(tree_iter == tree_.begin());
CHECK(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) {
CHECK_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;
}
CHECK(tree_riter == tree_.rbegin());
CHECK(checker_riter == checker_.rbegin());
}
// Access to the underlying btree.
const TreeType& tree() const { return tree_; }
// Size routines.
size_type size() const {
CHECK_EQ(tree_.size(), checker_.size());
return tree_.size();
}
size_type max_size() const { return tree_.max_size(); }
bool empty() const {
CHECK_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) {
insert(b, e);
}
// Insertion routines.
pair<iterator,bool> insert(const value_type &x) {
int size = this->tree_.size();
pair<typename CheckerType::iterator,bool> checker_res =
this->checker_.insert(x);
pair<iterator,bool> tree_res = this->tree_.insert(x);
CHECK_EQ(*tree_res.first, *checker_res.first);
CHECK_EQ(tree_res.second, checker_res.second);
CHECK_EQ(this->tree_.size(), this->checker_.size());
CHECK_EQ(this->tree_.size(), size + tree_res.second);
return tree_res;
}
iterator insert(iterator position, const value_type &x) {
int size = this->tree_.size();
pair<typename CheckerType::iterator,bool> checker_res =
this->checker_.insert(x);
iterator tree_res = this->tree_.insert(position, x);
CHECK_EQ(*tree_res, *checker_res.first);
CHECK_EQ(this->tree_.size(), this->checker_.size());
CHECK_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) {
insert(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);
CHECK_EQ(*tree_res, *checker_res);
CHECK_EQ(this->tree_.size(), this->checker_.size());
CHECK_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);
CHECK_EQ(*tree_res, *checker_res);
CHECK_EQ(this->tree_.size(), this->checker_.size());
CHECK_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) {
DCHECK_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 {
DCHECK_LE(i, maxval);
return i;
}
};
template <>
struct Generator<string> {
int maxval;
Generator(int m)
: maxval(m) {
}
string operator()(int i) const {
char buf[16];
return GenerateDigits(buf, i, maxval);
}
};
template <>
struct Generator<Cord> {
int maxval;
Generator(int m)
: maxval(m) {
}
Cord operator()(int i) const {
char buf[16];
return Cord(GenerateDigits(buf, i, maxval));
}
};
template <typename T, typename U>
struct Generator<pair<T, U> > {
Generator<typename base::remove_const<T>::type> tgen;
Generator<typename base::remove_const<U>::type> ugen;
Generator(int m)
: tgen(m),
ugen(m) {
}
pair<T, U> operator()(int i) const {
return make_pair(tgen(i), ugen(i));
}
};
// Generate values for our tests and benchmarks. Value range is [0, maxval].
const vector<int>& GenerateNumbers(int n, int maxval) {
static ACMRandom rand(FLAGS_test_random_seed);
static vector<int> values;
static set<int> unique_values;
if (values.size() < n) {
for (int i = values.size(); i < n; i++) {
int value;
do {
value = rand.Next() % (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>
vector<V> GenerateValues(int n) {
int two_times_max = 2 * max(FLAGS_benchmark_values, FLAGS_test_values);
int four_times_max = 2 * two_times_max;
DCHECK_LE(n, two_times_max);
const vector<int> &nums = GenerateNumbers(n, four_times_max);
Generator<V> gen(four_times_max);
vector<V> vec;
for (int i = 0; i < n; i++) {
vec.push_back(gen(nums[i]));
}
return vec;
}
template <typename K>
double ContainerInfo(const set<K> &s) {
int sizeof_node = sizeof(std::_Rb_tree_node<K>);
int bytes_used = sizeof(s) + s.size() * sizeof_node;
double bytes_per_value = (double) bytes_used / s.size();
VLOG(1) << " size=" << s.size()
<< " bytes-used=" << bytes_used
<< " bytes-per-value=" << bytes_per_value;
return bytes_per_value;
}
template <typename K>
double ContainerInfo(const multiset<K> &s) {
int sizeof_node = sizeof(std::_Rb_tree_node<K>);
int bytes_used = sizeof(s) + s.size() * sizeof_node;
double bytes_per_value = (double) bytes_used / s.size();
VLOG(1) << " size=" << s.size()
<< " bytes-used=" << bytes_used
<< " bytes-per-value=" << bytes_per_value;
return bytes_per_value;
}
template <typename K, typename V>
double ContainerInfo(const map<K, V> &m) {
int sizeof_node = sizeof(std::_Rb_tree_node<pair<K, V> >);
int bytes_used = sizeof(m) + m.size() * sizeof_node;
double bytes_per_value = (double) bytes_used / m.size();
VLOG(1) << " size=" << m.size()
<< " bytes-used=" << bytes_used
<< " bytes-per-value=" << bytes_per_value;
return bytes_per_value;
}
template <typename K, typename V>
double ContainerInfo(const multimap<K, V> &m) {
int sizeof_node = sizeof(std::_Rb_tree_node<pair<K, V> >);
int bytes_used = sizeof(m) + m.size() * sizeof_node;
double bytes_per_value = (double) bytes_used / m.size();
VLOG(1) << " size=" << m.size()
<< " bytes-used=" << bytes_used
<< " bytes-per-value=" << bytes_per_value;
return bytes_per_value;
}
template <typename P>
double ContainerInfo(const btree_container<P> &b) {
double bytes_used = sizeof(b) + b.bytes_used();
double bytes_per_value = (double) bytes_used / b.size();
VLOG(1) << " size=" << b.size()
<< " bytes-used=" << bytes_used
<< " bytes-per-value=" << bytes_per_value
<< " height=" << b.height()
<< " internal-nodes=" << b.internal_nodes()
<< " leaf-nodes=" << b.leaf_nodes()
<< " fullness=" << b.fullness()
<< " overhead=" << b.overhead();
return bytes_per_value;
}
template <typename T, typename V>
void DoTest(const char *name, T *b, const 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]);
}
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);
CHECK_EQ(b_copy.size(), const_b.size());
CHECK_LE(b_copy.height(), const_b.height());
CHECK_LE(b_copy.internal_nodes(), const_b.internal_nodes());
CHECK_LE(b_copy.leaf_nodes(), const_b.leaf_nodes());
for (int i = 0; i < values.size(); ++i) {
CHECK_EQ(*b_copy.find(key_of_value(values[i])), values[i]);
}
// Test range constructor.
T b_range(const_b.begin(), const_b.end());
CHECK_EQ(b_range.size(), const_b.size());
CHECK_LE(b_range.height(), const_b.height());
CHECK_LE(b_range.internal_nodes(), const_b.internal_nodes());
CHECK_LE(b_range.leaf_nodes(), const_b.leaf_nodes());
for (int i = 0; i < values.size(); ++i) {
CHECK_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());
CHECK_EQ(b_range.size(), b_copy.size());
CHECK_EQ(b_range.height(), b_copy.height());
CHECK_EQ(b_range.internal_nodes(), b_copy.internal_nodes());
CHECK_EQ(b_range.leaf_nodes(), b_copy.leaf_nodes());
for (int i = 0; i < values.size(); ++i) {
CHECK_EQ(*b_range.find(key_of_value(values[i])), values[i]);
}
// Test assignment to self. Nothing should change.
b_range.operator=(b_range);
CHECK_EQ(b_range.size(), b_copy.size());
CHECK_EQ(b_range.height(), b_copy.height());
CHECK_EQ(b_range.internal_nodes(), b_copy.internal_nodes());
CHECK_EQ(b_range.leaf_nodes(), b_copy.leaf_nodes());
// Test assignment of new values.
b_range.clear();
b_range = b_copy;
CHECK_EQ(b_range.size(), b_copy.size());
CHECK_EQ(b_range.height(), b_copy.height());
CHECK_EQ(b_range.internal_nodes(), b_copy.internal_nodes());
CHECK_EQ(b_range.leaf_nodes(), b_copy.leaf_nodes());
// Test swap.
b_range.clear();
b_range.swap(b_copy);
CHECK_EQ(b_copy.size(), 0);
CHECK_EQ(b_range.size(), const_b.size());
for (int i = 0; i < values.size(); ++i) {
CHECK_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.
CHECK_EQ(mutable_b.erase(key_of_value(values[i])), 0);
}
const_b.verify();
CHECK_EQ(const_b.internal_nodes(), 0);
CHECK_EQ(const_b.leaf_nodes(), 0);
CHECK_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();
CHECK_EQ(const_b.internal_nodes(), 0);
CHECK_EQ(const_b.leaf_nodes(), 0);
CHECK_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.
ostringstream strm;
strm << mutable_b.tree();
CHECK_EQ(mutable_b.size(), strcount(strm.str(), '\n'));
// Test range erase.
mutable_b.erase(mutable_b.begin(), mutable_b.end());
CHECK_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);
CHECK_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());
CHECK_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);
CHECK_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);
CHECK(mutable_b.find(key_of_value(value)) != const_b.end());
CHECK(const_b.find(key_of_value(value)) != mutable_b.end());
CHECK_EQ(*const_b.lower_bound(key_of_value(value)), value);
CHECK(const_b.upper_bound(key_of_value(value)) == const_b.end());
CHECK_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());
CHECK(mutable_iter == const_b.begin());
CHECK(mutable_iter != const_b.end());
CHECK(const_b.begin() == mutable_iter);
CHECK(const_b.end() != mutable_iter);
typename T::reverse_iterator mutable_riter(mutable_b.rbegin());
CHECK(mutable_riter == const_b.rbegin());
CHECK(mutable_riter != const_b.rend());
CHECK(const_b.rbegin() == mutable_riter);
CHECK(const_b.rend() != mutable_riter);
// We can create a const iterator from a non-const iterator.
typename T::const_iterator const_iter(mutable_iter);
CHECK(const_iter == mutable_b.begin());
CHECK(const_iter != mutable_b.end());
CHECK(mutable_b.begin() == const_iter);
CHECK(mutable_b.end() != const_iter);
typename T::const_reverse_iterator const_riter(mutable_riter);
CHECK(const_riter == mutable_b.rbegin());
CHECK(const_riter != mutable_b.rend());
CHECK(mutable_b.rbegin() == const_riter);
CHECK(mutable_b.rend() != const_riter);
// Make sure various methods can be invoked on a const container.
const_b.verify();
CHECK(!const_b.empty());
CHECK_EQ(const_b.size(), 1);
CHECK_GT(const_b.max_size(), 0);
CHECK_EQ(const_b.height(), 1);
CHECK_EQ(const_b.count(key_of_value(value)), 1);
CHECK_EQ(const_b.internal_nodes(), 0);
CHECK_EQ(const_b.leaf_nodes(), 1);
CHECK_EQ(const_b.nodes(), 1);
CHECK_GT(const_b.bytes_used(), 0);
CHECK_GT(const_b.fullness(), 0);
CHECK_GT(const_b.overhead(), 0);
}
template <typename T, typename C>
void BtreeTest() {
ConstTest<T>();
typedef typename base::remove_const<typename T::value_type>::type V;
vector<V> random_values = GenerateValues<V>(FLAGS_test_values);
unique_checker<T, C> container;
// Test key insertion/deletion in sorted order.
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 base::remove_const<typename T::value_type>::type V;
const vector<V>& random_values = GenerateValues<V>(FLAGS_test_values);
multi_checker<T, C> container;
// Test keys in sorted order.
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.
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.
vector<V> identical_values(100);
fill(identical_values.begin(), identical_values.end(), Generator<V>(2)(2));
DoTest("identical: ", &container, identical_values);
}
template <typename T>
void BtreeArenaTest() {
typedef typename T::value_type value_type;
UnsafeArena arena1(1000);
UnsafeArena arena2(1000);
T b1(typename T::key_compare(), &arena1);
T b2(typename T::key_compare(), &arena2);
// 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 arena2!
CHECK_LE(b1.bytes_used(), arena2.status().bytes_allocated());
CHECK_GT(arena2.block_count(), arena1.block_count());
}
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;
}
CHECK_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.
CHECK_EQ(b.begin()->first, Generator<value_type>(1000)(0).first);
CHECK_EQ(b.begin()->second, Generator<value_type>(1000)(0).second);
CHECK_EQ(b.rbegin()->first, Generator<value_type>(1000)(999).first);
CHECK_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
} // namespace util
#endif // UTIL_BTREE_BTREE_TEST_H__

9
btree_test_flags.cc Normal file
View File

@ -0,0 +1,9 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: pmattis@google.com (Peter Mattis)
#include "base/commandlineflags.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.");

64
btree_test_program.cc Normal file
View File

@ -0,0 +1,64 @@
// Copyright 2011 Google Inc. All Rights Reserved.
// Author: leg@google.com (Lawrence Greenfield)
#include "base/init_google.h"
#include "base/logging.h"
#include "devtools/gdb/component/gdb_test_utils.h"
#include "util/btree/btree_map.h"
#include "util/btree/btree_set.h"
using util::btree::btree_set;
using util::btree::btree_multiset;
using util::btree::btree_map;
using util::btree::btree_multimap;
static btree_set<int>* empty_set;
static btree_set<int>* small_set;
static btree_set<int>* big_set;
static btree_multiset<int>* small_multiset;
static btree_map<int, int>* empty_map;
static btree_map<int, int>* small_map;
static btree_map<int, int>* big_map;
static btree_multimap<int, int>* small_multimap;
static void SetupBtreeSets() {
empty_set = new btree_set<int>;
small_set = new btree_set<int>;
small_multiset = new btree_multiset<int>;
big_set = new btree_set<int>;
for (int i = 0; i < 10; ++i) {
small_set->insert(i);
small_multiset->insert(i / 2);
}
for (int i = 0; i < 80; ++i) {
big_set->insert(i);
}
}
static void SetupBtreeMaps() {
empty_map = new btree_map<int, int>;
small_map = new btree_map<int, int>;
small_multimap = new btree_multimap<int, int>;
big_map = new btree_map<int, int>;
for (int i = 0; i < 10; ++i) {
small_map->insert(make_pair(i, i * 13));
small_multimap->insert(make_pair(i / 2, i));
}
for (int i = 0; i < 80; ++i) {
big_map->insert(make_pair(i, i * 7));
}
}
int main(int argc, char** argv) {
FLAGS_logtostderr = true;
InitGoogle(argv[0], &argc, &argv, true);
SetupBtreeSets();
SetupBtreeMaps();
StopHereForDebugger();
return 0;
}

379
safe_btree.h Normal file
View File

@ -0,0 +1,379 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: pmattis@google.com (Peter Mattis)
//
// 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.
#ifndef UTIL_BTREE_SAFE_BTREE_H__
#define UTIL_BTREE_SAFE_BTREE_H__
#include <stddef.h>
#include <iosfwd>
#include <utility>
#include "base/integral_types.h"
#include "base/logging.h"
#include "util/btree/btree.h"
namespace util {
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 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_;
}
reference operator*() const {
DCHECK_GT(generation_, 0);
return iter().operator*();
}
pointer operator->() const {
DCHECK_GT(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 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));
}
pair<iterator, iterator> equal_range(const key_type &key) {
pair<tree_iterator, tree_iterator> p = tree_.equal_range(key);
return make_pair(iterator(this, p.first),
iterator(this, p.second));
}
pair<const_iterator, const_iterator> equal_range(const key_type &key) const {
pair<tree_const_iterator, tree_const_iterator> p = tree_.equal_range(key);
return 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>
pair<iterator, bool> insert_unique(const key_type &key, ValuePointer value) {
pair<tree_iterator, bool> p = tree_.insert_unique(key, value);
generation_ += p.second;
return make_pair(iterator(this, p.first), p.second);
}
pair<iterator, bool> insert_unique(const value_type &v) {
pair<tree_iterator, bool> p = tree_.insert_unique(v);
generation_ += p.second;
return 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(ostream &os) const {
tree_.dump(os);
}
void verify() const {
tree_.verify();
}
int64 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 generation_;
};
} // namespace btree
} // namespace util
#endif // UTIL_BTREE_SAFE_BTREE_H__

70
safe_btree_map.h Normal file
View File

@ -0,0 +1,70 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: pmattis@google.com (Peter Mattis)
//
// 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.
#ifndef UTIL_BTREE_SAFE_BTREE_MAP_H__
#define UTIL_BTREE_SAFE_BTREE_MAP_H__
#include <functional>
#include <memory>
#include <utility>
#include "util/btree/btree_container.h"
#include "util/btree/btree_map.h"
#include "util/btree/safe_btree.h"
namespace util {
namespace btree {
// The safe_btree_map class is needed mainly for it's constructors.
template <typename Key, typename Value,
typename Compare = less<Key>,
typename Alloc = std::allocator<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
} // namespace util
#endif // UTIL_BTREE_SAFE_BTREE_MAP_H__

68
safe_btree_set.h Normal file
View File

@ -0,0 +1,68 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: pmattis@google.com (Peter Mattis)
//
// 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.
#ifndef UTIL_BTREE_SAFE_BTREE_SET_H__
#define UTIL_BTREE_SAFE_BTREE_SET_H__
#include <functional>
#include <memory>
#include "util/btree/btree_container.h"
#include "util/btree/btree_set.h"
#include "util/btree/safe_btree.h"
namespace util {
namespace btree {
// The safe_btree_set class is needed mainly for it's constructors.
template <typename Key,
typename Compare = 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
} // namespace util
#endif // UTIL_BTREE_SAFE_BTREE_SET_H__

67
safe_btree_test.cc Normal file
View File

@ -0,0 +1,67 @@
// Copyright 2007 Google Inc. All Rights Reserved.
// Author: jmacd@google.com (Josh MacDonald)
// Author: pmattis@google.com (Peter Mattis)
// 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 "base/arena-inl.h"
#include "base/init_google.h"
#include "base/integral_types.h"
#include "base/logging.h"
#include "strings/cord.h"
#include "testing/base/public/gunit.h"
#include "util/btree/btree_test.h"
#include "util/btree/safe_btree_map.h"
#include "util/btree/safe_btree_set.h"
class UnsafeArena;
namespace util {
namespace btree {
namespace {
template <typename K, int N>
void SetTest() {
typedef ArenaAllocator<K, UnsafeArena> ArenaAlloc;
BtreeTest<safe_btree_set<K, less<K>, allocator<K>, N>, set<K> >();
BtreeArenaTest<safe_btree_set<K, less<K>, ArenaAlloc, N> >();
}
template <typename K, int N>
void MapTest() {
typedef ArenaAllocator<K, UnsafeArena> ArenaAlloc;
BtreeTest<safe_btree_map<K, K, less<K>, allocator<K>, N>, map<K, K> >();
BtreeArenaTest<safe_btree_map<K, K, less<K>, ArenaAlloc, N> >();
BtreeMapTest<safe_btree_map<K, K, less<K>, allocator<K>, N> >();
}
TEST(SafeBtree, set_int32_32) { SetTest<int32, 32>(); }
TEST(SafeBtree, set_int32_64) { SetTest<int32, 64>(); }
TEST(SafeBtree, set_int32_128) { SetTest<int32, 128>(); }
TEST(SafeBtree, set_int32_256) { SetTest<int32, 256>(); }
TEST(SafeBtree, set_int64_256) { SetTest<int64, 256>(); }
TEST(SafeBtree, set_string_256) { SetTest<string, 256>(); }
TEST(SafeBtree, set_cord_256) { SetTest<Cord, 256>(); }
TEST(SafeBtree, set_pair_256) { SetTest<pair<int, int>, 256>(); }
TEST(SafeBtree, map_int32_256) { MapTest<int32, 256>(); }
TEST(SafeBtree, map_int64_256) { MapTest<int64, 256>(); }
TEST(SafeBtree, map_string_256) { MapTest<string, 256>(); }
TEST(SafeBtree, map_cord_256) { MapTest<Cord, 256>(); }
TEST(SafeBtree, map_pair_256) { MapTest<pair<int, int>, 256>(); }
} // namespace
} // namespace btree
} // namespace util
int main(int argc, char **argv) {
FLAGS_logtostderr = true;
InitGoogle(argv[0], &argc, &argv, true);
return RUN_ALL_TESTS();
}