/src/s2geometry/src/s2/s2point_span.h
Line | Count | Source |
1 | | // Copyright 2018 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 | | // Author: ericv@google.com (Eric Veach) |
17 | | |
18 | | #ifndef S2_S2POINT_SPAN_H_ |
19 | | #define S2_S2POINT_SPAN_H_ |
20 | | |
21 | | #include <vector> |
22 | | |
23 | | #include "absl/log/absl_check.h" |
24 | | #include "absl/types/span.h" |
25 | | #include "s2/_fp_contract_off.h" // IWYU pragma: keep |
26 | | #include "s2/s2point.h" |
27 | | |
28 | | // S2PointSpan represents a view of an S2Point array. It is used to pass |
29 | | // vertex arrays to functions that don't care about the actual array type |
30 | | // (e.g. std::vector<S2Point> or S2Point[]). |
31 | | // |
32 | | // NOTE: S2PointSpan has an implicit constructor from any container type with |
33 | | // data() and size() methods (such as std::vector and std::array). Therefore |
34 | | // you can use such containers as arguments for any S2PointSpan parameter. |
35 | | using S2PointSpan = absl::Span<const S2Point>; |
36 | | |
37 | | // Like S2PointSpan, except that operator[] maps index values in the range |
38 | | // [n, 2*n-1] to the range [0, n-1] by subtracting n (where n == size()). |
39 | | // In other words, two full copies of the vertex array are available. (This |
40 | | // is a compromise between convenience and efficiency, since computing the |
41 | | // index modulo "n" is surprisingly expensive.) |
42 | | // |
43 | | // This property is useful for implementing algorithms where the elements of |
44 | | // the span represent the vertices of a loop. |
45 | | class S2PointLoopSpan : public S2PointSpan { |
46 | | public: |
47 | | // Inherit all constructors. |
48 | | using absl::Span<const S2Point>::Span; |
49 | | |
50 | | // Like operator[], but allows index values in the range [0, 2*size()-1] |
51 | | // where each index i >= size() is mapped to i - size(). |
52 | 0 | reference operator[](int i) const noexcept { |
53 | 0 | ABSL_DCHECK_GE(i, 0); |
54 | 0 | ABSL_DCHECK_LT(i, static_cast<int>(2 * size())); |
55 | 0 | int j = i - static_cast<int>(size()); |
56 | 0 | return S2PointSpan::operator[](j < 0 ? i : j); |
57 | 0 | } |
58 | | }; |
59 | | |
60 | | #endif // S2_S2POINT_SPAN_H_ |