@sentryware/s2-node 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.circleci/config.yml +45 -0
- package/.dockerignore +1 -0
- package/.gitmodules +3 -0
- package/CHANGELOG.md +33 -0
- package/LICENSE +201 -0
- package/README.md +147 -0
- package/binding.gyp +170 -0
- package/docker/Dockerfile.node20.test +8 -0
- package/docker/Dockerfile.node22.test +8 -0
- package/docker/Dockerfile.node24.test +8 -0
- package/index.d.ts +117 -0
- package/index.js +6 -0
- package/jest.config.js +184 -0
- package/package.json +43 -0
- package/publish-linux.sh +18 -0
- package/publish-osx.sh +19 -0
- package/src/builder.cc +84 -0
- package/src/builder.h +29 -0
- package/src/cell.cc +71 -0
- package/src/cell.h +26 -0
- package/src/cell_id.cc +210 -0
- package/src/cell_id.h +44 -0
- package/src/cell_union.cc +237 -0
- package/src/cell_union.h +34 -0
- package/src/earth.cc +185 -0
- package/src/earth.h +33 -0
- package/src/latlng.cc +132 -0
- package/src/latlng.h +28 -0
- package/src/loop.cc +51 -0
- package/src/loop.h +21 -0
- package/src/point.cc +69 -0
- package/src/point.h +23 -0
- package/src/polygon.cc +36 -0
- package/src/polygon.h +20 -0
- package/src/polyline.cc +186 -0
- package/src/polyline.h +34 -0
- package/src/region_coverer.cc +450 -0
- package/src/region_coverer.h +56 -0
- package/src/s2.cc +27 -0
- package/test/Cell.test.js +37 -0
- package/test/CellId.test.js +135 -0
- package/test/CellUnion.test.js +150 -0
- package/test/Earth.test.js +62 -0
- package/test/LatLng.test.js +45 -0
- package/test/Point.test.js +14 -0
- package/test/Polyline.test.js +78 -0
- package/test/RegionCoverer.test.js +301 -0
- package/test.sh +16 -0
- package/third_party/s2geometry/.travis.yml +163 -0
- package/third_party/s2geometry/AUTHORS +13 -0
- package/third_party/s2geometry/CONTRIBUTING.md +65 -0
- package/third_party/s2geometry/CONTRIBUTORS +30 -0
- package/third_party/s2geometry/LICENSE +202 -0
- package/third_party/s2geometry/NOTICE +5 -0
- package/third_party/s2geometry/README.md +127 -0
- package/third_party/s2geometry/doc/examples/point_index.cc +44 -0
- package/third_party/s2geometry/doc/examples/term_index.cc +99 -0
- package/third_party/s2geometry/doc/examples/term_index.py +101 -0
- package/third_party/s2geometry/src/python/coder.i +125 -0
- package/third_party/s2geometry/src/python/pywraps2_test.py +786 -0
- package/third_party/s2geometry/src/python/s2.i +37 -0
- package/third_party/s2geometry/src/python/s2_common.i +756 -0
- package/third_party/s2geometry/src/s2/_fp_contract_off.h +60 -0
- package/third_party/s2geometry/src/s2/base/casts.h +318 -0
- package/third_party/s2geometry/src/s2/base/commandlineflags.h +67 -0
- package/third_party/s2geometry/src/s2/base/integral_types.h +31 -0
- package/third_party/s2geometry/src/s2/base/log_severity.h +40 -0
- package/third_party/s2geometry/src/s2/base/logging.h +173 -0
- package/third_party/s2geometry/src/s2/base/mutex.h +61 -0
- package/third_party/s2geometry/src/s2/base/port.h +999 -0
- package/third_party/s2geometry/src/s2/base/spinlock.h +60 -0
- package/third_party/s2geometry/src/s2/base/stringprintf.cc +107 -0
- package/third_party/s2geometry/src/s2/base/stringprintf.h +53 -0
- package/third_party/s2geometry/src/s2/base/strtoint.cc +65 -0
- package/third_party/s2geometry/src/s2/base/strtoint.h +106 -0
- package/third_party/s2geometry/src/s2/base/timer.h +50 -0
- package/third_party/s2geometry/src/s2/encoded_s2cell_id_vector.cc +164 -0
- package/third_party/s2geometry/src/s2/encoded_s2cell_id_vector.h +110 -0
- package/third_party/s2geometry/src/s2/encoded_s2cell_id_vector_test.cc +232 -0
- package/third_party/s2geometry/src/s2/encoded_s2point_vector.cc +838 -0
- package/third_party/s2geometry/src/s2/encoded_s2point_vector.h +140 -0
- package/third_party/s2geometry/src/s2/encoded_s2point_vector_test.cc +344 -0
- package/third_party/s2geometry/src/s2/encoded_s2shape_index.cc +181 -0
- package/third_party/s2geometry/src/s2/encoded_s2shape_index.h +276 -0
- package/third_party/s2geometry/src/s2/encoded_s2shape_index_test.cc +244 -0
- package/third_party/s2geometry/src/s2/encoded_string_vector.cc +66 -0
- package/third_party/s2geometry/src/s2/encoded_string_vector.h +164 -0
- package/third_party/s2geometry/src/s2/encoded_string_vector_test.cc +69 -0
- package/third_party/s2geometry/src/s2/encoded_uint_vector.h +299 -0
- package/third_party/s2geometry/src/s2/encoded_uint_vector_test.cc +124 -0
- package/third_party/s2geometry/src/s2/id_set_lexicon.cc +81 -0
- package/third_party/s2geometry/src/s2/id_set_lexicon.h +199 -0
- package/third_party/s2geometry/src/s2/id_set_lexicon_test.cc +70 -0
- package/third_party/s2geometry/src/s2/mutable_s2shape_index.cc +1585 -0
- package/third_party/s2geometry/src/s2/mutable_s2shape_index.h +600 -0
- package/third_party/s2geometry/src/s2/mutable_s2shape_index_test.cc +589 -0
- package/third_party/s2geometry/src/s2/r1interval.h +220 -0
- package/third_party/s2geometry/src/s2/r1interval_test.cc +185 -0
- package/third_party/s2geometry/src/s2/r2.h +26 -0
- package/third_party/s2geometry/src/s2/r2rect.cc +93 -0
- package/third_party/s2geometry/src/s2/r2rect.h +234 -0
- package/third_party/s2geometry/src/s2/r2rect_test.cc +228 -0
- package/third_party/s2geometry/src/s2/s1angle.cc +54 -0
- package/third_party/s2geometry/src/s2/s1angle.h +336 -0
- package/third_party/s2geometry/src/s2/s1angle_test.cc +185 -0
- package/third_party/s2geometry/src/s2/s1chord_angle.cc +159 -0
- package/third_party/s2geometry/src/s2/s1chord_angle.h +369 -0
- package/third_party/s2geometry/src/s2/s1chord_angle_test.cc +207 -0
- package/third_party/s2geometry/src/s2/s1interval.cc +296 -0
- package/third_party/s2geometry/src/s2/s1interval.h +266 -0
- package/third_party/s2geometry/src/s2/s1interval_test.cc +469 -0
- package/third_party/s2geometry/src/s2/s2boolean_operation.cc +2391 -0
- package/third_party/s2geometry/src/s2/s2boolean_operation.h +501 -0
- package/third_party/s2geometry/src/s2/s2boolean_operation_test.cc +1400 -0
- package/third_party/s2geometry/src/s2/s2builder.cc +1828 -0
- package/third_party/s2geometry/src/s2/s2builder.h +1057 -0
- package/third_party/s2geometry/src/s2/s2builder_graph.cc +1084 -0
- package/third_party/s2geometry/src/s2/s2builder_graph.h +799 -0
- package/third_party/s2geometry/src/s2/s2builder_graph_test.cc +462 -0
- package/third_party/s2geometry/src/s2/s2builder_layer.h +50 -0
- package/third_party/s2geometry/src/s2/s2builder_test.cc +1329 -0
- package/third_party/s2geometry/src/s2/s2builderutil_closed_set_normalizer.cc +313 -0
- package/third_party/s2geometry/src/s2/s2builderutil_closed_set_normalizer.h +221 -0
- package/third_party/s2geometry/src/s2/s2builderutil_closed_set_normalizer_test.cc +261 -0
- package/third_party/s2geometry/src/s2/s2builderutil_find_polygon_degeneracies.cc +392 -0
- package/third_party/s2geometry/src/s2/s2builderutil_find_polygon_degeneracies.h +86 -0
- package/third_party/s2geometry/src/s2/s2builderutil_find_polygon_degeneracies_test.cc +182 -0
- package/third_party/s2geometry/src/s2/s2builderutil_graph_shape.h +57 -0
- package/third_party/s2geometry/src/s2/s2builderutil_lax_polygon_layer.cc +212 -0
- package/third_party/s2geometry/src/s2/s2builderutil_lax_polygon_layer.h +218 -0
- package/third_party/s2geometry/src/s2/s2builderutil_lax_polygon_layer_test.cc +367 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2point_vector_layer.cc +74 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2point_vector_layer.h +122 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2point_vector_layer_test.cc +167 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polygon_layer.cc +191 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polygon_layer.h +211 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polygon_layer_test.cc +312 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_layer.cc +105 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_layer.h +174 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_layer_test.cc +220 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_vector_layer.cc +98 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_vector_layer.h +292 -0
- package/third_party/s2geometry/src/s2/s2builderutil_s2polyline_vector_layer_test.cc +233 -0
- package/third_party/s2geometry/src/s2/s2builderutil_snap_functions.cc +354 -0
- package/third_party/s2geometry/src/s2/s2builderutil_snap_functions.h +239 -0
- package/third_party/s2geometry/src/s2/s2builderutil_snap_functions_test.cc +716 -0
- package/third_party/s2geometry/src/s2/s2builderutil_testing.cc +37 -0
- package/third_party/s2geometry/src/s2/s2builderutil_testing.h +100 -0
- package/third_party/s2geometry/src/s2/s2builderutil_testing_test.cc +85 -0
- package/third_party/s2geometry/src/s2/s2cap.cc +347 -0
- package/third_party/s2geometry/src/s2/s2cap.h +286 -0
- package/third_party/s2geometry/src/s2/s2cap_test.cc +379 -0
- package/third_party/s2geometry/src/s2/s2cell.cc +552 -0
- package/third_party/s2geometry/src/s2/s2cell.h +249 -0
- package/third_party/s2geometry/src/s2/s2cell_id.cc +619 -0
- package/third_party/s2geometry/src/s2/s2cell_id.h +705 -0
- package/third_party/s2geometry/src/s2/s2cell_id_test.cc +633 -0
- package/third_party/s2geometry/src/s2/s2cell_index.cc +149 -0
- package/third_party/s2geometry/src/s2/s2cell_index.h +660 -0
- package/third_party/s2geometry/src/s2/s2cell_index_test.cc +411 -0
- package/third_party/s2geometry/src/s2/s2cell_test.cc +687 -0
- package/third_party/s2geometry/src/s2/s2cell_union.cc +515 -0
- package/third_party/s2geometry/src/s2/s2cell_union.h +399 -0
- package/third_party/s2geometry/src/s2/s2cell_union_test.cc +598 -0
- package/third_party/s2geometry/src/s2/s2centroids.cc +84 -0
- package/third_party/s2geometry/src/s2/s2centroids.h +87 -0
- package/third_party/s2geometry/src/s2/s2centroids_test.cc +82 -0
- package/third_party/s2geometry/src/s2/s2closest_cell_query.cc +123 -0
- package/third_party/s2geometry/src/s2/s2closest_cell_query.h +385 -0
- package/third_party/s2geometry/src/s2/s2closest_cell_query_base.h +841 -0
- package/third_party/s2geometry/src/s2/s2closest_cell_query_base_test.cc +63 -0
- package/third_party/s2geometry/src/s2/s2closest_cell_query_test.cc +412 -0
- package/third_party/s2geometry/src/s2/s2closest_edge_query.cc +106 -0
- package/third_party/s2geometry/src/s2/s2closest_edge_query.h +421 -0
- package/third_party/s2geometry/src/s2/s2closest_edge_query_base.h +946 -0
- package/third_party/s2geometry/src/s2/s2closest_edge_query_base_test.cc +59 -0
- package/third_party/s2geometry/src/s2/s2closest_edge_query_test.cc +505 -0
- package/third_party/s2geometry/src/s2/s2closest_edge_query_testing.h +91 -0
- package/third_party/s2geometry/src/s2/s2closest_point_query.cc +66 -0
- package/third_party/s2geometry/src/s2/s2closest_point_query.h +465 -0
- package/third_party/s2geometry/src/s2/s2closest_point_query_base.h +767 -0
- package/third_party/s2geometry/src/s2/s2closest_point_query_base_test.cc +63 -0
- package/third_party/s2geometry/src/s2/s2closest_point_query_test.cc +312 -0
- package/third_party/s2geometry/src/s2/s2contains_point_query.h +328 -0
- package/third_party/s2geometry/src/s2/s2contains_point_query_test.cc +159 -0
- package/third_party/s2geometry/src/s2/s2contains_vertex_query.cc +39 -0
- package/third_party/s2geometry/src/s2/s2contains_vertex_query.h +66 -0
- package/third_party/s2geometry/src/s2/s2contains_vertex_query_test.cc +67 -0
- package/third_party/s2geometry/src/s2/s2convex_hull_query.cc +198 -0
- package/third_party/s2geometry/src/s2/s2convex_hull_query.h +110 -0
- package/third_party/s2geometry/src/s2/s2convex_hull_query_test.cc +208 -0
- package/third_party/s2geometry/src/s2/s2coords.cc +146 -0
- package/third_party/s2geometry/src/s2/s2coords.h +459 -0
- package/third_party/s2geometry/src/s2/s2coords_internal.h +71 -0
- package/third_party/s2geometry/src/s2/s2coords_test.cc +218 -0
- package/third_party/s2geometry/src/s2/s2crossing_edge_query.cc +380 -0
- package/third_party/s2geometry/src/s2/s2crossing_edge_query.h +220 -0
- package/third_party/s2geometry/src/s2/s2crossing_edge_query_test.cc +382 -0
- package/third_party/s2geometry/src/s2/s2debug.cc +23 -0
- package/third_party/s2geometry/src/s2/s2debug.h +69 -0
- package/third_party/s2geometry/src/s2/s2distance_target.h +165 -0
- package/third_party/s2geometry/src/s2/s2earth.cc +52 -0
- package/third_party/s2geometry/src/s2/s2earth.h +268 -0
- package/third_party/s2geometry/src/s2/s2earth_test.cc +146 -0
- package/third_party/s2geometry/src/s2/s2edge_clipping.cc +462 -0
- package/third_party/s2geometry/src/s2/s2edge_clipping.h +183 -0
- package/third_party/s2geometry/src/s2/s2edge_clipping_test.cc +335 -0
- package/third_party/s2geometry/src/s2/s2edge_crosser.cc +85 -0
- package/third_party/s2geometry/src/s2/s2edge_crosser.h +343 -0
- package/third_party/s2geometry/src/s2/s2edge_crosser_test.cc +264 -0
- package/third_party/s2geometry/src/s2/s2edge_crossings.cc +515 -0
- package/third_party/s2geometry/src/s2/s2edge_crossings.h +138 -0
- package/third_party/s2geometry/src/s2/s2edge_crossings_internal.h +59 -0
- package/third_party/s2geometry/src/s2/s2edge_crossings_test.cc +246 -0
- package/third_party/s2geometry/src/s2/s2edge_distances.cc +419 -0
- package/third_party/s2geometry/src/s2/s2edge_distances.h +192 -0
- package/third_party/s2geometry/src/s2/s2edge_distances_test.cc +539 -0
- package/third_party/s2geometry/src/s2/s2edge_tessellator.cc +276 -0
- package/third_party/s2geometry/src/s2/s2edge_tessellator.h +101 -0
- package/third_party/s2geometry/src/s2/s2edge_tessellator_test.cc +492 -0
- package/third_party/s2geometry/src/s2/s2edge_vector_shape.h +85 -0
- package/third_party/s2geometry/src/s2/s2edge_vector_shape_test.cc +66 -0
- package/third_party/s2geometry/src/s2/s2error.cc +29 -0
- package/third_party/s2geometry/src/s2/s2error.h +147 -0
- package/third_party/s2geometry/src/s2/s2error_test.cc +31 -0
- package/third_party/s2geometry/src/s2/s2furthest_edge_query.cc +117 -0
- package/third_party/s2geometry/src/s2/s2furthest_edge_query.h +439 -0
- package/third_party/s2geometry/src/s2/s2furthest_edge_query_test.cc +487 -0
- package/third_party/s2geometry/src/s2/s2latlng.cc +90 -0
- package/third_party/s2geometry/src/s2/s2latlng.h +234 -0
- package/third_party/s2geometry/src/s2/s2latlng_rect.cc +727 -0
- package/third_party/s2geometry/src/s2/s2latlng_rect.h +434 -0
- package/third_party/s2geometry/src/s2/s2latlng_rect_bounder.cc +344 -0
- package/third_party/s2geometry/src/s2/s2latlng_rect_bounder.h +89 -0
- package/third_party/s2geometry/src/s2/s2latlng_rect_bounder_test.cc +306 -0
- package/third_party/s2geometry/src/s2/s2latlng_rect_test.cc +1030 -0
- package/third_party/s2geometry/src/s2/s2latlng_test.cc +165 -0
- package/third_party/s2geometry/src/s2/s2lax_loop_shape.cc +104 -0
- package/third_party/s2geometry/src/s2/s2lax_loop_shape.h +153 -0
- package/third_party/s2geometry/src/s2/s2lax_loop_shape_test.cc +101 -0
- package/third_party/s2geometry/src/s2/s2lax_polygon_shape.cc +348 -0
- package/third_party/s2geometry/src/s2/s2lax_polygon_shape.h +183 -0
- package/third_party/s2geometry/src/s2/s2lax_polygon_shape_test.cc +234 -0
- package/third_party/s2geometry/src/s2/s2lax_polyline_shape.cc +118 -0
- package/third_party/s2geometry/src/s2/s2lax_polyline_shape.h +124 -0
- package/third_party/s2geometry/src/s2/s2lax_polyline_shape_test.cc +62 -0
- package/third_party/s2geometry/src/s2/s2loop.cc +1509 -0
- package/third_party/s2geometry/src/s2/s2loop.h +711 -0
- package/third_party/s2geometry/src/s2/s2loop_measures.cc +313 -0
- package/third_party/s2geometry/src/s2/s2loop_measures.h +280 -0
- package/third_party/s2geometry/src/s2/s2loop_measures_test.cc +367 -0
- package/third_party/s2geometry/src/s2/s2loop_test.cc +1371 -0
- package/third_party/s2geometry/src/s2/s2max_distance_targets.cc +265 -0
- package/third_party/s2geometry/src/s2/s2max_distance_targets.h +241 -0
- package/third_party/s2geometry/src/s2/s2max_distance_targets_test.cc +367 -0
- package/third_party/s2geometry/src/s2/s2measures.cc +128 -0
- package/third_party/s2geometry/src/s2/s2measures.h +78 -0
- package/third_party/s2geometry/src/s2/s2measures_test.cc +135 -0
- package/third_party/s2geometry/src/s2/s2metrics.cc +122 -0
- package/third_party/s2geometry/src/s2/s2metrics.h +199 -0
- package/third_party/s2geometry/src/s2/s2metrics_test.cc +127 -0
- package/third_party/s2geometry/src/s2/s2min_distance_targets.cc +295 -0
- package/third_party/s2geometry/src/s2/s2min_distance_targets.h +273 -0
- package/third_party/s2geometry/src/s2/s2min_distance_targets_test.cc +239 -0
- package/third_party/s2geometry/src/s2/s2padded_cell.cc +162 -0
- package/third_party/s2geometry/src/s2/s2padded_cell.h +108 -0
- package/third_party/s2geometry/src/s2/s2padded_cell_test.cc +138 -0
- package/third_party/s2geometry/src/s2/s2point.h +38 -0
- package/third_party/s2geometry/src/s2/s2point_compression.cc +388 -0
- package/third_party/s2geometry/src/s2/s2point_compression.h +78 -0
- package/third_party/s2geometry/src/s2/s2point_compression_test.cc +305 -0
- package/third_party/s2geometry/src/s2/s2point_index.h +345 -0
- package/third_party/s2geometry/src/s2/s2point_index_test.cc +147 -0
- package/third_party/s2geometry/src/s2/s2point_region.cc +72 -0
- package/third_party/s2geometry/src/s2/s2point_region.h +76 -0
- package/third_party/s2geometry/src/s2/s2point_region_test.cc +100 -0
- package/third_party/s2geometry/src/s2/s2point_span.h +57 -0
- package/third_party/s2geometry/src/s2/s2point_test.cc +47 -0
- package/third_party/s2geometry/src/s2/s2point_vector_shape.h +127 -0
- package/third_party/s2geometry/src/s2/s2point_vector_shape_test.cc +59 -0
- package/third_party/s2geometry/src/s2/s2pointutil.cc +131 -0
- package/third_party/s2geometry/src/s2/s2pointutil.h +138 -0
- package/third_party/s2geometry/src/s2/s2pointutil_test.cc +157 -0
- package/third_party/s2geometry/src/s2/s2polygon.cc +1569 -0
- package/third_party/s2geometry/src/s2/s2polygon.h +934 -0
- package/third_party/s2geometry/src/s2/s2polygon_test.cc +3025 -0
- package/third_party/s2geometry/src/s2/s2polyline.cc +645 -0
- package/third_party/s2geometry/src/s2/s2polyline.h +379 -0
- package/third_party/s2geometry/src/s2/s2polyline_alignment.cc +414 -0
- package/third_party/s2geometry/src/s2/s2polyline_alignment.h +245 -0
- package/third_party/s2geometry/src/s2/s2polyline_alignment_internal.h +158 -0
- package/third_party/s2geometry/src/s2/s2polyline_alignment_test.cc +610 -0
- package/third_party/s2geometry/src/s2/s2polyline_measures.cc +42 -0
- package/third_party/s2geometry/src/s2/s2polyline_measures.h +53 -0
- package/third_party/s2geometry/src/s2/s2polyline_measures_test.cc +57 -0
- package/third_party/s2geometry/src/s2/s2polyline_simplifier.cc +187 -0
- package/third_party/s2geometry/src/s2/s2polyline_simplifier.h +109 -0
- package/third_party/s2geometry/src/s2/s2polyline_simplifier_test.cc +165 -0
- package/third_party/s2geometry/src/s2/s2polyline_test.cc +554 -0
- package/third_party/s2geometry/src/s2/s2predicates.cc +1486 -0
- package/third_party/s2geometry/src/s2/s2predicates.h +282 -0
- package/third_party/s2geometry/src/s2/s2predicates_internal.h +135 -0
- package/third_party/s2geometry/src/s2/s2predicates_test.cc +1427 -0
- package/third_party/s2geometry/src/s2/s2projections.cc +109 -0
- package/third_party/s2geometry/src/s2/s2projections.h +161 -0
- package/third_party/s2geometry/src/s2/s2projections_test.cc +78 -0
- package/third_party/s2geometry/src/s2/s2r2rect.cc +88 -0
- package/third_party/s2geometry/src/s2/s2r2rect.h +292 -0
- package/third_party/s2geometry/src/s2/s2r2rect_test.cc +312 -0
- package/third_party/s2geometry/src/s2/s2region.cc +26 -0
- package/third_party/s2geometry/src/s2/s2region.h +142 -0
- package/third_party/s2geometry/src/s2/s2region_coverer.cc +514 -0
- package/third_party/s2geometry/src/s2/s2region_coverer.h +356 -0
- package/third_party/s2geometry/src/s2/s2region_coverer_test.cc +509 -0
- package/third_party/s2geometry/src/s2/s2region_intersection.cc +84 -0
- package/third_party/s2geometry/src/s2/s2region_intersection.h +79 -0
- package/third_party/s2geometry/src/s2/s2region_term_indexer.cc +270 -0
- package/third_party/s2geometry/src/s2/s2region_term_indexer.h +299 -0
- package/third_party/s2geometry/src/s2/s2region_term_indexer_test.cc +209 -0
- package/third_party/s2geometry/src/s2/s2region_test.cc +370 -0
- package/third_party/s2geometry/src/s2/s2region_union.cc +90 -0
- package/third_party/s2geometry/src/s2/s2region_union.h +83 -0
- package/third_party/s2geometry/src/s2/s2region_union_test.cc +89 -0
- package/third_party/s2geometry/src/s2/s2shape.h +283 -0
- package/third_party/s2geometry/src/s2/s2shape_index.cc +321 -0
- package/third_party/s2geometry/src/s2/s2shape_index.h +781 -0
- package/third_party/s2geometry/src/s2/s2shape_index_buffered_region.cc +113 -0
- package/third_party/s2geometry/src/s2/s2shape_index_buffered_region.h +135 -0
- package/third_party/s2geometry/src/s2/s2shape_index_buffered_region_test.cc +162 -0
- package/third_party/s2geometry/src/s2/s2shape_index_measures.cc +92 -0
- package/third_party/s2geometry/src/s2/s2shape_index_measures.h +100 -0
- package/third_party/s2geometry/src/s2/s2shape_index_measures_test.cc +136 -0
- package/third_party/s2geometry/src/s2/s2shape_index_region.h +350 -0
- package/third_party/s2geometry/src/s2/s2shape_index_region_test.cc +161 -0
- package/third_party/s2geometry/src/s2/s2shape_index_test.cc +24 -0
- package/third_party/s2geometry/src/s2/s2shape_measures.cc +138 -0
- package/third_party/s2geometry/src/s2/s2shape_measures.h +95 -0
- package/third_party/s2geometry/src/s2/s2shape_measures_test.cc +139 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_build_polygon_boundaries.cc +120 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_build_polygon_boundaries.h +66 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_build_polygon_boundaries_test.cc +170 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_coding.cc +253 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_coding.h +283 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_coding_test.cc +54 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_contains_brute_force.cc +40 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_contains_brute_force.h +41 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_contains_brute_force_test.cc +55 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_count_edges.h +57 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_count_edges_test.cc +43 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_edge_iterator.cc +45 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_edge_iterator.h +72 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_edge_iterator_test.cc +116 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_get_reference_point.cc +107 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_get_reference_point.h +48 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_get_reference_point_test.cc +104 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_range_iterator.cc +58 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_range_iterator.h +65 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_range_iterator_test.cc +61 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_shape_edge.h +58 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_shape_edge_id.h +97 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_testing.cc +104 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_testing.h +36 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_visit_crossing_edge_pairs.cc +440 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_visit_crossing_edge_pairs.h +72 -0
- package/third_party/s2geometry/src/s2/s2shapeutil_visit_crossing_edge_pairs_test.cc +184 -0
- package/third_party/s2geometry/src/s2/s2testing.cc +464 -0
- package/third_party/s2geometry/src/s2/s2testing.h +385 -0
- package/third_party/s2geometry/src/s2/s2testing_test.cc +166 -0
- package/third_party/s2geometry/src/s2/s2text_format.cc +506 -0
- package/third_party/s2geometry/src/s2/s2text_format.h +289 -0
- package/third_party/s2geometry/src/s2/s2text_format_test.cc +417 -0
- package/third_party/s2geometry/src/s2/s2wedge_relations.cc +80 -0
- package/third_party/s2geometry/src/s2/s2wedge_relations.h +64 -0
- package/third_party/s2geometry/src/s2/s2wedge_relations_test.cc +89 -0
- package/third_party/s2geometry/src/s2/sequence_lexicon.h +296 -0
- package/third_party/s2geometry/src/s2/sequence_lexicon_test.cc +113 -0
- package/third_party/s2geometry/src/s2/strings/ostringstream.cc +35 -0
- package/third_party/s2geometry/src/s2/strings/ostringstream.h +105 -0
- package/third_party/s2geometry/src/s2/strings/serialize.cc +46 -0
- package/third_party/s2geometry/src/s2/strings/serialize.h +40 -0
- package/third_party/s2geometry/src/s2/third_party/absl/algorithm/algorithm.h +187 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/attributes.h +666 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/casts.h +189 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/config.h +462 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/dynamic_annotations.cc +129 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/dynamic_annotations.h +394 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/atomic_hook.h +168 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/identity.h +33 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/inline_variable.h +117 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/invoke.h +188 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/raw_logging.cc +254 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/raw_logging.h +205 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/throw_delegate.cc +106 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/throw_delegate.h +71 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/internal/unaligned_access.h +322 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/log_severity.h +77 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/macros.h +236 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/optimization.h +177 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/policy_checks.h +124 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/port.h +97 -0
- package/third_party/s2geometry/src/s2/third_party/absl/base/thread_annotations.h +277 -0
- package/third_party/s2geometry/src/s2/third_party/absl/container/fixed_array.h +523 -0
- package/third_party/s2geometry/src/s2/third_party/absl/container/inlined_vector.h +1453 -0
- package/third_party/s2geometry/src/s2/third_party/absl/container/internal/compressed_tuple.h +191 -0
- package/third_party/s2geometry/src/s2/third_party/absl/container/internal/container_memory.h +424 -0
- package/third_party/s2geometry/src/s2/third_party/absl/container/internal/layout.h +739 -0
- package/third_party/s2geometry/src/s2/third_party/absl/memory/memory.h +755 -0
- package/third_party/s2geometry/src/s2/third_party/absl/meta/type_traits.h +436 -0
- package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128.cc +232 -0
- package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128.h +656 -0
- package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128_have_intrinsic.inc +3 -0
- package/third_party/s2geometry/src/s2/third_party/absl/numeric/int128_no_intrinsic.inc +3 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/ascii.cc +198 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/ascii.h +239 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/ascii_ctype.h +66 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/bits.h +53 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/memutil.cc +110 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/memutil.h +146 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/internal/resize_uninitialized.h +72 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/match.cc +38 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/match.h +89 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/numbers.cc +909 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/numbers.h +187 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/str_cat.cc +240 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/str_cat.h +398 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/str_join.h +22 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/str_split.cc +47 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/str_split.h +43 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/string_view.cc +245 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/string_view.h +602 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/strip.cc +42 -0
- package/third_party/s2geometry/src/s2/third_party/absl/strings/strip.h +130 -0
- package/third_party/s2geometry/src/s2/third_party/absl/types/span.h +793 -0
- package/third_party/s2geometry/src/s2/third_party/absl/utility/utility.h +299 -0
- package/third_party/s2geometry/src/s2/util/bits/bit-interleave.cc +274 -0
- package/third_party/s2geometry/src/s2/util/bits/bit-interleave.h +53 -0
- package/third_party/s2geometry/src/s2/util/bits/bits.cc +155 -0
- package/third_party/s2geometry/src/s2/util/bits/bits.h +745 -0
- package/third_party/s2geometry/src/s2/util/coding/coder.cc +83 -0
- package/third_party/s2geometry/src/s2/util/coding/coder.h +553 -0
- package/third_party/s2geometry/src/s2/util/coding/nth-derivative.h +134 -0
- package/third_party/s2geometry/src/s2/util/coding/transforms.h +62 -0
- package/third_party/s2geometry/src/s2/util/coding/varint.cc +289 -0
- package/third_party/s2geometry/src/s2/util/coding/varint.h +476 -0
- package/third_party/s2geometry/src/s2/util/endian/endian.h +859 -0
- package/third_party/s2geometry/src/s2/util/gtl/btree.h +2471 -0
- package/third_party/s2geometry/src/s2/util/gtl/btree_container.h +411 -0
- package/third_party/s2geometry/src/s2/util/gtl/btree_map.h +79 -0
- package/third_party/s2geometry/src/s2/util/gtl/btree_set.h +73 -0
- package/third_party/s2geometry/src/s2/util/gtl/compact_array.h +653 -0
- package/third_party/s2geometry/src/s2/util/gtl/container_logging.h +291 -0
- package/third_party/s2geometry/src/s2/util/gtl/dense_hash_set.h +358 -0
- package/third_party/s2geometry/src/s2/util/gtl/densehashtable.h +1493 -0
- package/third_party/s2geometry/src/s2/util/gtl/hashtable_common.h +253 -0
- package/third_party/s2geometry/src/s2/util/gtl/layout.h +28 -0
- package/third_party/s2geometry/src/s2/util/gtl/legacy_random_shuffle.h +77 -0
- package/third_party/s2geometry/src/s2/util/hash/mix.h +76 -0
- package/third_party/s2geometry/src/s2/util/math/exactfloat/exactfloat.cc +832 -0
- package/third_party/s2geometry/src/s2/util/math/exactfloat/exactfloat.h +646 -0
- package/third_party/s2geometry/src/s2/util/math/mathutil.cc +75 -0
- package/third_party/s2geometry/src/s2/util/math/mathutil.h +189 -0
- package/third_party/s2geometry/src/s2/util/math/matrix3x3.h +574 -0
- package/third_party/s2geometry/src/s2/util/math/vector.h +569 -0
- package/third_party/s2geometry/src/s2/util/math/vector3_hash.h +54 -0
- package/third_party/s2geometry/src/s2/util/units/length-units.cc +21 -0
- package/third_party/s2geometry/src/s2/util/units/length-units.h +135 -0
- package/third_party/s2geometry/src/s2/util/units/physical-units.h +313 -0
- package/third_party/s2geometry/src/s2/value_lexicon.h +234 -0
- package/third_party/s2geometry/src/s2/value_lexicon_test.cc +121 -0
- package/third_party/s2geometry/third_party/cmake/FindGFlags.cmake +48 -0
- package/third_party/s2geometry/third_party/cmake/FindGlog.cmake +48 -0
|
@@ -0,0 +1,2471 @@
|
|
|
1
|
+
// Copyright 2007 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
|
+
|
|
16
|
+
//
|
|
17
|
+
// A btree implementation of the STL set and map interfaces. A btree is smaller
|
|
18
|
+
// and generally also faster than STL set/map (refer to the benchmarks below).
|
|
19
|
+
// The red-black tree implementation of STL set/map has an overhead of 3
|
|
20
|
+
// pointers (left, right and parent) plus the node color information for each
|
|
21
|
+
// stored value. So a set<int32> consumes 40 bytes for each value stored in
|
|
22
|
+
// 64-bit mode. This btree implementation stores multiple values on fixed
|
|
23
|
+
// size nodes (usually 256 bytes) and doesn't store child pointers for leaf
|
|
24
|
+
// nodes. The result is that a btree_set<int32> may use much less memory per
|
|
25
|
+
// stored value. For the random insertion benchmark in btree_bench.cc, a
|
|
26
|
+
// btree_set<int32> with node-size of 256 uses 5.1 bytes per stored value.
|
|
27
|
+
//
|
|
28
|
+
// The packing of multiple values on to each node of a btree has another effect
|
|
29
|
+
// besides better space utilization: better cache locality due to fewer cache
|
|
30
|
+
// lines being accessed. Better cache locality translates into faster
|
|
31
|
+
// operations.
|
|
32
|
+
//
|
|
33
|
+
// CAVEATS
|
|
34
|
+
//
|
|
35
|
+
// Insertions and deletions on a btree can cause splitting, merging or
|
|
36
|
+
// rebalancing of btree nodes. And even without these operations, insertions
|
|
37
|
+
// and deletions on a btree will move values around within a node. In both
|
|
38
|
+
// cases, the result is that insertions and deletions can invalidate iterators
|
|
39
|
+
// pointing to values other than the one being inserted/deleted. Therefore, this
|
|
40
|
+
// container does not provide pointer stability. This is notably different from
|
|
41
|
+
// STL set/map which takes care to not invalidate iterators on insert/erase
|
|
42
|
+
// except, of course, for iterators pointing to the value being erased. A
|
|
43
|
+
// partial workaround when erasing is available: erase() returns an iterator
|
|
44
|
+
// pointing to the item just after the one that was erased (or end() if none
|
|
45
|
+
// exists).
|
|
46
|
+
|
|
47
|
+
// PERFORMANCE
|
|
48
|
+
//
|
|
49
|
+
// See the latest benchmark results at:
|
|
50
|
+
// https://paste.googleplex.com/5549632792821760
|
|
51
|
+
//
|
|
52
|
+
|
|
53
|
+
#ifndef S2_UTIL_GTL_BTREE_H_
|
|
54
|
+
#define S2_UTIL_GTL_BTREE_H_
|
|
55
|
+
|
|
56
|
+
#include <cstddef>
|
|
57
|
+
#include <cstring>
|
|
58
|
+
#include <algorithm>
|
|
59
|
+
#include <cassert>
|
|
60
|
+
#include <functional>
|
|
61
|
+
#include <iterator>
|
|
62
|
+
#include <limits>
|
|
63
|
+
#include <new>
|
|
64
|
+
#include <string>
|
|
65
|
+
#include <type_traits>
|
|
66
|
+
#include <utility>
|
|
67
|
+
|
|
68
|
+
#include "s2/base/integral_types.h"
|
|
69
|
+
#include "s2/third_party/absl/base/macros.h"
|
|
70
|
+
#include "s2/third_party/absl/container/internal/compressed_tuple.h"
|
|
71
|
+
#include "s2/third_party/absl/container/internal/container_memory.h"
|
|
72
|
+
#include "s2/third_party/absl/memory/memory.h"
|
|
73
|
+
#include "s2/third_party/absl/meta/type_traits.h"
|
|
74
|
+
#include "s2/third_party/absl/strings/string_view.h"
|
|
75
|
+
#include "s2/third_party/absl/utility/utility.h"
|
|
76
|
+
#include "s2/util/gtl/layout.h"
|
|
77
|
+
|
|
78
|
+
namespace gtl {
|
|
79
|
+
|
|
80
|
+
// A helper type used to indicate that a key-compare-to functor has been
|
|
81
|
+
// provided. A key-compare-to functor compares two arguments of type value_type
|
|
82
|
+
// and returns 0 if they are equal, a negative integer when the first argument
|
|
83
|
+
// should be first, and a positive integer otherwise. A user can specify a
|
|
84
|
+
// key-compare-to functor by doing:
|
|
85
|
+
//
|
|
86
|
+
// struct MyStringComparer
|
|
87
|
+
// : public gtl::btree_key_compare_to_tag {
|
|
88
|
+
// int operator()(const string &a, const string &b) const {
|
|
89
|
+
// return a.compare(b);
|
|
90
|
+
// }
|
|
91
|
+
// };
|
|
92
|
+
//
|
|
93
|
+
// Note that the return type is an int and not a bool. There is a
|
|
94
|
+
// static_assert which enforces this return type.
|
|
95
|
+
// TODO(user): get rid of this tag and just detect whether there is an operator
|
|
96
|
+
struct btree_key_compare_to_tag {};
|
|
97
|
+
|
|
98
|
+
// A helper class that indicates if the Compare parameter is derived from
|
|
99
|
+
// btree_key_compare_to_tag.
|
|
100
|
+
template <typename Compare>
|
|
101
|
+
using btree_is_key_compare_to =
|
|
102
|
+
std::is_convertible<Compare, btree_key_compare_to_tag>;
|
|
103
|
+
|
|
104
|
+
namespace internal_btree {
|
|
105
|
+
// A helper class used to indicate if the comparator provided is transparent
|
|
106
|
+
// and thus supports heterogeneous lookups. This is only used internally to
|
|
107
|
+
// check if the Compare parameter has a valid is_transparent member.
|
|
108
|
+
// A transparent comparator will see lookup keys with any type (lookup_type)
|
|
109
|
+
// passed by the user to any of the lookup methods. The comparator then has a
|
|
110
|
+
// chance to do the comparison without first converting the lookup key to a
|
|
111
|
+
// key_type.
|
|
112
|
+
//
|
|
113
|
+
// For example, a comparator that is transparent may look like:
|
|
114
|
+
//
|
|
115
|
+
// struct MyStringComparer {
|
|
116
|
+
// bool operator()(const string &a, const string &b) const {
|
|
117
|
+
// return a < b;
|
|
118
|
+
// }
|
|
119
|
+
// bool operator()(const string &a, const char* b) const {
|
|
120
|
+
// return strcmp(a.c_str(), b) < 0;
|
|
121
|
+
// }
|
|
122
|
+
// bool operator()(const char* a, const string& b) const {
|
|
123
|
+
// return strcmp(a, b.c_str()) < 0;
|
|
124
|
+
// }
|
|
125
|
+
// using is_transparent = void;
|
|
126
|
+
// };
|
|
127
|
+
//
|
|
128
|
+
// Note that we need to declare operator() for both combinations of key_type and
|
|
129
|
+
// lookup_type. Also note that setting is_transparent to void is an arbitrary
|
|
130
|
+
// decision; it can be std::true_type, int, or anything else, just as long as
|
|
131
|
+
// the member is_transparent is defined to be something.
|
|
132
|
+
template <typename, typename = void>
|
|
133
|
+
struct is_comparator_transparent : std::false_type {};
|
|
134
|
+
template <typename Compare>
|
|
135
|
+
struct is_comparator_transparent<Compare,
|
|
136
|
+
absl::void_t<typename Compare::is_transparent>>
|
|
137
|
+
: std::true_type {};
|
|
138
|
+
|
|
139
|
+
// A helper class to convert a boolean comparison into a three-way "compare-to"
|
|
140
|
+
// comparison that returns a negative value to indicate less-than, zero to
|
|
141
|
+
// indicate equality and a positive value to indicate greater-than. This helper
|
|
142
|
+
// class is specialized for less<string>, greater<string>, less<string_view>,
|
|
143
|
+
// and greater<string_view>. The
|
|
144
|
+
// key_compare_to_adapter is provided so that btree users
|
|
145
|
+
// automatically get the more efficient compare-to code when using common
|
|
146
|
+
// google string types with common comparison functors.
|
|
147
|
+
// TODO(user): see if we can extract this logic so that it can be used with
|
|
148
|
+
template <typename Compare>
|
|
149
|
+
struct key_compare_to_adapter {
|
|
150
|
+
using type = Compare;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
template <>
|
|
154
|
+
struct key_compare_to_adapter<std::less<std::string>> {
|
|
155
|
+
struct type : public btree_key_compare_to_tag {
|
|
156
|
+
type() = default;
|
|
157
|
+
explicit type(const std::less<std::string> &) {}
|
|
158
|
+
int operator()(const std::string &a, const std::string &b) const {
|
|
159
|
+
return a.compare(b);
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
template <>
|
|
165
|
+
struct key_compare_to_adapter<std::greater<std::string>> {
|
|
166
|
+
struct type : public btree_key_compare_to_tag {
|
|
167
|
+
type() = default;
|
|
168
|
+
explicit type(const std::greater<std::string> &) {}
|
|
169
|
+
int operator()(const std::string &a, const std::string &b) const {
|
|
170
|
+
return b.compare(a);
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
template <>
|
|
176
|
+
struct key_compare_to_adapter<std::less<absl::string_view>> {
|
|
177
|
+
struct type : public btree_key_compare_to_tag {
|
|
178
|
+
type() = default;
|
|
179
|
+
explicit type(const std::less<absl::string_view> &) {}
|
|
180
|
+
int operator()(const absl::string_view a, const absl::string_view b) const {
|
|
181
|
+
return a.compare(b);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
template <>
|
|
187
|
+
struct key_compare_to_adapter<std::greater<absl::string_view>> {
|
|
188
|
+
struct type : public btree_key_compare_to_tag {
|
|
189
|
+
type() = default;
|
|
190
|
+
explicit type(const std::greater<absl::string_view> &) {}
|
|
191
|
+
int operator()(const absl::string_view a, const absl::string_view b) const {
|
|
192
|
+
return b.compare(a);
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
// A helper function to do a boolean comparison of two keys given a boolean
|
|
199
|
+
// or key-compare-to (three-way) comparator.
|
|
200
|
+
template <typename K, typename LK, typename Compare>
|
|
201
|
+
bool bool_compare_keys(const Compare &comp, const K &x, const LK &y) {
|
|
202
|
+
return btree_is_key_compare_to<Compare>::value ? comp(x, y) < 0 : comp(x, y);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// Detects a 'goog_btree_prefer_linear_node_search' member. This is
|
|
206
|
+
// a protocol used as an opt-in or opt-out of linear search.
|
|
207
|
+
//
|
|
208
|
+
// For example, this would be useful for key types that wrap an integer
|
|
209
|
+
// and define their own cheap operator<(). For example:
|
|
210
|
+
//
|
|
211
|
+
// class K {
|
|
212
|
+
// public:
|
|
213
|
+
// using goog_btree_prefer_linear_node_search = std::true_type;
|
|
214
|
+
// ...
|
|
215
|
+
// private:
|
|
216
|
+
// friend bool operator<(K a, K b) { return a.k_ < b.k_; }
|
|
217
|
+
// int k_;
|
|
218
|
+
// };
|
|
219
|
+
//
|
|
220
|
+
// btree_map<K, V> m; // Uses linear search
|
|
221
|
+
// assert((btree_map<K, V>::testonly_uses_linear_node_search()));
|
|
222
|
+
//
|
|
223
|
+
// If T has the preference tag, then it has a preference.
|
|
224
|
+
// Btree will use the tag's truth value.
|
|
225
|
+
template <typename T, typename = void>
|
|
226
|
+
struct has_linear_node_search_preference : std::false_type {};
|
|
227
|
+
template <typename T, typename = void>
|
|
228
|
+
struct prefers_linear_node_search : std::false_type {};
|
|
229
|
+
template <typename T>
|
|
230
|
+
struct has_linear_node_search_preference<
|
|
231
|
+
T, absl::void_t<typename T::goog_btree_prefer_linear_node_search>>
|
|
232
|
+
: std::true_type {};
|
|
233
|
+
template <typename T>
|
|
234
|
+
struct prefers_linear_node_search<
|
|
235
|
+
T, absl::void_t<typename T::goog_btree_prefer_linear_node_search>>
|
|
236
|
+
: T::goog_btree_prefer_linear_node_search {};
|
|
237
|
+
|
|
238
|
+
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
|
|
239
|
+
int ValueSize, bool Multi>
|
|
240
|
+
struct common_params {
|
|
241
|
+
// If Compare is derived from btree_key_compare_to_tag then use it as the
|
|
242
|
+
// key_compare type. Otherwise, use key_compare_to_adapter<> which will
|
|
243
|
+
// fall-back to Compare if we don't have an appropriate specialization.
|
|
244
|
+
using key_compare =
|
|
245
|
+
absl::conditional_t<btree_is_key_compare_to<Compare>::value, Compare,
|
|
246
|
+
typename key_compare_to_adapter<Compare>::type>;
|
|
247
|
+
// A type which indicates if we have a key-compare-to functor or a plain old
|
|
248
|
+
// key-compare functor.
|
|
249
|
+
using is_key_compare_to = btree_is_key_compare_to<key_compare>;
|
|
250
|
+
|
|
251
|
+
using allocator_type = Alloc;
|
|
252
|
+
using key_type = Key;
|
|
253
|
+
using size_type = std::make_signed<size_t>::type;
|
|
254
|
+
using difference_type = ptrdiff_t;
|
|
255
|
+
|
|
256
|
+
// True if this is a multiset or multimap.
|
|
257
|
+
using is_multi_container = std::integral_constant<bool, Multi>;
|
|
258
|
+
|
|
259
|
+
enum {
|
|
260
|
+
kTargetNodeSize = TargetNodeSize,
|
|
261
|
+
|
|
262
|
+
// Upper bound for the available space for values. This is largest for leaf
|
|
263
|
+
// nodes, which have overhead of at least a pointer + 4 bytes (for storing
|
|
264
|
+
// 3 field_types and an enum).
|
|
265
|
+
kNodeValueSpace =
|
|
266
|
+
TargetNodeSize - /*minimum overhead=*/(sizeof(void *) + 4),
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
// This is an integral type large enough to hold as many
|
|
270
|
+
// ValueSize-values as will fit a node of TargetNodeSize bytes.
|
|
271
|
+
using node_count_type =
|
|
272
|
+
absl::conditional_t<(kNodeValueSpace / ValueSize >
|
|
273
|
+
std::numeric_limits<uint8>::max()),
|
|
274
|
+
uint16, uint8>; // NOLINT
|
|
275
|
+
static_assert(kNodeValueSpace / ValueSize <=
|
|
276
|
+
std::numeric_limits<uint16>::max(),
|
|
277
|
+
"uint16 is not big enough for node_count_type.");
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// A parameters structure for holding the type parameters for a btree_map.
|
|
281
|
+
// Compare and Alloc should be nothrow copy-constructible.
|
|
282
|
+
template <typename Key, typename Data, typename Compare, typename Alloc,
|
|
283
|
+
int TargetNodeSize, bool Multi>
|
|
284
|
+
struct map_params : common_params<Key, Compare, Alloc, TargetNodeSize,
|
|
285
|
+
sizeof(std::pair<const Key, Data>), Multi> {
|
|
286
|
+
using mapped_type = Data;
|
|
287
|
+
// This type allows us to move keys when it is safe to do so. It is safe
|
|
288
|
+
// for maps in which value_type and mutable_value_type are layout compatible.
|
|
289
|
+
using slot_type = absl::container_internal::slot_type<Key, mapped_type>;
|
|
290
|
+
using value_type = typename slot_type::value_type;
|
|
291
|
+
using mutable_value_type = typename slot_type::mutable_value_type;
|
|
292
|
+
using pointer = value_type *;
|
|
293
|
+
using const_pointer = const value_type *;
|
|
294
|
+
using reference = value_type &;
|
|
295
|
+
using const_reference = const value_type &;
|
|
296
|
+
|
|
297
|
+
using key_compare = typename map_params::common_params::key_compare;
|
|
298
|
+
// Inherit from key_compare for empty base class optimization.
|
|
299
|
+
struct value_compare : private key_compare {
|
|
300
|
+
value_compare() = default;
|
|
301
|
+
explicit value_compare(const key_compare &cmp) : key_compare(cmp) {}
|
|
302
|
+
|
|
303
|
+
template <typename T, typename U>
|
|
304
|
+
absl::conditional_t<btree_is_key_compare_to<key_compare>::value, int, bool>
|
|
305
|
+
operator()(const T &left, const U &right) const {
|
|
306
|
+
return key_compare::operator()(left.first, right.first);
|
|
307
|
+
}
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
static const Key& key(const value_type &x) { return x.first; }
|
|
311
|
+
static const Key& key(const mutable_value_type &x) { return x.first; }
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
// A parameters structure for holding the type parameters for a btree_set.
|
|
315
|
+
// Compare and Alloc should be nothrow copy-constructible.
|
|
316
|
+
template <typename Key, typename Compare, typename Alloc, int TargetNodeSize,
|
|
317
|
+
bool Multi>
|
|
318
|
+
struct set_params
|
|
319
|
+
: common_params<Key, Compare, Alloc, TargetNodeSize, sizeof(Key), Multi> {
|
|
320
|
+
using mapped_type = void;
|
|
321
|
+
using value_type = Key;
|
|
322
|
+
using mutable_value_type = Key;
|
|
323
|
+
// This type implements the necessary functions from the
|
|
324
|
+
// absl::container_internal::slot_type interface.
|
|
325
|
+
struct slot_type {
|
|
326
|
+
value_type value;
|
|
327
|
+
|
|
328
|
+
template <class... Args>
|
|
329
|
+
static void construct(Alloc *alloc, slot_type *slot, Args &&... args) {
|
|
330
|
+
absl::allocator_traits<Alloc>::construct(*alloc, &slot->value,
|
|
331
|
+
std::forward<Args>(args)...);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
static void construct(Alloc *alloc, slot_type *slot, slot_type *other) {
|
|
335
|
+
absl::allocator_traits<Alloc>::construct(*alloc, &slot->value,
|
|
336
|
+
std::move(other->value));
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
static void destroy(Alloc *alloc, slot_type *slot) {
|
|
340
|
+
absl::allocator_traits<Alloc>::destroy(*alloc, &slot->value);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
static void swap(Alloc * /*alloc*/, slot_type *a, slot_type *b) {
|
|
344
|
+
using std::swap;
|
|
345
|
+
swap(a->value, b->value);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
static void move(Alloc * /*alloc*/, slot_type *src, slot_type *dest) {
|
|
349
|
+
dest->value = std::move(src->value);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
static void move(Alloc *alloc, slot_type *first, slot_type *last,
|
|
353
|
+
slot_type *result) {
|
|
354
|
+
for (slot_type *src = first, *dest = result; src != last; ++src, ++dest)
|
|
355
|
+
move(alloc, src, dest);
|
|
356
|
+
}
|
|
357
|
+
};
|
|
358
|
+
using pointer = value_type *;
|
|
359
|
+
using const_pointer = const value_type *;
|
|
360
|
+
using reference = value_type &;
|
|
361
|
+
using const_reference = const value_type &;
|
|
362
|
+
using value_compare = typename set_params::common_params::key_compare;
|
|
363
|
+
|
|
364
|
+
static const Key& key(const value_type &x) { return x; }
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// An adapter class that converts a lower-bound compare into an upper-bound
|
|
368
|
+
// compare. Note: there is no need to make a version of this adapter specialized
|
|
369
|
+
// for key-compare-to functors because the upper-bound (the first value greater
|
|
370
|
+
// than the input) is never an exact match.
|
|
371
|
+
template <typename Compare>
|
|
372
|
+
struct upper_bound_adapter {
|
|
373
|
+
explicit upper_bound_adapter(const Compare &c) : comp(c) {}
|
|
374
|
+
template <typename K, typename LK>
|
|
375
|
+
bool operator()(const K &a, const LK &b) const {
|
|
376
|
+
// Returns true when a is not greater than b.
|
|
377
|
+
return !bool_compare_keys(comp, b, a);
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
private:
|
|
381
|
+
Compare comp;
|
|
382
|
+
};
|
|
383
|
+
|
|
384
|
+
// A node in the btree holding. The same node type is used for both internal
|
|
385
|
+
// and leaf nodes in the btree, though the nodes are allocated in such a way
|
|
386
|
+
// that the children array is only valid in internal nodes.
|
|
387
|
+
template <typename Params>
|
|
388
|
+
class btree_node {
|
|
389
|
+
using is_key_compare_to = typename Params::is_key_compare_to;
|
|
390
|
+
using is_multi_container = typename Params::is_multi_container;
|
|
391
|
+
using field_type = typename Params::node_count_type;
|
|
392
|
+
using allocator_type = typename Params::allocator_type;
|
|
393
|
+
using slot_type = typename Params::slot_type;
|
|
394
|
+
|
|
395
|
+
public:
|
|
396
|
+
using params_type = Params;
|
|
397
|
+
using key_type = typename Params::key_type;
|
|
398
|
+
using value_type = typename Params::value_type;
|
|
399
|
+
using mutable_value_type = typename Params::mutable_value_type;
|
|
400
|
+
using pointer = typename Params::pointer;
|
|
401
|
+
using const_pointer = typename Params::const_pointer;
|
|
402
|
+
using reference = typename Params::reference;
|
|
403
|
+
using const_reference = typename Params::const_reference;
|
|
404
|
+
using key_compare = typename Params::key_compare;
|
|
405
|
+
using size_type = typename Params::size_type;
|
|
406
|
+
using difference_type = typename Params::difference_type;
|
|
407
|
+
|
|
408
|
+
// Btree's choice of binary search or linear search is a customization
|
|
409
|
+
// point that can be configured via the key_compare and key_type.
|
|
410
|
+
// Btree decides whether to use linear node search as follows:
|
|
411
|
+
// - If the comparator expresses a preference, use that.
|
|
412
|
+
// - Otherwise, if the key expresses a preference, use that.
|
|
413
|
+
// - Otherwise, if the key is arithmetic and the comparator is std::less or
|
|
414
|
+
// std::greater, choose linear.
|
|
415
|
+
// - Otherwise, choose binary.
|
|
416
|
+
// See documentation for has_linear_node_search_preference and
|
|
417
|
+
// prefers_linear_node_search above.
|
|
418
|
+
// Might be wise to also configure linear search based on node-size.
|
|
419
|
+
using use_linear_search = absl::conditional_t<
|
|
420
|
+
has_linear_node_search_preference<key_compare>::value
|
|
421
|
+
? prefers_linear_node_search<key_compare>::value
|
|
422
|
+
: has_linear_node_search_preference<key_type>::value
|
|
423
|
+
? prefers_linear_node_search<key_type>::value
|
|
424
|
+
: std::is_arithmetic<key_type>::value &&
|
|
425
|
+
(std::is_same<std::less<key_type>, key_compare>::value ||
|
|
426
|
+
std::is_same<std::greater<key_type>,
|
|
427
|
+
key_compare>::value),
|
|
428
|
+
std::true_type, std::false_type>;
|
|
429
|
+
|
|
430
|
+
// This class is organized by gtl::Layout as if it had the following
|
|
431
|
+
// structure:
|
|
432
|
+
// // A pointer to the node's parent.
|
|
433
|
+
// btree_node *parent;
|
|
434
|
+
//
|
|
435
|
+
// // The position of the node in the node's parent.
|
|
436
|
+
// field_type position;
|
|
437
|
+
// // The index of the first populated value in `values`.
|
|
438
|
+
// // TODO(user): right now, `start` is always 0. Update insertion/merge
|
|
439
|
+
// // logic to allow for floating storage within nodes.
|
|
440
|
+
// field_type start;
|
|
441
|
+
// // The count of the number of populated values in the node.
|
|
442
|
+
// field_type count;
|
|
443
|
+
// // The maximum number of values the node can hold. This is an integer in
|
|
444
|
+
// // [1, kNodeValues] for root leaf nodes, kNodeValues for non-root leaf
|
|
445
|
+
// // nodes, and kInternalNodeMaxCount (as a sentinel value) for internal
|
|
446
|
+
// // nodes (even though there are still kNodeValues values in the node).
|
|
447
|
+
// // TODO(user): make max_count use only 4 bits and record log2(capacity)
|
|
448
|
+
// // to free extra bits for is_root, etc.
|
|
449
|
+
// field_type max_count;
|
|
450
|
+
//
|
|
451
|
+
// // The array of values. The capacity is `max_count` for leaf nodes and
|
|
452
|
+
// // kNodeValues for internal nodes. Only the values in
|
|
453
|
+
// // [start, start + count) have been initialized and are valid.
|
|
454
|
+
// slot_type values[max_count];
|
|
455
|
+
//
|
|
456
|
+
// // The array of child pointers. The keys in children[i] are all less
|
|
457
|
+
// // than key(i). The keys in children[i + 1] are all greater than key(i).
|
|
458
|
+
// // There are 0 children for leaf nodes and kNodeValues + 1 children for
|
|
459
|
+
// // internal nodes.
|
|
460
|
+
// btree_node *children[kNodeValues + 1];
|
|
461
|
+
//
|
|
462
|
+
// This class is never constructed or deleted. Instead, pointers to the layout
|
|
463
|
+
// above are allocated, cast to btree_node*, and de-allocated within the btree
|
|
464
|
+
// implementation.
|
|
465
|
+
btree_node() = delete;
|
|
466
|
+
~btree_node() = delete;
|
|
467
|
+
btree_node(btree_node const &) = delete;
|
|
468
|
+
btree_node &operator=(btree_node const &) = delete;
|
|
469
|
+
|
|
470
|
+
private:
|
|
471
|
+
using layout_type = Layout<btree_node *, field_type, slot_type, btree_node *>;
|
|
472
|
+
constexpr static size_type SizeWithNValues(size_type n) {
|
|
473
|
+
return layout_type(/*parent*/ 1,
|
|
474
|
+
/*position, start, count, max_count*/ 4,
|
|
475
|
+
/*values*/ n,
|
|
476
|
+
/*children*/ 0)
|
|
477
|
+
.AllocSize();
|
|
478
|
+
}
|
|
479
|
+
// A lower bound for the overhead of fields other than values in a leaf node.
|
|
480
|
+
constexpr static size_type MinimumOverhead() {
|
|
481
|
+
return SizeWithNValues(1) - sizeof(value_type);
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
// Compute how many values we can fit onto a leaf node taking into account
|
|
485
|
+
// padding.
|
|
486
|
+
constexpr static size_type NodeTargetValues(const int begin, const int end) {
|
|
487
|
+
return begin == end ? begin
|
|
488
|
+
: SizeWithNValues((begin + end) / 2 + 1) >
|
|
489
|
+
params_type::kTargetNodeSize
|
|
490
|
+
? NodeTargetValues(begin, (begin + end) / 2)
|
|
491
|
+
: NodeTargetValues((begin + end) / 2 + 1, end);
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
enum {
|
|
495
|
+
kTargetNodeSize = params_type::kTargetNodeSize,
|
|
496
|
+
kNodeTargetValues = NodeTargetValues(0, params_type::kTargetNodeSize),
|
|
497
|
+
|
|
498
|
+
// We need a minimum of 3 values per internal node in order to perform
|
|
499
|
+
// splitting (1 value for the two nodes involved in the split and 1 value
|
|
500
|
+
// propagated to the parent as the delimiter for the split).
|
|
501
|
+
kNodeValues = kNodeTargetValues >= 3 ? kNodeTargetValues : 3,
|
|
502
|
+
|
|
503
|
+
kExactMatch = 1 << 30,
|
|
504
|
+
kMatchMask = kExactMatch - 1,
|
|
505
|
+
|
|
506
|
+
// The node is internal (i.e. is not a leaf node) if and only if `max_count`
|
|
507
|
+
// has this value.
|
|
508
|
+
kInternalNodeMaxCount = 0,
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
// Leaves can have less than kNodeValues values.
|
|
512
|
+
constexpr static layout_type LeafLayout(const int max_values = kNodeValues) {
|
|
513
|
+
return layout_type(/*parent*/ 1,
|
|
514
|
+
/*position, start, count, max_count*/ 4,
|
|
515
|
+
/*values*/ max_values,
|
|
516
|
+
/*children*/ 0);
|
|
517
|
+
}
|
|
518
|
+
constexpr static layout_type InternalLayout() {
|
|
519
|
+
return layout_type(/*parent*/ 1,
|
|
520
|
+
/*position, start, count, max_count*/ 4,
|
|
521
|
+
/*values*/ kNodeValues,
|
|
522
|
+
/*children*/ kNodeValues + 1);
|
|
523
|
+
}
|
|
524
|
+
constexpr static size_type LeafSize(const int max_values = kNodeValues) {
|
|
525
|
+
return LeafLayout(max_values).AllocSize();
|
|
526
|
+
}
|
|
527
|
+
constexpr static size_type InternalSize() {
|
|
528
|
+
return InternalLayout().AllocSize();
|
|
529
|
+
}
|
|
530
|
+
constexpr static size_type Alignment() {
|
|
531
|
+
static_assert(LeafLayout(1).Alignment() == InternalLayout().Alignment(),
|
|
532
|
+
"Alignment of all nodes must be equal.");
|
|
533
|
+
return InternalLayout().Alignment();
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
// N is the index of the type in the Layout definition.
|
|
537
|
+
// ElementType<N> is the Nth type in the Layout definition.
|
|
538
|
+
template <size_type N>
|
|
539
|
+
inline typename layout_type::template ElementType<N> *GetField() {
|
|
540
|
+
// We assert that we don't read from values that aren't there.
|
|
541
|
+
assert(N < 3 || !leaf());
|
|
542
|
+
return InternalLayout().template Pointer<N>(reinterpret_cast<char *>(this));
|
|
543
|
+
}
|
|
544
|
+
template <size_type N>
|
|
545
|
+
inline const typename layout_type::template ElementType<N> *GetField() const {
|
|
546
|
+
assert(N < 3 || !leaf());
|
|
547
|
+
return InternalLayout().template Pointer<N>(
|
|
548
|
+
reinterpret_cast<const char *>(this));
|
|
549
|
+
}
|
|
550
|
+
void set_parent(btree_node *p) { *GetField<0>() = p; }
|
|
551
|
+
field_type &mutable_count() { return GetField<1>()[2]; }
|
|
552
|
+
slot_type *slot(int i) { return &GetField<2>()[i]; }
|
|
553
|
+
const slot_type *slot(int i) const { return &GetField<2>()[i]; }
|
|
554
|
+
void set_position(field_type v) { GetField<1>()[0] = v; }
|
|
555
|
+
void set_start(field_type v) { GetField<1>()[1] = v; }
|
|
556
|
+
void set_count(field_type v) { GetField<1>()[2] = v; }
|
|
557
|
+
// This method is only called by the node init methods.
|
|
558
|
+
void set_max_count(field_type v) { GetField<1>()[3] = v; }
|
|
559
|
+
|
|
560
|
+
public:
|
|
561
|
+
// Whether this is a leaf node or not. This value doesn't change after the
|
|
562
|
+
// node is created.
|
|
563
|
+
bool leaf() const { return GetField<1>()[3] != kInternalNodeMaxCount; }
|
|
564
|
+
|
|
565
|
+
// Getter for the position of this node in its parent.
|
|
566
|
+
field_type position() const { return GetField<1>()[0]; }
|
|
567
|
+
|
|
568
|
+
// Getter for the offset of the first value in the `values` array.
|
|
569
|
+
field_type start() const { return GetField<1>()[1]; }
|
|
570
|
+
|
|
571
|
+
// Getters for the number of values stored in this node.
|
|
572
|
+
field_type count() const { return GetField<1>()[2]; }
|
|
573
|
+
field_type max_count() const {
|
|
574
|
+
// Internal nodes have max_count==kInternalNodeMaxCount.
|
|
575
|
+
// Leaf nodes have max_count in [1, kNodeValues].
|
|
576
|
+
const field_type max_count = GetField<1>()[3];
|
|
577
|
+
return max_count == kInternalNodeMaxCount ? kNodeValues : max_count;
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
// Getter for the parent of this node.
|
|
581
|
+
btree_node *parent() const { return *GetField<0>(); }
|
|
582
|
+
// Getter for whether the node is the root of the tree. The parent of the
|
|
583
|
+
// root of the tree is the leftmost node in the tree which is guaranteed to
|
|
584
|
+
// be a leaf.
|
|
585
|
+
bool is_root() const { return parent()->leaf(); }
|
|
586
|
+
void make_root() {
|
|
587
|
+
assert(parent()->is_root());
|
|
588
|
+
set_parent(parent()->parent());
|
|
589
|
+
}
|
|
590
|
+
|
|
591
|
+
// Getters for the key/value at position i in the node.
|
|
592
|
+
const key_type &key(int i) const { return params_type::key(slot(i)->value); }
|
|
593
|
+
reference value(int i) { return slot(i)->value; }
|
|
594
|
+
const_reference value(int i) const { return slot(i)->value; }
|
|
595
|
+
|
|
596
|
+
// Getters/setter for the child at position i in the node.
|
|
597
|
+
btree_node *child(int i) const { return GetField<3>()[i]; }
|
|
598
|
+
btree_node *&mutable_child(int i) { return GetField<3>()[i]; }
|
|
599
|
+
void clear_child(int i) {
|
|
600
|
+
absl::container_internal::SanitizerPoisonObject(&mutable_child(i));
|
|
601
|
+
}
|
|
602
|
+
void set_child(int i, btree_node *c) {
|
|
603
|
+
absl::container_internal::SanitizerUnpoisonObject(&mutable_child(i));
|
|
604
|
+
mutable_child(i) = c;
|
|
605
|
+
c->set_position(i);
|
|
606
|
+
}
|
|
607
|
+
void init_child(int i, btree_node *c) {
|
|
608
|
+
set_child(i, c);
|
|
609
|
+
c->set_parent(this);
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
// Returns the position of the first value whose key is not less than k.
|
|
613
|
+
template <typename K>
|
|
614
|
+
int lower_bound(const K &k, const key_compare &comp) const {
|
|
615
|
+
return use_linear_search::value ? linear_search(k, comp)
|
|
616
|
+
: binary_search(k, comp);
|
|
617
|
+
}
|
|
618
|
+
// Returns the position of the first value whose key is greater than k.
|
|
619
|
+
template <typename K>
|
|
620
|
+
int upper_bound(const K &k, const key_compare &comp) const {
|
|
621
|
+
auto upper_compare = upper_bound_adapter<key_compare>(comp);
|
|
622
|
+
return use_linear_search::value ? linear_search(k, upper_compare)
|
|
623
|
+
: binary_search(k, upper_compare);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
template <typename K, typename Compare>
|
|
627
|
+
int linear_search(const K &k, const Compare &comp) const {
|
|
628
|
+
return btree_is_key_compare_to<Compare>::value
|
|
629
|
+
? linear_search_compare_to(k, 0, count(), comp)
|
|
630
|
+
: linear_search_plain_compare(k, 0, count(), comp);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
template <typename K, typename Compare>
|
|
634
|
+
int binary_search(const K &k, const Compare &comp) const {
|
|
635
|
+
return btree_is_key_compare_to<Compare>::value
|
|
636
|
+
? binary_search_compare_to(k, 0, count(), comp)
|
|
637
|
+
: binary_search_plain_compare(k, 0, count(), comp);
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
// Returns the position of the first value whose key is not less than k using
|
|
641
|
+
// linear search performed using plain compare.
|
|
642
|
+
template <typename K, typename Compare>
|
|
643
|
+
int linear_search_plain_compare(const K &k, int s, const int e,
|
|
644
|
+
const Compare &comp) const {
|
|
645
|
+
while (s < e) {
|
|
646
|
+
if (!comp(key(s), k)) {
|
|
647
|
+
break;
|
|
648
|
+
}
|
|
649
|
+
++s;
|
|
650
|
+
}
|
|
651
|
+
return s;
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
// Returns the position of the first value whose key is not less than k using
|
|
655
|
+
// linear search performed using compare-to.
|
|
656
|
+
template <typename K, typename Compare>
|
|
657
|
+
int linear_search_compare_to(const K &k, int s, const int e,
|
|
658
|
+
const Compare &comp) const {
|
|
659
|
+
while (s < e) {
|
|
660
|
+
const int c = comp(key(s), k);
|
|
661
|
+
if (c == 0) {
|
|
662
|
+
return s | kExactMatch;
|
|
663
|
+
} else if (c > 0) {
|
|
664
|
+
break;
|
|
665
|
+
}
|
|
666
|
+
++s;
|
|
667
|
+
}
|
|
668
|
+
return s;
|
|
669
|
+
}
|
|
670
|
+
|
|
671
|
+
// Returns the position of the first value whose key is not less than k using
|
|
672
|
+
// binary search performed using plain compare.
|
|
673
|
+
template <typename K, typename Compare>
|
|
674
|
+
int binary_search_plain_compare(const K &k, int s, int e,
|
|
675
|
+
const Compare &comp) const {
|
|
676
|
+
while (s != e) {
|
|
677
|
+
const int mid = (s + e) >> 1;
|
|
678
|
+
if (comp(key(mid), k)) {
|
|
679
|
+
s = mid + 1;
|
|
680
|
+
} else {
|
|
681
|
+
e = mid;
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
return s;
|
|
685
|
+
}
|
|
686
|
+
|
|
687
|
+
// Returns the position of the first value whose key is not less than k using
|
|
688
|
+
// binary search performed using compare-to.
|
|
689
|
+
template <typename K, typename CompareTo>
|
|
690
|
+
int binary_search_compare_to(
|
|
691
|
+
const K &k, int s, int e, const CompareTo &comp) const {
|
|
692
|
+
if (is_multi_container::value) {
|
|
693
|
+
int exact_match = 0;
|
|
694
|
+
while (s != e) {
|
|
695
|
+
const int mid = (s + e) >> 1;
|
|
696
|
+
const int c = comp(key(mid), k);
|
|
697
|
+
if (c < 0) {
|
|
698
|
+
s = mid + 1;
|
|
699
|
+
} else {
|
|
700
|
+
e = mid;
|
|
701
|
+
if (c == 0) {
|
|
702
|
+
// Need to return the first value whose key is not less than k,
|
|
703
|
+
// which requires continuing the binary search if this is a
|
|
704
|
+
// multi-container.
|
|
705
|
+
exact_match = kExactMatch;
|
|
706
|
+
}
|
|
707
|
+
}
|
|
708
|
+
}
|
|
709
|
+
return s | exact_match;
|
|
710
|
+
} else { // Not a multi-container.
|
|
711
|
+
while (s != e) {
|
|
712
|
+
const int mid = (s + e) >> 1;
|
|
713
|
+
const int c = comp(key(mid), k);
|
|
714
|
+
if (c < 0) {
|
|
715
|
+
s = mid + 1;
|
|
716
|
+
} else if (c > 0) {
|
|
717
|
+
e = mid;
|
|
718
|
+
} else {
|
|
719
|
+
return mid | kExactMatch;
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
return s;
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
// Emplaces a value at position i, shifting all existing values and
|
|
727
|
+
// children at positions >= i to the right by 1.
|
|
728
|
+
template <typename... Args>
|
|
729
|
+
void emplace_value(size_type i, allocator_type *alloc, Args &&... args);
|
|
730
|
+
|
|
731
|
+
// Removes the value at position i, shifting all existing values and children
|
|
732
|
+
// at positions > i to the left by 1.
|
|
733
|
+
void remove_value(int i, allocator_type *alloc);
|
|
734
|
+
|
|
735
|
+
// Rebalances a node with its right sibling.
|
|
736
|
+
void rebalance_right_to_left(int to_move, btree_node *right,
|
|
737
|
+
allocator_type *alloc);
|
|
738
|
+
void rebalance_left_to_right(int to_move, btree_node *right,
|
|
739
|
+
allocator_type *alloc);
|
|
740
|
+
|
|
741
|
+
// Splits a node, moving a portion of the node's values to its right sibling.
|
|
742
|
+
void split(int insert_position, btree_node *dest, allocator_type *alloc);
|
|
743
|
+
|
|
744
|
+
// Merges a node with its right sibling, moving all of the values and the
|
|
745
|
+
// delimiting key in the parent node onto itself.
|
|
746
|
+
void merge(btree_node *sibling, allocator_type *alloc);
|
|
747
|
+
|
|
748
|
+
// Swap the contents of "this" and "src".
|
|
749
|
+
void swap(btree_node *src, allocator_type *alloc);
|
|
750
|
+
|
|
751
|
+
// Node allocation/deletion routines.
|
|
752
|
+
static btree_node *init_leaf(btree_node *n, btree_node *parent,
|
|
753
|
+
int max_count) {
|
|
754
|
+
n->set_parent(parent);
|
|
755
|
+
n->set_position(0);
|
|
756
|
+
n->set_start(0);
|
|
757
|
+
n->set_count(0);
|
|
758
|
+
n->set_max_count(max_count);
|
|
759
|
+
absl::container_internal::SanitizerPoisonMemoryRegion(
|
|
760
|
+
n->slot(0), max_count * sizeof(slot_type));
|
|
761
|
+
return n;
|
|
762
|
+
}
|
|
763
|
+
static btree_node *init_internal(btree_node *n, btree_node *parent) {
|
|
764
|
+
init_leaf(n, parent, kNodeValues);
|
|
765
|
+
// Set `max_count` to a sentinel value to indicate that this node is
|
|
766
|
+
// internal.
|
|
767
|
+
n->set_max_count(kInternalNodeMaxCount);
|
|
768
|
+
absl::container_internal::SanitizerPoisonMemoryRegion(
|
|
769
|
+
&n->mutable_child(0), (kNodeValues + 1) * sizeof(btree_node *));
|
|
770
|
+
return n;
|
|
771
|
+
}
|
|
772
|
+
void destroy(allocator_type *alloc) {
|
|
773
|
+
for (int i = 0; i < count(); ++i) {
|
|
774
|
+
value_destroy(i, alloc);
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
public:
|
|
779
|
+
// Exposed only for tests.
|
|
780
|
+
static bool testonly_uses_linear_node_search() {
|
|
781
|
+
return use_linear_search::value;
|
|
782
|
+
}
|
|
783
|
+
|
|
784
|
+
private:
|
|
785
|
+
template <typename... Args>
|
|
786
|
+
void value_init(const size_type i, allocator_type *alloc, Args &&... args) {
|
|
787
|
+
absl::container_internal::SanitizerUnpoisonObject(slot(i));
|
|
788
|
+
slot_type::construct(alloc, slot(i), std::forward<Args>(args)...);
|
|
789
|
+
}
|
|
790
|
+
void value_destroy(const size_type i, allocator_type *alloc) {
|
|
791
|
+
slot_type::destroy(alloc, slot(i));
|
|
792
|
+
absl::container_internal::SanitizerPoisonObject(slot(i));
|
|
793
|
+
}
|
|
794
|
+
|
|
795
|
+
// Move n values starting at value i in this node into the values starting at
|
|
796
|
+
// value j in node x.
|
|
797
|
+
void uninitialized_move_n(const size_type n, const size_type i,
|
|
798
|
+
const size_type j, btree_node *x,
|
|
799
|
+
allocator_type *alloc) {
|
|
800
|
+
absl::container_internal::SanitizerUnpoisonMemoryRegion(
|
|
801
|
+
x->slot(j), n * sizeof(slot_type));
|
|
802
|
+
for (slot_type *src = slot(i), *end = src + n, *dest = x->slot(j);
|
|
803
|
+
src != end; ++src, ++dest) {
|
|
804
|
+
slot_type::construct(alloc, dest, src);
|
|
805
|
+
}
|
|
806
|
+
}
|
|
807
|
+
|
|
808
|
+
// Destroys a range of n values, starting at index i.
|
|
809
|
+
void value_destroy_n(const size_type i, const size_type n,
|
|
810
|
+
allocator_type *alloc) {
|
|
811
|
+
for (int j = 0; j < n; ++j) {
|
|
812
|
+
value_destroy(i + j, alloc);
|
|
813
|
+
}
|
|
814
|
+
}
|
|
815
|
+
|
|
816
|
+
template <typename P>
|
|
817
|
+
friend class btree;
|
|
818
|
+
friend class BtreeNodePeer;
|
|
819
|
+
};
|
|
820
|
+
|
|
821
|
+
template <typename Node, typename Reference, typename Pointer>
|
|
822
|
+
struct btree_iterator {
|
|
823
|
+
private:
|
|
824
|
+
using key_type = typename Node::key_type;
|
|
825
|
+
using size_type = typename Node::size_type;
|
|
826
|
+
using params_type = typename Node::params_type;
|
|
827
|
+
|
|
828
|
+
using node_type = Node;
|
|
829
|
+
using normal_node = typename std::remove_const<Node>::type;
|
|
830
|
+
using const_node = const Node;
|
|
831
|
+
using normal_pointer = typename params_type::pointer;
|
|
832
|
+
using normal_reference = typename params_type::reference;
|
|
833
|
+
using const_pointer = typename params_type::const_pointer;
|
|
834
|
+
using const_reference = typename params_type::const_reference;
|
|
835
|
+
|
|
836
|
+
using iterator =
|
|
837
|
+
btree_iterator<normal_node, normal_reference, normal_pointer>;
|
|
838
|
+
using const_iterator =
|
|
839
|
+
btree_iterator<const_node, const_reference, const_pointer>;
|
|
840
|
+
|
|
841
|
+
public:
|
|
842
|
+
// These aliases are public for std::iterator_traits.
|
|
843
|
+
using difference_type = typename Node::difference_type;
|
|
844
|
+
using value_type = typename params_type::value_type;
|
|
845
|
+
using pointer = Pointer;
|
|
846
|
+
using reference = Reference;
|
|
847
|
+
using iterator_category = std::bidirectional_iterator_tag;
|
|
848
|
+
|
|
849
|
+
btree_iterator() : node(nullptr), position(-1) {}
|
|
850
|
+
btree_iterator(Node *n, int p) : node(n), position(p) {}
|
|
851
|
+
|
|
852
|
+
// NOTE: this SFINAE allows for implicit conversions from iterator to
|
|
853
|
+
// const_iterator, but it specifically avoids defining copy constructors so
|
|
854
|
+
// that btree_iterator can be trivially copyable. This is for performance and
|
|
855
|
+
// binary size reasons.
|
|
856
|
+
template <
|
|
857
|
+
typename N, typename R, typename P,
|
|
858
|
+
absl::enable_if_t<
|
|
859
|
+
std::is_same<btree_iterator<N, R, P>, iterator>::value &&
|
|
860
|
+
!std::is_same<btree_iterator<N, R, P>, btree_iterator>::value,
|
|
861
|
+
int> = 0>
|
|
862
|
+
btree_iterator(const btree_iterator<N, R, P> &x) // NOLINT
|
|
863
|
+
: node(x.node), position(x.position) {}
|
|
864
|
+
|
|
865
|
+
private:
|
|
866
|
+
// Increment/decrement the iterator.
|
|
867
|
+
void increment() {
|
|
868
|
+
if (node->leaf() && ++position < node->count()) {
|
|
869
|
+
return;
|
|
870
|
+
}
|
|
871
|
+
increment_slow();
|
|
872
|
+
}
|
|
873
|
+
void increment_slow();
|
|
874
|
+
|
|
875
|
+
void decrement() {
|
|
876
|
+
if (node->leaf() && --position >= 0) {
|
|
877
|
+
return;
|
|
878
|
+
}
|
|
879
|
+
decrement_slow();
|
|
880
|
+
}
|
|
881
|
+
void decrement_slow();
|
|
882
|
+
|
|
883
|
+
public:
|
|
884
|
+
bool operator==(const const_iterator &x) const {
|
|
885
|
+
return node == x.node && position == x.position;
|
|
886
|
+
}
|
|
887
|
+
bool operator!=(const const_iterator &x) const {
|
|
888
|
+
return node != x.node || position != x.position;
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// Accessors for the key/value the iterator is pointing at.
|
|
892
|
+
reference operator*() const {
|
|
893
|
+
return node->value(position);
|
|
894
|
+
}
|
|
895
|
+
pointer operator->() const {
|
|
896
|
+
return &node->value(position);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
btree_iterator& operator++() {
|
|
900
|
+
increment();
|
|
901
|
+
return *this;
|
|
902
|
+
}
|
|
903
|
+
btree_iterator& operator--() {
|
|
904
|
+
decrement();
|
|
905
|
+
return *this;
|
|
906
|
+
}
|
|
907
|
+
btree_iterator operator++(int) {
|
|
908
|
+
btree_iterator tmp = *this;
|
|
909
|
+
++*this;
|
|
910
|
+
return tmp;
|
|
911
|
+
}
|
|
912
|
+
btree_iterator operator--(int) {
|
|
913
|
+
btree_iterator tmp = *this;
|
|
914
|
+
--*this;
|
|
915
|
+
return tmp;
|
|
916
|
+
}
|
|
917
|
+
|
|
918
|
+
private:
|
|
919
|
+
template <typename Params>
|
|
920
|
+
friend class btree;
|
|
921
|
+
template <typename N, typename R, typename P>
|
|
922
|
+
friend struct btree_iterator;
|
|
923
|
+
template <typename TreeType, typename CheckerType>
|
|
924
|
+
friend class base_checker;
|
|
925
|
+
|
|
926
|
+
const key_type &key() const { return node->key(position); }
|
|
927
|
+
|
|
928
|
+
// The node in the tree the iterator is pointing at.
|
|
929
|
+
Node *node;
|
|
930
|
+
// The position within the node of the tree the iterator is pointing at.
|
|
931
|
+
int position;
|
|
932
|
+
};
|
|
933
|
+
|
|
934
|
+
// Approximation of std::is_trivially_copyable (which is currently unsupported).
|
|
935
|
+
template <typename T>
|
|
936
|
+
using is_trivially_copyable = absl::conjunction<
|
|
937
|
+
absl::is_trivially_copy_constructible<T>,
|
|
938
|
+
absl::disjunction<absl::is_trivially_copy_assignable<T>,
|
|
939
|
+
absl::negation<std::is_copy_assignable<T>>>,
|
|
940
|
+
absl::is_trivially_destructible<T>>;
|
|
941
|
+
|
|
942
|
+
template <typename Params>
|
|
943
|
+
class btree {
|
|
944
|
+
using node_type = btree_node<Params>;
|
|
945
|
+
using is_key_compare_to = typename Params::is_key_compare_to;
|
|
946
|
+
|
|
947
|
+
template <typename K>
|
|
948
|
+
using const_lookup_key_reference = absl::conditional_t<
|
|
949
|
+
is_comparator_transparent<typename Params::key_compare>::value, const K &,
|
|
950
|
+
const typename Params::key_type &>;
|
|
951
|
+
|
|
952
|
+
enum {
|
|
953
|
+
kNodeValues = node_type::kNodeValues,
|
|
954
|
+
kMinNodeValues = kNodeValues / 2,
|
|
955
|
+
kExactMatch = node_type::kExactMatch,
|
|
956
|
+
kMatchMask = node_type::kMatchMask,
|
|
957
|
+
};
|
|
958
|
+
|
|
959
|
+
struct node_stats {
|
|
960
|
+
using size_type = typename Params::size_type;
|
|
961
|
+
|
|
962
|
+
node_stats(size_type l, size_type i)
|
|
963
|
+
: leaf_nodes(l),
|
|
964
|
+
internal_nodes(i) {
|
|
965
|
+
}
|
|
966
|
+
|
|
967
|
+
node_stats& operator+=(const node_stats &x) {
|
|
968
|
+
leaf_nodes += x.leaf_nodes;
|
|
969
|
+
internal_nodes += x.internal_nodes;
|
|
970
|
+
return *this;
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
size_type leaf_nodes;
|
|
974
|
+
size_type internal_nodes;
|
|
975
|
+
};
|
|
976
|
+
|
|
977
|
+
public:
|
|
978
|
+
using key_type = typename Params::key_type;
|
|
979
|
+
using mapped_type = typename Params::mapped_type;
|
|
980
|
+
using value_type = typename Params::value_type;
|
|
981
|
+
using size_type = typename Params::size_type;
|
|
982
|
+
using difference_type = typename Params::difference_type;
|
|
983
|
+
using key_compare = typename Params::key_compare;
|
|
984
|
+
using value_compare = typename Params::value_compare;
|
|
985
|
+
using allocator_type = typename Params::allocator_type;
|
|
986
|
+
using reference = typename Params::reference;
|
|
987
|
+
using const_reference = typename Params::const_reference;
|
|
988
|
+
using pointer = typename Params::pointer;
|
|
989
|
+
using const_pointer = typename Params::const_pointer;
|
|
990
|
+
using iterator = btree_iterator<node_type, reference, pointer>;
|
|
991
|
+
using const_iterator = typename iterator::const_iterator;
|
|
992
|
+
using reverse_iterator = std::reverse_iterator<iterator>;
|
|
993
|
+
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
|
|
994
|
+
|
|
995
|
+
// Internal types made public for use by btree_container types.
|
|
996
|
+
using params_type = Params;
|
|
997
|
+
using mutable_value_type = typename Params::mutable_value_type;
|
|
998
|
+
using slot_type = typename Params::slot_type;
|
|
999
|
+
|
|
1000
|
+
private:
|
|
1001
|
+
// Copies the values in x into this btree in their order in x.
|
|
1002
|
+
// This btree must be empty before this method is called.
|
|
1003
|
+
// This method is used in copy construction and copy assignment.
|
|
1004
|
+
void copy_values_in_order(const btree &x);
|
|
1005
|
+
|
|
1006
|
+
public:
|
|
1007
|
+
btree(const key_compare &comp, const allocator_type &alloc);
|
|
1008
|
+
|
|
1009
|
+
btree(const btree &x);
|
|
1010
|
+
btree(btree &&x) noexcept
|
|
1011
|
+
: root_(std::move(x.root_)),
|
|
1012
|
+
rightmost_(absl::exchange(x.rightmost_, nullptr)),
|
|
1013
|
+
size_(absl::exchange(x.size_, 0)) {
|
|
1014
|
+
x.mutable_root() = nullptr;
|
|
1015
|
+
}
|
|
1016
|
+
|
|
1017
|
+
~btree() {
|
|
1018
|
+
static_assert(std::is_nothrow_copy_constructible<key_compare>::value,
|
|
1019
|
+
"Key comparison must be nothrow copy constructible");
|
|
1020
|
+
static_assert(std::is_nothrow_copy_constructible<allocator_type>::value,
|
|
1021
|
+
"Allocator must be nothrow copy constructible");
|
|
1022
|
+
static_assert(is_trivially_copyable<iterator>::value,
|
|
1023
|
+
"iterator not trivially copyable.");
|
|
1024
|
+
clear();
|
|
1025
|
+
}
|
|
1026
|
+
|
|
1027
|
+
// Assign the contents of x to *this.
|
|
1028
|
+
btree &operator=(const btree &x);
|
|
1029
|
+
|
|
1030
|
+
btree &operator=(btree &&x) noexcept {
|
|
1031
|
+
clear();
|
|
1032
|
+
swap(x);
|
|
1033
|
+
return *this;
|
|
1034
|
+
}
|
|
1035
|
+
|
|
1036
|
+
iterator begin() {
|
|
1037
|
+
return iterator(leftmost(), 0);
|
|
1038
|
+
}
|
|
1039
|
+
const_iterator begin() const {
|
|
1040
|
+
return const_iterator(leftmost(), 0);
|
|
1041
|
+
}
|
|
1042
|
+
iterator end() {
|
|
1043
|
+
return iterator(rightmost_,
|
|
1044
|
+
rightmost_ != nullptr ? rightmost_->count() : 0);
|
|
1045
|
+
}
|
|
1046
|
+
const_iterator end() const {
|
|
1047
|
+
return const_iterator(rightmost_,
|
|
1048
|
+
rightmost_ != nullptr ? rightmost_->count() : 0);
|
|
1049
|
+
}
|
|
1050
|
+
reverse_iterator rbegin() {
|
|
1051
|
+
return reverse_iterator(end());
|
|
1052
|
+
}
|
|
1053
|
+
const_reverse_iterator rbegin() const {
|
|
1054
|
+
return const_reverse_iterator(end());
|
|
1055
|
+
}
|
|
1056
|
+
reverse_iterator rend() {
|
|
1057
|
+
return reverse_iterator(begin());
|
|
1058
|
+
}
|
|
1059
|
+
const_reverse_iterator rend() const {
|
|
1060
|
+
return const_reverse_iterator(begin());
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
// Finds the first element whose key is not less than key.
|
|
1064
|
+
template <typename K>
|
|
1065
|
+
iterator lower_bound(const K &key) {
|
|
1066
|
+
return internal_end(
|
|
1067
|
+
internal_lower_bound(key, iterator(root(), 0)));
|
|
1068
|
+
}
|
|
1069
|
+
template <typename K>
|
|
1070
|
+
const_iterator lower_bound(const K &key) const {
|
|
1071
|
+
return internal_end(
|
|
1072
|
+
internal_lower_bound(key, const_iterator(root(), 0)));
|
|
1073
|
+
}
|
|
1074
|
+
|
|
1075
|
+
// Finds the first element whose key is greater than key.
|
|
1076
|
+
template <typename K>
|
|
1077
|
+
iterator upper_bound(const K &key) {
|
|
1078
|
+
return internal_end(
|
|
1079
|
+
internal_upper_bound(key, iterator(root(), 0)));
|
|
1080
|
+
}
|
|
1081
|
+
template <typename K>
|
|
1082
|
+
const_iterator upper_bound(const K &key) const {
|
|
1083
|
+
return internal_end(
|
|
1084
|
+
internal_upper_bound(key, const_iterator(root(), 0)));
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
// Finds the range of values which compare equal to key. The first member of
|
|
1088
|
+
// the returned pair is equal to lower_bound(key). The second member pair of
|
|
1089
|
+
// the pair is equal to upper_bound(key).
|
|
1090
|
+
template <typename K>
|
|
1091
|
+
std::pair<iterator, iterator> equal_range(const K &key) {
|
|
1092
|
+
const_lookup_key_reference<K> lookup_key(key);
|
|
1093
|
+
return std::make_pair(lower_bound(lookup_key), upper_bound(lookup_key));
|
|
1094
|
+
}
|
|
1095
|
+
template <typename K>
|
|
1096
|
+
std::pair<const_iterator, const_iterator> equal_range(const K &key) const {
|
|
1097
|
+
const_lookup_key_reference<K> lookup_key(key);
|
|
1098
|
+
return std::make_pair(lower_bound(lookup_key), upper_bound(lookup_key));
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
// Inserts a value into the btree only if it does not already exist. The
|
|
1102
|
+
// boolean return value indicates whether insertion succeeded or failed.
|
|
1103
|
+
template <typename... Args>
|
|
1104
|
+
std::pair<iterator, bool> insert_unique(const key_type &key, Args &&... args);
|
|
1105
|
+
|
|
1106
|
+
// Insert with hint. Check to see if the value should be placed immediately
|
|
1107
|
+
// before position in the tree. If it does, then the insertion will take
|
|
1108
|
+
// amortized constant time. If not, the insertion will take amortized
|
|
1109
|
+
// logarithmic time as if a call to insert_unique(v) were made.
|
|
1110
|
+
template <typename... Args>
|
|
1111
|
+
iterator insert_hint_unique(iterator position, const key_type &key,
|
|
1112
|
+
Args &&... args);
|
|
1113
|
+
|
|
1114
|
+
// Insert a range of values into the btree.
|
|
1115
|
+
template <typename InputIterator>
|
|
1116
|
+
void insert_iterator_unique(InputIterator b, InputIterator e);
|
|
1117
|
+
|
|
1118
|
+
// Inserts a value into the btree.
|
|
1119
|
+
template <typename ValueType>
|
|
1120
|
+
iterator insert_multi(const key_type &key, ValueType &&v);
|
|
1121
|
+
|
|
1122
|
+
// Inserts a value into the btree.
|
|
1123
|
+
template <typename ValueType>
|
|
1124
|
+
iterator insert_multi(ValueType &&v) {
|
|
1125
|
+
return insert_multi(params_type::key(v), std::forward<ValueType>(v));
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
// Insert with hint. Check to see if the value should be placed immediately
|
|
1129
|
+
// before position in the tree. If it does, then the insertion will take
|
|
1130
|
+
// amortized constant time. If not, the insertion will take amortized
|
|
1131
|
+
// logarithmic time as if a call to insert_multi(v) were made.
|
|
1132
|
+
template <typename ValueType>
|
|
1133
|
+
iterator insert_hint_multi(iterator position, ValueType &&v);
|
|
1134
|
+
|
|
1135
|
+
// Insert a range of values into the btree.
|
|
1136
|
+
template <typename InputIterator>
|
|
1137
|
+
void insert_iterator_multi(InputIterator b, InputIterator e);
|
|
1138
|
+
|
|
1139
|
+
// Erase the specified iterator from the btree. The iterator must be valid
|
|
1140
|
+
// (i.e. not equal to end()). Return an iterator pointing to the node after
|
|
1141
|
+
// the one that was erased (or end() if none exists).
|
|
1142
|
+
iterator erase(iterator iter);
|
|
1143
|
+
|
|
1144
|
+
// Erases range. Returns the number of keys erased.
|
|
1145
|
+
int erase(iterator begin, iterator end);
|
|
1146
|
+
|
|
1147
|
+
// Erases the specified key from the btree. Returns 1 if an element was
|
|
1148
|
+
// erased and 0 otherwise.
|
|
1149
|
+
template <typename K>
|
|
1150
|
+
int erase_unique(const K &key);
|
|
1151
|
+
|
|
1152
|
+
// Erases all of the entries matching the specified key from the
|
|
1153
|
+
// btree. Returns the number of elements erased.
|
|
1154
|
+
template <typename K>
|
|
1155
|
+
int erase_multi(const K &key);
|
|
1156
|
+
|
|
1157
|
+
// Finds the iterator corresponding to a key or returns end() if the key is
|
|
1158
|
+
// not present.
|
|
1159
|
+
template <typename K>
|
|
1160
|
+
iterator find_unique(const K &key) {
|
|
1161
|
+
return internal_end(
|
|
1162
|
+
internal_find_unique(key, iterator(root(), 0)));
|
|
1163
|
+
}
|
|
1164
|
+
template <typename K>
|
|
1165
|
+
const_iterator find_unique(const K &key) const {
|
|
1166
|
+
return internal_end(
|
|
1167
|
+
internal_find_unique(key, const_iterator(root(), 0)));
|
|
1168
|
+
}
|
|
1169
|
+
template <typename K>
|
|
1170
|
+
iterator find_multi(const K &key) {
|
|
1171
|
+
return internal_end(
|
|
1172
|
+
internal_find_multi(key, iterator(root(), 0)));
|
|
1173
|
+
}
|
|
1174
|
+
template <typename K>
|
|
1175
|
+
const_iterator find_multi(const K &key) const {
|
|
1176
|
+
return internal_end(
|
|
1177
|
+
internal_find_multi(key, const_iterator(root(), 0)));
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
// Returns a count of the number of times the key appears in the btree.
|
|
1181
|
+
template <typename K>
|
|
1182
|
+
size_type count_unique(const K &key) const {
|
|
1183
|
+
const_iterator begin = internal_find_unique(
|
|
1184
|
+
key, const_iterator(root(), 0));
|
|
1185
|
+
if (!begin.node) {
|
|
1186
|
+
// The key doesn't exist in the tree.
|
|
1187
|
+
return 0;
|
|
1188
|
+
}
|
|
1189
|
+
return 1;
|
|
1190
|
+
}
|
|
1191
|
+
// Returns a count of the number of times the key appears in the btree.
|
|
1192
|
+
template <typename K>
|
|
1193
|
+
size_type count_multi(const K &key) const {
|
|
1194
|
+
const auto range = equal_range(key);
|
|
1195
|
+
return std::distance(range.first, range.second);
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1198
|
+
// Clear the btree, deleting all of the values it contains.
|
|
1199
|
+
void clear();
|
|
1200
|
+
|
|
1201
|
+
// Swap the contents of *this and x.
|
|
1202
|
+
void swap(btree &x);
|
|
1203
|
+
|
|
1204
|
+
const key_compare &key_comp() const noexcept {
|
|
1205
|
+
return root_.template get<0>();
|
|
1206
|
+
}
|
|
1207
|
+
template <typename K, typename LK>
|
|
1208
|
+
bool compare_keys(const K &x, const LK &y) const {
|
|
1209
|
+
return bool_compare_keys(key_comp(), x, y);
|
|
1210
|
+
}
|
|
1211
|
+
|
|
1212
|
+
value_compare value_comp() const { return value_compare(key_comp()); }
|
|
1213
|
+
|
|
1214
|
+
// Verifies the structure of the btree.
|
|
1215
|
+
void verify() const;
|
|
1216
|
+
|
|
1217
|
+
// Size routines.
|
|
1218
|
+
size_type size() const { return size_; }
|
|
1219
|
+
size_type max_size() const { return std::numeric_limits<size_type>::max(); }
|
|
1220
|
+
bool empty() const { return size_ == 0; }
|
|
1221
|
+
|
|
1222
|
+
// The height of the btree. An empty tree will have height 0.
|
|
1223
|
+
size_type height() const {
|
|
1224
|
+
size_type h = 0;
|
|
1225
|
+
if (root()) {
|
|
1226
|
+
// Count the length of the chain from the leftmost node up to the
|
|
1227
|
+
// root. We actually count from the root back around to the level below
|
|
1228
|
+
// the root, but the calculation is the same because of the circularity
|
|
1229
|
+
// of that traversal.
|
|
1230
|
+
const node_type *n = root();
|
|
1231
|
+
do {
|
|
1232
|
+
++h;
|
|
1233
|
+
n = n->parent();
|
|
1234
|
+
} while (n != root());
|
|
1235
|
+
}
|
|
1236
|
+
return h;
|
|
1237
|
+
}
|
|
1238
|
+
|
|
1239
|
+
// The number of internal, leaf and total nodes used by the btree.
|
|
1240
|
+
size_type leaf_nodes() const {
|
|
1241
|
+
return internal_stats(root()).leaf_nodes;
|
|
1242
|
+
}
|
|
1243
|
+
size_type internal_nodes() const {
|
|
1244
|
+
return internal_stats(root()).internal_nodes;
|
|
1245
|
+
}
|
|
1246
|
+
size_type nodes() const {
|
|
1247
|
+
node_stats stats = internal_stats(root());
|
|
1248
|
+
return stats.leaf_nodes + stats.internal_nodes;
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
// The total number of bytes used by the btree.
|
|
1252
|
+
size_type bytes_used() const {
|
|
1253
|
+
node_stats stats = internal_stats(root());
|
|
1254
|
+
if (stats.leaf_nodes == 1 && stats.internal_nodes == 0) {
|
|
1255
|
+
return sizeof(*this) +
|
|
1256
|
+
node_type::LeafSize(root()->max_count());
|
|
1257
|
+
} else {
|
|
1258
|
+
return sizeof(*this) +
|
|
1259
|
+
stats.leaf_nodes * node_type::LeafSize() +
|
|
1260
|
+
stats.internal_nodes * node_type::InternalSize();
|
|
1261
|
+
}
|
|
1262
|
+
}
|
|
1263
|
+
|
|
1264
|
+
// The average number of bytes used per value stored in the btree.
|
|
1265
|
+
static double average_bytes_per_value() {
|
|
1266
|
+
// Returns the number of bytes per value on a leaf node that is 75%
|
|
1267
|
+
// full. Experimentally, this matches up nicely with the computed number of
|
|
1268
|
+
// bytes per value in trees that had their values inserted in random order.
|
|
1269
|
+
return node_type::LeafSize() / (kNodeValues * 0.75);
|
|
1270
|
+
}
|
|
1271
|
+
|
|
1272
|
+
// The fullness of the btree. Computed as the number of elements in the btree
|
|
1273
|
+
// divided by the maximum number of elements a tree with the current number
|
|
1274
|
+
// of nodes could hold. A value of 1 indicates perfect space
|
|
1275
|
+
// utilization. Smaller values indicate space wastage.
|
|
1276
|
+
double fullness() const {
|
|
1277
|
+
return static_cast<double>(size()) / (nodes() * kNodeValues);
|
|
1278
|
+
}
|
|
1279
|
+
// The overhead of the btree structure in bytes per node. Computed as the
|
|
1280
|
+
// total number of bytes used by the btree minus the number of bytes used for
|
|
1281
|
+
// storing elements divided by the number of elements.
|
|
1282
|
+
double overhead() const {
|
|
1283
|
+
if (empty()) {
|
|
1284
|
+
return 0.0;
|
|
1285
|
+
}
|
|
1286
|
+
return (bytes_used() - size() * sizeof(value_type)) /
|
|
1287
|
+
static_cast<double>(size());
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
// The allocator used by the btree.
|
|
1291
|
+
allocator_type get_allocator() const {
|
|
1292
|
+
return allocator();
|
|
1293
|
+
}
|
|
1294
|
+
|
|
1295
|
+
private:
|
|
1296
|
+
// Internal accessor routines.
|
|
1297
|
+
node_type *root() { return root_.template get<2>(); }
|
|
1298
|
+
const node_type *root() const { return root_.template get<2>(); }
|
|
1299
|
+
node_type *&mutable_root() noexcept { return root_.template get<2>(); }
|
|
1300
|
+
key_compare *mutable_key_comp() noexcept { return &root_.template get<0>(); }
|
|
1301
|
+
|
|
1302
|
+
// The leftmost node is stored as the parent of the root node.
|
|
1303
|
+
node_type *leftmost() { return root() ? root()->parent() : nullptr; }
|
|
1304
|
+
const node_type *leftmost() const {
|
|
1305
|
+
return root() ? root()->parent() : nullptr;
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
// Allocator routines.
|
|
1309
|
+
allocator_type *mutable_allocator() noexcept {
|
|
1310
|
+
return &root_.template get<1>();
|
|
1311
|
+
}
|
|
1312
|
+
const allocator_type &allocator() const noexcept {
|
|
1313
|
+
return root_.template get<1>();
|
|
1314
|
+
}
|
|
1315
|
+
|
|
1316
|
+
// Allocates a correctly aligned node of at least size bytes using the
|
|
1317
|
+
// allocator.
|
|
1318
|
+
node_type *allocate(const size_type size) {
|
|
1319
|
+
return reinterpret_cast<node_type *>(
|
|
1320
|
+
absl::container_internal::Allocate<node_type::Alignment()>(
|
|
1321
|
+
mutable_allocator(), size));
|
|
1322
|
+
}
|
|
1323
|
+
|
|
1324
|
+
// Node creation/deletion routines.
|
|
1325
|
+
node_type* new_internal_node(node_type *parent) {
|
|
1326
|
+
node_type *p = allocate(node_type::InternalSize());
|
|
1327
|
+
return node_type::init_internal(p, parent);
|
|
1328
|
+
}
|
|
1329
|
+
node_type* new_leaf_node(node_type *parent) {
|
|
1330
|
+
node_type *p = allocate(node_type::LeafSize());
|
|
1331
|
+
return node_type::init_leaf(p, parent, kNodeValues);
|
|
1332
|
+
}
|
|
1333
|
+
node_type *new_leaf_root_node(const int max_count) {
|
|
1334
|
+
node_type *p = allocate(node_type::LeafSize(max_count));
|
|
1335
|
+
return node_type::init_leaf(p, p, max_count);
|
|
1336
|
+
}
|
|
1337
|
+
|
|
1338
|
+
// Deallocates a node of a certain size in bytes using the allocator.
|
|
1339
|
+
void deallocate(const size_type size, node_type *node) {
|
|
1340
|
+
absl::container_internal::Deallocate<node_type::Alignment()>(
|
|
1341
|
+
mutable_allocator(), node, size);
|
|
1342
|
+
}
|
|
1343
|
+
|
|
1344
|
+
void delete_internal_node(node_type *node) {
|
|
1345
|
+
node->destroy(mutable_allocator());
|
|
1346
|
+
deallocate(node_type::InternalSize(), node);
|
|
1347
|
+
}
|
|
1348
|
+
void delete_leaf_node(node_type *node) {
|
|
1349
|
+
node->destroy(mutable_allocator());
|
|
1350
|
+
deallocate(node_type::LeafSize(node->max_count()), node);
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
// Rebalances or splits the node iter points to.
|
|
1354
|
+
void rebalance_or_split(iterator *iter);
|
|
1355
|
+
|
|
1356
|
+
// Merges the values of left, right and the delimiting key on their parent
|
|
1357
|
+
// onto left, removing the delimiting key and deleting right.
|
|
1358
|
+
void merge_nodes(node_type *left, node_type *right);
|
|
1359
|
+
|
|
1360
|
+
// Tries to merge node with its left or right sibling, and failing that,
|
|
1361
|
+
// rebalance with its left or right sibling. Returns true if a merge
|
|
1362
|
+
// occurred, at which point it is no longer valid to access node. Returns
|
|
1363
|
+
// false if no merging took place.
|
|
1364
|
+
bool try_merge_or_rebalance(iterator *iter);
|
|
1365
|
+
|
|
1366
|
+
// Tries to shrink the height of the tree by 1.
|
|
1367
|
+
void try_shrink();
|
|
1368
|
+
|
|
1369
|
+
iterator internal_end(iterator iter) {
|
|
1370
|
+
return iter.node ? iter : end();
|
|
1371
|
+
}
|
|
1372
|
+
const_iterator internal_end(const_iterator iter) const {
|
|
1373
|
+
return iter.node ? iter : end();
|
|
1374
|
+
}
|
|
1375
|
+
|
|
1376
|
+
// Emplaces a value into the btree immediately before iter. Requires that
|
|
1377
|
+
// key(v) <= iter.key() and (--iter).key() <= key(v).
|
|
1378
|
+
template <typename... Args>
|
|
1379
|
+
iterator internal_emplace(iterator iter, Args &&... args);
|
|
1380
|
+
|
|
1381
|
+
// Returns an iterator pointing to the first value >= the value "iter" is
|
|
1382
|
+
// pointing at. Note that "iter" might be pointing to an invalid location as
|
|
1383
|
+
// iter.position == iter.node->count(). This routine simply moves iter up in
|
|
1384
|
+
// the tree to a valid location.
|
|
1385
|
+
template <typename IterType>
|
|
1386
|
+
static IterType internal_last(IterType iter);
|
|
1387
|
+
|
|
1388
|
+
// Returns an iterator pointing to the leaf position at which key would
|
|
1389
|
+
// reside in the tree. We provide 2 versions of internal_locate. The first
|
|
1390
|
+
// version (internal_locate_plain_compare) always returns 0 for the second
|
|
1391
|
+
// field of the pair. The second version (internal_locate_compare_to) is for
|
|
1392
|
+
// the key-compare-to specialization and returns either kExactMatch (if the
|
|
1393
|
+
// key was found in the tree) or -kExactMatch (if it wasn't) in the second
|
|
1394
|
+
// field of the pair. The compare_to specialization allows the caller to
|
|
1395
|
+
// avoid a subsequent comparison to determine if an exact match was made,
|
|
1396
|
+
// speeding up string, cord and string_view keys.
|
|
1397
|
+
template <typename K, typename IterType>
|
|
1398
|
+
std::pair<IterType, int> internal_locate(
|
|
1399
|
+
const K &key, IterType iter) const;
|
|
1400
|
+
template <typename K, typename IterType>
|
|
1401
|
+
std::pair<IterType, int> internal_locate_plain_compare(
|
|
1402
|
+
const K &key, IterType iter) const;
|
|
1403
|
+
template <typename K, typename IterType>
|
|
1404
|
+
std::pair<IterType, int> internal_locate_compare_to(
|
|
1405
|
+
const K &key, IterType iter) const;
|
|
1406
|
+
|
|
1407
|
+
// Internal routine which implements lower_bound().
|
|
1408
|
+
template <typename K, typename IterType>
|
|
1409
|
+
IterType internal_lower_bound(
|
|
1410
|
+
const K &key, IterType iter) const;
|
|
1411
|
+
|
|
1412
|
+
// Internal routine which implements upper_bound().
|
|
1413
|
+
template <typename K, typename IterType>
|
|
1414
|
+
IterType internal_upper_bound(
|
|
1415
|
+
const K &key, IterType iter) const;
|
|
1416
|
+
|
|
1417
|
+
// Internal routine which implements find_unique().
|
|
1418
|
+
template <typename K, typename IterType>
|
|
1419
|
+
IterType internal_find_unique(
|
|
1420
|
+
const K &key, IterType iter) const;
|
|
1421
|
+
|
|
1422
|
+
// Internal routine which implements find_multi().
|
|
1423
|
+
template <typename K, typename IterType>
|
|
1424
|
+
IterType internal_find_multi(
|
|
1425
|
+
const K &key, IterType iter) const;
|
|
1426
|
+
|
|
1427
|
+
// Deletes a node and all of its children.
|
|
1428
|
+
void internal_clear(node_type *node);
|
|
1429
|
+
|
|
1430
|
+
// Verifies the tree structure of node.
|
|
1431
|
+
int internal_verify(const node_type *node,
|
|
1432
|
+
const key_type *lo, const key_type *hi) const;
|
|
1433
|
+
|
|
1434
|
+
node_stats internal_stats(const node_type *node) const {
|
|
1435
|
+
if (!node) {
|
|
1436
|
+
return node_stats(0, 0);
|
|
1437
|
+
}
|
|
1438
|
+
if (node->leaf()) {
|
|
1439
|
+
return node_stats(1, 0);
|
|
1440
|
+
}
|
|
1441
|
+
node_stats res(0, 1);
|
|
1442
|
+
for (int i = 0; i <= node->count(); ++i) {
|
|
1443
|
+
res += internal_stats(node->child(i));
|
|
1444
|
+
}
|
|
1445
|
+
return res;
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
public:
|
|
1449
|
+
// Exposed only for tests.
|
|
1450
|
+
static bool testonly_uses_linear_node_search() {
|
|
1451
|
+
return node_type::testonly_uses_linear_node_search();
|
|
1452
|
+
}
|
|
1453
|
+
|
|
1454
|
+
private:
|
|
1455
|
+
// We use compressed tuple in order to save space because key_compare and
|
|
1456
|
+
// allocator_type are usually empty.
|
|
1457
|
+
absl::container_internal::CompressedTuple<key_compare, allocator_type,
|
|
1458
|
+
node_type *>
|
|
1459
|
+
root_;
|
|
1460
|
+
|
|
1461
|
+
// A pointer to the rightmost node. Note that the leftmost node is stored as
|
|
1462
|
+
// the root's parent.
|
|
1463
|
+
node_type *rightmost_;
|
|
1464
|
+
|
|
1465
|
+
// Number of values.
|
|
1466
|
+
size_type size_;
|
|
1467
|
+
|
|
1468
|
+
// Verify that key_compare returns an int or bool, as appropriate
|
|
1469
|
+
// depending on the value of is_key_compare_to.
|
|
1470
|
+
static_assert(std::is_same<absl::result_of_t<key_compare(key_type, key_type)>,
|
|
1471
|
+
absl::conditional_t<is_key_compare_to::value, int,
|
|
1472
|
+
bool>>::value,
|
|
1473
|
+
"key comparison function must return bool");
|
|
1474
|
+
|
|
1475
|
+
// Note: We assert that kTargetValues, which is computed from
|
|
1476
|
+
// Params::kTargetNodeSize, must fit the node_type::field_type.
|
|
1477
|
+
static_assert(kNodeValues <
|
|
1478
|
+
(1 << (8 * sizeof(typename node_type::field_type))),
|
|
1479
|
+
"target node size too large");
|
|
1480
|
+
|
|
1481
|
+
// Test the assumption made in setting kNodeValueSpace.
|
|
1482
|
+
static_assert(node_type::MinimumOverhead() >= sizeof(void *) + 4,
|
|
1483
|
+
"node space assumption incorrect");
|
|
1484
|
+
};
|
|
1485
|
+
|
|
1486
|
+
////
|
|
1487
|
+
// btree_node methods
|
|
1488
|
+
template <typename P>
|
|
1489
|
+
template <typename... Args>
|
|
1490
|
+
inline void btree_node<P>::emplace_value(const size_type i,
|
|
1491
|
+
allocator_type *alloc,
|
|
1492
|
+
Args &&... args) {
|
|
1493
|
+
assert(i <= count());
|
|
1494
|
+
// Shift old values to create space for new value and then construct it in
|
|
1495
|
+
// place.
|
|
1496
|
+
if (i < count()) {
|
|
1497
|
+
value_init(count(), alloc, slot(count() - 1));
|
|
1498
|
+
for (size_type j = count() - 1; j > i; --j)
|
|
1499
|
+
slot_type::move(alloc, slot(j - 1), slot(j));
|
|
1500
|
+
value_destroy(i, alloc);
|
|
1501
|
+
}
|
|
1502
|
+
value_init(i, alloc, std::forward<Args>(args)...);
|
|
1503
|
+
set_count(count() + 1);
|
|
1504
|
+
|
|
1505
|
+
if (!leaf() && count() > i + 1) {
|
|
1506
|
+
for (int j = count(); j > i + 1; --j) {
|
|
1507
|
+
set_child(j, child(j - 1));
|
|
1508
|
+
}
|
|
1509
|
+
clear_child(i + 1);
|
|
1510
|
+
}
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
template <typename P>
|
|
1514
|
+
inline void btree_node<P>::remove_value(const int i, allocator_type *alloc) {
|
|
1515
|
+
if (!leaf() && count() > i + 1) {
|
|
1516
|
+
assert(child(i + 1)->count() == 0);
|
|
1517
|
+
for (size_type j = i + 1; j < count(); ++j) {
|
|
1518
|
+
set_child(j, child(j + 1));
|
|
1519
|
+
}
|
|
1520
|
+
clear_child(count());
|
|
1521
|
+
}
|
|
1522
|
+
|
|
1523
|
+
slot_type::move(alloc, slot(i + 1), slot(count()), slot(i));
|
|
1524
|
+
value_destroy(count() - 1, alloc);
|
|
1525
|
+
set_count(count() - 1);
|
|
1526
|
+
}
|
|
1527
|
+
|
|
1528
|
+
template <typename P>
|
|
1529
|
+
void btree_node<P>::rebalance_right_to_left(const int to_move,
|
|
1530
|
+
btree_node *right,
|
|
1531
|
+
allocator_type *alloc) {
|
|
1532
|
+
assert(parent() == right->parent());
|
|
1533
|
+
assert(position() + 1 == right->position());
|
|
1534
|
+
assert(right->count() >= count());
|
|
1535
|
+
assert(to_move >= 1);
|
|
1536
|
+
assert(to_move <= right->count());
|
|
1537
|
+
|
|
1538
|
+
// 1) Move the delimiting value in the parent to the left node.
|
|
1539
|
+
value_init(count(), alloc, parent()->slot(position()));
|
|
1540
|
+
|
|
1541
|
+
// 2) Move the (to_move - 1) values from the right node to the left node.
|
|
1542
|
+
right->uninitialized_move_n(to_move - 1, 0, count() + 1, this, alloc);
|
|
1543
|
+
|
|
1544
|
+
// 3) Move the new delimiting value to the parent from the right node.
|
|
1545
|
+
slot_type::move(alloc, right->slot(to_move - 1), parent()->slot(position()));
|
|
1546
|
+
|
|
1547
|
+
// 4) Shift the values in the right node to their correct position.
|
|
1548
|
+
slot_type::move(alloc, right->slot(to_move), right->slot(right->count()),
|
|
1549
|
+
right->slot(0));
|
|
1550
|
+
|
|
1551
|
+
// 5) Destroy the now-empty to_move entries in the right node.
|
|
1552
|
+
right->value_destroy_n(right->count() - to_move, to_move, alloc);
|
|
1553
|
+
|
|
1554
|
+
if (!leaf()) {
|
|
1555
|
+
// Move the child pointers from the right to the left node.
|
|
1556
|
+
for (int i = 0; i < to_move; ++i) {
|
|
1557
|
+
init_child(count() + i + 1, right->child(i));
|
|
1558
|
+
}
|
|
1559
|
+
for (int i = 0; i <= right->count() - to_move; ++i) {
|
|
1560
|
+
assert(i + to_move <= right->max_count());
|
|
1561
|
+
right->init_child(i, right->child(i + to_move));
|
|
1562
|
+
right->clear_child(i + to_move);
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
|
|
1566
|
+
// Fixup the counts on the left and right nodes.
|
|
1567
|
+
set_count(count() + to_move);
|
|
1568
|
+
right->set_count(right->count() - to_move);
|
|
1569
|
+
}
|
|
1570
|
+
|
|
1571
|
+
template <typename P>
|
|
1572
|
+
void btree_node<P>::rebalance_left_to_right(const int to_move,
|
|
1573
|
+
btree_node *right,
|
|
1574
|
+
allocator_type *alloc) {
|
|
1575
|
+
assert(parent() == right->parent());
|
|
1576
|
+
assert(position() + 1 == right->position());
|
|
1577
|
+
assert(count() >= right->count());
|
|
1578
|
+
assert(to_move >= 1);
|
|
1579
|
+
assert(to_move <= count());
|
|
1580
|
+
|
|
1581
|
+
// Values in the right node are shifted to the right to make room for the
|
|
1582
|
+
// new to_move values. Then, the delimiting value in the parent and the
|
|
1583
|
+
// other (to_move - 1) values in the left node are moved into the right node.
|
|
1584
|
+
// Lastly, a new delimiting value is moved from the left node into the
|
|
1585
|
+
// parent, and the remaining empty left node entries are destroyed.
|
|
1586
|
+
|
|
1587
|
+
if (right->count() >= to_move) {
|
|
1588
|
+
// The original location of the right->count() values are sufficient to hold
|
|
1589
|
+
// the new to_move entries from the parent and left node.
|
|
1590
|
+
|
|
1591
|
+
// 1) Shift existing values in the right node to their correct positions.
|
|
1592
|
+
right->uninitialized_move_n(to_move, right->count() - to_move,
|
|
1593
|
+
right->count(), right, alloc);
|
|
1594
|
+
for (slot_type *src = right->slot(right->count() - to_move - 1),
|
|
1595
|
+
*dest = right->slot(right->count() - 1),
|
|
1596
|
+
*end = right->slot(0);
|
|
1597
|
+
src >= end; --src, --dest) {
|
|
1598
|
+
slot_type::move(alloc, src, dest);
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
// 2) Move the delimiting value in the parent to the right node.
|
|
1602
|
+
slot_type::move(alloc, parent()->slot(position()),
|
|
1603
|
+
right->slot(to_move - 1));
|
|
1604
|
+
|
|
1605
|
+
// 3) Move the (to_move - 1) values from the left node to the right node.
|
|
1606
|
+
slot_type::move(alloc, slot(count() - (to_move - 1)), slot(count()),
|
|
1607
|
+
right->slot(0));
|
|
1608
|
+
} else {
|
|
1609
|
+
// The right node does not have enough initialized space to hold the new
|
|
1610
|
+
// to_move entries, so part of them will move to uninitialized space.
|
|
1611
|
+
|
|
1612
|
+
// 1) Shift existing values in the right node to their correct positions.
|
|
1613
|
+
right->uninitialized_move_n(right->count(), 0, to_move, right, alloc);
|
|
1614
|
+
|
|
1615
|
+
// 2) Move the delimiting value in the parent to the right node.
|
|
1616
|
+
right->value_init(to_move - 1, alloc, parent()->slot(position()));
|
|
1617
|
+
|
|
1618
|
+
// 3) Move the (to_move - 1) values from the left node to the right node.
|
|
1619
|
+
const size_type uninitialized_remaining = to_move - right->count() - 1;
|
|
1620
|
+
uninitialized_move_n(uninitialized_remaining,
|
|
1621
|
+
count() - uninitialized_remaining, right->count(),
|
|
1622
|
+
right, alloc);
|
|
1623
|
+
slot_type::move(alloc, slot(count() - (to_move - 1)),
|
|
1624
|
+
slot(count() - uninitialized_remaining), right->slot(0));
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
// 4) Move the new delimiting value to the parent from the left node.
|
|
1628
|
+
slot_type::move(alloc, slot(count() - to_move), parent()->slot(position()));
|
|
1629
|
+
|
|
1630
|
+
// 5) Destroy the now-empty to_move entries in the left node.
|
|
1631
|
+
value_destroy_n(count() - to_move, to_move, alloc);
|
|
1632
|
+
|
|
1633
|
+
if (!leaf()) {
|
|
1634
|
+
// Move the child pointers from the left to the right node.
|
|
1635
|
+
for (int i = right->count(); i >= 0; --i) {
|
|
1636
|
+
right->init_child(i + to_move, right->child(i));
|
|
1637
|
+
right->clear_child(i);
|
|
1638
|
+
}
|
|
1639
|
+
for (int i = 1; i <= to_move; ++i) {
|
|
1640
|
+
right->init_child(i - 1, child(count() - to_move + i));
|
|
1641
|
+
clear_child(count() - to_move + i);
|
|
1642
|
+
}
|
|
1643
|
+
}
|
|
1644
|
+
|
|
1645
|
+
// Fixup the counts on the left and right nodes.
|
|
1646
|
+
set_count(count() - to_move);
|
|
1647
|
+
right->set_count(right->count() + to_move);
|
|
1648
|
+
}
|
|
1649
|
+
|
|
1650
|
+
template <typename P>
|
|
1651
|
+
void btree_node<P>::split(const int insert_position, btree_node *dest,
|
|
1652
|
+
allocator_type *alloc) {
|
|
1653
|
+
assert(dest->count() == 0);
|
|
1654
|
+
assert(max_count() == kNodeValues);
|
|
1655
|
+
|
|
1656
|
+
// We bias the split based on the position being inserted. If we're
|
|
1657
|
+
// inserting at the beginning of the left node then bias the split to put
|
|
1658
|
+
// more values on the right node. If we're inserting at the end of the
|
|
1659
|
+
// right node then bias the split to put more values on the left node.
|
|
1660
|
+
if (insert_position == 0) {
|
|
1661
|
+
dest->set_count(count() - 1);
|
|
1662
|
+
} else if (insert_position == kNodeValues) {
|
|
1663
|
+
dest->set_count(0);
|
|
1664
|
+
} else {
|
|
1665
|
+
dest->set_count(count() / 2);
|
|
1666
|
+
}
|
|
1667
|
+
set_count(count() - dest->count());
|
|
1668
|
+
assert(count() >= 1);
|
|
1669
|
+
|
|
1670
|
+
// Move values from the left sibling to the right sibling.
|
|
1671
|
+
uninitialized_move_n(dest->count(), count(), 0, dest, alloc);
|
|
1672
|
+
|
|
1673
|
+
// Destroy the now-empty entries in the left node.
|
|
1674
|
+
value_destroy_n(count(), dest->count(), alloc);
|
|
1675
|
+
|
|
1676
|
+
// The split key is the largest value in the left sibling.
|
|
1677
|
+
set_count(count() - 1);
|
|
1678
|
+
parent()->emplace_value(position(), alloc, slot(count()));
|
|
1679
|
+
value_destroy(count(), alloc);
|
|
1680
|
+
parent()->init_child(position() + 1, dest);
|
|
1681
|
+
|
|
1682
|
+
if (!leaf()) {
|
|
1683
|
+
for (int i = 0; i <= dest->count(); ++i) {
|
|
1684
|
+
assert(child(count() + i + 1) != nullptr);
|
|
1685
|
+
dest->init_child(i, child(count() + i + 1));
|
|
1686
|
+
clear_child(count() + i + 1);
|
|
1687
|
+
}
|
|
1688
|
+
}
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
template <typename P>
|
|
1692
|
+
void btree_node<P>::merge(btree_node *src, allocator_type *alloc) {
|
|
1693
|
+
assert(parent() == src->parent());
|
|
1694
|
+
assert(position() + 1 == src->position());
|
|
1695
|
+
|
|
1696
|
+
// Move the delimiting value to the left node.
|
|
1697
|
+
value_init(count(), alloc, parent()->slot(position()));
|
|
1698
|
+
|
|
1699
|
+
// Move the values from the right to the left node.
|
|
1700
|
+
src->uninitialized_move_n(src->count(), 0, count() + 1, this, alloc);
|
|
1701
|
+
|
|
1702
|
+
// Destroy the now-empty entries in the right node.
|
|
1703
|
+
src->value_destroy_n(0, src->count(), alloc);
|
|
1704
|
+
|
|
1705
|
+
if (!leaf()) {
|
|
1706
|
+
// Move the child pointers from the right to the left node.
|
|
1707
|
+
for (int i = 0; i <= src->count(); ++i) {
|
|
1708
|
+
init_child(count() + i + 1, src->child(i));
|
|
1709
|
+
src->clear_child(i);
|
|
1710
|
+
}
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
// Fixup the counts on the src and dest nodes.
|
|
1714
|
+
set_count(1 + count() + src->count());
|
|
1715
|
+
src->set_count(0);
|
|
1716
|
+
|
|
1717
|
+
// Remove the value on the parent node.
|
|
1718
|
+
parent()->remove_value(position(), alloc);
|
|
1719
|
+
}
|
|
1720
|
+
|
|
1721
|
+
template <typename P>
|
|
1722
|
+
void btree_node<P>::swap(btree_node *x, allocator_type *alloc) {
|
|
1723
|
+
using std::swap;
|
|
1724
|
+
assert(leaf() == x->leaf());
|
|
1725
|
+
|
|
1726
|
+
// Determine which is the smaller/larger node.
|
|
1727
|
+
btree_node *smaller = this, *larger = x;
|
|
1728
|
+
if (smaller->count() > larger->count()) {
|
|
1729
|
+
swap(smaller, larger);
|
|
1730
|
+
}
|
|
1731
|
+
|
|
1732
|
+
// Swap the values.
|
|
1733
|
+
for (slot_type *a = smaller->slot(0), *b = larger->slot(0),
|
|
1734
|
+
*end = a + smaller->count();
|
|
1735
|
+
a != end; ++a, ++b) {
|
|
1736
|
+
slot_type::swap(alloc, a, b);
|
|
1737
|
+
}
|
|
1738
|
+
|
|
1739
|
+
// Move values that can't be swapped.
|
|
1740
|
+
const size_type to_move = larger->count() - smaller->count();
|
|
1741
|
+
larger->uninitialized_move_n(to_move, smaller->count(), smaller->count(),
|
|
1742
|
+
smaller, alloc);
|
|
1743
|
+
larger->value_destroy_n(smaller->count(), to_move, alloc);
|
|
1744
|
+
|
|
1745
|
+
if (!leaf()) {
|
|
1746
|
+
// Swap the child pointers.
|
|
1747
|
+
std::swap_ranges(&smaller->mutable_child(0),
|
|
1748
|
+
&smaller->mutable_child(smaller->count() + 1),
|
|
1749
|
+
&larger->mutable_child(0));
|
|
1750
|
+
// Update swapped children's parent pointers.
|
|
1751
|
+
int i = 0;
|
|
1752
|
+
for (; i <= smaller->count(); ++i) {
|
|
1753
|
+
smaller->child(i)->set_parent(smaller);
|
|
1754
|
+
larger->child(i)->set_parent(larger);
|
|
1755
|
+
}
|
|
1756
|
+
// Move the child pointers that couldn't be swapped.
|
|
1757
|
+
for (; i <= larger->count(); ++i) {
|
|
1758
|
+
smaller->init_child(i, larger->child(i));
|
|
1759
|
+
larger->clear_child(i);
|
|
1760
|
+
}
|
|
1761
|
+
}
|
|
1762
|
+
|
|
1763
|
+
// Swap the counts.
|
|
1764
|
+
swap(mutable_count(), x->mutable_count());
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
////
|
|
1768
|
+
// btree_iterator methods
|
|
1769
|
+
template <typename N, typename R, typename P>
|
|
1770
|
+
void btree_iterator<N, R, P>::increment_slow() {
|
|
1771
|
+
if (node->leaf()) {
|
|
1772
|
+
assert(position >= node->count());
|
|
1773
|
+
btree_iterator save(*this);
|
|
1774
|
+
while (position == node->count() && !node->is_root()) {
|
|
1775
|
+
assert(node->parent()->child(node->position()) == node);
|
|
1776
|
+
position = node->position();
|
|
1777
|
+
node = node->parent();
|
|
1778
|
+
}
|
|
1779
|
+
if (position == node->count()) {
|
|
1780
|
+
*this = save;
|
|
1781
|
+
}
|
|
1782
|
+
} else {
|
|
1783
|
+
assert(position < node->count());
|
|
1784
|
+
node = node->child(position + 1);
|
|
1785
|
+
while (!node->leaf()) {
|
|
1786
|
+
node = node->child(0);
|
|
1787
|
+
}
|
|
1788
|
+
position = 0;
|
|
1789
|
+
}
|
|
1790
|
+
}
|
|
1791
|
+
|
|
1792
|
+
template <typename N, typename R, typename P>
|
|
1793
|
+
void btree_iterator<N, R, P>::decrement_slow() {
|
|
1794
|
+
if (node->leaf()) {
|
|
1795
|
+
assert(position <= -1);
|
|
1796
|
+
btree_iterator save(*this);
|
|
1797
|
+
while (position < 0 && !node->is_root()) {
|
|
1798
|
+
assert(node->parent()->child(node->position()) == node);
|
|
1799
|
+
position = node->position() - 1;
|
|
1800
|
+
node = node->parent();
|
|
1801
|
+
}
|
|
1802
|
+
if (position < 0) {
|
|
1803
|
+
*this = save;
|
|
1804
|
+
}
|
|
1805
|
+
} else {
|
|
1806
|
+
assert(position >= 0);
|
|
1807
|
+
node = node->child(position);
|
|
1808
|
+
while (!node->leaf()) {
|
|
1809
|
+
node = node->child(node->count());
|
|
1810
|
+
}
|
|
1811
|
+
position = node->count() - 1;
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
|
|
1815
|
+
////
|
|
1816
|
+
// btree methods
|
|
1817
|
+
template <typename P>
|
|
1818
|
+
void btree<P>::copy_values_in_order(const btree &x) {
|
|
1819
|
+
assert(empty());
|
|
1820
|
+
|
|
1821
|
+
// We can avoid key comparisons because we know the order of the
|
|
1822
|
+
// values is the same order we'll store them in.
|
|
1823
|
+
const_iterator iter = x.begin();
|
|
1824
|
+
if (iter == x.end()) return;
|
|
1825
|
+
insert_multi(*iter);
|
|
1826
|
+
++iter;
|
|
1827
|
+
for (; iter != x.end(); ++iter) {
|
|
1828
|
+
// If the btree is not empty, we can just insert the new value at the end
|
|
1829
|
+
// of the tree!
|
|
1830
|
+
internal_emplace(end(), *iter);
|
|
1831
|
+
}
|
|
1832
|
+
}
|
|
1833
|
+
|
|
1834
|
+
template <typename P>
|
|
1835
|
+
btree<P>::btree(const key_compare &comp, const allocator_type &alloc)
|
|
1836
|
+
: root_(comp, alloc, nullptr), rightmost_(nullptr), size_(0) {}
|
|
1837
|
+
|
|
1838
|
+
template <typename P>
|
|
1839
|
+
btree<P>::btree(const btree &x) : btree(x.key_comp(), x.allocator()) {
|
|
1840
|
+
copy_values_in_order(x);
|
|
1841
|
+
}
|
|
1842
|
+
|
|
1843
|
+
template <typename P>
|
|
1844
|
+
template <typename... Args>
|
|
1845
|
+
auto btree<P>::insert_unique(const key_type &key, Args &&... args)
|
|
1846
|
+
-> std::pair<iterator, bool> {
|
|
1847
|
+
if (empty()) {
|
|
1848
|
+
mutable_root() = rightmost_ = new_leaf_root_node(1);
|
|
1849
|
+
}
|
|
1850
|
+
|
|
1851
|
+
std::pair<iterator, int> res = internal_locate(key, iterator(root(), 0));
|
|
1852
|
+
iterator &iter = res.first;
|
|
1853
|
+
if (res.second == kExactMatch) {
|
|
1854
|
+
// The key already exists in the tree, do nothing.
|
|
1855
|
+
return std::make_pair(internal_last(iter), false);
|
|
1856
|
+
} else if (!res.second) {
|
|
1857
|
+
iterator last = internal_last(iter);
|
|
1858
|
+
if (last.node && !compare_keys(key, last.key())) {
|
|
1859
|
+
// The key already exists in the tree, do nothing.
|
|
1860
|
+
return std::make_pair(last, false);
|
|
1861
|
+
}
|
|
1862
|
+
}
|
|
1863
|
+
|
|
1864
|
+
return std::make_pair(internal_emplace(iter, std::forward<Args>(args)...),
|
|
1865
|
+
true);
|
|
1866
|
+
}
|
|
1867
|
+
|
|
1868
|
+
template <typename P>
|
|
1869
|
+
template <typename... Args>
|
|
1870
|
+
inline auto btree<P>::insert_hint_unique(iterator position, const key_type &key,
|
|
1871
|
+
Args &&... args) -> iterator {
|
|
1872
|
+
if (!empty()) {
|
|
1873
|
+
if (position == end() || compare_keys(key, position.key())) {
|
|
1874
|
+
iterator prev = position;
|
|
1875
|
+
if (position == begin() || compare_keys((--prev).key(), key)) {
|
|
1876
|
+
// prev.key() < key < position.key()
|
|
1877
|
+
return internal_emplace(position, std::forward<Args>(args)...);
|
|
1878
|
+
}
|
|
1879
|
+
} else if (compare_keys(position.key(), key)) {
|
|
1880
|
+
iterator next = position;
|
|
1881
|
+
++next;
|
|
1882
|
+
if (next == end() || compare_keys(key, next.key())) {
|
|
1883
|
+
// position.key() < key < next.key()
|
|
1884
|
+
return internal_emplace(next, std::forward<Args>(args)...);
|
|
1885
|
+
}
|
|
1886
|
+
} else {
|
|
1887
|
+
// position.key() == key
|
|
1888
|
+
return position;
|
|
1889
|
+
}
|
|
1890
|
+
}
|
|
1891
|
+
return insert_unique(key, std::forward<Args>(args)...).first;
|
|
1892
|
+
}
|
|
1893
|
+
|
|
1894
|
+
template <typename P>
|
|
1895
|
+
template <typename InputIterator>
|
|
1896
|
+
void btree<P>::insert_iterator_unique(InputIterator b, InputIterator e) {
|
|
1897
|
+
for (; b != e; ++b) {
|
|
1898
|
+
insert_hint_unique(end(), params_type::key(*b), *b);
|
|
1899
|
+
}
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
template <typename P>
|
|
1903
|
+
template <typename ValueType>
|
|
1904
|
+
auto btree<P>::insert_multi(const key_type &key, ValueType &&v) -> iterator {
|
|
1905
|
+
if (empty()) {
|
|
1906
|
+
mutable_root() = rightmost_ = new_leaf_root_node(1);
|
|
1907
|
+
}
|
|
1908
|
+
|
|
1909
|
+
iterator iter = internal_upper_bound(key, iterator(root(), 0));
|
|
1910
|
+
if (!iter.node) {
|
|
1911
|
+
iter = end();
|
|
1912
|
+
}
|
|
1913
|
+
return internal_emplace(iter, std::forward<ValueType>(v));
|
|
1914
|
+
}
|
|
1915
|
+
|
|
1916
|
+
template <typename P>
|
|
1917
|
+
template <typename ValueType>
|
|
1918
|
+
auto btree<P>::insert_hint_multi(iterator position, ValueType &&v) -> iterator {
|
|
1919
|
+
if (!empty()) {
|
|
1920
|
+
const key_type &key = params_type::key(v);
|
|
1921
|
+
if (position == end() || !compare_keys(position.key(), key)) {
|
|
1922
|
+
iterator prev = position;
|
|
1923
|
+
if (position == begin() || !compare_keys(key, (--prev).key())) {
|
|
1924
|
+
// prev.key() <= key <= position.key()
|
|
1925
|
+
return internal_emplace(position, std::forward<ValueType>(v));
|
|
1926
|
+
}
|
|
1927
|
+
} else {
|
|
1928
|
+
iterator next = position;
|
|
1929
|
+
++next;
|
|
1930
|
+
if (next == end() || !compare_keys(next.key(), key)) {
|
|
1931
|
+
// position.key() < key <= next.key()
|
|
1932
|
+
return internal_emplace(next, std::forward<ValueType>(v));
|
|
1933
|
+
}
|
|
1934
|
+
}
|
|
1935
|
+
}
|
|
1936
|
+
return insert_multi(std::forward<ValueType>(v));
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
template <typename P>
|
|
1940
|
+
template <typename InputIterator>
|
|
1941
|
+
void btree<P>::insert_iterator_multi(InputIterator b, InputIterator e) {
|
|
1942
|
+
for (; b != e; ++b) {
|
|
1943
|
+
insert_hint_multi(end(), *b);
|
|
1944
|
+
}
|
|
1945
|
+
}
|
|
1946
|
+
|
|
1947
|
+
template <typename P>
|
|
1948
|
+
auto btree<P>::operator=(const btree &x) -> btree & {
|
|
1949
|
+
if (this != &x) {
|
|
1950
|
+
clear();
|
|
1951
|
+
|
|
1952
|
+
*mutable_key_comp() = x.key_comp();
|
|
1953
|
+
*mutable_allocator() = x.allocator();
|
|
1954
|
+
|
|
1955
|
+
copy_values_in_order(x);
|
|
1956
|
+
}
|
|
1957
|
+
return *this;
|
|
1958
|
+
}
|
|
1959
|
+
|
|
1960
|
+
template <typename P>
|
|
1961
|
+
auto btree<P>::erase(iterator iter) -> iterator {
|
|
1962
|
+
bool internal_delete = false;
|
|
1963
|
+
if (!iter.node->leaf()) {
|
|
1964
|
+
// Deletion of a value on an internal node. First, move the largest value
|
|
1965
|
+
// from our left child here, then delete that position (in remove_value()
|
|
1966
|
+
// below). We can get to the largest value from our left child by
|
|
1967
|
+
// decrementing iter.
|
|
1968
|
+
iterator internal_iter(iter);
|
|
1969
|
+
--iter;
|
|
1970
|
+
assert(iter.node->leaf());
|
|
1971
|
+
assert(!compare_keys(internal_iter.key(), iter.key()));
|
|
1972
|
+
slot_type::move(mutable_allocator(), iter.node->slot(iter.position),
|
|
1973
|
+
internal_iter.node->slot(internal_iter.position));
|
|
1974
|
+
internal_delete = true;
|
|
1975
|
+
}
|
|
1976
|
+
|
|
1977
|
+
// Delete the key from the leaf.
|
|
1978
|
+
iter.node->remove_value(iter.position, mutable_allocator());
|
|
1979
|
+
--size_;
|
|
1980
|
+
|
|
1981
|
+
// We want to return the next value after the one we just erased. If we
|
|
1982
|
+
// erased from an internal node (internal_delete == true), then the next
|
|
1983
|
+
// value is ++(++iter). If we erased from a leaf node (internal_delete ==
|
|
1984
|
+
// false) then the next value is ++iter. Note that ++iter may point to an
|
|
1985
|
+
// internal node and the value in the internal node may move to a leaf node
|
|
1986
|
+
// (iter.node) when rebalancing is performed at the leaf level.
|
|
1987
|
+
|
|
1988
|
+
// Merge/rebalance as we walk back up the tree.
|
|
1989
|
+
iterator res(iter);
|
|
1990
|
+
for (;;) {
|
|
1991
|
+
if (iter.node == root()) {
|
|
1992
|
+
try_shrink();
|
|
1993
|
+
if (empty()) {
|
|
1994
|
+
return end();
|
|
1995
|
+
}
|
|
1996
|
+
break;
|
|
1997
|
+
}
|
|
1998
|
+
if (iter.node->count() >= kMinNodeValues) {
|
|
1999
|
+
break;
|
|
2000
|
+
}
|
|
2001
|
+
bool merged = try_merge_or_rebalance(&iter);
|
|
2002
|
+
if (iter.node->leaf()) {
|
|
2003
|
+
res = iter;
|
|
2004
|
+
}
|
|
2005
|
+
if (!merged) {
|
|
2006
|
+
break;
|
|
2007
|
+
}
|
|
2008
|
+
iter.node = iter.node->parent();
|
|
2009
|
+
}
|
|
2010
|
+
|
|
2011
|
+
// Adjust our return value. If we're pointing at the end of a node, advance
|
|
2012
|
+
// the iterator.
|
|
2013
|
+
if (res.position == res.node->count()) {
|
|
2014
|
+
res.position = res.node->count() - 1;
|
|
2015
|
+
++res;
|
|
2016
|
+
}
|
|
2017
|
+
// If we erased from an internal node, advance the iterator.
|
|
2018
|
+
if (internal_delete) {
|
|
2019
|
+
++res;
|
|
2020
|
+
}
|
|
2021
|
+
return res;
|
|
2022
|
+
}
|
|
2023
|
+
|
|
2024
|
+
template <typename P>
|
|
2025
|
+
int btree<P>::erase(iterator begin, iterator end) {
|
|
2026
|
+
int count = std::distance(begin, end);
|
|
2027
|
+
for (int i = 0; i < count; i++) {
|
|
2028
|
+
begin = erase(begin);
|
|
2029
|
+
}
|
|
2030
|
+
return count;
|
|
2031
|
+
}
|
|
2032
|
+
|
|
2033
|
+
template <typename P> template <typename K>
|
|
2034
|
+
int btree<P>::erase_unique(const K &key) {
|
|
2035
|
+
iterator iter = internal_find_unique(key, iterator(root(), 0));
|
|
2036
|
+
if (!iter.node) {
|
|
2037
|
+
// The key doesn't exist in the tree, return nothing done.
|
|
2038
|
+
return 0;
|
|
2039
|
+
}
|
|
2040
|
+
erase(iter);
|
|
2041
|
+
return 1;
|
|
2042
|
+
}
|
|
2043
|
+
|
|
2044
|
+
template <typename P> template <typename K>
|
|
2045
|
+
int btree<P>::erase_multi(const K &key) {
|
|
2046
|
+
iterator begin = internal_lower_bound(key, iterator(root(), 0));
|
|
2047
|
+
if (!begin.node) {
|
|
2048
|
+
// The key doesn't exist in the tree, return nothing done.
|
|
2049
|
+
return 0;
|
|
2050
|
+
}
|
|
2051
|
+
// Delete all of the keys between begin and upper_bound(key).
|
|
2052
|
+
iterator end = internal_end(
|
|
2053
|
+
internal_upper_bound(key, iterator(root(), 0)));
|
|
2054
|
+
return erase(begin, end);
|
|
2055
|
+
}
|
|
2056
|
+
|
|
2057
|
+
template <typename P>
|
|
2058
|
+
void btree<P>::clear() {
|
|
2059
|
+
if (root() != nullptr) {
|
|
2060
|
+
internal_clear(root());
|
|
2061
|
+
}
|
|
2062
|
+
mutable_root() = nullptr;
|
|
2063
|
+
rightmost_ = nullptr;
|
|
2064
|
+
size_ = 0;
|
|
2065
|
+
}
|
|
2066
|
+
|
|
2067
|
+
template <typename P>
|
|
2068
|
+
void btree<P>::swap(btree &x) {
|
|
2069
|
+
using std::swap;
|
|
2070
|
+
swap(root_, x.root_);
|
|
2071
|
+
swap(rightmost_, x.rightmost_);
|
|
2072
|
+
swap(size_, x.size_);
|
|
2073
|
+
}
|
|
2074
|
+
|
|
2075
|
+
template <typename P>
|
|
2076
|
+
void btree<P>::verify() const {
|
|
2077
|
+
if (root() != nullptr) {
|
|
2078
|
+
assert(size() == internal_verify(root(), nullptr, nullptr));
|
|
2079
|
+
assert(leftmost() == (++const_iterator(root(), -1)).node);
|
|
2080
|
+
assert(rightmost_ == (--const_iterator(root(), root()->count())).node);
|
|
2081
|
+
assert(leftmost()->leaf());
|
|
2082
|
+
assert(rightmost_->leaf());
|
|
2083
|
+
} else {
|
|
2084
|
+
assert(empty());
|
|
2085
|
+
assert(leftmost() == nullptr);
|
|
2086
|
+
assert(rightmost_ == nullptr);
|
|
2087
|
+
}
|
|
2088
|
+
}
|
|
2089
|
+
|
|
2090
|
+
template <typename P>
|
|
2091
|
+
void btree<P>::rebalance_or_split(iterator *iter) {
|
|
2092
|
+
node_type *&node = iter->node;
|
|
2093
|
+
int &insert_position = iter->position;
|
|
2094
|
+
assert(node->count() == node->max_count());
|
|
2095
|
+
assert(kNodeValues == node->max_count());
|
|
2096
|
+
|
|
2097
|
+
// First try to make room on the node by rebalancing.
|
|
2098
|
+
node_type *parent = node->parent();
|
|
2099
|
+
if (node != root()) {
|
|
2100
|
+
if (node->position() > 0) {
|
|
2101
|
+
// Try rebalancing with our left sibling.
|
|
2102
|
+
node_type *left = parent->child(node->position() - 1);
|
|
2103
|
+
assert(left->max_count() == kNodeValues);
|
|
2104
|
+
if (left->count() < kNodeValues) {
|
|
2105
|
+
// We bias rebalancing based on the position being inserted. If we're
|
|
2106
|
+
// inserting at the end of the right node then we bias rebalancing to
|
|
2107
|
+
// fill up the left node.
|
|
2108
|
+
int to_move = (kNodeValues - left->count()) /
|
|
2109
|
+
(1 + (insert_position < kNodeValues));
|
|
2110
|
+
to_move = std::max(1, to_move);
|
|
2111
|
+
|
|
2112
|
+
if (((insert_position - to_move) >= 0) ||
|
|
2113
|
+
((left->count() + to_move) < kNodeValues)) {
|
|
2114
|
+
left->rebalance_right_to_left(to_move, node, mutable_allocator());
|
|
2115
|
+
|
|
2116
|
+
assert(node->max_count() - node->count() == to_move);
|
|
2117
|
+
insert_position = insert_position - to_move;
|
|
2118
|
+
if (insert_position < 0) {
|
|
2119
|
+
insert_position = insert_position + left->count() + 1;
|
|
2120
|
+
node = left;
|
|
2121
|
+
}
|
|
2122
|
+
|
|
2123
|
+
assert(node->count() < node->max_count());
|
|
2124
|
+
return;
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
}
|
|
2128
|
+
|
|
2129
|
+
if (node->position() < parent->count()) {
|
|
2130
|
+
// Try rebalancing with our right sibling.
|
|
2131
|
+
node_type *right = parent->child(node->position() + 1);
|
|
2132
|
+
assert(right->max_count() == kNodeValues);
|
|
2133
|
+
if (right->count() < kNodeValues) {
|
|
2134
|
+
// We bias rebalancing based on the position being inserted. If we're
|
|
2135
|
+
// inserting at the beginning of the left node then we bias rebalancing
|
|
2136
|
+
// to fill up the right node.
|
|
2137
|
+
int to_move =
|
|
2138
|
+
(kNodeValues - right->count()) / (1 + (insert_position > 0));
|
|
2139
|
+
to_move = std::max(1, to_move);
|
|
2140
|
+
|
|
2141
|
+
if ((insert_position <= (node->count() - to_move)) ||
|
|
2142
|
+
((right->count() + to_move) < kNodeValues)) {
|
|
2143
|
+
node->rebalance_left_to_right(to_move, right, mutable_allocator());
|
|
2144
|
+
|
|
2145
|
+
if (insert_position > node->count()) {
|
|
2146
|
+
insert_position = insert_position - node->count() - 1;
|
|
2147
|
+
node = right;
|
|
2148
|
+
}
|
|
2149
|
+
|
|
2150
|
+
assert(node->count() < node->max_count());
|
|
2151
|
+
return;
|
|
2152
|
+
}
|
|
2153
|
+
}
|
|
2154
|
+
}
|
|
2155
|
+
|
|
2156
|
+
// Rebalancing failed, make sure there is room on the parent node for a new
|
|
2157
|
+
// value.
|
|
2158
|
+
assert(parent->max_count() == kNodeValues);
|
|
2159
|
+
if (parent->count() == kNodeValues) {
|
|
2160
|
+
iterator parent_iter(node->parent(), node->position());
|
|
2161
|
+
rebalance_or_split(&parent_iter);
|
|
2162
|
+
}
|
|
2163
|
+
} else {
|
|
2164
|
+
// Rebalancing not possible because this is the root node.
|
|
2165
|
+
// Create a new root node and set the current root node as the child of the
|
|
2166
|
+
// new root.
|
|
2167
|
+
parent = new_internal_node(parent);
|
|
2168
|
+
parent->init_child(0, root());
|
|
2169
|
+
mutable_root() = parent;
|
|
2170
|
+
// If the former root was a leaf node, then it's now the rightmost node.
|
|
2171
|
+
assert(!parent->child(0)->leaf() || parent->child(0) == rightmost_);
|
|
2172
|
+
}
|
|
2173
|
+
|
|
2174
|
+
// Split the node.
|
|
2175
|
+
node_type *split_node;
|
|
2176
|
+
if (node->leaf()) {
|
|
2177
|
+
split_node = new_leaf_node(parent);
|
|
2178
|
+
node->split(insert_position, split_node, mutable_allocator());
|
|
2179
|
+
if (rightmost_ == node) rightmost_ = split_node;
|
|
2180
|
+
} else {
|
|
2181
|
+
split_node = new_internal_node(parent);
|
|
2182
|
+
node->split(insert_position, split_node, mutable_allocator());
|
|
2183
|
+
}
|
|
2184
|
+
|
|
2185
|
+
if (insert_position > node->count()) {
|
|
2186
|
+
insert_position = insert_position - node->count() - 1;
|
|
2187
|
+
node = split_node;
|
|
2188
|
+
}
|
|
2189
|
+
}
|
|
2190
|
+
|
|
2191
|
+
template <typename P>
|
|
2192
|
+
void btree<P>::merge_nodes(node_type *left, node_type *right) {
|
|
2193
|
+
left->merge(right, mutable_allocator());
|
|
2194
|
+
if (right->leaf()) {
|
|
2195
|
+
if (rightmost_ == right) rightmost_ = left;
|
|
2196
|
+
delete_leaf_node(right);
|
|
2197
|
+
} else {
|
|
2198
|
+
delete_internal_node(right);
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
|
|
2202
|
+
template <typename P>
|
|
2203
|
+
bool btree<P>::try_merge_or_rebalance(iterator *iter) {
|
|
2204
|
+
node_type *parent = iter->node->parent();
|
|
2205
|
+
if (iter->node->position() > 0) {
|
|
2206
|
+
// Try merging with our left sibling.
|
|
2207
|
+
node_type *left = parent->child(iter->node->position() - 1);
|
|
2208
|
+
assert(left->max_count() == kNodeValues);
|
|
2209
|
+
if ((1 + left->count() + iter->node->count()) <= kNodeValues) {
|
|
2210
|
+
iter->position += 1 + left->count();
|
|
2211
|
+
merge_nodes(left, iter->node);
|
|
2212
|
+
iter->node = left;
|
|
2213
|
+
return true;
|
|
2214
|
+
}
|
|
2215
|
+
}
|
|
2216
|
+
if (iter->node->position() < parent->count()) {
|
|
2217
|
+
// Try merging with our right sibling.
|
|
2218
|
+
node_type *right = parent->child(iter->node->position() + 1);
|
|
2219
|
+
assert(right->max_count() == kNodeValues);
|
|
2220
|
+
if ((1 + iter->node->count() + right->count()) <= kNodeValues) {
|
|
2221
|
+
merge_nodes(iter->node, right);
|
|
2222
|
+
return true;
|
|
2223
|
+
}
|
|
2224
|
+
// Try rebalancing with our right sibling. We don't perform rebalancing if
|
|
2225
|
+
// we deleted the first element from iter->node and the node is not
|
|
2226
|
+
// empty. This is a small optimization for the common pattern of deleting
|
|
2227
|
+
// from the front of the tree.
|
|
2228
|
+
if ((right->count() > kMinNodeValues) &&
|
|
2229
|
+
((iter->node->count() == 0) ||
|
|
2230
|
+
(iter->position > 0))) {
|
|
2231
|
+
int to_move = (right->count() - iter->node->count()) / 2;
|
|
2232
|
+
to_move = std::min(to_move, right->count() - 1);
|
|
2233
|
+
iter->node->rebalance_right_to_left(to_move, right, mutable_allocator());
|
|
2234
|
+
return false;
|
|
2235
|
+
}
|
|
2236
|
+
}
|
|
2237
|
+
if (iter->node->position() > 0) {
|
|
2238
|
+
// Try rebalancing with our left sibling. We don't perform rebalancing if
|
|
2239
|
+
// we deleted the last element from iter->node and the node is not
|
|
2240
|
+
// empty. This is a small optimization for the common pattern of deleting
|
|
2241
|
+
// from the back of the tree.
|
|
2242
|
+
node_type *left = parent->child(iter->node->position() - 1);
|
|
2243
|
+
if ((left->count() > kMinNodeValues) &&
|
|
2244
|
+
((iter->node->count() == 0) ||
|
|
2245
|
+
(iter->position < iter->node->count()))) {
|
|
2246
|
+
int to_move = (left->count() - iter->node->count()) / 2;
|
|
2247
|
+
to_move = std::min(to_move, left->count() - 1);
|
|
2248
|
+
left->rebalance_left_to_right(to_move, iter->node, mutable_allocator());
|
|
2249
|
+
iter->position += to_move;
|
|
2250
|
+
return false;
|
|
2251
|
+
}
|
|
2252
|
+
}
|
|
2253
|
+
return false;
|
|
2254
|
+
}
|
|
2255
|
+
|
|
2256
|
+
template <typename P>
|
|
2257
|
+
void btree<P>::try_shrink() {
|
|
2258
|
+
if (root()->count() > 0) {
|
|
2259
|
+
return;
|
|
2260
|
+
}
|
|
2261
|
+
// Deleted the last item on the root node, shrink the height of the tree.
|
|
2262
|
+
if (root()->leaf()) {
|
|
2263
|
+
assert(size() == 0);
|
|
2264
|
+
delete_leaf_node(root());
|
|
2265
|
+
mutable_root() = nullptr;
|
|
2266
|
+
rightmost_ = nullptr;
|
|
2267
|
+
} else {
|
|
2268
|
+
node_type *child = root()->child(0);
|
|
2269
|
+
child->make_root();
|
|
2270
|
+
delete_internal_node(root());
|
|
2271
|
+
mutable_root() = child;
|
|
2272
|
+
}
|
|
2273
|
+
}
|
|
2274
|
+
|
|
2275
|
+
template <typename P> template <typename IterType>
|
|
2276
|
+
inline IterType btree<P>::internal_last(IterType iter) {
|
|
2277
|
+
while (iter.node && iter.position == iter.node->count()) {
|
|
2278
|
+
iter.position = iter.node->position();
|
|
2279
|
+
iter.node = iter.node->parent();
|
|
2280
|
+
if (iter.node->leaf()) {
|
|
2281
|
+
iter.node = nullptr;
|
|
2282
|
+
}
|
|
2283
|
+
}
|
|
2284
|
+
return iter;
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
template <typename P>
|
|
2288
|
+
template <typename... Args>
|
|
2289
|
+
inline auto btree<P>::internal_emplace(iterator iter, Args &&... args)
|
|
2290
|
+
-> iterator {
|
|
2291
|
+
if (!iter.node->leaf()) {
|
|
2292
|
+
// We can't insert on an internal node. Instead, we'll insert after the
|
|
2293
|
+
// previous value which is guaranteed to be on a leaf node.
|
|
2294
|
+
--iter;
|
|
2295
|
+
++iter.position;
|
|
2296
|
+
}
|
|
2297
|
+
const int max_count = iter.node->max_count();
|
|
2298
|
+
if (iter.node->count() == max_count) {
|
|
2299
|
+
// Make room in the leaf for the new item.
|
|
2300
|
+
if (max_count < kNodeValues) {
|
|
2301
|
+
// Insertion into the root where the root is smaller than the full node
|
|
2302
|
+
// size. Simply grow the size of the root node.
|
|
2303
|
+
assert(iter.node == root());
|
|
2304
|
+
iter.node = new_leaf_root_node(std::min<int>(kNodeValues, 2 * max_count));
|
|
2305
|
+
iter.node->swap(root(), mutable_allocator());
|
|
2306
|
+
delete_leaf_node(root());
|
|
2307
|
+
mutable_root() = iter.node;
|
|
2308
|
+
rightmost_ = iter.node;
|
|
2309
|
+
} else {
|
|
2310
|
+
rebalance_or_split(&iter);
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
iter.node->emplace_value(iter.position, mutable_allocator(),
|
|
2314
|
+
std::forward<Args>(args)...);
|
|
2315
|
+
++size_;
|
|
2316
|
+
return iter;
|
|
2317
|
+
}
|
|
2318
|
+
|
|
2319
|
+
template <typename P> template <typename K, typename IterType>
|
|
2320
|
+
inline std::pair<IterType, int> btree<P>::internal_locate(
|
|
2321
|
+
const K &key, IterType iter) const {
|
|
2322
|
+
return is_key_compare_to::value ? internal_locate_compare_to(key, iter)
|
|
2323
|
+
: internal_locate_plain_compare(key, iter);
|
|
2324
|
+
}
|
|
2325
|
+
|
|
2326
|
+
template <typename P> template <typename K, typename IterType>
|
|
2327
|
+
inline std::pair<IterType, int> btree<P>::internal_locate_plain_compare(
|
|
2328
|
+
const K &key, IterType iter) const {
|
|
2329
|
+
for (;;) {
|
|
2330
|
+
iter.position = iter.node->lower_bound(key, key_comp());
|
|
2331
|
+
if (iter.node->leaf()) {
|
|
2332
|
+
break;
|
|
2333
|
+
}
|
|
2334
|
+
iter.node = iter.node->child(iter.position);
|
|
2335
|
+
}
|
|
2336
|
+
return std::make_pair(iter, 0);
|
|
2337
|
+
}
|
|
2338
|
+
|
|
2339
|
+
template <typename P> template <typename K, typename IterType>
|
|
2340
|
+
inline std::pair<IterType, int> btree<P>::internal_locate_compare_to(
|
|
2341
|
+
const K &key, IterType iter) const {
|
|
2342
|
+
for (;;) {
|
|
2343
|
+
int res = iter.node->lower_bound(key, key_comp());
|
|
2344
|
+
iter.position = res & kMatchMask;
|
|
2345
|
+
if (res & kExactMatch) {
|
|
2346
|
+
return std::make_pair(iter, static_cast<int>(kExactMatch));
|
|
2347
|
+
}
|
|
2348
|
+
if (iter.node->leaf()) {
|
|
2349
|
+
break;
|
|
2350
|
+
}
|
|
2351
|
+
iter.node = iter.node->child(iter.position);
|
|
2352
|
+
}
|
|
2353
|
+
return std::make_pair(iter, -kExactMatch);
|
|
2354
|
+
}
|
|
2355
|
+
|
|
2356
|
+
template <typename P> template <typename K, typename IterType>
|
|
2357
|
+
IterType btree<P>::internal_lower_bound(
|
|
2358
|
+
const K &key, IterType iter) const {
|
|
2359
|
+
const_lookup_key_reference<K> lookup_key(key);
|
|
2360
|
+
if (iter.node) {
|
|
2361
|
+
for (;;) {
|
|
2362
|
+
iter.position =
|
|
2363
|
+
iter.node->lower_bound(lookup_key, key_comp()) & kMatchMask;
|
|
2364
|
+
if (iter.node->leaf()) {
|
|
2365
|
+
break;
|
|
2366
|
+
}
|
|
2367
|
+
iter.node = iter.node->child(iter.position);
|
|
2368
|
+
}
|
|
2369
|
+
iter = internal_last(iter);
|
|
2370
|
+
}
|
|
2371
|
+
return iter;
|
|
2372
|
+
}
|
|
2373
|
+
|
|
2374
|
+
template <typename P> template <typename K, typename IterType>
|
|
2375
|
+
IterType btree<P>::internal_upper_bound(
|
|
2376
|
+
const K &key, IterType iter) const {
|
|
2377
|
+
const_lookup_key_reference<K> lookup_key(key);
|
|
2378
|
+
if (iter.node) {
|
|
2379
|
+
for (;;) {
|
|
2380
|
+
iter.position = iter.node->upper_bound(lookup_key, key_comp());
|
|
2381
|
+
if (iter.node->leaf()) {
|
|
2382
|
+
break;
|
|
2383
|
+
}
|
|
2384
|
+
iter.node = iter.node->child(iter.position);
|
|
2385
|
+
}
|
|
2386
|
+
iter = internal_last(iter);
|
|
2387
|
+
}
|
|
2388
|
+
return iter;
|
|
2389
|
+
}
|
|
2390
|
+
|
|
2391
|
+
template <typename P> template <typename K, typename IterType>
|
|
2392
|
+
IterType btree<P>::internal_find_unique(
|
|
2393
|
+
const K &key, IterType iter) const {
|
|
2394
|
+
const_lookup_key_reference<K> lookup_key(key);
|
|
2395
|
+
if (iter.node) {
|
|
2396
|
+
std::pair<IterType, int> res = internal_locate(lookup_key, iter);
|
|
2397
|
+
if (res.second == kExactMatch) {
|
|
2398
|
+
return res.first;
|
|
2399
|
+
}
|
|
2400
|
+
if (!res.second) {
|
|
2401
|
+
iter = internal_last(res.first);
|
|
2402
|
+
if (iter.node && !compare_keys(lookup_key, iter.key())) {
|
|
2403
|
+
return iter;
|
|
2404
|
+
}
|
|
2405
|
+
}
|
|
2406
|
+
}
|
|
2407
|
+
return IterType(nullptr, 0);
|
|
2408
|
+
}
|
|
2409
|
+
|
|
2410
|
+
template <typename P> template <typename K, typename IterType>
|
|
2411
|
+
IterType btree<P>::internal_find_multi(
|
|
2412
|
+
const K &key, IterType iter) const {
|
|
2413
|
+
const_lookup_key_reference<K> lookup_key(key);
|
|
2414
|
+
if (iter.node) {
|
|
2415
|
+
iter = internal_lower_bound(lookup_key, iter);
|
|
2416
|
+
if (iter.node) {
|
|
2417
|
+
iter = internal_last(iter);
|
|
2418
|
+
if (iter.node && !compare_keys(lookup_key, iter.key())) {
|
|
2419
|
+
return iter;
|
|
2420
|
+
}
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
return IterType(nullptr, 0);
|
|
2424
|
+
}
|
|
2425
|
+
|
|
2426
|
+
template <typename P>
|
|
2427
|
+
void btree<P>::internal_clear(node_type *node) {
|
|
2428
|
+
if (!node->leaf()) {
|
|
2429
|
+
for (int i = 0; i <= node->count(); ++i) {
|
|
2430
|
+
internal_clear(node->child(i));
|
|
2431
|
+
}
|
|
2432
|
+
delete_internal_node(node);
|
|
2433
|
+
} else {
|
|
2434
|
+
delete_leaf_node(node);
|
|
2435
|
+
}
|
|
2436
|
+
}
|
|
2437
|
+
|
|
2438
|
+
template <typename P>
|
|
2439
|
+
int btree<P>::internal_verify(
|
|
2440
|
+
const node_type *node, const key_type *lo, const key_type *hi) const {
|
|
2441
|
+
assert(node->count() > 0);
|
|
2442
|
+
assert(node->count() <= node->max_count());
|
|
2443
|
+
if (lo) {
|
|
2444
|
+
assert(!compare_keys(node->key(0), *lo));
|
|
2445
|
+
}
|
|
2446
|
+
if (hi) {
|
|
2447
|
+
assert(!compare_keys(*hi, node->key(node->count() - 1)));
|
|
2448
|
+
}
|
|
2449
|
+
for (int i = 1; i < node->count(); ++i) {
|
|
2450
|
+
assert(!compare_keys(node->key(i), node->key(i - 1)));
|
|
2451
|
+
}
|
|
2452
|
+
int count = node->count();
|
|
2453
|
+
if (!node->leaf()) {
|
|
2454
|
+
for (int i = 0; i <= node->count(); ++i) {
|
|
2455
|
+
assert(node->child(i) != nullptr);
|
|
2456
|
+
assert(node->child(i)->parent() == node);
|
|
2457
|
+
assert(node->child(i)->position() == i);
|
|
2458
|
+
count += internal_verify(
|
|
2459
|
+
node->child(i),
|
|
2460
|
+
(i == 0) ? lo : &node->key(i - 1),
|
|
2461
|
+
(i == node->count()) ? hi : &node->key(i));
|
|
2462
|
+
}
|
|
2463
|
+
}
|
|
2464
|
+
return count;
|
|
2465
|
+
}
|
|
2466
|
+
|
|
2467
|
+
} // namespace internal_btree
|
|
2468
|
+
} // namespace gtl
|
|
2469
|
+
|
|
2470
|
+
|
|
2471
|
+
#endif // S2_UTIL_GTL_BTREE_H_
|