Import cpp-btree library into 3rdparty section.
Repo: https://github.com/JGRennison/cpp-btree Commmit: 6cabdb40fcbb7e12e6d499f92b898f6ec80ae0f1 Remove test program, build scripts, etc.pull/11/head
parent
8a2f9311a9
commit
b4cfb1adbc
@ -0,0 +1,202 @@
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright [yyyy] [name of copyright owner]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
@ -0,0 +1,45 @@
|
||||
This library is a C++ template library and, as such, there is no
|
||||
library to build and install. Copy the .h files and use them!
|
||||
|
||||
See http://code.google.com/p/cpp-btree/wiki/UsageInstructions for
|
||||
details.
|
||||
|
||||
----
|
||||
|
||||
To build and run the provided tests, however, you will need to install
|
||||
CMake, the Google C++ Test framework, and the Google flags package.
|
||||
|
||||
Download and install CMake from http://www.cmake.org
|
||||
|
||||
Download and build the GoogleTest framework from
|
||||
http://code.google.com/p/googletest
|
||||
|
||||
Download and install gflags from https://code.google.com/p/gflags
|
||||
|
||||
Set GTEST_ROOT to the directory where GTEST was built.
|
||||
Set GFLAGS_ROOT to the directory prefix where GFLAGS is installed.
|
||||
|
||||
export GTEST_ROOT=/path/for/gtest-x.y
|
||||
export GFLAGS_ROOT=/opt
|
||||
|
||||
cmake . -Dbuild_tests=ON
|
||||
|
||||
For example, to build on a Unix system with the clang++ compiler,
|
||||
|
||||
export GTEST_ROOT=$(HOME)/src/googletest
|
||||
export GFLAGS_ROOT=/opt
|
||||
cmake . -G "Unix Makefiles" -Dbuild_tests=ON -DCMAKE_CXX_COMPILER=clang++
|
||||
|
||||
----
|
||||
|
||||
Note that this is a modified version of the original at http://code.google.com/p/cpp-btree
|
||||
|
||||
Changes include:
|
||||
- Changing size_type from ssize_t to size_t
|
||||
- Adding cbegin, cend, crbegin, crend.
|
||||
- Adding key_comp
|
||||
- Adding move constructors/assignment.
|
||||
- No longer #define NDEBUG if unset, this can clash with other headers, test for BTREE_DEBUG being defined instead
|
||||
|
||||
CMakeLists-pthreads-fix.txt is a modified copy of CMakeLists.txt which includes -lpthreads when building the tests/benchmarks.
|
||||
Using this instead fixes compilation on some platforms.
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,357 @@
|
||||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef UTIL_BTREE_BTREE_CONTAINER_H__
|
||||
#define UTIL_BTREE_BTREE_CONTAINER_H__
|
||||
|
||||
#include <iosfwd>
|
||||
#include <utility>
|
||||
|
||||
#include "btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// A common base class for btree_set, btree_map, btree_multiset and
|
||||
// btree_multimap.
|
||||
template <typename Tree>
|
||||
class btree_container {
|
||||
typedef btree_container<Tree> self_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::params_type params_type;
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
typedef typename Tree::pointer pointer;
|
||||
typedef typename Tree::const_pointer const_pointer;
|
||||
typedef typename Tree::reference reference;
|
||||
typedef typename Tree::const_reference const_reference;
|
||||
typedef typename Tree::size_type size_type;
|
||||
typedef typename Tree::difference_type difference_type;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
typedef typename Tree::reverse_iterator reverse_iterator;
|
||||
typedef typename Tree::const_reverse_iterator const_reverse_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_container(const key_compare &comp, const allocator_type &alloc)
|
||||
: tree_(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_container(const self_type &x)
|
||||
: tree_(x.tree_) {
|
||||
}
|
||||
|
||||
// Iterator routines.
|
||||
iterator begin() { return tree_.begin(); }
|
||||
const_iterator begin() const { return tree_.begin(); }
|
||||
iterator end() { return tree_.end(); }
|
||||
const_iterator end() const { return tree_.end(); }
|
||||
reverse_iterator rbegin() { return tree_.rbegin(); }
|
||||
const_reverse_iterator rbegin() const { return tree_.rbegin(); }
|
||||
reverse_iterator rend() { return tree_.rend(); }
|
||||
const_reverse_iterator rend() const { return tree_.rend(); }
|
||||
|
||||
// Const iterator routines.
|
||||
const_iterator cbegin() const { return begin(); }
|
||||
const_iterator cend() const { return end(); }
|
||||
const_reverse_iterator crbegin() const { return rbegin(); }
|
||||
const_reverse_iterator crend() const { return rend(); }
|
||||
|
||||
// Lookup routines.
|
||||
iterator lower_bound(const key_type &key) {
|
||||
return tree_.lower_bound(key);
|
||||
}
|
||||
const_iterator lower_bound(const key_type &key) const {
|
||||
return tree_.lower_bound(key);
|
||||
}
|
||||
iterator upper_bound(const key_type &key) {
|
||||
return tree_.upper_bound(key);
|
||||
}
|
||||
const_iterator upper_bound(const key_type &key) const {
|
||||
return tree_.upper_bound(key);
|
||||
}
|
||||
std::pair<iterator,iterator> equal_range(const key_type &key) {
|
||||
return tree_.equal_range(key);
|
||||
}
|
||||
std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const {
|
||||
return tree_.equal_range(key);
|
||||
}
|
||||
|
||||
// Utility routines.
|
||||
void clear() {
|
||||
tree_.clear();
|
||||
}
|
||||
void swap(self_type &x) {
|
||||
tree_.swap(x.tree_);
|
||||
}
|
||||
void dump(std::ostream &os) const {
|
||||
tree_.dump(os);
|
||||
}
|
||||
void verify() const {
|
||||
tree_.verify();
|
||||
}
|
||||
|
||||
// Size routines.
|
||||
size_type size() const { return tree_.size(); }
|
||||
size_type max_size() const { return tree_.max_size(); }
|
||||
bool empty() const { return tree_.empty(); }
|
||||
size_type height() const { return tree_.height(); }
|
||||
size_type internal_nodes() const { return tree_.internal_nodes(); }
|
||||
size_type leaf_nodes() const { return tree_.leaf_nodes(); }
|
||||
size_type nodes() const { return tree_.nodes(); }
|
||||
size_type bytes_used() const { return tree_.bytes_used(); }
|
||||
static double average_bytes_per_value() {
|
||||
return Tree::average_bytes_per_value();
|
||||
}
|
||||
double fullness() const { return tree_.fullness(); }
|
||||
double overhead() const { return tree_.overhead(); }
|
||||
|
||||
bool operator==(const self_type& x) const {
|
||||
if (size() != x.size()) {
|
||||
return false;
|
||||
}
|
||||
for (const_iterator i = begin(), xi = x.begin(); i != end(); ++i, ++xi) {
|
||||
if (*i != *xi) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool operator!=(const self_type& other) const {
|
||||
return !operator==(other);
|
||||
}
|
||||
|
||||
// Functor retrieval
|
||||
key_compare key_comp() const { return tree_.key_comp(); }
|
||||
|
||||
protected:
|
||||
Tree tree_;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
inline std::ostream& operator<<(std::ostream &os, const btree_container<T> &b) {
|
||||
b.dump(os);
|
||||
return os;
|
||||
}
|
||||
|
||||
// A common base class for btree_set and safe_btree_set.
|
||||
template <typename Tree>
|
||||
class btree_unique_container : public btree_container<Tree> {
|
||||
typedef btree_unique_container<Tree> self_type;
|
||||
typedef btree_container<Tree> super_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::size_type size_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_unique_container(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_unique_container(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_unique_container(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
insert(b, e);
|
||||
}
|
||||
|
||||
// Lookup routines.
|
||||
iterator find(const key_type &key) {
|
||||
return this->tree_.find_unique(key);
|
||||
}
|
||||
const_iterator find(const key_type &key) const {
|
||||
return this->tree_.find_unique(key);
|
||||
}
|
||||
size_type count(const key_type &key) const {
|
||||
return this->tree_.count_unique(key);
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
std::pair<iterator,bool> insert(const value_type &x) {
|
||||
return this->tree_.insert_unique(x);
|
||||
}
|
||||
iterator insert(iterator position, const value_type &x) {
|
||||
return this->tree_.insert_unique(position, x);
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert(InputIterator b, InputIterator e) {
|
||||
this->tree_.insert_unique(b, e);
|
||||
}
|
||||
|
||||
// Deletion routines.
|
||||
int erase(const key_type &key) {
|
||||
return this->tree_.erase_unique(key);
|
||||
}
|
||||
// Erase the specified iterator from the btree. The iterator must be valid
|
||||
// (i.e. not equal to end()). Return an iterator pointing to the node after
|
||||
// the one that was erased (or end() if none exists).
|
||||
iterator erase(const iterator &iter) {
|
||||
return this->tree_.erase(iter);
|
||||
}
|
||||
void erase(const iterator &first, const iterator &last) {
|
||||
this->tree_.erase(first, last);
|
||||
}
|
||||
};
|
||||
|
||||
// A common base class for btree_map and safe_btree_map.
|
||||
template <typename Tree>
|
||||
class btree_map_container : public btree_unique_container<Tree> {
|
||||
typedef btree_map_container<Tree> self_type;
|
||||
typedef btree_unique_container<Tree> super_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::data_type data_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::mapped_type mapped_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
|
||||
private:
|
||||
// A pointer-like object which only generates its value when
|
||||
// dereferenced. Used by operator[] to avoid constructing an empty data_type
|
||||
// if the key already exists in the map.
|
||||
struct generate_value {
|
||||
generate_value(const key_type &k)
|
||||
: key(k) {
|
||||
}
|
||||
value_type operator*() const {
|
||||
return std::make_pair(key, data_type());
|
||||
}
|
||||
const key_type &key;
|
||||
};
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_map_container(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_map_container(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_map_container(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
data_type& operator[](const key_type &key) {
|
||||
return this->tree_.insert_unique(key, generate_value(key)).first->second;
|
||||
}
|
||||
};
|
||||
|
||||
// A common base class for btree_multiset and btree_multimap.
|
||||
template <typename Tree>
|
||||
class btree_multi_container : public btree_container<Tree> {
|
||||
typedef btree_multi_container<Tree> self_type;
|
||||
typedef btree_container<Tree> super_type;
|
||||
|
||||
public:
|
||||
typedef typename Tree::key_type key_type;
|
||||
typedef typename Tree::value_type value_type;
|
||||
typedef typename Tree::size_type size_type;
|
||||
typedef typename Tree::key_compare key_compare;
|
||||
typedef typename Tree::allocator_type allocator_type;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_multi_container(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_multi_container(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_multi_container(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
insert(b, e);
|
||||
}
|
||||
|
||||
// Lookup routines.
|
||||
iterator find(const key_type &key) {
|
||||
return this->tree_.find_multi(key);
|
||||
}
|
||||
const_iterator find(const key_type &key) const {
|
||||
return this->tree_.find_multi(key);
|
||||
}
|
||||
size_type count(const key_type &key) const {
|
||||
return this->tree_.count_multi(key);
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
iterator insert(const value_type &x) {
|
||||
return this->tree_.insert_multi(x);
|
||||
}
|
||||
iterator insert(iterator position, const value_type &x) {
|
||||
return this->tree_.insert_multi(position, x);
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert(InputIterator b, InputIterator e) {
|
||||
this->tree_.insert_multi(b, e);
|
||||
}
|
||||
|
||||
// Deletion routines.
|
||||
int erase(const key_type &key) {
|
||||
return this->tree_.erase_multi(key);
|
||||
}
|
||||
// Erase the specified iterator from the btree. The iterator must be valid
|
||||
// (i.e. not equal to end()). Return an iterator pointing to the node after
|
||||
// the one that was erased (or end() if none exists).
|
||||
iterator erase(const iterator &iter) {
|
||||
return this->tree_.erase(iter);
|
||||
}
|
||||
void erase(const iterator &first, const iterator &last) {
|
||||
this->tree_.erase(first, last);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_BTREE_CONTAINER_H__
|
@ -0,0 +1,154 @@
|
||||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// A btree_map<> implements the STL unique sorted associative container
|
||||
// interface and the pair associative container interface (a.k.a map<>) using a
|
||||
// btree. A btree_multimap<> implements the STL multiple sorted associative
|
||||
// container interface and the pair associtive container interface (a.k.a
|
||||
// multimap<>) using a btree. See btree.h for details of the btree
|
||||
// implementation and caveats.
|
||||
|
||||
#ifndef UTIL_BTREE_BTREE_MAP_H__
|
||||
#define UTIL_BTREE_BTREE_MAP_H__
|
||||
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#include "btree.h"
|
||||
#include "btree_container.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The btree_map class is needed mainly for its constructors.
|
||||
template <typename Key, typename Value,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<std::pair<const Key, Value> >,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_map : public btree_map_container<
|
||||
btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_map<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_map_params<
|
||||
Key, Value, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_map_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_map(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_map(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Move constructor.
|
||||
btree_map(self_type &&x)
|
||||
: super_type() {
|
||||
this->swap(x);
|
||||
}
|
||||
|
||||
// Copy/move assignment
|
||||
self_type& operator=(self_type x) {
|
||||
this->swap(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_map(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename V, typename C, typename A, int N>
|
||||
inline void swap(btree_map<K, V, C, A, N> &x,
|
||||
btree_map<K, V, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
// The btree_multimap class is needed mainly for its constructors.
|
||||
template <typename Key, typename Value,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<std::pair<const Key, Value> >,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_multimap : public btree_multi_container<
|
||||
btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_multimap<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_map_params<
|
||||
Key, Value, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_multi_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
typedef typename btree_type::data_type data_type;
|
||||
typedef typename btree_type::mapped_type mapped_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_multimap(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_multimap(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Move constructor.
|
||||
btree_multimap(self_type &&x)
|
||||
: super_type() {
|
||||
this->swap(x);
|
||||
}
|
||||
|
||||
// Copy/move assignment
|
||||
self_type& operator=(self_type x) {
|
||||
this->swap(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_multimap(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename V, typename C, typename A, int N>
|
||||
inline void swap(btree_multimap<K, V, C, A, N> &x,
|
||||
btree_multimap<K, V, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_BTREE_MAP_H__
|
@ -0,0 +1,145 @@
|
||||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// A btree_set<> implements the STL unique sorted associative container
|
||||
// interface (a.k.a set<>) using a btree. A btree_multiset<> implements the STL
|
||||
// multiple sorted associative container interface (a.k.a multiset<>) using a
|
||||
// btree. See btree.h for details of the btree implementation and caveats.
|
||||
|
||||
#ifndef UTIL_BTREE_BTREE_SET_H__
|
||||
#define UTIL_BTREE_BTREE_SET_H__
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
#include "btree.h"
|
||||
#include "btree_container.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The btree_set class is needed mainly for its constructors.
|
||||
template <typename Key,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<Key>,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_set : public btree_unique_container<
|
||||
btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_set<Key, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_unique_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_set(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_set(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Move constructor.
|
||||
btree_set(self_type &&x)
|
||||
: super_type() {
|
||||
this->swap(x);
|
||||
}
|
||||
|
||||
// Copy/move assignment
|
||||
self_type& operator=(self_type x) {
|
||||
this->swap(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_set(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename C, typename A, int N>
|
||||
inline void swap(btree_set<K, C, A, N> &x, btree_set<K, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
// The btree_multiset class is needed mainly for its constructors.
|
||||
template <typename Key,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<Key>,
|
||||
int TargetNodeSize = 256>
|
||||
class btree_multiset : public btree_multi_container<
|
||||
btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef btree_multiset<Key, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef btree<params_type> btree_type;
|
||||
typedef btree_multi_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
btree_multiset(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
btree_multiset(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Move constructor.
|
||||
btree_multiset(self_type &&x)
|
||||
: super_type() {
|
||||
this->swap(x);
|
||||
}
|
||||
|
||||
// Copy/move assignment
|
||||
self_type& operator=(self_type x) {
|
||||
this->swap(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
btree_multiset(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename C, typename A, int N>
|
||||
inline void swap(btree_multiset<K, C, A, N> &x,
|
||||
btree_multiset<K, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_BTREE_SET_H__
|
@ -0,0 +1,395 @@
|
||||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// A safe_btree<> wraps around a btree<> and removes the caveat that insertion
|
||||
// and deletion invalidate iterators. A safe_btree<> maintains a generation
|
||||
// number that is incremented on every mutation. A safe_btree<>::iterator keeps
|
||||
// a pointer to the safe_btree<> it came from, the generation of the tree when
|
||||
// it was last validated and the key the underlying btree<>::iterator points
|
||||
// to. If an iterator is accessed and its generation differs from the tree
|
||||
// generation it is revalidated.
|
||||
//
|
||||
// References and pointers returned by safe_btree iterators are not safe.
|
||||
//
|
||||
// See the incorrect usage examples mentioned in safe_btree_set.h and
|
||||
// safe_btree_map.h.
|
||||
|
||||
#ifndef UTIL_BTREE_SAFE_BTREE_H__
|
||||
#define UTIL_BTREE_SAFE_BTREE_H__
|
||||
|
||||
#include <stddef.h>
|
||||
#include <iosfwd>
|
||||
#include <utility>
|
||||
|
||||
#include "btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
template <typename Tree, typename Iterator>
|
||||
class safe_btree_iterator {
|
||||
public:
|
||||
typedef typename Iterator::key_type key_type;
|
||||
typedef typename Iterator::value_type value_type;
|
||||
typedef typename Iterator::size_type size_type;
|
||||
typedef typename Iterator::difference_type difference_type;
|
||||
typedef typename Iterator::pointer pointer;
|
||||
typedef typename Iterator::reference reference;
|
||||
typedef typename Iterator::const_pointer const_pointer;
|
||||
typedef typename Iterator::const_reference const_reference;
|
||||
typedef typename Iterator::iterator_category iterator_category;
|
||||
typedef typename Tree::iterator iterator;
|
||||
typedef typename Tree::const_iterator const_iterator;
|
||||
typedef safe_btree_iterator<Tree, Iterator> self_type;
|
||||
|
||||
void update() const {
|
||||
if (iter_ != tree_->internal_btree()->end()) {
|
||||
// A positive generation indicates a valid key.
|
||||
generation_ = tree_->generation();
|
||||
key_ = iter_.key();
|
||||
} else {
|
||||
// Use a negative generation to indicate iter_ points to end().
|
||||
generation_ = -tree_->generation();
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
safe_btree_iterator()
|
||||
: generation_(0),
|
||||
key_(),
|
||||
iter_(),
|
||||
tree_(NULL) {
|
||||
}
|
||||
safe_btree_iterator(const iterator &x)
|
||||
: generation_(x.generation()),
|
||||
key_(x.key()),
|
||||
iter_(x.iter()),
|
||||
tree_(x.tree()) {
|
||||
}
|
||||
safe_btree_iterator(Tree *tree, const Iterator &iter)
|
||||
: generation_(),
|
||||
key_(),
|
||||
iter_(iter),
|
||||
tree_(tree) {
|
||||
update();
|
||||
}
|
||||
|
||||
Tree* tree() const { return tree_; }
|
||||
int64_t generation() const { return generation_; }
|
||||
|
||||
Iterator* mutable_iter() const {
|
||||
if (generation_ != tree_->generation()) {
|
||||
if (generation_ > 0) {
|
||||
// This does the wrong thing for a multi{set,map}. If my iter was
|
||||
// pointing to the 2nd of 2 values with the same key, then this will
|
||||
// reset it to point to the first. This is why we don't provide a
|
||||
// safe_btree_multi{set,map}.
|
||||
iter_ = tree_->internal_btree()->lower_bound(key_);
|
||||
update();
|
||||
} else if (-generation_ != tree_->generation()) {
|
||||
iter_ = tree_->internal_btree()->end();
|
||||
generation_ = -tree_->generation();
|
||||
}
|
||||
}
|
||||
return &iter_;
|
||||
}
|
||||
const Iterator& iter() const {
|
||||
return *mutable_iter();
|
||||
}
|
||||
|
||||
// Equality/inequality operators.
|
||||
bool operator==(const const_iterator &x) const {
|
||||
return iter() == x.iter();
|
||||
}
|
||||
bool operator!=(const const_iterator &x) const {
|
||||
return iter() != x.iter();
|
||||
}
|
||||
|
||||
// Accessors for the key/value the iterator is pointing at.
|
||||
const key_type& key() const {
|
||||
return key_;
|
||||
}
|
||||
// This reference value is potentially invalidated by any non-const
|
||||
// method on the tree; it is NOT safe.
|
||||
reference operator*() const {
|
||||
assert(generation_ > 0);
|
||||
return iter().operator*();
|
||||
}
|
||||
// This pointer value is potentially invalidated by any non-const
|
||||
// method on the tree; it is NOT safe.
|
||||
pointer operator->() const {
|
||||
assert(generation_ > 0);
|
||||
return iter().operator->();
|
||||
}
|
||||
|
||||
// Increment/decrement operators.
|
||||
self_type& operator++() {
|
||||
++(*mutable_iter());
|
||||
update();
|
||||
return *this;
|
||||
}
|
||||
self_type& operator--() {
|
||||
--(*mutable_iter());
|
||||
update();
|
||||
return *this;
|
||||
}
|
||||
self_type operator++(int) {
|
||||
self_type tmp = *this;
|
||||
++*this;
|
||||
return tmp;
|
||||
}
|
||||
self_type operator--(int) {
|
||||
self_type tmp = *this;
|
||||
--*this;
|
||||
return tmp;
|
||||
}
|
||||
|
||||
private:
|
||||
// The generation of the tree when "iter" was updated.
|
||||
mutable int64_t generation_;
|
||||
// The key the iterator points to.
|
||||
mutable key_type key_;
|
||||
// The underlying iterator.
|
||||
mutable Iterator iter_;
|
||||
// The tree the iterator is associated with.
|
||||
Tree *tree_;
|
||||
};
|
||||
|
||||
template <typename Params>
|
||||
class safe_btree {
|
||||
typedef safe_btree<Params> self_type;
|
||||
|
||||
typedef btree<Params> btree_type;
|
||||
typedef typename btree_type::iterator tree_iterator;
|
||||
typedef typename btree_type::const_iterator tree_const_iterator;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::params_type params_type;
|
||||
typedef typename btree_type::key_type key_type;
|
||||
typedef typename btree_type::data_type data_type;
|
||||
typedef typename btree_type::mapped_type mapped_type;
|
||||
typedef typename btree_type::value_type value_type;
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
typedef typename btree_type::pointer pointer;
|
||||
typedef typename btree_type::const_pointer const_pointer;
|
||||
typedef typename btree_type::reference reference;
|
||||
typedef typename btree_type::const_reference const_reference;
|
||||
typedef typename btree_type::size_type size_type;
|
||||
typedef typename btree_type::difference_type difference_type;
|
||||
typedef safe_btree_iterator<self_type, tree_iterator> iterator;
|
||||
typedef safe_btree_iterator<
|
||||
const self_type, tree_const_iterator> const_iterator;
|
||||
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
|
||||
typedef std::reverse_iterator<iterator> reverse_iterator;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
safe_btree(const key_compare &comp, const allocator_type &alloc)
|
||||
: tree_(comp, alloc),
|
||||
generation_(1) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
safe_btree(const self_type &x)
|
||||
: tree_(x.tree_),
|
||||
generation_(1) {
|
||||
}
|
||||
|
||||
iterator begin() {
|
||||
return iterator(this, tree_.begin());
|
||||
}
|
||||
const_iterator begin() const {
|
||||
return const_iterator(this, tree_.begin());
|
||||
}
|
||||
iterator end() {
|
||||
return iterator(this, tree_.end());
|
||||
}
|
||||
const_iterator end() const {
|
||||
return const_iterator(this, tree_.end());
|
||||
}
|
||||
reverse_iterator rbegin() {
|
||||
return reverse_iterator(end());
|
||||
}
|
||||
const_reverse_iterator rbegin() const {
|
||||
return const_reverse_iterator(end());
|
||||
}
|
||||
reverse_iterator rend() {
|
||||
return reverse_iterator(begin());
|
||||
}
|
||||
const_reverse_iterator rend() const {
|
||||
return const_reverse_iterator(begin());
|
||||
}
|
||||
|
||||
// Lookup routines.
|
||||
iterator lower_bound(const key_type &key) {
|
||||
return iterator(this, tree_.lower_bound(key));
|
||||
}
|
||||
const_iterator lower_bound(const key_type &key) const {
|
||||
return const_iterator(this, tree_.lower_bound(key));
|
||||
}
|
||||
iterator upper_bound(const key_type &key) {
|
||||
return iterator(this, tree_.upper_bound(key));
|
||||
}
|
||||
const_iterator upper_bound(const key_type &key) const {
|
||||
return const_iterator(this, tree_.upper_bound(key));
|
||||
}
|
||||
std::pair<iterator, iterator> equal_range(const key_type &key) {
|
||||
std::pair<tree_iterator, tree_iterator> p = tree_.equal_range(key);
|
||||
return std::make_pair(iterator(this, p.first),
|
||||
iterator(this, p.second));
|
||||
}
|
||||
std::pair<const_iterator, const_iterator> equal_range(const key_type &key) const {
|
||||
std::pair<tree_const_iterator, tree_const_iterator> p = tree_.equal_range(key);
|
||||
return std::make_pair(const_iterator(this, p.first),
|
||||
const_iterator(this, p.second));
|
||||
}
|
||||
iterator find_unique(const key_type &key) {
|
||||
return iterator(this, tree_.find_unique(key));
|
||||
}
|
||||
const_iterator find_unique(const key_type &key) const {
|
||||
return const_iterator(this, tree_.find_unique(key));
|
||||
}
|
||||
iterator find_multi(const key_type &key) {
|
||||
return iterator(this, tree_.find_multi(key));
|
||||
}
|
||||
const_iterator find_multi(const key_type &key) const {
|
||||
return const_iterator(this, tree_.find_multi(key));
|
||||
}
|
||||
size_type count_unique(const key_type &key) const {
|
||||
return tree_.count_unique(key);
|
||||
}
|
||||
size_type count_multi(const key_type &key) const {
|
||||
return tree_.count_multi(key);
|
||||
}
|
||||
|
||||
// Insertion routines.
|
||||
template <typename ValuePointer>
|
||||
std::pair<iterator, bool> insert_unique(const key_type &key, ValuePointer value) {
|
||||
std::pair<tree_iterator, bool> p = tree_.insert_unique(key, value);
|
||||
generation_ += p.second;
|
||||
return std::make_pair(iterator(this, p.first), p.second);
|
||||
}
|
||||
std::pair<iterator, bool> insert_unique(const value_type &v) {
|
||||
std::pair<tree_iterator, bool> p = tree_.insert_unique(v);
|
||||
generation_ += p.second;
|
||||
return std::make_pair(iterator(this, p.first), p.second);
|
||||
}
|
||||
iterator insert_unique(iterator position, const value_type &v) {
|
||||
tree_iterator tree_pos = position.iter();
|
||||
++generation_;
|
||||
return iterator(this, tree_.insert_unique(tree_pos, v));
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert_unique(InputIterator b, InputIterator e) {
|
||||
for (; b != e; ++b) {
|
||||
insert_unique(*b);
|
||||
}
|
||||
}
|
||||
iterator insert_multi(const value_type &v) {
|
||||
++generation_;
|
||||
return iterator(this, tree_.insert_multi(v));
|
||||
}
|
||||
iterator insert_multi(iterator position, const value_type &v) {
|
||||
tree_iterator tree_pos = position.iter();
|
||||
++generation_;
|
||||
return iterator(this, tree_.insert_multi(tree_pos, v));
|
||||
}
|
||||
template <typename InputIterator>
|
||||
void insert_multi(InputIterator b, InputIterator e) {
|
||||
for (; b != e; ++b) {
|
||||
insert_multi(*b);
|
||||
}
|
||||
}
|
||||
self_type& operator=(const self_type &x) {
|
||||
if (&x == this) {
|
||||
// Don't copy onto ourselves.
|
||||
return *this;
|
||||
}
|
||||
++generation_;
|
||||
tree_ = x.tree_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Deletion routines.
|
||||
void erase(const iterator &begin, const iterator &end) {
|
||||
tree_.erase(begin.iter(), end.iter());
|
||||
++generation_;
|
||||
}
|
||||
// Erase the specified iterator from the btree. The iterator must be valid
|
||||
// (i.e. not equal to end()). Return an iterator pointing to the node after
|
||||
// the one that was erased (or end() if none exists).
|
||||
iterator erase(iterator iter) {
|
||||
tree_iterator res = tree_.erase(iter.iter());
|
||||
++generation_;
|
||||
return iterator(this, res);
|
||||
}
|
||||
int erase_unique(const key_type &key) {
|
||||
int res = tree_.erase_unique(key);
|
||||
generation_ += res;
|
||||
return res;
|
||||
}
|
||||
int erase_multi(const key_type &key) {
|
||||
int res = tree_.erase_multi(key);
|
||||
generation_ += res;
|
||||
return res;
|
||||
}
|
||||
|
||||
// Access to the underlying btree.
|
||||
btree_type* internal_btree() { return &tree_; }
|
||||
const btree_type* internal_btree() const { return &tree_; }
|
||||
|
||||
// Utility routines.
|
||||
void clear() {
|
||||
++generation_;
|
||||
tree_.clear();
|
||||
}
|
||||
void swap(self_type &x) {
|
||||
++generation_;
|
||||
++x.generation_;
|
||||
tree_.swap(x.tree_);
|
||||
}
|
||||
void dump(std::ostream &os) const {
|
||||
tree_.dump(os);
|
||||
}
|
||||
void verify() const {
|
||||
tree_.verify();
|
||||
}
|
||||
int64_t generation() const {
|
||||
return generation_;
|
||||
}
|
||||
key_compare key_comp() const { return tree_.key_comp(); }
|
||||
|
||||
// Size routines.
|
||||
size_type size() const { return tree_.size(); }
|
||||
size_type max_size() const { return tree_.max_size(); }
|
||||
bool empty() const { return tree_.empty(); }
|
||||
size_type height() const { return tree_.height(); }
|
||||
size_type internal_nodes() const { return tree_.internal_nodes(); }
|
||||
size_type leaf_nodes() const { return tree_.leaf_nodes(); }
|
||||
size_type nodes() const { return tree_.nodes(); }
|
||||
size_type bytes_used() const { return tree_.bytes_used(); }
|
||||
static double average_bytes_per_value() {
|
||||
return btree_type::average_bytes_per_value();
|
||||
}
|
||||
double fullness() const { return tree_.fullness(); }
|
||||
double overhead() const { return tree_.overhead(); }
|
||||
|
||||
private:
|
||||
btree_type tree_;
|
||||
int64_t generation_;
|
||||
};
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_SAFE_BTREE_H__
|
@ -0,0 +1,101 @@
|
||||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// The safe_btree_map<> is like btree_map<> except that it removes the caveat
|
||||
// about insertion and deletion invalidating existing iterators at a small cost
|
||||
// in making iterators larger and slower.
|
||||
//
|
||||
// Revalidation occurs whenever an iterator is accessed. References
|
||||
// and pointers returned by safe_btree_map<> iterators are not stable,
|
||||
// they are potentially invalidated by any non-const method on the map.
|
||||
//
|
||||
// BEGIN INCORRECT EXAMPLE
|
||||
// for (auto i = safe_map->begin(); i != safe_map->end(); ++i) {
|
||||
// const T *value = &i->second; // DO NOT DO THIS
|
||||
// [code that modifies safe_map and uses value];
|
||||
// }
|
||||
// END INCORRECT EXAMPLE
|
||||
#ifndef UTIL_BTREE_SAFE_BTREE_MAP_H__
|
||||
#define UTIL_BTREE_SAFE_BTREE_MAP_H__
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
#include "btree_container.h"
|
||||
#include "btree_map.h"
|
||||
#include "safe_btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The safe_btree_map class is needed mainly for its constructors.
|
||||
template <typename Key, typename Value,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<std::pair<const Key, Value> >,
|
||||
int TargetNodeSize = 256>
|
||||
class safe_btree_map : public btree_map_container<
|
||||
safe_btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef safe_btree_map<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_map_params<
|
||||
Key, Value, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef safe_btree<params_type> btree_type;
|
||||
typedef btree_map_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
safe_btree_map(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
safe_btree_map(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Move constructor.
|
||||
safe_btree_map(self_type &&x)
|
||||
: super_type() {
|
||||
this->swap(x);
|
||||
}
|
||||
|
||||
// Copy/move assignment
|
||||
self_type& operator=(self_type x) {
|
||||
this->swap(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
safe_btree_map(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename V, typename C, typename A, int N>
|
||||
inline void swap(safe_btree_map<K, V, C, A, N> &x,
|
||||
safe_btree_map<K, V, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_SAFE_BTREE_MAP_H__
|
@ -0,0 +1,100 @@
|
||||
// Copyright 2013 Google Inc. All Rights Reserved.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
// The safe_btree_set<> is like btree_set<> except that it removes the caveat
|
||||
// about insertion and deletion invalidating existing iterators at a small cost
|
||||
// in making iterators larger and slower.
|
||||
//
|
||||
// Revalidation occurs whenever an iterator is accessed. References
|
||||
// and pointers returned by safe_btree_map<> iterators are not stable,
|
||||
// they are potentially invalidated by any non-const method on the set.
|
||||
//
|
||||
// BEGIN INCORRECT EXAMPLE
|
||||
// for (auto i = safe_set->begin(); i != safe_set->end(); ++i) {
|
||||
// const T &value = *i; // DO NOT DO THIS
|
||||
// [code that modifies safe_set and uses value];
|
||||
// }
|
||||
// END INCORRECT EXAMPLE
|
||||
|
||||
#ifndef UTIL_BTREE_SAFE_BTREE_SET_H__
|
||||
#define UTIL_BTREE_SAFE_BTREE_SET_H__
|
||||
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
|
||||
#include "btree_container.h"
|
||||
#include "btree_set.h"
|
||||
#include "safe_btree.h"
|
||||
|
||||
namespace btree {
|
||||
|
||||
// The safe_btree_set class is needed mainly for its constructors.
|
||||
template <typename Key,
|
||||
typename Compare = std::less<Key>,
|
||||
typename Alloc = std::allocator<Key>,
|
||||
int TargetNodeSize = 256>
|
||||
class safe_btree_set : public btree_unique_container<
|
||||
safe_btree<btree_set_params<Key, Compare, Alloc, TargetNodeSize> > > {
|
||||
|
||||
typedef safe_btree_set<Key, Compare, Alloc, TargetNodeSize> self_type;
|
||||
typedef btree_set_params<Key, Compare, Alloc, TargetNodeSize> params_type;
|
||||
typedef safe_btree<params_type> btree_type;
|
||||
typedef btree_unique_container<btree_type> super_type;
|
||||
|
||||
public:
|
||||
typedef typename btree_type::key_compare key_compare;
|
||||
typedef typename btree_type::allocator_type allocator_type;
|
||||
|
||||
public:
|
||||
// Default constructor.
|
||||
safe_btree_set(const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(comp, alloc) {
|
||||
}
|
||||
|
||||
// Copy constructor.
|
||||
safe_btree_set(const self_type &x)
|
||||
: super_type(x) {
|
||||
}
|
||||
|
||||
// Move constructor.
|
||||
safe_btree_set(self_type &&x)
|
||||
: super_type() {
|
||||
this->swap(x);
|
||||
}
|
||||
|
||||
// Copy/move assignment
|
||||
self_type& operator=(self_type x) {
|
||||
this->swap(x);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Range constructor.
|
||||
template <class InputIterator>
|
||||
safe_btree_set(InputIterator b, InputIterator e,
|
||||
const key_compare &comp = key_compare(),
|
||||
const allocator_type &alloc = allocator_type())
|
||||
: super_type(b, e, comp, alloc) {
|
||||
}
|
||||
};
|
||||
|
||||
template <typename K, typename C, typename A, int N>
|
||||
inline void swap(safe_btree_set<K, C, A, N> &x,
|
||||
safe_btree_set<K, C, A, N> &y) {
|
||||
x.swap(y);
|
||||
}
|
||||
|
||||
} // namespace btree
|
||||
|
||||
#endif // UTIL_BTREE_SAFE_BTREE_SET_H__
|
Loading…
Reference in New Issue