olp-cpp-sdk  1.22.0
SubTiles.h
1 /*
2  * Copyright (C) 2019-2020 HERE Europe B.V.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * SPDX-License-Identifier: Apache-2.0
17  * License-Filename: LICENSE
18  */
19 
20 #pragma once
21 
22 #include <algorithm>
23 #include <cstdint>
24 #include <iterator>
25 
26 #include <olp/core/CoreApi.h>
27 #include <olp/core/geo/tiling/TileKey.h>
28 
29 namespace olp {
30 namespace geo {
31 
35 class CORE_API SubTiles {
36  friend class Iterator;
37 
38  public:
40  class Iterator : public std::iterator<std::forward_iterator_tag, TileKey> {
41  friend class SubTiles;
42 
43  public:
45  using ValueType = TileKey;
46 
52  ValueType operator*() const;
53 
59  Iterator& operator++();
60 
66  Iterator operator++(int);
67 
75  bool operator==(Iterator& other) const;
76 
84  bool operator!=(Iterator& other) const;
85 
86  private:
87  explicit Iterator(const SubTiles& parent, std::uint32_t index = 0);
88 
89  const SubTiles& parent_;
90  std::uint32_t index_{0};
91  };
92 
95 
103  SubTiles(const TileKey& tile_key, std::uint32_t level = 1,
104  std::uint16_t mask = ~0);
105 
111  size_t Size() const;
112 
114  Iterator begin();
116  Iterator end();
117 
119  ConstIterator begin() const;
121  ConstIterator end() const;
122 
124  ConstIterator cbegin() const;
126  ConstIterator cend() const;
127 
128  private:
129  void Skip(std::uint32_t& index) const;
130 
131  const TileKey tile_key_;
132  const std::uint32_t level_{0};
133  const std::uint32_t count_{0};
134  const std::uint16_t mask_{0};
135  const std::uint32_t shift_{0};
136 };
137 
138 inline SubTiles::Iterator::Iterator(const SubTiles& parent, std::uint32_t index)
139  : parent_(parent), index_(index) {
140  // Skip zeros
141  parent_.Skip(index_);
142 }
143 
145  const TileKey& parent_key = parent_.tile_key_;
146  const std::uint32_t sub_level = parent_.level_;
147 
149  (parent_key.Row() << sub_level) | (index_ >> sub_level),
150  (parent_key.Column() << sub_level) | (index_ & ((1 << sub_level) - 1)),
151  parent_key.Level() + sub_level);
152 }
153 
155  parent_.Skip(++index_);
156  return *this;
157 }
158 
160  const Iterator copy(*this);
161  parent_.Skip(++index_);
162  return copy;
163 }
164 
165 inline bool SubTiles::Iterator::operator==(Iterator& other) const {
166  return parent_.tile_key_ == other.parent_.tile_key_ && index_ == other.index_;
167 }
168 
169 inline bool SubTiles::Iterator::operator!=(Iterator& other) const {
170  return !(*this == other);
171 }
172 
173 inline SubTiles::SubTiles(const TileKey& tile_key, std::uint32_t level,
174  std::uint16_t mask)
175  : tile_key_(tile_key),
176  level_(level),
177  count_(1 << (level_ << 1)),
178  mask_(mask),
179  shift_(level_ > 2 ? (level_ - 2) << 1 : 0) {}
180 
181 inline size_t SubTiles::Size() const { return count_; }
182 
183 inline SubTiles::Iterator SubTiles::begin() { return Iterator(*this); }
184 
185 inline SubTiles::Iterator SubTiles::end() { return Iterator(*this, count_); }
186 
188  return ConstIterator(*this);
189 }
190 
192  return ConstIterator(*this, count_);
193 }
194 
196  return ConstIterator(*this);
197 }
198 
200  return ConstIterator(*this, count_);
201 }
202 
203 inline void SubTiles::Skip(std::uint32_t& index) const {
204  if (mask_ == static_cast<std::uint16_t>(-1))
205  return;
206 
207  while (index < count_ && (mask_ & (1 << (index >> shift_))) == 0)
208  ++index;
209 }
210 
211 } // namespace geo
212 } // namespace olp
The tile key iterator.
Definition: SubTiles.h:40
bool operator==(Iterator &other) const
Checks whether the iterators are equal.
Definition: SubTiles.h:165
ValueType operator*() const
Gets a reference to the tile key.
Definition: SubTiles.h:144
bool operator!=(Iterator &other) const
Checks whether the iterators are not equal.
Definition: SubTiles.h:169
Iterator & operator++()
Iterates to the next tile.
Definition: SubTiles.h:154
A container of child tiles.
Definition: SubTiles.h:35
ConstIterator cend() const
Returns a constant iterator to the end of the container.
Definition: SubTiles.h:199
Iterator begin()
Returns an iterator to the beginning.
Definition: SubTiles.h:183
Iterator ConstIterator
An alias for the iterator.
Definition: SubTiles.h:94
SubTiles(const TileKey &tile_key, std::uint32_t level=1, std::uint16_t mask=~0)
Creates a SubTiles instance.
Definition: SubTiles.h:173
size_t Size() const
Gets the size of the child tile.
Definition: SubTiles.h:181
Iterator end()
Returns an iterator to the end.
Definition: SubTiles.h:185
ConstIterator cbegin() const
Returns a constant iterator to the beginning of the container.
Definition: SubTiles.h:195
Addresses a tile in a quadtree.
Definition: TileKey.h:63
static TileKey FromRowColumnLevel(std::uint32_t row, std::uint32_t column, std::uint32_t level)
Creates a tile key.
constexpr std::uint32_t Column() const
Gets the tile column.
Definition: TileKey.h:219
constexpr std::uint32_t Row() const
Gets the tile row.
Definition: TileKey.h:200
constexpr std::uint32_t Level() const
Gets the tile level.
Definition: TileKey.h:191
Rules all the other namespaces.
Definition: AppleSignInProperties.h:24