Skip to content

Commit 64f414e

Browse files
committed
Merge latest google3 version
* 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. Merge branch 'google3-update-2024-06-21' into update-2024-06-21
2 parents ec9ce9c + 4913684 commit 64f414e

File tree

255 files changed

+5914
-5289
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

255 files changed

+5914
-5289
lines changed

CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ add_library(s2
174174
src/s2/s2predicates.cc
175175
src/s2/s2projections.cc
176176
src/s2/s2r2rect.cc
177+
src/s2/s2random.cc
177178
src/s2/s2region_coverer.cc
178179
src/s2/s2region_intersection.cc
179180
src/s2/s2region_sharder.cc
@@ -371,6 +372,7 @@ install(FILES src/s2/_fp_contract_off.h
371372
src/s2/s2predicates_internal.h
372373
src/s2/s2projections.h
373374
src/s2/s2r2rect.h
375+
src/s2/s2random.h
374376
src/s2/s2region.h
375377
src/s2/s2region_coverer.h
376378
src/s2/s2region_intersection.h
@@ -406,6 +408,8 @@ install(FILES src/s2/_fp_contract_off.h
406408
src/s2/thread_testing.h
407409
src/s2/value_lexicon.h
408410
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2")
411+
install(FILES src/s2/internal/s2meta.h
412+
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2/internal")
409413
install(FILES src/s2/base/casts.h
410414
src/s2/base/commandlineflags.h
411415
src/s2/base/commandlineflags_declare.h
@@ -430,10 +434,6 @@ install(FILES src/s2/util/gtl/compact_array.h
430434
src/s2/util/gtl/dense_hash_set.h
431435
src/s2/util/gtl/densehashtable.h
432436
src/s2/util/gtl/hashtable_common.h
433-
src/s2/util/gtl/requires.h
434-
src/s2/util/gtl/type_traits.h
435-
src/s2/util/gtl/unaligned.h
436-
src/s2/util/gtl/unaligned_internal.h
437437
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2/util/gtl")
438438
install(FILES src/s2/util/hash/mix.h
439439
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/s2/util/hash")
@@ -570,6 +570,7 @@ if (BUILD_TESTS)
570570
src/s2/s2predicates_test.cc
571571
src/s2/s2projections_test.cc
572572
src/s2/s2r2rect_test.cc
573+
src/s2/s2random_test.cc
573574
src/s2/s2region_coverer_test.cc
574575
src/s2/s2region_sharder_test.cc
575576
src/s2/s2region_term_indexer_test.cc

doc/examples/point_index.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include <cinttypes>
88
#include <cstdint>
9+
#include <random>
910
#include <vector>
1011

1112
#include "s2/base/commandlineflags.h"
@@ -15,17 +16,18 @@
1516
#include "s2/s1angle.h"
1617
#include "s2/s2closest_point_query.h"
1718
#include "s2/s2point_index.h"
18-
#include "s2/s2testing.h"
19+
#include "s2/s2random.h"
1920

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

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

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

doc/examples/term_index.cc

+5-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <cinttypes>
1414
#include <cstdint>
15+
#include <random>
1516
#include <string>
1617
#include <vector>
1718

@@ -24,8 +25,8 @@
2425
#include "s2/s1angle.h"
2526
#include "s2/s2cap.h"
2627
#include "s2/s2point.h"
28+
#include "s2/s2random.h"
2729
#include "s2/s2region_term_indexer.h"
28-
#include "s2/s2testing.h"
2930

3031
using std::string;
3132

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

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

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

8183
// Convert the query region to a set of terms, and compute the union of
8284
// the document ids associated with those terms. (An actual information

src/python/CMakeLists.txt

+10-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,16 @@ endif()
1717
include(${SWIG_USE_FILE})
1818
include_directories(${Python3_INCLUDE_DIRS})
1919

20-
set(CMAKE_SWIG_FLAGS "")
20+
# SWIG does not understand `uint64_t`. `SWIGWORDSIZE64` controls whether
21+
# it maps `uint64_t` to `unsigned long` or `unsigned long long`.
22+
# (See `stdint.i`.) https://github.com/swig/swig/issues/2923
23+
# TODO: This works for Linux, but may need extra conditions for macOS /
24+
# Windows.
25+
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
26+
set(CMAKE_SWIG_FLAGS "-DSWIGWORDSIZE64")
27+
else()
28+
set(CMAKE_SWIG_FLAGS "")
29+
endif()
2130
set_property(SOURCE s2.i PROPERTY SWIG_FLAGS "-module" "s2geometry")
2231
set_property(SOURCE s2.i PROPERTY CPLUSPLUS ON)
2332

src/python/coder.i

+3-3
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,9 @@
113113
%unignore Encoder::clear();
114114
%unignore Encoder::length() const;
115115
%unignore Encoder::put8(unsigned char);
116-
%unignore Encoder::put16(uint16);
117-
%unignore Encoder::put32(uint32);
118-
%unignore Encoder::put64(uint64);
116+
%unignore Encoder::put16(uint16_t);
117+
%unignore Encoder::put32(uint32_t);
118+
%unignore Encoder::put64(uint64_t);
119119
%unignore Encoder::putdouble(double);
120120
%unignore Encoder::putfloat(float);
121121
%unignore Encoder::reset(void *, size_t);

src/python/s2.i

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,12 @@
2929
}
3030
}
3131

32-
%template() std::vector<unsigned long long>;
32+
%template() std::vector<uint64_t>;
3333
%template() std::vector<std::string>;
3434
%template() std::vector<S2CellId>;
3535
%template() std::vector<S2Point>;
3636
%template() std::vector<S2LatLng>;
3737

38-
%apply int {int32};
39-
%apply unsigned long long {uint64};
40-
%apply std::vector<unsigned long long> const & {std::vector<uint64> const &};
41-
4238
// Standard Google convention is to ignore all functions and methods, and
4339
// selectively add back those for which wrapping is both required and
4440
// functional.

src/python/s2_common.i

+11-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// open source releases of s2.
55

66
%{
7+
#include <memory>
78
#include <sstream>
89
#include <string>
910

@@ -121,7 +122,7 @@ public:
121122
SWIG_exception(SWIG_ValueError, $1->text().c_str());
122123
}
123124

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

@@ -636,7 +637,7 @@ public:
636637
%unignore S2CellId::End;
637638
%unignore S2CellId::FromDebugString(absl::string_view);
638639
%unignore S2CellId::FromFaceIJ(int, int, int);
639-
%unignore S2CellId::FromFacePosLevel(int, uint64, int);
640+
%unignore S2CellId::FromFacePosLevel(int, uint64_t, int);
640641
%unignore S2CellId::FromLatLng;
641642
%unignore S2CellId::FromPoint;
642643
%unignore S2CellId::FromToken(absl::string_view);
@@ -668,7 +669,7 @@ public:
668669
%unignore S2CellId::range_min;
669670
%unignore S2CellUnion;
670671
%ignore S2CellUnion::operator[]; // Silence the SWIG warning.
671-
%unignore S2CellUnion::S2CellUnion;
672+
%unignore S2CellUnion::S2CellUnion(const std::vector<uint64_t> &);
672673
%unignore S2CellUnion::~S2CellUnion;
673674
%unignore S2CellUnion::ApproxArea;
674675
%unignore S2CellUnion::Clone;
@@ -681,7 +682,7 @@ public:
681682
%unignore S2CellUnion::GetCapBound() const;
682683
%unignore S2CellUnion::GetDifference;
683684
%unignore S2CellUnion::GetRectBound;
684-
%unignore S2CellUnion::Init(std::vector<uint64> const &);
685+
%unignore S2CellUnion::Init(std::vector<uint64_t> const &);
685686
%unignore S2CellUnion::Intersection;
686687
%unignore S2CellUnion::Intersects;
687688
%unignore S2CellUnion::IsNormalized() const;
@@ -811,6 +812,10 @@ public:
811812
%unignore S2Loop::vertex;
812813
%unignore S2Polygon;
813814
%unignore S2Polygon::S2Polygon;
815+
%ignore S2Polygon::S2Polygon(std::unique_ptr<S2Loop>, S2Debug);
816+
%ignore S2Polygon::S2Polygon(std::unique_ptr<S2Loop>);
817+
%ignore S2Polygon::S2Polygon(std::vector<std::unique_ptr<S2Loop>>, S2Debug);
818+
%ignore S2Polygon::S2Polygon(std::vector<std::unique_ptr<S2Loop>>);
814819
%unignore S2Polygon::~S2Polygon;
815820
%unignore S2Polygon::BoundaryNear;
816821
%unignore S2Polygon::Clone;
@@ -827,10 +832,11 @@ public:
827832
%unignore S2Polygon::GetOverlapFractions(const S2Polygon&, const S2Polygon&);
828833
%unignore S2Polygon::GetRectBound;
829834
%unignore S2Polygon::Init;
835+
%ignore S2Polygon::Init(std::unique_ptr<S2Loop>);
830836
%unignore S2Polygon::InitNested;
837+
%ignore S2Polygon::InitNested(std::vector<std::unique_ptr<S2Loop>>);
831838
%unignore S2Polygon::InitToUnion;
832839
%unignore S2Polygon::Intersects;
833-
%unignore S2Polygon::IntersectWithPolyline;
834840
%unignore S2Polygon::IsValid;
835841
%unignore S2Polygon::MayIntersect(const S2Cell&) const;
836842
%unignore S2Polygon::Project;

src/s2/base/commandlineflags.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef S2_BASE_COMMANDLINEFLAGS_H_
1717
#define S2_BASE_COMMANDLINEFLAGS_H_
1818

19+
#include <cstdint>
1920
#include <string>
2021

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

3233
#define S2_DEFINE_int32(name, default_value, description) \
33-
ABSL_FLAG(int32, name, default_value, description)
34+
ABSL_FLAG(int32_t, name, default_value, description)
3435

3536
#define S2_DEFINE_int64(name, default_value, description) \
36-
ABSL_FLAG(int64, name, default_value, description)
37+
ABSL_FLAG(int64_t, name, default_value, description)
3738

3839
#define S2_DEFINE_string(name, default_value, description) \
3940
ABSL_FLAG(std::string, name, default_value, description)

src/s2/base/commandlineflags_declare.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#ifndef S2_BASE_COMMANDLINEFLAGS_DECLARE_H_
1717
#define S2_BASE_COMMANDLINEFLAGS_DECLARE_H_
1818

19+
#include <cstdint>
1920
#include <string>
2021

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

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

29-
#define S2_DECLARE_int32(name) ABSL_DECLARE_FLAG(int32, name)
30+
#define S2_DECLARE_int32(name) ABSL_DECLARE_FLAG(int32_t, name)
3031

31-
#define S2_DECLARE_int64(name) ABSL_DECLARE_FLAG(int64, name)
32+
#define S2_DECLARE_int64(name) ABSL_DECLARE_FLAG(int64_t, name)
3233

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

src/s2/base/log_severity.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,10 @@
1818

1919
#include "absl/base/log_severity.h"
2020

21-
// Stay compatible with glog.
22-
namespace google {
23-
2421
#ifdef NDEBUG
25-
constexpr bool DEBUG_MODE = false;
22+
constexpr bool S2_DEBUG_MODE = false;
2623
#else
27-
constexpr bool DEBUG_MODE = true;
24+
constexpr bool S2_DEBUG_MODE = true;
2825
#endif
2926

30-
} // namespace google
31-
3227
#endif // S2_BASE_LOG_SEVERITY_H_

src/s2/base/port.h

+10-11
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
// - Endianness
2222
// - Performance optimization (alignment)
2323

24+
#include <cstdint>
2425
#include <cstring>
2526

26-
#include "s2/base/types.h"
27-
2827
// -----------------------------------------------------------------------------
2928
// Endianness
3029
// -----------------------------------------------------------------------------
@@ -121,27 +120,27 @@ void UnalignedStore(void *p, T t) {
121120
}
122121
} // namespace base
123122

124-
inline uint16 UNALIGNED_LOAD16(const void *p) {
125-
return base::UnalignedLoad<uint16>(p);
123+
inline uint16_t UNALIGNED_LOAD16(const void *p) {
124+
return base::UnalignedLoad<uint16_t>(p);
126125
}
127126

128-
inline uint32 UNALIGNED_LOAD32(const void *p) {
129-
return base::UnalignedLoad<uint32>(p);
127+
inline uint32_t UNALIGNED_LOAD32(const void *p) {
128+
return base::UnalignedLoad<uint32_t>(p);
130129
}
131130

132-
inline uint64 UNALIGNED_LOAD64(const void *p) {
133-
return base::UnalignedLoad<uint64>(p);
131+
inline uint64_t UNALIGNED_LOAD64(const void *p) {
132+
return base::UnalignedLoad<uint64_t>(p);
134133
}
135134

136-
inline void UNALIGNED_STORE16(void *p, uint16 v) {
135+
inline void UNALIGNED_STORE16(void *p, uint16_t v) {
137136
base::UnalignedStore(p, v);
138137
}
139138

140-
inline void UNALIGNED_STORE32(void *p, uint32 v) {
139+
inline void UNALIGNED_STORE32(void *p, uint32_t v) {
141140
base::UnalignedStore(p, v);
142141
}
143142

144-
inline void UNALIGNED_STORE64(void *p, uint64 v) {
143+
inline void UNALIGNED_STORE64(void *p, uint64_t v) {
145144
base::UnalignedStore(p, v);
146145
}
147146

src/s2/base/timer.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
#define S2_BASE_TIMER_H_
1818

1919
#include <chrono>
20-
21-
#include "s2/base/types.h"
20+
#include <cstdint>
2221

2322
class CycleTimer {
2423
public:
@@ -28,7 +27,7 @@ class CycleTimer {
2827
start_ = Now();
2928
}
3029

31-
int64 GetInMs() const {
30+
int64_t GetInMs() const {
3231
using msec = std::chrono::milliseconds;
3332
return std::chrono::duration_cast<msec>(GetDuration()).count();
3433
}

src/s2/base/types.h

+2-12
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,8 @@
1616
#ifndef S2_BASE_TYPES_H_
1717
#define S2_BASE_TYPES_H_
1818

19-
// NOLINTBEGIN(runtime/int)
20-
using int8 = signed char;
21-
using int16 = short;
22-
using int32 = int;
23-
using int64 = long long;
19+
#include <cstdint>
2420

25-
using uint8 = unsigned char;
26-
using uint16 = unsigned short;
27-
using uint32 = unsigned int;
28-
using uint64 = unsigned long long;
29-
30-
using uword_t = unsigned long;
31-
// NOLINTEND(runtime/int)
21+
using uword_t = unsigned long; // NOLINT(runtime/int)
3222

3323
#endif // S2_BASE_TYPES_H_

0 commit comments

Comments
 (0)