@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,1493 @@
|
|
|
1
|
+
// Copyright 2005 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
|
+
// All rights reserved.
|
|
17
|
+
//
|
|
18
|
+
// Redistribution and use in source and binary forms, with or without
|
|
19
|
+
// modification, are permitted provided that the following conditions are
|
|
20
|
+
// met:
|
|
21
|
+
//
|
|
22
|
+
// * Redistributions of source code must retain the above copyright
|
|
23
|
+
// notice, this list of conditions and the following disclaimer.
|
|
24
|
+
// * Redistributions in binary form must reproduce the above
|
|
25
|
+
// copyright notice, this list of conditions and the following disclaimer
|
|
26
|
+
// in the documentation and/or other materials provided with the
|
|
27
|
+
// distribution.
|
|
28
|
+
// * Neither the name of Google Inc. nor the names of its
|
|
29
|
+
// contributors may be used to endorse or promote products derived from
|
|
30
|
+
// this software without specific prior written permission.
|
|
31
|
+
//
|
|
32
|
+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
33
|
+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
34
|
+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
35
|
+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
36
|
+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
37
|
+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
38
|
+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
39
|
+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
40
|
+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
41
|
+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
42
|
+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
43
|
+
|
|
44
|
+
// ---
|
|
45
|
+
//
|
|
46
|
+
// A dense hashtable is a particular implementation of
|
|
47
|
+
// a hashtable: one that is meant to minimize memory allocation.
|
|
48
|
+
// It does this by using an array to store all the data. We
|
|
49
|
+
// steal a value from the key space to indicate "empty" array
|
|
50
|
+
// elements (ie indices where no item lives) and another to indicate
|
|
51
|
+
// "deleted" elements.
|
|
52
|
+
//
|
|
53
|
+
// (Note it is possible to change the value of the delete key
|
|
54
|
+
// on the fly; you can even remove it, though after that point
|
|
55
|
+
// the hashtable is insert_only until you set it again. The empty
|
|
56
|
+
// value however can't be changed.)
|
|
57
|
+
//
|
|
58
|
+
// To minimize allocation and pointer overhead, we use internal
|
|
59
|
+
// probing, in which the hashtable is a single table, and collisions
|
|
60
|
+
// are resolved by trying to insert again in another bucket. The
|
|
61
|
+
// most cache-efficient internal probing schemes are linear probing
|
|
62
|
+
// (which suffers, alas, from clumping) and quadratic probing, which
|
|
63
|
+
// is what we implement by default.
|
|
64
|
+
//
|
|
65
|
+
// Type requirements: value_type is required to be Move Constructible
|
|
66
|
+
// and Default Constructible. It is not required to be (and commonly
|
|
67
|
+
// isn't) Assignable.
|
|
68
|
+
//
|
|
69
|
+
// You probably shouldn't use this code directly. Use dense_hash_map<>
|
|
70
|
+
// or dense_hash_set<> instead.
|
|
71
|
+
|
|
72
|
+
// You can change the following below:
|
|
73
|
+
// HT_OCCUPANCY_PCT -- how full before we double size
|
|
74
|
+
// HT_EMPTY_PCT -- how empty before we halve size
|
|
75
|
+
// HT_MIN_BUCKETS -- default smallest bucket size
|
|
76
|
+
//
|
|
77
|
+
// You can also change enlarge_factor (which defaults to
|
|
78
|
+
// HT_OCCUPANCY_PCT), and shrink_factor (which defaults to
|
|
79
|
+
// HT_EMPTY_PCT) with set_resizing_parameters().
|
|
80
|
+
//
|
|
81
|
+
// How to decide what values to use?
|
|
82
|
+
// shrink_factor's default of .4 * OCCUPANCY_PCT, is probably good.
|
|
83
|
+
// HT_MIN_BUCKETS is probably unnecessary since you can specify
|
|
84
|
+
// (indirectly) the starting number of buckets at construct-time.
|
|
85
|
+
// For enlarge_factor, you can use this chart to try to trade-off
|
|
86
|
+
// expected lookup time to the space taken up. By default, this
|
|
87
|
+
// code uses quadratic probing, though you can change it to linear
|
|
88
|
+
// via JUMP_ below if you really want to.
|
|
89
|
+
//
|
|
90
|
+
// From
|
|
91
|
+
// L = N / M,
|
|
92
|
+
// where N is the number of data items in the table and M is the table size.
|
|
93
|
+
// NUMBER OF PROBES / LOOKUP Successful Unsuccessful
|
|
94
|
+
// Quadratic collision resolution 1 - ln(1-L) - L/2 1/(1-L) - L - ln(1-L)
|
|
95
|
+
// Linear collision resolution [1+1/(1-L)]/2 [1+1/(1-L)^2]/2
|
|
96
|
+
//
|
|
97
|
+
// -- enlarge_factor -- 0.10 0.50 0.60 0.75 0.80 0.90 0.99
|
|
98
|
+
// QUADRATIC COLLISION RES.
|
|
99
|
+
// probes/successful lookup 1.05 1.44 1.62 2.01 2.21 2.85 5.11
|
|
100
|
+
// probes/unsuccessful lookup 1.11 2.19 2.82 4.64 5.81 11.4 103.6
|
|
101
|
+
// LINEAR COLLISION RES.
|
|
102
|
+
// probes/successful lookup 1.06 1.5 1.75 2.5 3.0 5.5 50.5
|
|
103
|
+
// probes/unsuccessful lookup 1.12 2.5 3.6 8.5 13.0 50.0 5000.0
|
|
104
|
+
|
|
105
|
+
#ifndef S2_UTIL_GTL_DENSEHASHTABLE_H_
|
|
106
|
+
#define S2_UTIL_GTL_DENSEHASHTABLE_H_
|
|
107
|
+
|
|
108
|
+
#include <cassert>
|
|
109
|
+
#include <cstddef>
|
|
110
|
+
#include <cstdio> // for FILE, fwrite, fread
|
|
111
|
+
#include <algorithm> // For swap(), eg
|
|
112
|
+
#include <functional>
|
|
113
|
+
#include <iterator> // For iterator tags
|
|
114
|
+
#include <limits> // for numeric_limits
|
|
115
|
+
#include <memory> // For uninitialized_fill
|
|
116
|
+
#include <new>
|
|
117
|
+
#include <string>
|
|
118
|
+
#include <utility>
|
|
119
|
+
#include <vector>
|
|
120
|
+
#include <type_traits>
|
|
121
|
+
|
|
122
|
+
#include "s2/util/gtl/hashtable_common.h"
|
|
123
|
+
#include "s2/base/port.h"
|
|
124
|
+
#include <stdexcept> // For length_error
|
|
125
|
+
|
|
126
|
+
namespace gtl {
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
// Some files test for this symbol.
|
|
130
|
+
#define S2__DENSEHASHTABLE_H_
|
|
131
|
+
|
|
132
|
+
// The probing method
|
|
133
|
+
// Linear probing
|
|
134
|
+
// #define JUMP_(key, num_probes) ( 1 )
|
|
135
|
+
// Quadratic probing
|
|
136
|
+
#define JUMP_(key, num_probes) (num_probes)
|
|
137
|
+
|
|
138
|
+
// The weird mod in the offset is entirely to quiet compiler warnings
|
|
139
|
+
// as is the cast to int after doing the "x mod 256"
|
|
140
|
+
#define PUT_(take_from, offset) do { \
|
|
141
|
+
if (putc(static_cast<int>(offset >= sizeof(take_from)*8) \
|
|
142
|
+
? 0 : ((take_from) >> (offset)) % 256, fp) \
|
|
143
|
+
== EOF) \
|
|
144
|
+
return false; \
|
|
145
|
+
} while (0)
|
|
146
|
+
|
|
147
|
+
#define GET_(add_to, offset) do { \
|
|
148
|
+
if ((x=getc(fp)) == EOF) \
|
|
149
|
+
return false; \
|
|
150
|
+
else if (offset >= sizeof(add_to) * 8) \
|
|
151
|
+
assert(x == 0); /* otherwise it's too big for us to represent */ \
|
|
152
|
+
else \
|
|
153
|
+
add_to |= (static_cast<size_type>(x) << ((offset) % (sizeof(add_to)*8))); \
|
|
154
|
+
} while (0)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
// Hashtable class, used to implement the hashed associative containers
|
|
158
|
+
// hash_set and hash_map.
|
|
159
|
+
|
|
160
|
+
// Value: what is stored in the table (each bucket is a Value).
|
|
161
|
+
// Key: something in a 1-to-1 correspondence to a Value, that can be used
|
|
162
|
+
// to search for a Value in the table (find() takes a Key).
|
|
163
|
+
// HashFcn: Takes a Key and returns an integer, the more unique the better.
|
|
164
|
+
// ExtractKey: given a Value, returns the unique Key associated with it.
|
|
165
|
+
// Must have a result_type enum indicating the return type of
|
|
166
|
+
// operator().
|
|
167
|
+
// SetKey: given a Value* and a Key, modifies the value such that
|
|
168
|
+
// ExtractKey(value) == key. We guarantee this is only called
|
|
169
|
+
// with key == deleted_key or key == empty_key.
|
|
170
|
+
// EqualKey: Given two Keys, says whether they are the same (that is,
|
|
171
|
+
// if they are both associated with the same Value).
|
|
172
|
+
// Alloc: STL allocator to use to allocate memory.
|
|
173
|
+
|
|
174
|
+
template <class Value, class Key, class HashFcn,
|
|
175
|
+
class ExtractKey, class SetKey, class EqualKey, class Alloc>
|
|
176
|
+
class dense_hashtable;
|
|
177
|
+
|
|
178
|
+
template <class V, class K, class HF, class ExK, class SetK, class EqK, class A>
|
|
179
|
+
struct dense_hashtable_const_iterator;
|
|
180
|
+
|
|
181
|
+
// We're just an array, but we need to skip over empty and deleted elements
|
|
182
|
+
template <class V, class K, class HF, class ExK, class SetK, class EqK, class A>
|
|
183
|
+
struct dense_hashtable_iterator {
|
|
184
|
+
private:
|
|
185
|
+
typedef typename A::template rebind<V>::other value_alloc_type;
|
|
186
|
+
|
|
187
|
+
public:
|
|
188
|
+
typedef dense_hashtable_iterator<V, K, HF, ExK, SetK, EqK, A>
|
|
189
|
+
iterator;
|
|
190
|
+
typedef dense_hashtable_const_iterator<V, K, HF, ExK, SetK, EqK, A>
|
|
191
|
+
const_iterator;
|
|
192
|
+
|
|
193
|
+
typedef std::forward_iterator_tag iterator_category; // very little defined!
|
|
194
|
+
typedef V value_type;
|
|
195
|
+
typedef typename value_alloc_type::difference_type difference_type;
|
|
196
|
+
typedef typename value_alloc_type::size_type size_type;
|
|
197
|
+
typedef typename value_alloc_type::reference reference;
|
|
198
|
+
typedef typename value_alloc_type::pointer pointer;
|
|
199
|
+
|
|
200
|
+
// "Real" constructor and default constructor
|
|
201
|
+
dense_hashtable_iterator(
|
|
202
|
+
const dense_hashtable<V, K, HF, ExK, SetK, EqK, A> *h,
|
|
203
|
+
pointer it, pointer it_end, bool advance)
|
|
204
|
+
: ht(h), pos(it), end(it_end) {
|
|
205
|
+
if (advance) advance_past_empty_and_deleted();
|
|
206
|
+
}
|
|
207
|
+
dense_hashtable_iterator() { }
|
|
208
|
+
// The default destructor is fine; we don't define one
|
|
209
|
+
// The default operator= is fine; we don't define one
|
|
210
|
+
|
|
211
|
+
// Happy dereferencer
|
|
212
|
+
reference operator*() const { return *pos; }
|
|
213
|
+
pointer operator->() const { return &(operator*()); }
|
|
214
|
+
|
|
215
|
+
// Arithmetic. The only hard part is making sure that
|
|
216
|
+
// we're not on an empty or marked-deleted array element
|
|
217
|
+
void advance_past_empty_and_deleted() {
|
|
218
|
+
while (pos != end && (ht->test_empty(*this) || ht->test_deleted(*this)) )
|
|
219
|
+
++pos;
|
|
220
|
+
}
|
|
221
|
+
iterator& operator++() {
|
|
222
|
+
assert(pos != end);
|
|
223
|
+
++pos;
|
|
224
|
+
advance_past_empty_and_deleted();
|
|
225
|
+
return *this;
|
|
226
|
+
}
|
|
227
|
+
iterator operator++(int /*unused*/) {
|
|
228
|
+
auto tmp(*this);
|
|
229
|
+
++*this;
|
|
230
|
+
return tmp;
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
// Comparison.
|
|
234
|
+
bool operator==(const iterator& it) const { return pos == it.pos; }
|
|
235
|
+
bool operator!=(const iterator& it) const { return pos != it.pos; }
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
// The actual data
|
|
239
|
+
const dense_hashtable<V, K, HF, ExK, SetK, EqK, A> *ht;
|
|
240
|
+
pointer pos, end;
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
|
|
244
|
+
// Now do it all again, but with const-ness!
|
|
245
|
+
template <class V, class K, class HF, class ExK, class SetK, class EqK, class A>
|
|
246
|
+
struct dense_hashtable_const_iterator {
|
|
247
|
+
private:
|
|
248
|
+
typedef typename A::template rebind<V>::other value_alloc_type;
|
|
249
|
+
|
|
250
|
+
public:
|
|
251
|
+
typedef dense_hashtable_iterator<V, K, HF, ExK, SetK, EqK, A>
|
|
252
|
+
iterator;
|
|
253
|
+
typedef dense_hashtable_const_iterator<V, K, HF, ExK, SetK, EqK, A>
|
|
254
|
+
const_iterator;
|
|
255
|
+
|
|
256
|
+
typedef std::forward_iterator_tag iterator_category; // very little defined!
|
|
257
|
+
typedef V value_type;
|
|
258
|
+
typedef typename value_alloc_type::difference_type difference_type;
|
|
259
|
+
typedef typename value_alloc_type::size_type size_type;
|
|
260
|
+
typedef typename value_alloc_type::const_reference reference;
|
|
261
|
+
typedef typename value_alloc_type::const_pointer pointer;
|
|
262
|
+
|
|
263
|
+
// "Real" constructor and default constructor
|
|
264
|
+
dense_hashtable_const_iterator(
|
|
265
|
+
const dense_hashtable<V, K, HF, ExK, SetK, EqK, A> *h,
|
|
266
|
+
pointer it, pointer it_end, bool advance)
|
|
267
|
+
: ht(h), pos(it), end(it_end) {
|
|
268
|
+
if (advance) advance_past_empty_and_deleted();
|
|
269
|
+
}
|
|
270
|
+
dense_hashtable_const_iterator()
|
|
271
|
+
: ht(nullptr), pos(pointer()), end(pointer()) { }
|
|
272
|
+
// This lets us convert regular iterators to const iterators
|
|
273
|
+
dense_hashtable_const_iterator(const iterator &it)
|
|
274
|
+
: ht(it.ht), pos(it.pos), end(it.end) { }
|
|
275
|
+
// The default destructor is fine; we don't define one
|
|
276
|
+
// The default operator= is fine; we don't define one
|
|
277
|
+
|
|
278
|
+
// Happy dereferencer
|
|
279
|
+
reference operator*() const { return *pos; }
|
|
280
|
+
pointer operator->() const { return &(operator*()); }
|
|
281
|
+
|
|
282
|
+
// Arithmetic. The only hard part is making sure that
|
|
283
|
+
// we're not on an empty or marked-deleted array element
|
|
284
|
+
void advance_past_empty_and_deleted() {
|
|
285
|
+
while (pos != end && (ht->test_empty(*this) || ht->test_deleted(*this)))
|
|
286
|
+
++pos;
|
|
287
|
+
}
|
|
288
|
+
const_iterator& operator++() {
|
|
289
|
+
assert(pos != end);
|
|
290
|
+
++pos;
|
|
291
|
+
advance_past_empty_and_deleted();
|
|
292
|
+
return *this;
|
|
293
|
+
}
|
|
294
|
+
const_iterator operator++(int /*unused*/) {
|
|
295
|
+
auto tmp(*this);
|
|
296
|
+
++*this;
|
|
297
|
+
return tmp;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Comparison.
|
|
301
|
+
bool operator==(const const_iterator& it) const { return pos == it.pos; }
|
|
302
|
+
bool operator!=(const const_iterator& it) const { return pos != it.pos; }
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
// The actual data
|
|
306
|
+
const dense_hashtable<V, K, HF, ExK, SetK, EqK, A> *ht;
|
|
307
|
+
pointer pos, end;
|
|
308
|
+
};
|
|
309
|
+
|
|
310
|
+
template <class Value, class Key, class HashFcn,
|
|
311
|
+
class ExtractKey, class SetKey, class EqualKey, class Alloc>
|
|
312
|
+
class dense_hashtable {
|
|
313
|
+
private:
|
|
314
|
+
typedef typename Alloc::template rebind<Value>::other value_alloc_type;
|
|
315
|
+
|
|
316
|
+
|
|
317
|
+
public:
|
|
318
|
+
typedef Key key_type;
|
|
319
|
+
typedef Value value_type;
|
|
320
|
+
typedef HashFcn hasher;
|
|
321
|
+
typedef EqualKey key_equal;
|
|
322
|
+
typedef Alloc allocator_type;
|
|
323
|
+
|
|
324
|
+
typedef typename value_alloc_type::size_type size_type;
|
|
325
|
+
typedef typename value_alloc_type::difference_type difference_type;
|
|
326
|
+
typedef typename value_alloc_type::reference reference;
|
|
327
|
+
typedef typename value_alloc_type::const_reference const_reference;
|
|
328
|
+
typedef typename value_alloc_type::pointer pointer;
|
|
329
|
+
typedef typename value_alloc_type::const_pointer const_pointer;
|
|
330
|
+
typedef dense_hashtable_iterator<Value, Key, HashFcn,
|
|
331
|
+
ExtractKey, SetKey, EqualKey, Alloc>
|
|
332
|
+
iterator;
|
|
333
|
+
|
|
334
|
+
typedef dense_hashtable_const_iterator<Value, Key, HashFcn,
|
|
335
|
+
ExtractKey, SetKey, EqualKey, Alloc>
|
|
336
|
+
const_iterator;
|
|
337
|
+
|
|
338
|
+
// These come from tr1. For us they're the same as regular iterators.
|
|
339
|
+
typedef iterator local_iterator;
|
|
340
|
+
typedef const_iterator const_local_iterator;
|
|
341
|
+
|
|
342
|
+
|
|
343
|
+
// How full we let the table get before we resize, by default.
|
|
344
|
+
// Knuth says .8 is good -- higher causes us to probe too much,
|
|
345
|
+
// though it saves memory.
|
|
346
|
+
static const int HT_OCCUPANCY_PCT; // defined at the bottom of this file
|
|
347
|
+
|
|
348
|
+
// How empty we let the table get before we resize lower, by default.
|
|
349
|
+
// (0.0 means never resize lower.)
|
|
350
|
+
// It should be less than OCCUPANCY_PCT / 2 or we thrash resizing
|
|
351
|
+
static const int HT_EMPTY_PCT; // defined at the bottom of this file
|
|
352
|
+
|
|
353
|
+
// Minimum size we're willing to let hashtables be.
|
|
354
|
+
// Must be a power of two, and at least 4.
|
|
355
|
+
// Note, however, that for a given hashtable, the initial size is a
|
|
356
|
+
// function of the first constructor arg, and may be >HT_MIN_BUCKETS.
|
|
357
|
+
static const size_type HT_MIN_BUCKETS = 4;
|
|
358
|
+
|
|
359
|
+
// By default, if you don't specify a hashtable size at
|
|
360
|
+
// construction-time, we use this size. Must be a power of two, and
|
|
361
|
+
// at least HT_MIN_BUCKETS.
|
|
362
|
+
static const size_type HT_DEFAULT_STARTING_BUCKETS = 32;
|
|
363
|
+
|
|
364
|
+
// ITERATOR FUNCTIONS
|
|
365
|
+
iterator begin() {
|
|
366
|
+
return iterator(this, table, table + num_buckets, true);
|
|
367
|
+
}
|
|
368
|
+
iterator end() {
|
|
369
|
+
return iterator(this, table + num_buckets, table + num_buckets, true);
|
|
370
|
+
}
|
|
371
|
+
const_iterator begin() const {
|
|
372
|
+
return const_iterator(this, table, table+num_buckets, true);
|
|
373
|
+
}
|
|
374
|
+
const_iterator end() const {
|
|
375
|
+
return const_iterator(this, table + num_buckets, table+num_buckets, true);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// These come from tr1 unordered_map. They iterate over 'bucket' n.
|
|
379
|
+
// We'll just consider bucket n to be the n-th element of the table.
|
|
380
|
+
local_iterator begin(size_type i) {
|
|
381
|
+
return local_iterator(this, table + i, table + i+1, false);
|
|
382
|
+
}
|
|
383
|
+
local_iterator end(size_type i) {
|
|
384
|
+
local_iterator it = begin(i);
|
|
385
|
+
if (!test_empty(i) && !test_deleted(i))
|
|
386
|
+
++it;
|
|
387
|
+
return it;
|
|
388
|
+
}
|
|
389
|
+
const_local_iterator begin(size_type i) const {
|
|
390
|
+
return const_local_iterator(this, table + i, table + i+1, false);
|
|
391
|
+
}
|
|
392
|
+
const_local_iterator end(size_type i) const {
|
|
393
|
+
const_local_iterator it = begin(i);
|
|
394
|
+
if (!test_empty(i) && !test_deleted(i))
|
|
395
|
+
++it;
|
|
396
|
+
return it;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// ACCESSOR FUNCTIONS for the things we templatize on, basically
|
|
400
|
+
hasher hash_funct() const { return settings; }
|
|
401
|
+
key_equal key_eq() const { return key_info; }
|
|
402
|
+
value_alloc_type get_allocator() const { return key_info; }
|
|
403
|
+
|
|
404
|
+
// Accessor function for statistics gathering.
|
|
405
|
+
int num_table_copies() const { return settings.num_ht_copies(); }
|
|
406
|
+
|
|
407
|
+
private:
|
|
408
|
+
// Annoyingly, we can't copy values around, because they might have
|
|
409
|
+
// const components (they're probably std::pair<const X, Y>). We use
|
|
410
|
+
// explicit destructor invocation and placement new to get around
|
|
411
|
+
// this. Arg.
|
|
412
|
+
static void set_value(pointer dst, const_reference src) {
|
|
413
|
+
dst->~value_type(); // delete the old value, if any
|
|
414
|
+
new(dst) value_type(src);
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
static void set_value(pointer dst, value_type&& src) { // NOLINT
|
|
418
|
+
dst->~value_type();
|
|
419
|
+
new(dst) value_type(std::move(src));
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
void destroy_buckets(size_type first, size_type last) {
|
|
423
|
+
for ( ; first != last; ++first)
|
|
424
|
+
table[first].~value_type();
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
// DELETE HELPER FUNCTIONS
|
|
428
|
+
// This lets the user describe a key that will indicate deleted
|
|
429
|
+
// table entries. This key should be an "impossible" entry --
|
|
430
|
+
// if you try to insert it for real, you won't be able to retrieve it!
|
|
431
|
+
// (NB: while you pass in an entire value, only the key part is looked
|
|
432
|
+
// at. This is just because I don't know how to assign just a key.)
|
|
433
|
+
private:
|
|
434
|
+
// Gets rid of any deleted entries we have.
|
|
435
|
+
void squash_deleted() {
|
|
436
|
+
if (num_deleted > 0) {
|
|
437
|
+
rebucket(settings.min_buckets(size(), num_buckets));
|
|
438
|
+
}
|
|
439
|
+
assert(num_deleted == 0);
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
// Test if the given key is the deleted indicator. Requires
|
|
443
|
+
// num_deleted > 0, for correctness of read(), and because that
|
|
444
|
+
// guarantees that key_info.delkey is valid.
|
|
445
|
+
bool test_deleted_key(const key_type& key) const {
|
|
446
|
+
assert(num_deleted > 0);
|
|
447
|
+
return equals(key_info.delkey, key);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
public:
|
|
451
|
+
void set_deleted_key(const key_type &key) {
|
|
452
|
+
// the empty indicator (if specified) and the deleted indicator
|
|
453
|
+
// must be different
|
|
454
|
+
assert((!settings.use_empty() || !equals(key, key_info.empty)) &&
|
|
455
|
+
"Passed the empty-key to set_deleted_key");
|
|
456
|
+
// It's only safe to change what "deleted" means if we purge deleted guys
|
|
457
|
+
squash_deleted();
|
|
458
|
+
settings.set_use_deleted(true);
|
|
459
|
+
key_info.delkey = key;
|
|
460
|
+
}
|
|
461
|
+
key_type deleted_key() const {
|
|
462
|
+
assert(settings.use_deleted()
|
|
463
|
+
&& "Must set deleted key before calling deleted_key");
|
|
464
|
+
return key_info.delkey;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
// These are public so the iterators can use them
|
|
468
|
+
// True if the item at position bucknum is "deleted" marker
|
|
469
|
+
bool test_deleted(size_type bucknum) const {
|
|
470
|
+
// Invariant: !use_deleted() implies num_deleted is 0.
|
|
471
|
+
assert(settings.use_deleted() || num_deleted == 0);
|
|
472
|
+
return num_deleted > 0 && test_deleted_key(get_key(table[bucknum]));
|
|
473
|
+
}
|
|
474
|
+
bool test_deleted(const iterator &it) const {
|
|
475
|
+
// Invariant: !use_deleted() implies num_deleted is 0.
|
|
476
|
+
assert(settings.use_deleted() || num_deleted == 0);
|
|
477
|
+
return num_deleted > 0 && test_deleted_key(get_key(*it));
|
|
478
|
+
}
|
|
479
|
+
bool test_deleted(const const_iterator &it) const {
|
|
480
|
+
// Invariant: !use_deleted() implies num_deleted is 0.
|
|
481
|
+
assert(settings.use_deleted() || num_deleted == 0);
|
|
482
|
+
return num_deleted > 0 && test_deleted_key(get_key(*it));
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
private:
|
|
486
|
+
void check_use_deleted(const char* caller) {
|
|
487
|
+
(void)caller; // could log it if the assert failed
|
|
488
|
+
assert(settings.use_deleted());
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
// Write the deleted key to the position specified.
|
|
492
|
+
// Requires: !test_deleted(it)
|
|
493
|
+
void set_deleted(iterator &it) {
|
|
494
|
+
check_use_deleted("set_deleted()");
|
|
495
|
+
assert(!test_deleted(it));
|
|
496
|
+
// &* converts from iterator to value-type.
|
|
497
|
+
set_key(&(*it), key_info.delkey);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// We also allow to set/clear the deleted bit on a const iterator.
|
|
501
|
+
// We allow a const_iterator for the same reason you can delete a
|
|
502
|
+
// const pointer: it's convenient, and semantically you can't use
|
|
503
|
+
// 'it' after it's been deleted anyway, so its const-ness doesn't
|
|
504
|
+
// really matter.
|
|
505
|
+
// Requires: !test_deleted(it)
|
|
506
|
+
void set_deleted(const_iterator &it) {
|
|
507
|
+
check_use_deleted("set_deleted()");
|
|
508
|
+
assert(!test_deleted(it));
|
|
509
|
+
set_key(const_cast<pointer>(&(*it)), key_info.delkey);
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// EMPTY HELPER FUNCTIONS
|
|
513
|
+
// This lets the user describe a key that will indicate empty (unused)
|
|
514
|
+
// table entries. This key should be an "impossible" entry --
|
|
515
|
+
// if you try to insert it for real, you won't be able to retrieve it!
|
|
516
|
+
// (NB: while you pass in an entire value, only the key part is looked
|
|
517
|
+
// at. This is just because I don't know how to assign just a key.)
|
|
518
|
+
public:
|
|
519
|
+
// These are public so the iterators can use them
|
|
520
|
+
// True if the item at position bucknum is "empty" marker
|
|
521
|
+
bool test_empty(size_type bucknum) const {
|
|
522
|
+
assert(settings.use_empty()); // we always need to know what's empty!
|
|
523
|
+
return equals(key_info.empty, get_key(table[bucknum]));
|
|
524
|
+
}
|
|
525
|
+
bool test_empty(const iterator &it) const {
|
|
526
|
+
assert(settings.use_empty()); // we always need to know what's empty!
|
|
527
|
+
return equals(key_info.empty, get_key(*it));
|
|
528
|
+
}
|
|
529
|
+
bool test_empty(const const_iterator &it) const {
|
|
530
|
+
assert(settings.use_empty()); // we always need to know what's empty!
|
|
531
|
+
return equals(key_info.empty, get_key(*it));
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
private:
|
|
535
|
+
bool test_empty(size_type bucknum, const_pointer table) const {
|
|
536
|
+
assert(settings.use_empty());
|
|
537
|
+
return equals(key_info.empty, get_key(table[bucknum]));
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
void fill_range_with_empty(pointer table_start, pointer table_end) {
|
|
541
|
+
for (; table_start != table_end; ++table_start) {
|
|
542
|
+
new (table_start) value_type();
|
|
543
|
+
set_key(table_start, key_info.empty);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
public:
|
|
548
|
+
// TODO(user): change all callers of this to pass in a key instead,
|
|
549
|
+
// and take a const key_type instead of const value_type.
|
|
550
|
+
void set_empty_key(const_reference val) {
|
|
551
|
+
// Once you set the empty key, you can't change it
|
|
552
|
+
assert(!settings.use_empty() && "Calling set_empty_key multiple times");
|
|
553
|
+
// The deleted indicator (if specified) and the empty indicator
|
|
554
|
+
// must be different.
|
|
555
|
+
const key_type& key = get_key(val);
|
|
556
|
+
assert((!settings.use_deleted() || !equals(key, key_info.delkey)) &&
|
|
557
|
+
"Setting the empty key the same as the deleted key");
|
|
558
|
+
settings.set_use_empty(true);
|
|
559
|
+
key_info.empty.~key_type();
|
|
560
|
+
new (&key_info.empty) key_type(key);
|
|
561
|
+
|
|
562
|
+
assert(!table); // must set before first use
|
|
563
|
+
// num_buckets was set in constructor even though table was nullptr
|
|
564
|
+
table = get_internal_allocator().allocate(num_buckets);
|
|
565
|
+
fill_range_with_empty(table, table + num_buckets);
|
|
566
|
+
}
|
|
567
|
+
// TODO(user): this should return the key by const reference.
|
|
568
|
+
value_type empty_key() const {
|
|
569
|
+
assert(settings.use_empty());
|
|
570
|
+
value_type ret = value_type();
|
|
571
|
+
set_key(&ret, key_info.empty);
|
|
572
|
+
return ret;
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
// FUNCTIONS CONCERNING SIZE
|
|
576
|
+
public:
|
|
577
|
+
size_type size() const { return num_elements - num_deleted; }
|
|
578
|
+
size_type max_size() const { return get_allocator().max_size(); }
|
|
579
|
+
bool empty() const { return size() == 0; }
|
|
580
|
+
size_type bucket_count() const { return num_buckets; }
|
|
581
|
+
size_type max_bucket_count() const { return max_size(); }
|
|
582
|
+
size_type nonempty_bucket_count() const { return num_elements; }
|
|
583
|
+
// These are tr1 methods. Their idea of 'bucket' doesn't map well to
|
|
584
|
+
// what we do. We just say every bucket has 0 or 1 items in it.
|
|
585
|
+
size_type bucket_size(size_type i) const {
|
|
586
|
+
return begin(i) == end(i) ? 0 : 1;
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
private:
|
|
590
|
+
// Because of the above, size_type(-1) is never legal; use it for errors
|
|
591
|
+
static const size_type ILLEGAL_BUCKET = size_type(-1);
|
|
592
|
+
|
|
593
|
+
// Used after a string of deletes. Returns true if we actually shrunk.
|
|
594
|
+
// TODO(user): take a delta so we can take into account inserts
|
|
595
|
+
// done after shrinking. Maybe make part of the Settings class?
|
|
596
|
+
bool maybe_shrink() {
|
|
597
|
+
assert(num_elements >= num_deleted);
|
|
598
|
+
assert((bucket_count() & (bucket_count()-1)) == 0); // is a power of two
|
|
599
|
+
assert(bucket_count() >= HT_MIN_BUCKETS);
|
|
600
|
+
bool retval = false;
|
|
601
|
+
|
|
602
|
+
// If you construct a hashtable with < HT_DEFAULT_STARTING_BUCKETS,
|
|
603
|
+
// we'll never shrink until you get relatively big, and we'll never
|
|
604
|
+
// shrink below HT_DEFAULT_STARTING_BUCKETS. Otherwise, something
|
|
605
|
+
// like "dense_hash_set<int> x; x.insert(4); x.erase(4);" will
|
|
606
|
+
// shrink us down to HT_MIN_BUCKETS buckets, which is too small.
|
|
607
|
+
const size_type num_remain = num_elements - num_deleted;
|
|
608
|
+
const size_type shrink_threshold = settings.shrink_threshold();
|
|
609
|
+
if (shrink_threshold > 0 && num_remain < shrink_threshold &&
|
|
610
|
+
bucket_count() > HT_DEFAULT_STARTING_BUCKETS) {
|
|
611
|
+
const float shrink_factor = settings.shrink_factor();
|
|
612
|
+
size_type sz = bucket_count() / 2; // find how much we should shrink
|
|
613
|
+
while (sz > HT_DEFAULT_STARTING_BUCKETS &&
|
|
614
|
+
num_remain < sz * shrink_factor) {
|
|
615
|
+
sz /= 2; // stay a power of 2
|
|
616
|
+
}
|
|
617
|
+
rebucket(sz);
|
|
618
|
+
retval = true;
|
|
619
|
+
}
|
|
620
|
+
settings.set_consider_shrink(false); // because we just considered it
|
|
621
|
+
return retval;
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
// We'll let you resize a hashtable -- though this makes us copy all!
|
|
625
|
+
// When you resize, you say, "make it big enough for this many more elements"
|
|
626
|
+
// Returns true if we actually resized, false if size was already ok.
|
|
627
|
+
bool resize_delta(size_type delta) {
|
|
628
|
+
bool did_resize = false;
|
|
629
|
+
if (settings.consider_shrink()) { // see if lots of deletes happened
|
|
630
|
+
if (maybe_shrink())
|
|
631
|
+
did_resize = true;
|
|
632
|
+
}
|
|
633
|
+
if (num_elements >= std::numeric_limits<size_type>::max() - delta) {
|
|
634
|
+
throw std::length_error("resize overflow");
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
assert(settings.enlarge_threshold() < bucket_count());
|
|
638
|
+
// Check if our work is done.
|
|
639
|
+
if (bucket_count() >= HT_MIN_BUCKETS &&
|
|
640
|
+
num_elements + delta <= settings.enlarge_threshold()) {
|
|
641
|
+
return did_resize;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
// Sometimes, we need to resize just to get rid of all the
|
|
645
|
+
// "deleted" buckets that are clogging up the hashtable. So when
|
|
646
|
+
// deciding whether to resize, count the deleted buckets (which
|
|
647
|
+
// are currently taking up room). But later, when we decide what
|
|
648
|
+
// size to resize to, *don't* count deleted buckets, since they
|
|
649
|
+
// get discarded during the resize.
|
|
650
|
+
const size_type needed_size = settings.min_buckets(num_elements + delta, 0);
|
|
651
|
+
if (needed_size <= bucket_count()) // we have enough buckets
|
|
652
|
+
return did_resize;
|
|
653
|
+
|
|
654
|
+
// We will rebucket.
|
|
655
|
+
size_type resize_to =
|
|
656
|
+
settings.min_buckets(num_elements - num_deleted + delta, bucket_count());
|
|
657
|
+
|
|
658
|
+
if (resize_to < needed_size) {
|
|
659
|
+
// This situation means that we have enough deleted elements,
|
|
660
|
+
// that once we purge them, we won't actually have needed to
|
|
661
|
+
// grow. But we may want to grow anyway: if we just purge one
|
|
662
|
+
// element, say, we'll have to grow anyway next time we
|
|
663
|
+
// insert. Might as well grow now, since we're already going
|
|
664
|
+
// through the trouble of rebucketing in order to purge the
|
|
665
|
+
// deleted elements. (Safety note: Can resize_to * 2 overflow? No.
|
|
666
|
+
// The output of min_buckets() is always a power of two, so resize_to
|
|
667
|
+
// and needed_size are powers of two. That plus resize_to < needed_size
|
|
668
|
+
// proves that overflow isn't a concern.)
|
|
669
|
+
const size_type target =
|
|
670
|
+
static_cast<size_type>(settings.shrink_size(resize_to*2));
|
|
671
|
+
if (num_elements - num_deleted + delta >= target) {
|
|
672
|
+
// Good, we won't be below the shrink threshhold even if we double.
|
|
673
|
+
resize_to *= 2;
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
rebucket(resize_to);
|
|
677
|
+
return true;
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
// We require table be non-null and empty before calling this.
|
|
681
|
+
void resize_table(size_type old_size, size_type new_size) {
|
|
682
|
+
get_internal_allocator().deallocate(table, old_size);
|
|
683
|
+
table = get_internal_allocator().allocate(new_size);
|
|
684
|
+
}
|
|
685
|
+
|
|
686
|
+
// Copy (or, if Iter is a move_iterator, move) the elements from
|
|
687
|
+
// [src_first, src_last) into dest_table, which we assume has size
|
|
688
|
+
// dest_bucket_count and has been initialized to the empty key.
|
|
689
|
+
template<class Iter>
|
|
690
|
+
void copy_elements(Iter src_first, Iter src_last, pointer dest_table,
|
|
691
|
+
size_type dest_bucket_count) {
|
|
692
|
+
assert((dest_bucket_count & (dest_bucket_count - 1)) == 0); // a power of 2
|
|
693
|
+
// We use a normal iterator to get non-deleted bcks from ht
|
|
694
|
+
// We could use insert() here, but since we know there are
|
|
695
|
+
// no duplicates and no deleted items, we can be more efficient
|
|
696
|
+
for (; src_first != src_last; ++src_first) {
|
|
697
|
+
size_type num_probes = 0; // how many times we've probed
|
|
698
|
+
size_type bucknum;
|
|
699
|
+
const size_type bucket_count_minus_one = dest_bucket_count - 1;
|
|
700
|
+
for (bucknum = hash(get_key(*src_first)) & bucket_count_minus_one;
|
|
701
|
+
!test_empty(bucknum, dest_table); // not empty
|
|
702
|
+
bucknum = (bucknum +
|
|
703
|
+
JUMP_(key, num_probes)) & bucket_count_minus_one) {
|
|
704
|
+
++num_probes;
|
|
705
|
+
assert(num_probes < dest_bucket_count
|
|
706
|
+
&& "Hashtable is full: an error in key_equal<> or hash<>");
|
|
707
|
+
}
|
|
708
|
+
// Copies or moves the value into dest_table.
|
|
709
|
+
set_value(&dest_table[bucknum], *src_first);
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
// Used to actually do the rehashing when we grow/shrink a hashtable
|
|
714
|
+
void copy_from(const dense_hashtable &ht, size_type min_buckets_wanted) {
|
|
715
|
+
size_type size = ht.size(); // clear_to_size() sets ht.size() to 0.
|
|
716
|
+
clear_to_size(settings.min_buckets(ht.size(), min_buckets_wanted));
|
|
717
|
+
copy_elements(ht.begin(), ht.end(), table, bucket_count());
|
|
718
|
+
num_elements = size;
|
|
719
|
+
settings.inc_num_ht_copies();
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// Rebuckets and resizes the hashtable. Gets rid of any deleted entries.
|
|
723
|
+
void rebucket(size_type new_num_buckets) {
|
|
724
|
+
if (table == nullptr) {
|
|
725
|
+
// When we eventually allocate the table, it will have this many buckets.
|
|
726
|
+
num_buckets = new_num_buckets;
|
|
727
|
+
return;
|
|
728
|
+
}
|
|
729
|
+
assert(settings.use_empty());
|
|
730
|
+
assert((new_num_buckets & (new_num_buckets - 1)) == 0); // a power of two
|
|
731
|
+
// If settings.shrink_factor() is zero then we must not shrink.
|
|
732
|
+
assert(settings.shrink_factor() > 0 || new_num_buckets >= num_buckets);
|
|
733
|
+
pointer new_table = get_internal_allocator().allocate(new_num_buckets);
|
|
734
|
+
|
|
735
|
+
fill_range_with_empty(new_table, new_table + new_num_buckets);
|
|
736
|
+
copy_elements(std::make_move_iterator(begin()),
|
|
737
|
+
std::make_move_iterator(end()),
|
|
738
|
+
new_table, new_num_buckets);
|
|
739
|
+
|
|
740
|
+
destroy_buckets(0, num_buckets); // Destroy table's elements.
|
|
741
|
+
get_internal_allocator().deallocate(table, num_buckets);
|
|
742
|
+
|
|
743
|
+
table = new_table;
|
|
744
|
+
num_buckets = new_num_buckets;
|
|
745
|
+
assert(num_elements >= num_deleted);
|
|
746
|
+
num_elements -= num_deleted;
|
|
747
|
+
num_deleted = 0;
|
|
748
|
+
settings.reset_thresholds(bucket_count());
|
|
749
|
+
settings.inc_num_ht_copies();
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// Required by the spec for hashed associative container
|
|
753
|
+
public:
|
|
754
|
+
// Though the docs say this should be num_buckets, I think it's much
|
|
755
|
+
// more useful as num_elements. As a special feature, calling with
|
|
756
|
+
// req_elements==0 will cause us to shrink if we can, saving space.
|
|
757
|
+
void resize(size_type req_elements) { // resize to this or larger
|
|
758
|
+
if ( settings.consider_shrink() || req_elements == 0 )
|
|
759
|
+
maybe_shrink();
|
|
760
|
+
if ( req_elements > num_elements )
|
|
761
|
+
resize_delta(req_elements - num_elements);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Get and change the value of shrink_factor and enlarge_factor. The
|
|
765
|
+
// description at the beginning of this file explains how to choose
|
|
766
|
+
// the values. Setting the shrink parameter to 0.0 ensures that the
|
|
767
|
+
// table never shrinks.
|
|
768
|
+
void get_resizing_parameters(float* shrink, float* grow) const {
|
|
769
|
+
*shrink = settings.shrink_factor();
|
|
770
|
+
*grow = settings.enlarge_factor();
|
|
771
|
+
}
|
|
772
|
+
void set_resizing_parameters(float shrink, float grow) {
|
|
773
|
+
settings.set_resizing_parameters(shrink, grow);
|
|
774
|
+
settings.reset_thresholds(bucket_count());
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// CONSTRUCTORS -- as required by the specs, we take a size,
|
|
778
|
+
// but also let you specify a hashfunction, key comparator,
|
|
779
|
+
// and key extractor. We also define a copy constructor and =.
|
|
780
|
+
// DESTRUCTOR -- needs to free the table
|
|
781
|
+
explicit dense_hashtable(size_type expected_max_items_in_table = 0,
|
|
782
|
+
const HashFcn& hf = HashFcn(),
|
|
783
|
+
const EqualKey& eql = EqualKey(),
|
|
784
|
+
const ExtractKey& ext = ExtractKey(),
|
|
785
|
+
const SetKey& set = SetKey(),
|
|
786
|
+
const Alloc& alloc = Alloc())
|
|
787
|
+
: settings(hf),
|
|
788
|
+
key_info(ext, set, eql, value_alloc_type(alloc)),
|
|
789
|
+
num_deleted(0),
|
|
790
|
+
num_elements(0),
|
|
791
|
+
num_buckets(expected_max_items_in_table == 0
|
|
792
|
+
? HT_DEFAULT_STARTING_BUCKETS
|
|
793
|
+
: settings.min_buckets(expected_max_items_in_table, 0)),
|
|
794
|
+
table(nullptr) {
|
|
795
|
+
// table is nullptr until the empty key is set. However, we set num_buckets
|
|
796
|
+
// here so we know how much space to allocate once the empty key is set.
|
|
797
|
+
settings.reset_thresholds(bucket_count());
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
// As a convenience for resize(), we allow an optional second argument
|
|
801
|
+
// which lets you make this new hashtable a different size than ht
|
|
802
|
+
dense_hashtable(const dense_hashtable& ht,
|
|
803
|
+
size_type min_buckets_wanted = HT_DEFAULT_STARTING_BUCKETS)
|
|
804
|
+
: settings(ht.settings),
|
|
805
|
+
key_info(ht.key_info.as_extract_key(), ht.key_info.as_set_key(),
|
|
806
|
+
ht.key_info.as_equal_key(),
|
|
807
|
+
value_alloc_type(
|
|
808
|
+
std::allocator_traits<value_alloc_type>::
|
|
809
|
+
select_on_container_copy_construction(
|
|
810
|
+
ht.key_info.as_value_alloc()))),
|
|
811
|
+
num_deleted(0),
|
|
812
|
+
num_elements(0),
|
|
813
|
+
num_buckets(0),
|
|
814
|
+
table(nullptr) {
|
|
815
|
+
key_info.delkey = ht.key_info.delkey;
|
|
816
|
+
key_info.empty = ht.key_info.empty;
|
|
817
|
+
if (!ht.settings.use_empty()) {
|
|
818
|
+
// If use_empty isn't set, copy_from will crash, so we do our own copying.
|
|
819
|
+
assert(ht.empty());
|
|
820
|
+
num_buckets = settings.min_buckets(ht.size(), min_buckets_wanted);
|
|
821
|
+
settings.reset_thresholds(bucket_count());
|
|
822
|
+
return;
|
|
823
|
+
}
|
|
824
|
+
settings.reset_thresholds(bucket_count());
|
|
825
|
+
copy_from(ht, min_buckets_wanted); // copy_from() ignores deleted entries
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
dense_hashtable& operator=(const dense_hashtable& ht) {
|
|
829
|
+
if (&ht == this) return *this; // don't copy onto ourselves
|
|
830
|
+
settings = ht.settings;
|
|
831
|
+
key_info.as_extract_key() = ht.key_info.as_extract_key();
|
|
832
|
+
key_info.as_set_key() = ht.key_info.as_set_key();
|
|
833
|
+
key_info.as_equal_key() = ht.key_info.as_equal_key();
|
|
834
|
+
if (std::allocator_traits<
|
|
835
|
+
value_alloc_type>::propagate_on_container_copy_assignment::value) {
|
|
836
|
+
// If we're about to overwrite our allocator, we need to free all
|
|
837
|
+
// memory using our old allocator.
|
|
838
|
+
if (key_info.as_value_alloc() != ht.key_info.as_value_alloc()) {
|
|
839
|
+
destroy_table();
|
|
840
|
+
}
|
|
841
|
+
static_cast<value_alloc_type&>(key_info) =
|
|
842
|
+
static_cast<const value_alloc_type&>(ht.key_info);
|
|
843
|
+
}
|
|
844
|
+
key_info.empty = ht.key_info.empty;
|
|
845
|
+
key_info.delkey = ht.key_info.delkey;
|
|
846
|
+
|
|
847
|
+
if (ht.settings.use_empty()) {
|
|
848
|
+
// copy_from() calls clear and sets num_deleted to 0 too
|
|
849
|
+
copy_from(ht, HT_MIN_BUCKETS);
|
|
850
|
+
} else {
|
|
851
|
+
assert(ht.empty());
|
|
852
|
+
destroy_table();
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
// we purposefully don't copy the allocator, which may not be copyable
|
|
856
|
+
return *this;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
dense_hashtable(dense_hashtable&& ht)
|
|
860
|
+
: settings(std::move(ht.settings)),
|
|
861
|
+
key_info(std::move(ht.key_info)),
|
|
862
|
+
num_deleted(ht.num_deleted),
|
|
863
|
+
num_elements(ht.num_elements),
|
|
864
|
+
num_buckets(ht.num_buckets),
|
|
865
|
+
table(ht.table) {
|
|
866
|
+
ht.num_deleted = 0;
|
|
867
|
+
ht.num_elements = 0;
|
|
868
|
+
ht.table = nullptr;
|
|
869
|
+
ht.num_buckets = HT_DEFAULT_STARTING_BUCKETS;
|
|
870
|
+
ht.settings.set_use_empty(false);
|
|
871
|
+
ht.settings.set_use_deleted(false);
|
|
872
|
+
}
|
|
873
|
+
|
|
874
|
+
dense_hashtable& operator=(dense_hashtable&& ht) {
|
|
875
|
+
if (&ht == this) return *this; // don't move onto ourselves
|
|
876
|
+
|
|
877
|
+
const bool can_move_table =
|
|
878
|
+
std::allocator_traits<
|
|
879
|
+
Alloc>::propagate_on_container_move_assignment::value ||
|
|
880
|
+
key_info.as_value_alloc() == ht.key_info.as_value_alloc();
|
|
881
|
+
|
|
882
|
+
// First, deallocate with this's allocator.
|
|
883
|
+
destroy_table();
|
|
884
|
+
|
|
885
|
+
if (std::allocator_traits<
|
|
886
|
+
value_alloc_type>::propagate_on_container_move_assignment::value) {
|
|
887
|
+
// This moves the allocator.
|
|
888
|
+
key_info = std::move(ht.key_info);
|
|
889
|
+
} else {
|
|
890
|
+
// Move all other base classes of key_info from ht, but don't move the
|
|
891
|
+
// allocator.
|
|
892
|
+
key_info.as_extract_key() = std::move(ht.key_info.as_extract_key());
|
|
893
|
+
key_info.as_set_key() = std::move(ht.key_info.as_set_key());
|
|
894
|
+
key_info.as_equal_key() = std::move(ht.key_info.as_equal_key());
|
|
895
|
+
key_info.delkey = std::move(ht.key_info.delkey);
|
|
896
|
+
key_info.empty = std::move(ht.key_info.empty);
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
settings = std::move(ht.settings);
|
|
900
|
+
num_deleted = ht.num_deleted;
|
|
901
|
+
ht.num_deleted = 0;
|
|
902
|
+
num_elements = ht.num_elements;
|
|
903
|
+
ht.num_elements = 0;
|
|
904
|
+
num_buckets = ht.num_buckets;
|
|
905
|
+
ht.num_buckets = HT_DEFAULT_STARTING_BUCKETS;
|
|
906
|
+
ht.settings.set_use_empty(false);
|
|
907
|
+
ht.settings.set_use_deleted(false);
|
|
908
|
+
|
|
909
|
+
if (can_move_table) {
|
|
910
|
+
// We can transfer ownership of the table from ht to this because either
|
|
911
|
+
// we're propagating the allocator or ht's allocator is equal to this's.
|
|
912
|
+
table = ht.table;
|
|
913
|
+
ht.table = nullptr;
|
|
914
|
+
} else if (ht.table) {
|
|
915
|
+
// We can't transfer ownership of any memory from ht to this, so the
|
|
916
|
+
// best we can do is move element-by-element.
|
|
917
|
+
table = get_internal_allocator().allocate(num_buckets);
|
|
918
|
+
for (size_type i = 0; i < num_buckets; ++i) {
|
|
919
|
+
new(table + i) Value(std::move(ht.table[i]));
|
|
920
|
+
}
|
|
921
|
+
ht.destroy_table();
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
return *this;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
~dense_hashtable() { destroy_table(); }
|
|
928
|
+
|
|
929
|
+
// Many STL algorithms use swap instead of copy constructors
|
|
930
|
+
void swap(dense_hashtable& ht) {
|
|
931
|
+
if (this == &ht) return; // swap with self.
|
|
932
|
+
using std::swap;
|
|
933
|
+
swap(settings, ht.settings);
|
|
934
|
+
// Swap everything in key_info but the allocator.
|
|
935
|
+
swap(key_info.as_extract_key(), ht.key_info.as_extract_key());
|
|
936
|
+
swap(key_info.as_set_key(), ht.key_info.as_set_key());
|
|
937
|
+
swap(key_info.as_equal_key(), ht.key_info.as_equal_key());
|
|
938
|
+
if (std::allocator_traits<
|
|
939
|
+
value_alloc_type>::propagate_on_container_swap::value) {
|
|
940
|
+
swap(static_cast<value_alloc_type&>(key_info),
|
|
941
|
+
static_cast<value_alloc_type&>(ht.key_info));
|
|
942
|
+
} else {
|
|
943
|
+
// Swapping when allocators are unequal and
|
|
944
|
+
// propagate_on_container_swap is false is undefined behavior.
|
|
945
|
+
S2_CHECK(key_info.as_value_alloc() == ht.key_info.as_value_alloc());
|
|
946
|
+
}
|
|
947
|
+
swap(key_info.empty, ht.key_info.empty);
|
|
948
|
+
swap(key_info.delkey, ht.key_info.delkey);
|
|
949
|
+
swap(num_deleted, ht.num_deleted);
|
|
950
|
+
swap(num_elements, ht.num_elements);
|
|
951
|
+
swap(num_buckets, ht.num_buckets);
|
|
952
|
+
swap(table, ht.table);
|
|
953
|
+
}
|
|
954
|
+
|
|
955
|
+
private:
|
|
956
|
+
void destroy_table() {
|
|
957
|
+
if (table) {
|
|
958
|
+
destroy_buckets(0, num_buckets);
|
|
959
|
+
get_internal_allocator().deallocate(table, num_buckets);
|
|
960
|
+
table = nullptr;
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
void clear_to_size(size_type new_num_buckets) {
|
|
965
|
+
if (!table) {
|
|
966
|
+
table = get_internal_allocator().allocate(new_num_buckets);
|
|
967
|
+
} else {
|
|
968
|
+
destroy_buckets(0, num_buckets);
|
|
969
|
+
if (new_num_buckets != num_buckets) { // resize, if necessary
|
|
970
|
+
resize_table(num_buckets, new_num_buckets);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
assert(table);
|
|
974
|
+
fill_range_with_empty(table, table + new_num_buckets);
|
|
975
|
+
num_elements = 0;
|
|
976
|
+
num_deleted = 0;
|
|
977
|
+
num_buckets = new_num_buckets; // our new size
|
|
978
|
+
settings.reset_thresholds(bucket_count());
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
public:
|
|
982
|
+
// It's always nice to be able to clear a table without deallocating it
|
|
983
|
+
void clear() {
|
|
984
|
+
// If the table is already empty, and the number of buckets is
|
|
985
|
+
// already as we desire, there's nothing to do.
|
|
986
|
+
const size_type new_num_buckets = settings.min_buckets(0, 0);
|
|
987
|
+
if (num_elements == 0 && new_num_buckets == num_buckets) {
|
|
988
|
+
return;
|
|
989
|
+
}
|
|
990
|
+
clear_to_size(new_num_buckets);
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
// Clear the table without resizing it.
|
|
994
|
+
// Mimicks the stl_hashtable's behaviour when clear()-ing in that it
|
|
995
|
+
// does not modify the bucket count
|
|
996
|
+
void clear_no_resize() {
|
|
997
|
+
if (num_elements > 0) {
|
|
998
|
+
assert(table);
|
|
999
|
+
destroy_buckets(0, num_buckets);
|
|
1000
|
+
fill_range_with_empty(table, table + num_buckets);
|
|
1001
|
+
}
|
|
1002
|
+
// don't consider to shrink before another erase()
|
|
1003
|
+
settings.reset_thresholds(bucket_count());
|
|
1004
|
+
num_elements = 0;
|
|
1005
|
+
num_deleted = 0;
|
|
1006
|
+
}
|
|
1007
|
+
|
|
1008
|
+
// LOOKUP ROUTINES
|
|
1009
|
+
private:
|
|
1010
|
+
template <class K>
|
|
1011
|
+
void assert_key_is_not_empty_or_deleted(const K& key) const {
|
|
1012
|
+
assert(settings.use_empty() && "set_empty_key() was not called");
|
|
1013
|
+
assert(!equals(key, key_info.empty) &&
|
|
1014
|
+
"Using the empty key as a regular key");
|
|
1015
|
+
assert((!settings.use_deleted() || !equals(key, key_info.delkey))
|
|
1016
|
+
&& "Using the deleted key as a regular key");
|
|
1017
|
+
}
|
|
1018
|
+
|
|
1019
|
+
template <class K>
|
|
1020
|
+
std::pair<size_type, size_type> find_position(const K& key) const {
|
|
1021
|
+
return find_position_using_hash(hash(key), key);
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
// Returns a pair of positions: 1st where the object is, 2nd where
|
|
1025
|
+
// it would go if you wanted to insert it. 1st is ILLEGAL_BUCKET
|
|
1026
|
+
// if object is not found; 2nd is ILLEGAL_BUCKET if it is.
|
|
1027
|
+
// Note: because of deletions where-to-insert is not trivial: it's the
|
|
1028
|
+
// first deleted bucket we see, as long as we don't find the key later
|
|
1029
|
+
template <class K>
|
|
1030
|
+
std::pair<size_type, size_type> find_position_using_hash(
|
|
1031
|
+
const size_type key_hash, const K& key) const {
|
|
1032
|
+
assert_key_is_not_empty_or_deleted(key);
|
|
1033
|
+
size_type num_probes = 0; // how many times we've probed
|
|
1034
|
+
const size_type bucket_count_minus_one = bucket_count() - 1;
|
|
1035
|
+
size_type bucknum = key_hash & bucket_count_minus_one;
|
|
1036
|
+
size_type insert_pos = ILLEGAL_BUCKET; // where we would insert
|
|
1037
|
+
while (1) { // probe until something happens
|
|
1038
|
+
if (test_empty(bucknum)) { // bucket is empty
|
|
1039
|
+
if (insert_pos == ILLEGAL_BUCKET) // found no prior place to insert
|
|
1040
|
+
return std::pair<size_type, size_type>(ILLEGAL_BUCKET, bucknum);
|
|
1041
|
+
else
|
|
1042
|
+
return std::pair<size_type, size_type>(ILLEGAL_BUCKET, insert_pos);
|
|
1043
|
+
|
|
1044
|
+
} else if (test_deleted(bucknum)) {
|
|
1045
|
+
// keep searching, but mark to insert
|
|
1046
|
+
if ( insert_pos == ILLEGAL_BUCKET )
|
|
1047
|
+
insert_pos = bucknum;
|
|
1048
|
+
|
|
1049
|
+
} else if (equals(key, get_key(table[bucknum]))) {
|
|
1050
|
+
return std::pair<size_type, size_type>(bucknum, ILLEGAL_BUCKET);
|
|
1051
|
+
}
|
|
1052
|
+
++num_probes; // we're doing another probe
|
|
1053
|
+
bucknum = (bucknum + JUMP_(key, num_probes)) & bucket_count_minus_one;
|
|
1054
|
+
assert(num_probes < bucket_count()
|
|
1055
|
+
&& "Hashtable is full: an error in key_equal<> or hash<>");
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
|
|
1059
|
+
template <class K>
|
|
1060
|
+
std::pair<size_type, bool> find_if_present(const K& key) const {
|
|
1061
|
+
return find_if_present_using_hash(hash(key), key);
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
// Return where the key is (if at all), and if it is present. If
|
|
1065
|
+
// the key isn't present then the first part of the return value is
|
|
1066
|
+
// undefined. The same information can be extracted from the result
|
|
1067
|
+
// of find_position(), but that tends to be slower in practice.
|
|
1068
|
+
template <class K>
|
|
1069
|
+
std::pair<size_type, bool> find_if_present_using_hash(
|
|
1070
|
+
const size_type key_hash, const K& key) const {
|
|
1071
|
+
assert_key_is_not_empty_or_deleted(key);
|
|
1072
|
+
size_type num_probes = 0; // how many times we've probed
|
|
1073
|
+
const size_type bucket_count_minus_one = bucket_count() - 1;
|
|
1074
|
+
size_type bucknum = key_hash & bucket_count_minus_one;
|
|
1075
|
+
while (1) { // probe until something happens
|
|
1076
|
+
if (equals(key, get_key(table[bucknum]))) {
|
|
1077
|
+
return std::pair<size_type, bool>(bucknum, true);
|
|
1078
|
+
} else if (test_empty(bucknum)) {
|
|
1079
|
+
return std::pair<size_type, bool>(0, false);
|
|
1080
|
+
}
|
|
1081
|
+
++num_probes; // we're doing another probe
|
|
1082
|
+
bucknum = (bucknum + JUMP_(key, num_probes)) & bucket_count_minus_one;
|
|
1083
|
+
assert(num_probes < bucket_count()
|
|
1084
|
+
&& "Hashtable is full: an error in key_equal<> or hash<>");
|
|
1085
|
+
}
|
|
1086
|
+
}
|
|
1087
|
+
|
|
1088
|
+
private:
|
|
1089
|
+
template <class K>
|
|
1090
|
+
iterator find_impl(const K& key) {
|
|
1091
|
+
std::pair<size_type, bool> pos = find_if_present(key);
|
|
1092
|
+
return pos.second ?
|
|
1093
|
+
iterator(this, table + pos.first, table + num_buckets, false) :
|
|
1094
|
+
end();
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
template <class K>
|
|
1098
|
+
const_iterator find_impl(const K& key) const {
|
|
1099
|
+
std::pair<size_type, bool> pos = find_if_present(key);
|
|
1100
|
+
return pos.second ?
|
|
1101
|
+
const_iterator(this, table + pos.first, table + num_buckets, false) :
|
|
1102
|
+
end();
|
|
1103
|
+
}
|
|
1104
|
+
|
|
1105
|
+
template <class K>
|
|
1106
|
+
size_type bucket_impl(const K& key) const {
|
|
1107
|
+
std::pair<size_type, size_type> pos = find_position(key);
|
|
1108
|
+
return pos.first == ILLEGAL_BUCKET ? pos.second : pos.first;
|
|
1109
|
+
}
|
|
1110
|
+
|
|
1111
|
+
template <class K>
|
|
1112
|
+
size_type count_impl(const K& key) const {
|
|
1113
|
+
return find_if_present(key).second ? 1 : 0;
|
|
1114
|
+
}
|
|
1115
|
+
|
|
1116
|
+
template <class K>
|
|
1117
|
+
std::pair<iterator, iterator>
|
|
1118
|
+
equal_range_impl(const K& key) {
|
|
1119
|
+
iterator pos = find(key);
|
|
1120
|
+
if (pos == end()) {
|
|
1121
|
+
return std::pair<iterator, iterator>(pos, pos);
|
|
1122
|
+
} else {
|
|
1123
|
+
const iterator startpos = pos++;
|
|
1124
|
+
return std::pair<iterator, iterator>(startpos, pos);
|
|
1125
|
+
}
|
|
1126
|
+
}
|
|
1127
|
+
|
|
1128
|
+
template <class K>
|
|
1129
|
+
std::pair<const_iterator, const_iterator>
|
|
1130
|
+
equal_range_impl(const K& key) const {
|
|
1131
|
+
const_iterator pos = find(key);
|
|
1132
|
+
if (pos == end()) {
|
|
1133
|
+
return std::pair<const_iterator, const_iterator>(pos, pos);
|
|
1134
|
+
} else {
|
|
1135
|
+
const const_iterator startpos = pos++;
|
|
1136
|
+
return std::pair<const_iterator, const_iterator>(startpos, pos);
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
public:
|
|
1141
|
+
iterator find(const key_type& key) { return find_impl(key); }
|
|
1142
|
+
|
|
1143
|
+
const_iterator find(const key_type& key) const { return find_impl(key); }
|
|
1144
|
+
|
|
1145
|
+
// This is a tr1 method: the bucket a given key is in, or what bucket
|
|
1146
|
+
// it would be put in, if it were to be inserted. Shrug.
|
|
1147
|
+
size_type bucket(const key_type& key) const { return bucket_impl(key); }
|
|
1148
|
+
|
|
1149
|
+
// Counts how many elements have key key. For maps, it's either 0 or 1.
|
|
1150
|
+
size_type count(const key_type &key) const { return count_impl(key); }
|
|
1151
|
+
|
|
1152
|
+
// Likewise, equal_range doesn't really make sense for us. Oh well.
|
|
1153
|
+
std::pair<iterator, iterator>
|
|
1154
|
+
equal_range(const key_type& key) { return equal_range_impl(key); }
|
|
1155
|
+
|
|
1156
|
+
std::pair<const_iterator, const_iterator>
|
|
1157
|
+
equal_range(const key_type& key) const { return equal_range_impl(key); }
|
|
1158
|
+
|
|
1159
|
+
|
|
1160
|
+
|
|
1161
|
+
// INSERTION ROUTINES
|
|
1162
|
+
private:
|
|
1163
|
+
// Private method used by insert_noresize and find_or_insert.
|
|
1164
|
+
// 'obj' is either value_type&& or const value_type&.
|
|
1165
|
+
template <typename U>
|
|
1166
|
+
iterator insert_at(U&& obj, size_type pos) {
|
|
1167
|
+
if (size() >= max_size()) {
|
|
1168
|
+
throw std::length_error("insert overflow");
|
|
1169
|
+
}
|
|
1170
|
+
if ( test_deleted(pos) ) { // just replace if it's been del.
|
|
1171
|
+
assert(num_deleted > 0);
|
|
1172
|
+
--num_deleted; // used to be, now it isn't
|
|
1173
|
+
} else {
|
|
1174
|
+
++num_elements; // replacing an empty bucket
|
|
1175
|
+
}
|
|
1176
|
+
set_value(&table[pos], std::forward<U>(obj));
|
|
1177
|
+
return iterator(this, table + pos, table + num_buckets, false);
|
|
1178
|
+
}
|
|
1179
|
+
|
|
1180
|
+
// If you know *this is big enough to hold obj, use this routine
|
|
1181
|
+
// 'obj' is value_type&& or const value_type&.
|
|
1182
|
+
template <typename U>
|
|
1183
|
+
std::pair<iterator, bool> insert_noresize(U&& obj) { // NOLINT
|
|
1184
|
+
return insert_noresize_using_hash(hash(get_key(obj)), std::forward<U>(obj));
|
|
1185
|
+
}
|
|
1186
|
+
|
|
1187
|
+
// If you know *this is big enough to hold obj, use this routine
|
|
1188
|
+
// 'obj' is value_type&& or const value_type&.
|
|
1189
|
+
template <typename U>
|
|
1190
|
+
std::pair<iterator, bool> insert_noresize_using_hash(const size_type key_hash,
|
|
1191
|
+
U&& obj) {
|
|
1192
|
+
const std::pair<size_type, size_type> pos =
|
|
1193
|
+
find_position_using_hash(key_hash, get_key(obj));
|
|
1194
|
+
if (pos.first != ILLEGAL_BUCKET) { // object was already there
|
|
1195
|
+
return std::pair<iterator, bool>(iterator(this, table + pos.first,
|
|
1196
|
+
table + num_buckets, false),
|
|
1197
|
+
false); // false: we didn't insert
|
|
1198
|
+
} else { // pos.second says where to put it
|
|
1199
|
+
iterator i = insert_at(std::forward<U>(obj), pos.second);
|
|
1200
|
+
return std::pair<iterator, bool>(i, true);
|
|
1201
|
+
}
|
|
1202
|
+
}
|
|
1203
|
+
|
|
1204
|
+
// Specializations of insert(it, it) depending on the power of the iterator:
|
|
1205
|
+
// (1) Iterator supports operator-, resize before inserting
|
|
1206
|
+
template <class ForwardIterator>
|
|
1207
|
+
void insert(ForwardIterator f, ForwardIterator l, std::forward_iterator_tag) {
|
|
1208
|
+
size_t dist = std::distance(f, l);
|
|
1209
|
+
if (dist >= std::numeric_limits<size_type>::max()) {
|
|
1210
|
+
throw std::length_error("insert-range overflow");
|
|
1211
|
+
}
|
|
1212
|
+
resize_delta(static_cast<size_type>(dist));
|
|
1213
|
+
for ( ; dist > 0; --dist, ++f) {
|
|
1214
|
+
insert_noresize(*f);
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
// (2) Arbitrary iterator, can't tell how much to resize
|
|
1219
|
+
template <class InputIterator>
|
|
1220
|
+
void insert(InputIterator f, InputIterator l, std::input_iterator_tag) {
|
|
1221
|
+
for ( ; f != l; ++f)
|
|
1222
|
+
insert(*f);
|
|
1223
|
+
}
|
|
1224
|
+
|
|
1225
|
+
public:
|
|
1226
|
+
// This is the normal insert routine, used by the outside world
|
|
1227
|
+
std::pair<iterator, bool> insert(const value_type& obj) {
|
|
1228
|
+
resize_delta(1); // adding an object, grow if need be
|
|
1229
|
+
return insert_noresize(obj);
|
|
1230
|
+
}
|
|
1231
|
+
|
|
1232
|
+
std::pair<iterator, bool> insert(value_type&& obj) { // NOLINT
|
|
1233
|
+
resize_delta(1); // adding an object, grow if need be
|
|
1234
|
+
return insert_noresize(std::move(obj));
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
// When inserting a lot at a time, we specialize on the type of iterator
|
|
1238
|
+
template <class InputIterator>
|
|
1239
|
+
void insert(InputIterator f, InputIterator l) {
|
|
1240
|
+
// specializes on iterator type
|
|
1241
|
+
insert(f, l,
|
|
1242
|
+
typename std::iterator_traits<InputIterator>::iterator_category());
|
|
1243
|
+
}
|
|
1244
|
+
|
|
1245
|
+
template <class DefaultValue>
|
|
1246
|
+
value_type& find_or_insert(const key_type& key) {
|
|
1247
|
+
return find_or_insert_using_hash<DefaultValue>(hash(key), key);
|
|
1248
|
+
}
|
|
1249
|
+
|
|
1250
|
+
// DefaultValue is a functor that takes a key and returns a value_type
|
|
1251
|
+
// representing the default value to be inserted if none is found.
|
|
1252
|
+
template <class DefaultValue>
|
|
1253
|
+
value_type& find_or_insert_using_hash(const size_type key_hash,
|
|
1254
|
+
const key_type& key) {
|
|
1255
|
+
const std::pair<size_type, size_type> pos =
|
|
1256
|
+
find_position_using_hash(key_hash, key);
|
|
1257
|
+
DefaultValue default_value;
|
|
1258
|
+
if (pos.first != ILLEGAL_BUCKET) { // object was already there
|
|
1259
|
+
return table[pos.first];
|
|
1260
|
+
} else if (resize_delta(1)) { // needed to rehash to make room
|
|
1261
|
+
// Since we resized, we can't use pos, so recalculate where to insert.
|
|
1262
|
+
return *insert_noresize(default_value(key)).first;
|
|
1263
|
+
} else { // no need to rehash, insert right here
|
|
1264
|
+
return *insert_at(default_value(key), pos.second);
|
|
1265
|
+
}
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
|
|
1269
|
+
// DELETION ROUTINES
|
|
1270
|
+
private:
|
|
1271
|
+
template <class K>
|
|
1272
|
+
size_type erase_impl(const K& key) {
|
|
1273
|
+
iterator pos = find(key);
|
|
1274
|
+
if (pos != end()) {
|
|
1275
|
+
assert(!test_deleted(pos)); // or find() shouldn't have returned it
|
|
1276
|
+
set_deleted(pos);
|
|
1277
|
+
++num_deleted;
|
|
1278
|
+
// will think about shrink after next insert
|
|
1279
|
+
settings.set_consider_shrink(true);
|
|
1280
|
+
return 1; // because we deleted one thing
|
|
1281
|
+
} else {
|
|
1282
|
+
return 0; // because we deleted nothing
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
|
|
1286
|
+
public:
|
|
1287
|
+
size_type erase(const key_type& key) {
|
|
1288
|
+
return erase_impl(key);
|
|
1289
|
+
}
|
|
1290
|
+
|
|
1291
|
+
|
|
1292
|
+
void erase(iterator pos) {
|
|
1293
|
+
if (pos == end()) return; // sanity check
|
|
1294
|
+
set_deleted(pos);
|
|
1295
|
+
++num_deleted;
|
|
1296
|
+
// will think about shrink after next insert
|
|
1297
|
+
settings.set_consider_shrink(true);
|
|
1298
|
+
}
|
|
1299
|
+
|
|
1300
|
+
void erase(iterator f, iterator l) {
|
|
1301
|
+
for (; f != l; ++f) {
|
|
1302
|
+
set_deleted(f);
|
|
1303
|
+
++num_deleted;
|
|
1304
|
+
}
|
|
1305
|
+
// will think about shrink after next insert
|
|
1306
|
+
settings.set_consider_shrink(true);
|
|
1307
|
+
}
|
|
1308
|
+
|
|
1309
|
+
// We allow you to erase a const_iterator just like we allow you to
|
|
1310
|
+
// erase an iterator. This is in parallel to 'delete': you can delete
|
|
1311
|
+
// a const pointer just like a non-const pointer. The logic is that
|
|
1312
|
+
// you can't use the object after it's erased anyway, so it doesn't matter
|
|
1313
|
+
// if it's const or not.
|
|
1314
|
+
void erase(const_iterator pos) {
|
|
1315
|
+
if (pos == end()) return; // sanity check
|
|
1316
|
+
set_deleted(pos);
|
|
1317
|
+
++num_deleted;
|
|
1318
|
+
// will think about shrink after next insert
|
|
1319
|
+
settings.set_consider_shrink(true);
|
|
1320
|
+
}
|
|
1321
|
+
void erase(const_iterator f, const_iterator l) {
|
|
1322
|
+
for ( ; f != l; ++f) {
|
|
1323
|
+
set_deleted(f);
|
|
1324
|
+
++num_deleted;
|
|
1325
|
+
}
|
|
1326
|
+
// will think about shrink after next insert
|
|
1327
|
+
settings.set_consider_shrink(true);
|
|
1328
|
+
}
|
|
1329
|
+
|
|
1330
|
+
|
|
1331
|
+
// COMPARISON
|
|
1332
|
+
bool operator==(const dense_hashtable& ht) const {
|
|
1333
|
+
if (size() != ht.size()) {
|
|
1334
|
+
return false;
|
|
1335
|
+
} else if (this == &ht) {
|
|
1336
|
+
return true;
|
|
1337
|
+
} else {
|
|
1338
|
+
// Iterate through the elements in "this" and see if the
|
|
1339
|
+
// corresponding element is in ht
|
|
1340
|
+
for ( const_iterator it = begin(); it != end(); ++it ) {
|
|
1341
|
+
const_iterator it2 = ht.find(get_key(*it));
|
|
1342
|
+
if ((it2 == ht.end()) || (*it != *it2)) {
|
|
1343
|
+
return false;
|
|
1344
|
+
}
|
|
1345
|
+
}
|
|
1346
|
+
return true;
|
|
1347
|
+
}
|
|
1348
|
+
}
|
|
1349
|
+
bool operator!=(const dense_hashtable& ht) const {
|
|
1350
|
+
return !(*this == ht);
|
|
1351
|
+
}
|
|
1352
|
+
|
|
1353
|
+
|
|
1354
|
+
// I/O
|
|
1355
|
+
// We support reading and writing hashtables to disk. Alas, since
|
|
1356
|
+
// I don't know how to write a hasher or key_equal, you have to make
|
|
1357
|
+
// sure everything but the table is the same. We compact before writing.
|
|
1358
|
+
private:
|
|
1359
|
+
// Every time the disk format changes, this should probably change too
|
|
1360
|
+
typedef unsigned long MagicNumberType;
|
|
1361
|
+
static const MagicNumberType MAGIC_NUMBER = 0x13578642;
|
|
1362
|
+
|
|
1363
|
+
// Package functors with another class to eliminate memory needed for
|
|
1364
|
+
// zero-size functors. Since ExtractKey and hasher's operator() might
|
|
1365
|
+
// have the same function signature, they must be packaged in
|
|
1366
|
+
// different classes.
|
|
1367
|
+
struct Settings :
|
|
1368
|
+
sh_hashtable_settings<key_type, hasher, size_type, HT_MIN_BUCKETS> {
|
|
1369
|
+
explicit Settings(const hasher& hf)
|
|
1370
|
+
: sh_hashtable_settings<key_type, hasher, size_type, HT_MIN_BUCKETS>(
|
|
1371
|
+
hf, HT_OCCUPANCY_PCT / 100.0f, HT_EMPTY_PCT / 100.0f) {}
|
|
1372
|
+
};
|
|
1373
|
+
|
|
1374
|
+
// Packages ExtractKey, SetKey, EqualKey functors, allocator and deleted and
|
|
1375
|
+
// empty key values.
|
|
1376
|
+
struct KeyInfo : public ExtractKey,
|
|
1377
|
+
public SetKey,
|
|
1378
|
+
public EqualKey,
|
|
1379
|
+
public value_alloc_type {
|
|
1380
|
+
KeyInfo(const ExtractKey& ek, const SetKey& sk, const EqualKey& eq,
|
|
1381
|
+
const value_alloc_type& a)
|
|
1382
|
+
: ExtractKey(ek),
|
|
1383
|
+
SetKey(sk),
|
|
1384
|
+
EqualKey(eq),
|
|
1385
|
+
value_alloc_type(a),
|
|
1386
|
+
delkey(),
|
|
1387
|
+
empty() {}
|
|
1388
|
+
|
|
1389
|
+
// Accessors for convenient access to base classes.
|
|
1390
|
+
ExtractKey& as_extract_key() { return *this; }
|
|
1391
|
+
const ExtractKey& as_extract_key() const { return *this; }
|
|
1392
|
+
SetKey& as_set_key() { return *this; }
|
|
1393
|
+
const SetKey& as_set_key() const { return *this; }
|
|
1394
|
+
EqualKey& as_equal_key() { return *this; }
|
|
1395
|
+
const EqualKey& as_equal_key() const { return *this; }
|
|
1396
|
+
value_alloc_type& as_value_alloc() { return *this; }
|
|
1397
|
+
const value_alloc_type& as_value_alloc() const { return *this; }
|
|
1398
|
+
|
|
1399
|
+
// We want to return the exact same type as ExtractKey: Key or const Key&
|
|
1400
|
+
typename ExtractKey::result_type get_key(const_reference v) const {
|
|
1401
|
+
return ExtractKey::operator()(v);
|
|
1402
|
+
}
|
|
1403
|
+
void set_key(pointer v, const key_type& k) const {
|
|
1404
|
+
SetKey::operator()(v, k);
|
|
1405
|
+
}
|
|
1406
|
+
|
|
1407
|
+
// We only ever call EqualKey::operator()(key_type, K) -- we never use the
|
|
1408
|
+
// other order of args. This allows consumers to get away with implementing
|
|
1409
|
+
// only half of operator==.
|
|
1410
|
+
template <class K>
|
|
1411
|
+
bool equals(const key_type& a, const K& b) const {
|
|
1412
|
+
return EqualKey::operator()(a, b);
|
|
1413
|
+
}
|
|
1414
|
+
|
|
1415
|
+
pointer allocate(size_type size) {
|
|
1416
|
+
pointer memory = value_alloc_type::allocate(size);
|
|
1417
|
+
assert(memory != nullptr);
|
|
1418
|
+
return memory;
|
|
1419
|
+
}
|
|
1420
|
+
|
|
1421
|
+
// Which key marks deleted entries.
|
|
1422
|
+
// TODO(user): make a pointer, and get rid of use_deleted (benchmark!)
|
|
1423
|
+
typename std::remove_const<key_type>::type delkey;
|
|
1424
|
+
// Key value used to mark unused entries.
|
|
1425
|
+
typename std::remove_const<key_type>::type empty;
|
|
1426
|
+
};
|
|
1427
|
+
|
|
1428
|
+
// Returns the value_alloc_type used to allocate and deallocate
|
|
1429
|
+
// the table. This can be different from the one returned by get_allocator().
|
|
1430
|
+
value_alloc_type& get_internal_allocator() { return key_info; }
|
|
1431
|
+
|
|
1432
|
+
// Utility functions to access the templated operators
|
|
1433
|
+
size_type hash(const key_type& v) const {
|
|
1434
|
+
return settings.hash(v);
|
|
1435
|
+
}
|
|
1436
|
+
bool equals(const key_type& a, const key_type& b) const {
|
|
1437
|
+
return key_info.equals(a, b);
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
|
|
1441
|
+
typename ExtractKey::result_type get_key(const_reference v) const {
|
|
1442
|
+
return key_info.get_key(v);
|
|
1443
|
+
}
|
|
1444
|
+
void set_key(pointer v, const key_type& k) const {
|
|
1445
|
+
key_info.set_key(v, k);
|
|
1446
|
+
}
|
|
1447
|
+
|
|
1448
|
+
private:
|
|
1449
|
+
// Actual data
|
|
1450
|
+
Settings settings;
|
|
1451
|
+
KeyInfo key_info;
|
|
1452
|
+
|
|
1453
|
+
size_type num_deleted; // how many occupied buckets are marked deleted
|
|
1454
|
+
size_type num_elements;
|
|
1455
|
+
size_type num_buckets;
|
|
1456
|
+
pointer table;
|
|
1457
|
+
};
|
|
1458
|
+
|
|
1459
|
+
|
|
1460
|
+
// We need a global swap as well
|
|
1461
|
+
template <class V, class K, class HF, class ExK, class SetK, class EqK, class A>
|
|
1462
|
+
inline void swap(dense_hashtable<V, K, HF, ExK, SetK, EqK, A> &x,
|
|
1463
|
+
dense_hashtable<V, K, HF, ExK, SetK, EqK, A> &y) {
|
|
1464
|
+
x.swap(y);
|
|
1465
|
+
}
|
|
1466
|
+
|
|
1467
|
+
#undef JUMP_
|
|
1468
|
+
#undef PUT_
|
|
1469
|
+
#undef GET_
|
|
1470
|
+
|
|
1471
|
+
template <class V, class K, class HF, class ExK, class SetK, class EqK, class A>
|
|
1472
|
+
const typename dense_hashtable<V, K, HF, ExK, SetK, EqK, A>::size_type
|
|
1473
|
+
dense_hashtable<V, K, HF, ExK, SetK, EqK, A>::ILLEGAL_BUCKET;
|
|
1474
|
+
|
|
1475
|
+
// How full we let the table get before we resize. Knuth says .8 is
|
|
1476
|
+
// good -- higher causes us to probe too much, though saves memory.
|
|
1477
|
+
// However, we go with .5, getting better performance at the cost of
|
|
1478
|
+
// more space (a trade-off densehashtable explicitly chooses to make).
|
|
1479
|
+
// Feel free to play around with different values, though, via
|
|
1480
|
+
// max_load_factor() and/or set_resizing_parameters().
|
|
1481
|
+
template <class V, class K, class HF, class ExK, class SetK, class EqK, class A>
|
|
1482
|
+
const int dense_hashtable<V,K,HF,ExK,SetK,EqK,A>::HT_OCCUPANCY_PCT = 50;
|
|
1483
|
+
|
|
1484
|
+
// How empty we let the table get before we resize lower.
|
|
1485
|
+
// It should be less than OCCUPANCY_PCT / 2 or we thrash resizing.
|
|
1486
|
+
template <class V, class K, class HF, class ExK, class SetK, class EqK, class A>
|
|
1487
|
+
const int dense_hashtable<V, K, HF, ExK, SetK, EqK, A>::HT_EMPTY_PCT =
|
|
1488
|
+
static_cast<int>(
|
|
1489
|
+
0.4 * dense_hashtable<V, K, HF, ExK, SetK, EqK, A>::HT_OCCUPANCY_PCT);
|
|
1490
|
+
|
|
1491
|
+
}
|
|
1492
|
+
|
|
1493
|
+
#endif // S2_UTIL_GTL_DENSEHASHTABLE_H_
|