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.

130 line
4.3 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. //
  15. // A btree_map<> implements the STL unique sorted associative container
  16. // interface and the pair associative container interface (a.k.a map<>) using a
  17. // btree. A btree_multimap<> implements the STL multiple sorted associative
  18. // container interface and the pair associtive container interface (a.k.a
  19. // multimap<>) using a btree. See btree.h for details of the btree
  20. // implementation and caveats.
  21. #ifndef UTIL_BTREE_BTREE_MAP_H__
  22. #define UTIL_BTREE_BTREE_MAP_H__
  23. #include <algorithm>
  24. #include <functional>
  25. #include <memory>
  26. #include <string>
  27. #include <utility>
  28. #include "btree.h"
  29. #include "btree_container.h"
  30. namespace btree {
  31. // The btree_map class is needed mainly for its constructors.
  32. template <typename Key, typename Value,
  33. typename Compare = std::less<Key>,
  34. typename Alloc = std::allocator<std::pair<const Key, Value> >,
  35. int TargetNodeSize = 256>
  36. class btree_map : public btree_map_container<
  37. btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
  38. typedef btree_map<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
  39. typedef btree_map_params<
  40. Key, Value, Compare, Alloc, TargetNodeSize> params_type;
  41. typedef btree<params_type> btree_type;
  42. typedef btree_map_container<btree_type> super_type;
  43. public:
  44. typedef typename btree_type::key_compare key_compare;
  45. typedef typename btree_type::allocator_type allocator_type;
  46. public:
  47. // Default constructor.
  48. btree_map(const key_compare &comp = key_compare(),
  49. const allocator_type &alloc = allocator_type())
  50. : super_type(comp, alloc) {
  51. }
  52. // Copy constructor.
  53. btree_map(const self_type &x)
  54. : super_type(x) {
  55. }
  56. // Range constructor.
  57. template <class InputIterator>
  58. btree_map(InputIterator b, InputIterator e,
  59. const key_compare &comp = key_compare(),
  60. const allocator_type &alloc = allocator_type())
  61. : super_type(b, e, comp, alloc) {
  62. }
  63. };
  64. template <typename K, typename V, typename C, typename A, int N>
  65. inline void swap(btree_map<K, V, C, A, N> &x,
  66. btree_map<K, V, C, A, N> &y) {
  67. x.swap(y);
  68. }
  69. // The btree_multimap class is needed mainly for its constructors.
  70. template <typename Key, typename Value,
  71. typename Compare = std::less<Key>,
  72. typename Alloc = std::allocator<std::pair<const Key, Value> >,
  73. int TargetNodeSize = 256>
  74. class btree_multimap : public btree_multi_container<
  75. btree<btree_map_params<Key, Value, Compare, Alloc, TargetNodeSize> > > {
  76. typedef btree_multimap<Key, Value, Compare, Alloc, TargetNodeSize> self_type;
  77. typedef btree_map_params<
  78. Key, Value, Compare, Alloc, TargetNodeSize> params_type;
  79. typedef btree<params_type> btree_type;
  80. typedef btree_multi_container<btree_type> super_type;
  81. public:
  82. typedef typename btree_type::key_compare key_compare;
  83. typedef typename btree_type::allocator_type allocator_type;
  84. typedef typename btree_type::data_type data_type;
  85. typedef typename btree_type::mapped_type mapped_type;
  86. public:
  87. // Default constructor.
  88. btree_multimap(const key_compare &comp = key_compare(),
  89. const allocator_type &alloc = allocator_type())
  90. : super_type(comp, alloc) {
  91. }
  92. // Copy constructor.
  93. btree_multimap(const self_type &x)
  94. : super_type(x) {
  95. }
  96. // Range constructor.
  97. template <class InputIterator>
  98. btree_multimap(InputIterator b, InputIterator e,
  99. const key_compare &comp = key_compare(),
  100. const allocator_type &alloc = allocator_type())
  101. : super_type(b, e, comp, alloc) {
  102. }
  103. };
  104. template <typename K, typename V, typename C, typename A, int N>
  105. inline void swap(btree_multimap<K, V, C, A, N> &x,
  106. btree_multimap<K, V, C, A, N> &y) {
  107. x.swap(y);
  108. }
  109. } // namespace btree
  110. #endif // UTIL_BTREE_BTREE_MAP_H__