erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

349 line
11 KiB

5 年之前
  1. // Copyright 2013 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef UTIL_BTREE_BTREE_CONTAINER_H__
  15. #define UTIL_BTREE_BTREE_CONTAINER_H__
  16. #include <iosfwd>
  17. #include <utility>
  18. #include "btree.h"
  19. namespace btree {
  20. // A common base class for btree_set, btree_map, btree_multiset and
  21. // btree_multimap.
  22. template <typename Tree>
  23. class btree_container {
  24. typedef btree_container<Tree> self_type;
  25. public:
  26. typedef typename Tree::params_type params_type;
  27. typedef typename Tree::key_type key_type;
  28. typedef typename Tree::value_type value_type;
  29. typedef typename Tree::key_compare key_compare;
  30. typedef typename Tree::allocator_type allocator_type;
  31. typedef typename Tree::pointer pointer;
  32. typedef typename Tree::const_pointer const_pointer;
  33. typedef typename Tree::reference reference;
  34. typedef typename Tree::const_reference const_reference;
  35. typedef typename Tree::size_type size_type;
  36. typedef typename Tree::difference_type difference_type;
  37. typedef typename Tree::iterator iterator;
  38. typedef typename Tree::const_iterator const_iterator;
  39. typedef typename Tree::reverse_iterator reverse_iterator;
  40. typedef typename Tree::const_reverse_iterator const_reverse_iterator;
  41. public:
  42. // Default constructor.
  43. btree_container(const key_compare &comp, const allocator_type &alloc)
  44. : tree_(comp, alloc) {
  45. }
  46. // Copy constructor.
  47. btree_container(const self_type &x)
  48. : tree_(x.tree_) {
  49. }
  50. // Iterator routines.
  51. iterator begin() { return tree_.begin(); }
  52. const_iterator begin() const { return tree_.begin(); }
  53. iterator end() { return tree_.end(); }
  54. const_iterator end() const { return tree_.end(); }
  55. reverse_iterator rbegin() { return tree_.rbegin(); }
  56. const_reverse_iterator rbegin() const { return tree_.rbegin(); }
  57. reverse_iterator rend() { return tree_.rend(); }
  58. const_reverse_iterator rend() const { return tree_.rend(); }
  59. // Lookup routines.
  60. iterator lower_bound(const key_type &key) {
  61. return tree_.lower_bound(key);
  62. }
  63. const_iterator lower_bound(const key_type &key) const {
  64. return tree_.lower_bound(key);
  65. }
  66. iterator upper_bound(const key_type &key) {
  67. return tree_.upper_bound(key);
  68. }
  69. const_iterator upper_bound(const key_type &key) const {
  70. return tree_.upper_bound(key);
  71. }
  72. std::pair<iterator,iterator> equal_range(const key_type &key) {
  73. return tree_.equal_range(key);
  74. }
  75. std::pair<const_iterator,const_iterator> equal_range(const key_type &key) const {
  76. return tree_.equal_range(key);
  77. }
  78. // Utility routines.
  79. void clear() {
  80. tree_.clear();
  81. }
  82. void swap(self_type &x) {
  83. tree_.swap(x.tree_);
  84. }
  85. void dump(std::ostream &os) const {
  86. tree_.dump(os);
  87. }
  88. void verify() const {
  89. tree_.verify();
  90. }
  91. // Size routines.
  92. size_type size() const { return tree_.size(); }
  93. size_type max_size() const { return tree_.max_size(); }
  94. bool empty() const { return tree_.empty(); }
  95. size_type height() const { return tree_.height(); }
  96. size_type internal_nodes() const { return tree_.internal_nodes(); }
  97. size_type leaf_nodes() const { return tree_.leaf_nodes(); }
  98. size_type nodes() const { return tree_.nodes(); }
  99. size_type bytes_used() const { return tree_.bytes_used(); }
  100. static double average_bytes_per_value() {
  101. return Tree::average_bytes_per_value();
  102. }
  103. double fullness() const { return tree_.fullness(); }
  104. double overhead() const { return tree_.overhead(); }
  105. bool operator==(const self_type& x) const {
  106. if (size() != x.size()) {
  107. return false;
  108. }
  109. for (const_iterator i = begin(), xi = x.begin(); i != end(); ++i, ++xi) {
  110. if (*i != *xi) {
  111. return false;
  112. }
  113. }
  114. return true;
  115. }
  116. bool operator!=(const self_type& other) const {
  117. return !operator==(other);
  118. }
  119. protected:
  120. Tree tree_;
  121. };
  122. template <typename T>
  123. inline std::ostream& operator<<(std::ostream &os, const btree_container<T> &b) {
  124. b.dump(os);
  125. return os;
  126. }
  127. // A common base class for btree_set and safe_btree_set.
  128. template <typename Tree>
  129. class btree_unique_container : public btree_container<Tree> {
  130. typedef btree_unique_container<Tree> self_type;
  131. typedef btree_container<Tree> super_type;
  132. public:
  133. typedef typename Tree::key_type key_type;
  134. typedef typename Tree::value_type value_type;
  135. typedef typename Tree::size_type size_type;
  136. typedef typename Tree::key_compare key_compare;
  137. typedef typename Tree::allocator_type allocator_type;
  138. typedef typename Tree::iterator iterator;
  139. typedef typename Tree::const_iterator const_iterator;
  140. public:
  141. // Default constructor.
  142. btree_unique_container(const key_compare &comp = key_compare(),
  143. const allocator_type &alloc = allocator_type())
  144. : super_type(comp, alloc) {
  145. }
  146. // Copy constructor.
  147. btree_unique_container(const self_type &x)
  148. : super_type(x) {
  149. }
  150. // Range constructor.
  151. template <class InputIterator>
  152. btree_unique_container(InputIterator b, InputIterator e,
  153. const key_compare &comp = key_compare(),
  154. const allocator_type &alloc = allocator_type())
  155. : super_type(comp, alloc) {
  156. insert(b, e);
  157. }
  158. // Lookup routines.
  159. iterator find(const key_type &key) {
  160. return this->tree_.find_unique(key);
  161. }
  162. const_iterator find(const key_type &key) const {
  163. return this->tree_.find_unique(key);
  164. }
  165. size_type count(const key_type &key) const {
  166. return this->tree_.count_unique(key);
  167. }
  168. // Insertion routines.
  169. std::pair<iterator,bool> insert(const value_type &x) {
  170. return this->tree_.insert_unique(x);
  171. }
  172. iterator insert(iterator position, const value_type &x) {
  173. return this->tree_.insert_unique(position, x);
  174. }
  175. template <typename InputIterator>
  176. void insert(InputIterator b, InputIterator e) {
  177. this->tree_.insert_unique(b, e);
  178. }
  179. // Deletion routines.
  180. int erase(const key_type &key) {
  181. return this->tree_.erase_unique(key);
  182. }
  183. // Erase the specified iterator from the btree. The iterator must be valid
  184. // (i.e. not equal to end()). Return an iterator pointing to the node after
  185. // the one that was erased (or end() if none exists).
  186. iterator erase(const iterator &iter) {
  187. return this->tree_.erase(iter);
  188. }
  189. void erase(const iterator &first, const iterator &last) {
  190. this->tree_.erase(first, last);
  191. }
  192. };
  193. // A common base class for btree_map and safe_btree_map.
  194. template <typename Tree>
  195. class btree_map_container : public btree_unique_container<Tree> {
  196. typedef btree_map_container<Tree> self_type;
  197. typedef btree_unique_container<Tree> super_type;
  198. public:
  199. typedef typename Tree::key_type key_type;
  200. typedef typename Tree::data_type data_type;
  201. typedef typename Tree::value_type value_type;
  202. typedef typename Tree::mapped_type mapped_type;
  203. typedef typename Tree::key_compare key_compare;
  204. typedef typename Tree::allocator_type allocator_type;
  205. private:
  206. // A pointer-like object which only generates its value when
  207. // dereferenced. Used by operator[] to avoid constructing an empty data_type
  208. // if the key already exists in the map.
  209. struct generate_value {
  210. generate_value(const key_type &k)
  211. : key(k) {
  212. }
  213. value_type operator*() const {
  214. return std::make_pair(key, data_type());
  215. }
  216. const key_type &key;
  217. };
  218. public:
  219. // Default constructor.
  220. btree_map_container(const key_compare &comp = key_compare(),
  221. const allocator_type &alloc = allocator_type())
  222. : super_type(comp, alloc) {
  223. }
  224. // Copy constructor.
  225. btree_map_container(const self_type &x)
  226. : super_type(x) {
  227. }
  228. // Range constructor.
  229. template <class InputIterator>
  230. btree_map_container(InputIterator b, InputIterator e,
  231. const key_compare &comp = key_compare(),
  232. const allocator_type &alloc = allocator_type())
  233. : super_type(b, e, comp, alloc) {
  234. }
  235. // Insertion routines.
  236. data_type& operator[](const key_type &key) {
  237. return this->tree_.insert_unique(key, generate_value(key)).first->second;
  238. }
  239. };
  240. // A common base class for btree_multiset and btree_multimap.
  241. template <typename Tree>
  242. class btree_multi_container : public btree_container<Tree> {
  243. typedef btree_multi_container<Tree> self_type;
  244. typedef btree_container<Tree> super_type;
  245. public:
  246. typedef typename Tree::key_type key_type;
  247. typedef typename Tree::value_type value_type;
  248. typedef typename Tree::size_type size_type;
  249. typedef typename Tree::key_compare key_compare;
  250. typedef typename Tree::allocator_type allocator_type;
  251. typedef typename Tree::iterator iterator;
  252. typedef typename Tree::const_iterator const_iterator;
  253. public:
  254. // Default constructor.
  255. btree_multi_container(const key_compare &comp = key_compare(),
  256. const allocator_type &alloc = allocator_type())
  257. : super_type(comp, alloc) {
  258. }
  259. // Copy constructor.
  260. btree_multi_container(const self_type &x)
  261. : super_type(x) {
  262. }
  263. // Range constructor.
  264. template <class InputIterator>
  265. btree_multi_container(InputIterator b, InputIterator e,
  266. const key_compare &comp = key_compare(),
  267. const allocator_type &alloc = allocator_type())
  268. : super_type(comp, alloc) {
  269. insert(b, e);
  270. }
  271. // Lookup routines.
  272. iterator find(const key_type &key) {
  273. return this->tree_.find_multi(key);
  274. }
  275. const_iterator find(const key_type &key) const {
  276. return this->tree_.find_multi(key);
  277. }
  278. size_type count(const key_type &key) const {
  279. return this->tree_.count_multi(key);
  280. }
  281. // Insertion routines.
  282. iterator insert(const value_type &x) {
  283. return this->tree_.insert_multi(x);
  284. }
  285. iterator insert(iterator position, const value_type &x) {
  286. return this->tree_.insert_multi(position, x);
  287. }
  288. template <typename InputIterator>
  289. void insert(InputIterator b, InputIterator e) {
  290. this->tree_.insert_multi(b, e);
  291. }
  292. // Deletion routines.
  293. int erase(const key_type &key) {
  294. return this->tree_.erase_multi(key);
  295. }
  296. // Erase the specified iterator from the btree. The iterator must be valid
  297. // (i.e. not equal to end()). Return an iterator pointing to the node after
  298. // the one that was erased (or end() if none exists).
  299. iterator erase(const iterator &iter) {
  300. return this->tree_.erase(iter);
  301. }
  302. void erase(const iterator &first, const iterator &last) {
  303. this->tree_.erase(first, last);
  304. }
  305. };
  306. } // namespace btree
  307. #endif // UTIL_BTREE_BTREE_CONTAINER_H__