@@ -126,6 +126,14 @@ string S2RegionTermIndexer::GetTerm(TermType term_type, const S2CellId id,
126126
127127vector<string> S2RegionTermIndexer::GetIndexTerms (const S2Point& point,
128128 string_view prefix) {
129+ vector<string> terms;
130+ GetIndexTerms (point, prefix, &terms);
131+ return terms;
132+ }
133+
134+ void S2RegionTermIndexer::GetIndexTerms (const S2Point& point,
135+ string_view prefix,
136+ vector<string>* terms) {
129137 // See the top of this file for an overview of the indexing strategy.
130138 //
131139 // The last cell generated by this loop is effectively the covering for
@@ -136,12 +144,13 @@ vector<string> S2RegionTermIndexer::GetIndexTerms(const S2Point& point,
136144 // max_level() != true_max_level() (see S2RegionCoverer::Options).
137145
138146 const S2CellId id (point);
139- vector<string> terms;
140- for (int level = options_.min_level (); level <= options_.max_level ();
141- level += options_.level_mod ()) {
142- terms.push_back (GetTerm (TermType::ANCESTOR, id.parent (level), prefix));
147+ int level = options_.min_level ();
148+ if (options_.query_contains_points_only ()) {
149+ level = options_.true_max_level ();
150+ }
151+ for (; level <= options_.max_level (); level += options_.level_mod ()) {
152+ terms->push_back (GetTerm (TermType::ANCESTOR, id.parent (level), prefix));
143153 }
144- return terms;
145154}
146155
147156vector<string> S2RegionTermIndexer::GetIndexTerms (const S2Region& region,
@@ -154,6 +163,13 @@ vector<string> S2RegionTermIndexer::GetIndexTerms(const S2Region& region,
154163
155164vector<string> S2RegionTermIndexer::GetIndexTermsForCanonicalCovering (
156165 const S2CellUnion& covering, string_view prefix) {
166+ vector<string> terms;
167+ GetIndexTermsForCanonicalCovering (covering, prefix, &terms);
168+ return terms;
169+ }
170+
171+ void S2RegionTermIndexer::GetIndexTermsForCanonicalCovering (
172+ const S2CellUnion& covering, string_view prefix, vector<string>* terms) {
157173 // See the top of this file for an overview of the indexing strategy.
158174 //
159175 // Cells in the covering are normally indexed as covering terms. If we are
@@ -168,24 +184,29 @@ vector<string> S2RegionTermIndexer::GetIndexTermsForCanonicalCovering(
168184 *coverer_.mutable_options () = options_;
169185 S2_CHECK (coverer_.IsCanonical (covering));
170186 }
171- vector<string> terms;
172187 S2CellId prev_id = S2CellId::None ();
173188 int true_max_level = options_.true_max_level ();
174- for (S2CellId id : covering) {
189+ for (const S2CellId id : covering) {
175190 // IsCanonical() already checks the following conditions, but we repeat
176191 // them here for documentation purposes.
177192 int level = id.level ();
178193 S2_DCHECK_GE (level, options_.min_level ());
179194 S2_DCHECK_LE (level, options_.max_level ());
180195 S2_DCHECK_EQ (0 , (level - options_.min_level ()) % options_.level_mod ());
196+ // assume level <= options_.true_max_level()
181197
182- if (level < true_max_level) {
183- // Add a covering term for this cell.
184- terms.push_back (GetTerm (TermType::COVERING, id, prefix));
185- }
186- if (level == true_max_level || !options_.optimize_for_space ()) {
187- // Add an ancestor term for this cell at the constrained level.
188- terms.push_back (GetTerm (TermType::ANCESTOR, id.parent (level), prefix));
198+ const bool is_max_level_cell = level == true_max_level;
199+ // Add a term for this cell, max_level cell ANCESTOR is optimization
200+ terms->push_back (GetTerm (is_max_level_cell ? TermType::ANCESTOR
201+ : TermType::COVERING,
202+ id, prefix));
203+
204+ // If query only contains points, there are no need other terms.
205+ if (options_.query_contains_points_only ()) continue ;
206+
207+ if (!options_.optimize_for_space () && !is_max_level_cell) {
208+ // Add an ancestor term for this cell.
209+ terms->push_back (GetTerm (TermType::ANCESTOR, id, prefix));
189210 }
190211 // Finally, add ancestor terms for all the ancestors of this cell.
191212 while ((level -= options_.level_mod ()) >= options_.min_level ()) {
@@ -194,29 +215,34 @@ vector<string> S2RegionTermIndexer::GetIndexTermsForCanonicalCovering(
194215 prev_id.parent (level) == ancestor_id) {
195216 break ; // We have already processed this cell and its ancestors.
196217 }
197- terms. push_back (GetTerm (TermType::ANCESTOR, ancestor_id, prefix));
218+ terms-> push_back (GetTerm (TermType::ANCESTOR, ancestor_id, prefix));
198219 }
199220 prev_id = id;
200221 }
201- return terms;
202222}
203223
204224vector<string> S2RegionTermIndexer::GetQueryTerms (const S2Point& point,
205225 string_view prefix) {
226+ vector<string> terms;
227+ GetQueryTerms (point, prefix, &terms);
228+ return terms;
229+ }
230+
231+ void S2RegionTermIndexer::GetQueryTerms (const S2Point& point,
232+ string_view prefix,
233+ vector<string>* terms) {
206234 // See the top of this file for an overview of the indexing strategy.
207235
208236 const S2CellId id (point);
209- vector<string> terms;
210237 // Recall that all true_max_level() cells are indexed only as ancestor terms.
211238 int level = options_.true_max_level ();
212- terms. push_back (GetTerm (TermType::ANCESTOR, id.parent (level), prefix));
213- if (options_.index_contains_points_only ()) return terms ;
239+ terms-> push_back (GetTerm (TermType::ANCESTOR, id.parent (level), prefix));
240+ if (options_.index_contains_points_only ()) return ;
214241
215242 // Add covering terms for all the ancestor cells.
216243 for (; level >= options_.min_level (); level -= options_.level_mod ()) {
217- terms. push_back (GetTerm (TermType::COVERING, id.parent (level), prefix));
244+ terms-> push_back (GetTerm (TermType::COVERING, id.parent (level), prefix));
218245 }
219- return terms;
220246}
221247
222248vector<string> S2RegionTermIndexer::GetQueryTerms (const S2Region& region,
@@ -229,13 +255,20 @@ vector<string> S2RegionTermIndexer::GetQueryTerms(const S2Region& region,
229255
230256vector<string> S2RegionTermIndexer::GetQueryTermsForCanonicalCovering (
231257 const S2CellUnion& covering, string_view prefix) {
258+ vector<string> terms;
259+ GetQueryTermsForCanonicalCovering (covering, prefix, &terms);
260+ return terms;
261+ }
262+
263+ void S2RegionTermIndexer::GetQueryTermsForCanonicalCovering (
264+ const S2CellUnion& covering, string_view prefix, vector<string>* terms) {
232265 // See the top of this file for an overview of the indexing strategy.
233266
267+ S2_CHECK (!options_.query_contains_points_only ());
234268 if (google::DEBUG_MODE) {
235269 *coverer_.mutable_options () = options_;
236270 S2_CHECK (coverer_.IsCanonical (covering));
237271 }
238- vector<string> terms;
239272 S2CellId prev_id = S2CellId::None ();
240273 int true_max_level = options_.true_max_level ();
241274 for (S2CellId id : covering) {
@@ -245,18 +278,19 @@ vector<string> S2RegionTermIndexer::GetQueryTermsForCanonicalCovering(
245278 S2_DCHECK_GE (level, options_.min_level ());
246279 S2_DCHECK_LE (level, options_.max_level ());
247280 S2_DCHECK_EQ (0 , (level - options_.min_level ()) % options_.level_mod ());
281+ // assume level <= options_.true_max_level()
248282
249283 // Cells in the covering are always queried as ancestor terms.
250- terms. push_back (GetTerm (TermType::ANCESTOR, id, prefix));
284+ terms-> push_back (GetTerm (TermType::ANCESTOR, id, prefix));
251285
252286 // If the index only contains points, there are no covering terms.
253287 if (options_.index_contains_points_only ()) continue ;
254288
255289 // If we are optimizing for index space rather than query time, cells are
256290 // also queried as covering terms (except for true_max_level() cells,
257291 // which are indexed and queried as ancestor cells only).
258- if (options_.optimize_for_space () && level < true_max_level) {
259- terms. push_back (GetTerm (TermType::COVERING, id, prefix));
292+ if (options_.optimize_for_space () && level != true_max_level) {
293+ terms-> push_back (GetTerm (TermType::COVERING, id, prefix));
260294 }
261295 // Finally, add covering terms for all the ancestors of this cell.
262296 while ((level -= options_.level_mod ()) >= options_.min_level ()) {
@@ -265,9 +299,8 @@ vector<string> S2RegionTermIndexer::GetQueryTermsForCanonicalCovering(
265299 prev_id.parent (level) == ancestor_id) {
266300 break ; // We have already processed this cell and its ancestors.
267301 }
268- terms. push_back (GetTerm (TermType::COVERING, ancestor_id, prefix));
302+ terms-> push_back (GetTerm (TermType::COVERING, ancestor_id, prefix));
269303 }
270304 prev_id = id;
271305 }
272- return terms;
273306}
0 commit comments