Skip to content

Commit ad1489e

Browse files
authored
Merge pull request #222 from jmr/update-2021-12-06
Update to latest google3 version
2 parents bdaaf97 + a16a8ec commit ad1489e

28 files changed

+203
-95
lines changed

doc/examples/point_index.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,5 @@ int main(int argc, char **argv) {
4242

4343
std::printf("Found %" PRId64 " points in %d queries\n", num_found,
4444
absl::GetFlag(FLAGS_num_queries));
45-
return 0;
45+
return 0;
4646
}

doc/examples/term_index.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include <cstdint>
1515
#include <cstdio>
1616
#include <set>
17-
#include <unordered_map>
1817
#include <vector>
1918

2019
#include "s2/base/commandlineflags.h"
@@ -37,7 +36,7 @@ S2_DEFINE_double(query_radius_km, 100, "Query radius in kilometers");
3736
// (e.g. representing words or phrases).
3837
static const char kPrefix[] = "s2:";
3938

40-
int main(int argc, char **argv) {
39+
int main(int argc, char** argv) {
4140
// Create a set of "documents" to be indexed. Each document consists of a
4241
// single point. (You can easily substitute any S2Region type here, or even
4342
// index a mixture of region types using std::unique_ptr<S2Region>. Other
@@ -100,5 +99,5 @@ int main(int argc, char **argv) {
10099
}
101100
std::printf("Found %" PRId64 " points in %d queries\n", num_found,
102101
absl::GetFlag(FLAGS_num_queries));
103-
return 0;
102+
return 0;
104103
}

src/s2/encoded_s2cell_id_vector.cc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ bool EncodedS2CellIdVector::Init(Decoder* decoder) {
139139
int shift_code = code_plus_len >> 3;
140140
if (shift_code == 31) {
141141
shift_code = 29 + decoder->get8();
142+
if (shift_code > 56) return false; // Valid range 0..56
142143
}
144+
143145
// Decode the "base_len" most-significant bytes of "base".
144146
int base_len = code_plus_len & 7;
145147
if (!DecodeUintWithLength(base_len, decoder, &base_)) return false;

src/s2/encoded_s2cell_id_vector_test.cc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
#include "s2/encoded_s2cell_id_vector.h"
1919

2020
#include <vector>
21+
2122
#include <gtest/gtest.h>
2223
#include "absl/memory/memory.h"
24+
#include "s2/s2cell_id.h"
2325
#include "s2/s2loop.h"
2426
#include "s2/s2pointutil.h"
2527
#include "s2/s2shape_index.h"
@@ -31,6 +33,7 @@ using s2textformat::MakeCellIdOrDie;
3133
using std::vector;
3234

3335
namespace s2coding {
36+
namespace {
3437

3538
// Encodes the given vector and returns the corresponding
3639
// EncodedS2CellIdVector (which points into the Encoder's data buffer).
@@ -139,6 +142,30 @@ TEST(EncodedS2CellIdVector, OneByteRangeWithBaseValue) {
139142
0x0100500000000000, 0x0100330000000000}, 9);
140143
}
141144

145+
TEST(EncodedS2CellIdVector, MaxShiftRange) {
146+
const std::vector<uint8> bytes = {
147+
(31 << 3) // 31 -> add 29 to bytes[1].
148+
+ 1, // Number of encoded cell IDs.
149+
27, // 27+29 is the maximum supported shift.
150+
1, 0 // Encoded cell ID. Not important.
151+
};
152+
Decoder decoder(bytes.data(), bytes.size());
153+
EncodedS2CellIdVector cell_ids;
154+
EXPECT_TRUE(cell_ids.Init(&decoder));
155+
}
156+
157+
TEST(EncodedS2CellIdVector, ShiftOutOfRange) {
158+
const std::vector<uint8> bytes = {
159+
(31 << 3) // 31 -> add 29 to bytes[1].
160+
+ 1, // Number of encoded cell IDs.
161+
28, // 28+29 is greater than the maximum supported shift of 56.
162+
1, 0 // Encoded cell ID. Not important.
163+
};
164+
Decoder decoder(bytes.data(), bytes.size());
165+
EncodedS2CellIdVector cell_ids;
166+
EXPECT_FALSE(cell_ids.Init(&decoder));
167+
}
168+
142169
TEST(EncodedS2CellIdVector, SixFaceCells) {
143170
vector<S2CellId> ids;
144171
for (int face = 0; face < 6; ++face) {
@@ -229,4 +256,5 @@ TEST(EncodedS2CellIdVector, LowerBoundLimits) {
229256
EXPECT_EQ(2, cell_ids.lower_bound(S2CellId::Sentinel()));
230257
}
231258

259+
} // namespace
232260
} // namespace s2coding

src/s2/encoded_string_vector.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void StringVectorEncoder::Encode(Span<const string> v, Encoder* encoder) {
4646

4747
bool EncodedStringVector::Init(Decoder* decoder) {
4848
if (!offsets_.Init(decoder)) return false;
49-
data_ = reinterpret_cast<const char*>(decoder->skip(0));
49+
data_ = decoder->skip(0);
5050
if (offsets_.size() > 0) {
5151
uint64 length = offsets_[offsets_.size() - 1];
5252
if (decoder->avail() < length) return false;

src/s2/encoded_uint_vector.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ inline T GetUintWithLength(const char* ptr, int length) {
192192
template <class T>
193193
bool DecodeUintWithLength(int length, Decoder* decoder, T* result) {
194194
if (decoder->avail() < length) return false;
195-
const char* ptr = reinterpret_cast<const char*>(decoder->skip(0));
195+
const char* ptr = decoder->skip(0);
196196
*result = GetUintWithLength<T>(ptr, length);
197197
decoder->skip(length);
198198
return true;
@@ -231,7 +231,7 @@ bool EncodedUintVector<T>::Init(Decoder* decoder) {
231231
if (size_ > std::numeric_limits<size_t>::max() / sizeof(T)) return false;
232232
size_t bytes = size_ * len_;
233233
if (decoder->avail() < bytes) return false;
234-
data_ = reinterpret_cast<const char*>(decoder->skip(0));
234+
data_ = decoder->skip(0);
235235
decoder->skip(bytes);
236236
return true;
237237
}

src/s2/r1interval.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ class R1Interval {
6666
}
6767
}
6868

69-
// Accessors methods.
69+
// The low bound of the interval.
7070
double lo() const { return bounds_[0]; }
71+
// The high bound of the interval.
7172
double hi() const { return bounds_[1]; }
7273

7374
// Methods to modify one endpoint of an existing R1Interval. Do not use
@@ -97,10 +98,12 @@ class R1Interval {
9798
// is negative.
9899
double GetLength() const { return hi() - lo(); }
99100

101+
// Returns true if the given point is in the closed interval [lo, hi].
100102
bool Contains(double p) const {
101103
return p >= lo() && p <= hi();
102104
}
103105

106+
// Returns true if the given point is in the open interval (lo, hi).
104107
bool InteriorContains(double p) const {
105108
return p > lo() && p < hi();
106109
}

src/s2/s2cell_id.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,10 @@ class S2LatLng;
8282
// the default copy constructor and assignment operator.
8383
class S2CellId {
8484
public:
85-
// The extra position bit (61 rather than 60) let us encode each cell as its
86-
// Hilbert curve position at the cell center (which is halfway along the
87-
// portion of the Hilbert curve that fills that cell).
85+
// Although only 60 bits are needed to represent the index of a leaf cell, the
86+
// extra position bit lets us encode each cell as its Hilbert curve position
87+
// at the cell center, which is halfway along the portion of the Hilbert curve
88+
// that fills that cell.
8889
static constexpr int kFaceBits = 3;
8990
static constexpr int kNumFaces = 6;
9091
static constexpr int kMaxLevel =
@@ -98,7 +99,7 @@ class S2CellId {
9899
explicit IFNDEF_SWIG(constexpr) S2CellId(uint64 id) : id_(id) {}
99100

100101
// Construct a leaf cell containing the given point "p". Usually there is
101-
// is exactly one such cell, but for points along the edge of a cell, any
102+
// exactly one such cell, but for points along the edge of a cell, any
102103
// adjacent cell may be (deterministically) chosen. This is because
103104
// S2CellIds are considered to be closed sets. The returned cell will
104105
// always contain the given point, i.e.
@@ -117,6 +118,7 @@ class S2CellId {
117118

118119
// The default constructor returns an invalid cell id.
119120
IFNDEF_SWIG(constexpr) S2CellId() : id_(0) {}
121+
// Returns an invalid cell id.
120122
static constexpr S2CellId None() { return S2CellId(); }
121123

122124
// Returns an invalid cell id guaranteed to be larger than any

src/s2/s2centroids.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ S2Point TrueCentroid(const S2Point& a, const S2Point& b, const S2Point& c) {
5757
// The result is the true centroid of the triangle multiplied by the
5858
// triangle's area.
5959
//
60-
// TODO(ericv): This code still isn't as numerically stable as it could be.
61-
// The biggest potential improvement is to compute B-A and C-A more
60+
// TODO(b/205027737): This code still isn't as numerically stable as it could
61+
// be. The biggest potential improvement is to compute B-A and C-A more
6262
// accurately so that (B-A)x(C-A) is always inside triangle ABC.
6363
S2Point x(a.x(), b.x() - a.x(), c.x() - a.x());
6464
S2Point y(a.y(), b.y() - a.y(), c.y() - a.y());

src/s2/s2closest_cell_query_base.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "s2/base/logging.h"
2626
#include "absl/container/btree_set.h"
2727
#include "absl/container/inlined_vector.h"
28+
#include "absl/hash/hash.h"
2829
#include "s2/s1chord_angle.h"
2930
#include "s2/s2cap.h"
3031
#include "s2/s2cell_id.h"

0 commit comments

Comments
 (0)