Skip to content

Commit

Permalink
Update to 2024-06-21 google3 version
Browse files Browse the repository at this point in the history
* Replace S2Testing::Random, which was based on random(3), with
   new s2random:: namespace, based on abseil-cpp's random library.
   This removes the use of global state.
* Use int64_t instead of int64, etc.
* constexpr fixes
* Reworked S2ClosestEdgeQuery
* Remove some old SWIG workarounds.
  • Loading branch information
jmr authored Jun 29, 2024
2 parents ec9ce9c + 850f3e3 commit 755fe37
Show file tree
Hide file tree
Showing 255 changed files with 5,917 additions and 5,289 deletions.
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ add_library(s2
src/s2/s2predicates.cc
src/s2/s2projections.cc
src/s2/s2r2rect.cc
src/s2/s2random.cc
src/s2/s2region_coverer.cc
src/s2/s2region_intersection.cc
src/s2/s2region_sharder.cc
Expand Down Expand Up @@ -371,6 +372,7 @@ install(FILES src/s2/_fp_contract_off.h
src/s2/s2predicates_internal.h
src/s2/s2projections.h
src/s2/s2r2rect.h
src/s2/s2random.h
src/s2/s2region.h
src/s2/s2region_coverer.h
src/s2/s2region_intersection.h
Expand Down Expand Up @@ -406,6 +408,8 @@ install(FILES src/s2/_fp_contract_off.h
src/s2/thread_testing.h
src/s2/value_lexicon.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2")
install(FILES src/s2/internal/s2meta.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2/internal")
install(FILES src/s2/base/casts.h
src/s2/base/commandlineflags.h
src/s2/base/commandlineflags_declare.h
Expand All @@ -430,10 +434,6 @@ install(FILES src/s2/util/gtl/compact_array.h
src/s2/util/gtl/dense_hash_set.h
src/s2/util/gtl/densehashtable.h
src/s2/util/gtl/hashtable_common.h
src/s2/util/gtl/requires.h
src/s2/util/gtl/type_traits.h
src/s2/util/gtl/unaligned.h
src/s2/util/gtl/unaligned_internal.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2/util/gtl")
install(FILES src/s2/util/hash/mix.h
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2/util/hash")
Expand Down Expand Up @@ -570,6 +570,7 @@ if (BUILD_TESTS)
src/s2/s2predicates_test.cc
src/s2/s2projections_test.cc
src/s2/s2r2rect_test.cc
src/s2/s2random_test.cc
src/s2/s2region_coverer_test.cc
src/s2/s2region_sharder_test.cc
src/s2/s2region_term_indexer_test.cc
Expand Down
8 changes: 5 additions & 3 deletions doc/examples/point_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <cinttypes>
#include <cstdint>
#include <random>
#include <vector>

#include "s2/base/commandlineflags.h"
Expand All @@ -15,17 +16,18 @@
#include "s2/s1angle.h"
#include "s2/s2closest_point_query.h"
#include "s2/s2point_index.h"
#include "s2/s2testing.h"
#include "s2/s2random.h"

S2_DEFINE_int32(num_index_points, 10000, "Number of points to index");
S2_DEFINE_int32(num_queries, 10000, "Number of queries");
S2_DEFINE_double(query_radius_km, 100, "Query radius in kilometers");

int main(int argc, char **argv) {
std::mt19937_64 bitgen;
// Build an index containing random points anywhere on the Earth.
S2PointIndex<int> index;
for (int i = 0; i < absl::GetFlag(FLAGS_num_index_points); ++i) {
index.Add(S2Testing::RandomPoint(), i);
index.Add(s2random::Point(bitgen), i);
}

// Create a query to search within the given radius of a target point.
Expand All @@ -37,7 +39,7 @@ int main(int argc, char **argv) {
// are within the given radius of that point.
int64_t num_found = 0;
for (int i = 0; i < absl::GetFlag(FLAGS_num_queries); ++i) {
S2ClosestPointQuery<int>::PointTarget target(S2Testing::RandomPoint());
S2ClosestPointQuery<int>::PointTarget target(s2random::Point(bitgen));
num_found += query.FindClosestPoints(&target).size();
}

Expand Down
8 changes: 5 additions & 3 deletions doc/examples/term_index.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include <cinttypes>
#include <cstdint>
#include <random>
#include <string>
#include <vector>

Expand All @@ -24,8 +25,8 @@
#include "s2/s1angle.h"
#include "s2/s2cap.h"
#include "s2/s2point.h"
#include "s2/s2random.h"
#include "s2/s2region_term_indexer.h"
#include "s2/s2testing.h"

using std::string;

Expand All @@ -38,6 +39,7 @@ S2_DEFINE_double(query_radius_km, 100, "Query radius in kilometers");
static const char kPrefix[] = "s2:";

int main(int argc, char** argv) {
std::mt19937_64 bitgen;
// Create a set of "documents" to be indexed. Each document consists of a
// single point. (You can easily substitute any S2Region type here, or even
// index a mixture of region types using std::unique_ptr<S2Region>. Other
Expand All @@ -46,7 +48,7 @@ int main(int argc, char** argv) {
std::vector<S2Point> documents;
documents.reserve(absl::GetFlag(FLAGS_num_documents));
for (int docid = 0; docid < absl::GetFlag(FLAGS_num_documents); ++docid) {
documents.push_back(S2Testing::RandomPoint());
documents.push_back(s2random::Point(bitgen));
}

// We use a hash map as our inverted index. The key is an index term, and
Expand Down Expand Up @@ -76,7 +78,7 @@ int main(int argc, char** argv) {
int64_t num_found = 0;
for (int i = 0; i < absl::GetFlag(FLAGS_num_queries); ++i) {
// Choose a random center for query.
S2Cap query_region(S2Testing::RandomPoint(), radius);
S2Cap query_region(s2random::Point(bitgen), radius);

// Convert the query region to a set of terms, and compute the union of
// the document ids associated with those terms. (An actual information
Expand Down
14 changes: 13 additions & 1 deletion src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,19 @@ endif()
include(${SWIG_USE_FILE})
include_directories(${Python3_INCLUDE_DIRS})

set(CMAKE_SWIG_FLAGS "")
# SWIG does not understand `uint64_t`. `SWIGWORDSIZE64` controls whether
# it maps `uint64_t` to `unsigned long` or `unsigned long long`.
# (See `stdint.i`.) `SWIGWORDSIZE64` should really be called
# `INT64_IS_LONG_INT`. Unices other that macOS use `long`. Windows uses
# `long long`. If you get error involving `vector<long long unsigned int>`
# and `vector<long unsigned int>` while compiling `s2PYTHON_wrap.cxx`, then
# this is probably defined incorrectly.
# https://github.com/swig/swig/issues/2923
if (CMAKE_SIZEOF_VOID_P EQUAL 8 AND UNIX AND NOT APPLE)
set(CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
else()
set(CMAKE_SWIG_FLAGS "")
endif()
set_property(SOURCE s2.i PROPERTY SWIG_FLAGS "-module" "s2geometry")
set_property(SOURCE s2.i PROPERTY CPLUSPLUS ON)

Expand Down
6 changes: 3 additions & 3 deletions src/python/coder.i
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@
%unignore Encoder::clear();
%unignore Encoder::length() const;
%unignore Encoder::put8(unsigned char);
%unignore Encoder::put16(uint16);
%unignore Encoder::put32(uint32);
%unignore Encoder::put64(uint64);
%unignore Encoder::put16(uint16_t);
%unignore Encoder::put32(uint32_t);
%unignore Encoder::put64(uint64_t);
%unignore Encoder::putdouble(double);
%unignore Encoder::putfloat(float);
%unignore Encoder::reset(void *, size_t);
Expand Down
6 changes: 1 addition & 5 deletions src/python/s2.i
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@
}
}

%template() std::vector<unsigned long long>;
%template() std::vector<uint64_t>;
%template() std::vector<std::string>;
%template() std::vector<S2CellId>;
%template() std::vector<S2Point>;
%template() std::vector<S2LatLng>;

%apply int {int32};
%apply unsigned long long {uint64};
%apply std::vector<unsigned long long> const & {std::vector<uint64> const &};

// Standard Google convention is to ignore all functions and methods, and
// selectively add back those for which wrapping is both required and
// functional.
Expand Down
16 changes: 11 additions & 5 deletions src/python/s2_common.i
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// open source releases of s2.

%{
#include <memory>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -121,7 +122,7 @@ public:
SWIG_exception(SWIG_ValueError, $1->text().c_str());
}

// This overload shadows the one the takes vector<uint64>&, and it
// This overload shadows the one the takes vector<uint64_t>&, and it
// does not work anyway.
%ignore S2CellUnion::Init(std::vector<S2CellId> const& cell_ids);

Expand Down Expand Up @@ -636,7 +637,7 @@ public:
%unignore S2CellId::End;
%unignore S2CellId::FromDebugString(absl::string_view);
%unignore S2CellId::FromFaceIJ(int, int, int);
%unignore S2CellId::FromFacePosLevel(int, uint64, int);
%unignore S2CellId::FromFacePosLevel(int, uint64_t, int);
%unignore S2CellId::FromLatLng;
%unignore S2CellId::FromPoint;
%unignore S2CellId::FromToken(absl::string_view);
Expand Down Expand Up @@ -668,7 +669,7 @@ public:
%unignore S2CellId::range_min;
%unignore S2CellUnion;
%ignore S2CellUnion::operator[]; // Silence the SWIG warning.
%unignore S2CellUnion::S2CellUnion;
%unignore S2CellUnion::S2CellUnion(const std::vector<uint64_t> &);
%unignore S2CellUnion::~S2CellUnion;
%unignore S2CellUnion::ApproxArea;
%unignore S2CellUnion::Clone;
Expand All @@ -681,7 +682,7 @@ public:
%unignore S2CellUnion::GetCapBound() const;
%unignore S2CellUnion::GetDifference;
%unignore S2CellUnion::GetRectBound;
%unignore S2CellUnion::Init(std::vector<uint64> const &);
%unignore S2CellUnion::Init(std::vector<uint64_t> const &);
%unignore S2CellUnion::Intersection;
%unignore S2CellUnion::Intersects;
%unignore S2CellUnion::IsNormalized() const;
Expand Down Expand Up @@ -811,6 +812,10 @@ public:
%unignore S2Loop::vertex;
%unignore S2Polygon;
%unignore S2Polygon::S2Polygon;
%ignore S2Polygon::S2Polygon(std::unique_ptr<S2Loop>, S2Debug);
%ignore S2Polygon::S2Polygon(std::unique_ptr<S2Loop>);
%ignore S2Polygon::S2Polygon(std::vector<std::unique_ptr<S2Loop>>, S2Debug);
%ignore S2Polygon::S2Polygon(std::vector<std::unique_ptr<S2Loop>>);
%unignore S2Polygon::~S2Polygon;
%unignore S2Polygon::BoundaryNear;
%unignore S2Polygon::Clone;
Expand All @@ -827,10 +832,11 @@ public:
%unignore S2Polygon::GetOverlapFractions(const S2Polygon&, const S2Polygon&);
%unignore S2Polygon::GetRectBound;
%unignore S2Polygon::Init;
%ignore S2Polygon::Init(std::unique_ptr<S2Loop>);
%unignore S2Polygon::InitNested;
%ignore S2Polygon::InitNested(std::vector<std::unique_ptr<S2Loop>>);
%unignore S2Polygon::InitToUnion;
%unignore S2Polygon::Intersects;
%unignore S2Polygon::IntersectWithPolyline;
%unignore S2Polygon::IsValid;
%unignore S2Polygon::MayIntersect(const S2Cell&) const;
%unignore S2Polygon::Project;
Expand Down
5 changes: 3 additions & 2 deletions src/s2/base/commandlineflags.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef S2_BASE_COMMANDLINEFLAGS_H_
#define S2_BASE_COMMANDLINEFLAGS_H_

#include <cstdint>
#include <string>

#include "absl/flags/flag.h"
Expand All @@ -30,10 +31,10 @@
ABSL_FLAG(double, name, default_value, description)

#define S2_DEFINE_int32(name, default_value, description) \
ABSL_FLAG(int32, name, default_value, description)
ABSL_FLAG(int32_t, name, default_value, description)

#define S2_DEFINE_int64(name, default_value, description) \
ABSL_FLAG(int64, name, default_value, description)
ABSL_FLAG(int64_t, name, default_value, description)

#define S2_DEFINE_string(name, default_value, description) \
ABSL_FLAG(std::string, name, default_value, description)
Expand Down
5 changes: 3 additions & 2 deletions src/s2/base/commandlineflags_declare.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#ifndef S2_BASE_COMMANDLINEFLAGS_DECLARE_H_
#define S2_BASE_COMMANDLINEFLAGS_DECLARE_H_

#include <cstdint>
#include <string>

#include "absl/flags/declare.h"
Expand All @@ -26,9 +27,9 @@

#define S2_DECLARE_double(name) ABSL_DECLARE_FLAG(double, name)

#define S2_DECLARE_int32(name) ABSL_DECLARE_FLAG(int32, name)
#define S2_DECLARE_int32(name) ABSL_DECLARE_FLAG(int32_t, name)

#define S2_DECLARE_int64(name) ABSL_DECLARE_FLAG(int64, name)
#define S2_DECLARE_int64(name) ABSL_DECLARE_FLAG(int64_t, name)

#define S2_DECLARE_string(name) ABSL_DECLARE_FLAG(std::string, name)

Expand Down
9 changes: 2 additions & 7 deletions src/s2/base/log_severity.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,10 @@

#include "absl/base/log_severity.h"

// Stay compatible with glog.
namespace google {

#ifdef NDEBUG
constexpr bool DEBUG_MODE = false;
constexpr bool S2_DEBUG_MODE = false;
#else
constexpr bool DEBUG_MODE = true;
constexpr bool S2_DEBUG_MODE = true;
#endif

} // namespace google

#endif // S2_BASE_LOG_SEVERITY_H_
21 changes: 10 additions & 11 deletions src/s2/base/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
// - Endianness
// - Performance optimization (alignment)

#include <cstdint>
#include <cstring>

#include "s2/base/types.h"

// -----------------------------------------------------------------------------
// Endianness
// -----------------------------------------------------------------------------
Expand Down Expand Up @@ -121,27 +120,27 @@ void UnalignedStore(void *p, T t) {
}
} // namespace base

inline uint16 UNALIGNED_LOAD16(const void *p) {
return base::UnalignedLoad<uint16>(p);
inline uint16_t UNALIGNED_LOAD16(const void *p) {
return base::UnalignedLoad<uint16_t>(p);
}

inline uint32 UNALIGNED_LOAD32(const void *p) {
return base::UnalignedLoad<uint32>(p);
inline uint32_t UNALIGNED_LOAD32(const void *p) {
return base::UnalignedLoad<uint32_t>(p);
}

inline uint64 UNALIGNED_LOAD64(const void *p) {
return base::UnalignedLoad<uint64>(p);
inline uint64_t UNALIGNED_LOAD64(const void *p) {
return base::UnalignedLoad<uint64_t>(p);
}

inline void UNALIGNED_STORE16(void *p, uint16 v) {
inline void UNALIGNED_STORE16(void *p, uint16_t v) {
base::UnalignedStore(p, v);
}

inline void UNALIGNED_STORE32(void *p, uint32 v) {
inline void UNALIGNED_STORE32(void *p, uint32_t v) {
base::UnalignedStore(p, v);
}

inline void UNALIGNED_STORE64(void *p, uint64 v) {
inline void UNALIGNED_STORE64(void *p, uint64_t v) {
base::UnalignedStore(p, v);
}

Expand Down
5 changes: 2 additions & 3 deletions src/s2/base/timer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
#define S2_BASE_TIMER_H_

#include <chrono>

#include "s2/base/types.h"
#include <cstdint>

class CycleTimer {
public:
Expand All @@ -28,7 +27,7 @@ class CycleTimer {
start_ = Now();
}

int64 GetInMs() const {
int64_t GetInMs() const {
using msec = std::chrono::milliseconds;
return std::chrono::duration_cast<msec>(GetDuration()).count();
}
Expand Down
14 changes: 2 additions & 12 deletions src/s2/base/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,8 @@
#ifndef S2_BASE_TYPES_H_
#define S2_BASE_TYPES_H_

// NOLINTBEGIN(runtime/int)
using int8 = signed char;
using int16 = short;
using int32 = int;
using int64 = long long;
#include <cstdint>

using uint8 = unsigned char;
using uint16 = unsigned short;
using uint32 = unsigned int;
using uint64 = unsigned long long;

using uword_t = unsigned long;
// NOLINTEND(runtime/int)
using uword_t = unsigned long; // NOLINT(runtime/int)

#endif // S2_BASE_TYPES_H_
Loading

0 comments on commit 755fe37

Please sign in to comment.