Overpass API regression suite coverage (Nova Scotia) / overpass_api/data/abstract_processing.h

overpass_api/data/abstract_processing.h

122 of 154 instrumented lines run by the suite (79.2%) at 8edf11b0419c (this file on GitHub).

runnever runcolumns: line, executions, queries (click for the queries that run the line)

1/** Copyright 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Roland Olbricht et al.
2 *
3 * This file is part of Overpass_API.
4 *
5 * Overpass_API is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * Overpass_API is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with Overpass_API. If not, see <http://www.gnu.org/licenses/>.
17 */
19#ifndef DE__OSM3S___OVERPASS_API__DATA__ABSTRACT_PROCESSING_H
20#define DE__OSM3S___OVERPASS_API__DATA__ABSTRACT_PROCESSING_H
22#include "../core/datatypes.h"
23#include "../statements/statement.h"
24#include "filenames.h"
27//-----------------------------------------------------------------------------
29template < class Object, class TPredicateA, class TPredicateB >
30class And_Predicate
32public:
33 And_Predicate(const TPredicateA& predicate_a_, const TPredicateB& predicate_b_)
341514 q : predicate_a(predicate_a_), predicate_b(predicate_b_) {}
35449668 q bool match(const Object& obj) const { return (predicate_a.match(obj) && predicate_b.match(obj)); }
36427695 q bool match(const Handle< Object >& h) const { return (predicate_a.match(h) && predicate_b.match(h)); }
37 bool match(const Handle< Attic< Object > >& h) const { return (predicate_a.match(h) && predicate_b.match(h)); }
39private:
40 TPredicateA predicate_a;
41 TPredicateB predicate_b;
42};
44template < class Object, class TPredicateA, class TPredicateB >
45class Or_Predicate
47public:
48 Or_Predicate(const TPredicateA& predicate_a_, const TPredicateB& predicate_b_)
490 : predicate_a(predicate_a_), predicate_b(predicate_b_) {}
500 bool match(const Object& obj) const { return (predicate_a.match(obj) || predicate_b.match(obj)); }
51 bool match(const Handle< Object >& h) const { return (predicate_a.match(h) || predicate_b.match(h)); }
52 bool match(const Handle< Attic< Object > >& h) const { return (predicate_a.match(h) || predicate_b.match(h)); }
54private:
55 TPredicateA predicate_a;
56 TPredicateB predicate_b;
57};
59template < class Object, class TPredicateA >
60class Not_Predicate
62public:
63 Not_Predicate(const TPredicateA& predicate_a_)
64107 q : predicate_a(predicate_a_) {}
6525810483 q bool match(const Object& obj) const { return (!predicate_a.match(obj)); }
6635620555 q bool match(const Handle< Object >& h) const { return (!predicate_a.match(h)); }
67 bool match(const Handle< Attic< Object > >& h) const { return (!predicate_a.match(h)); }
69private:
70 TPredicateA predicate_a;
71};
73template < class Object >
74class Trivial_Predicate
76public:
77670335 q Trivial_Predicate() {}
789690815139 q bool match(const Object& obj) const { return true; }
7955581411213 q bool match(const Handle< Object >& h) const { return true; }
80 bool match(const Handle< Attic< Object > >& h) const { return true; }
814945 q bool possible() const { return true; }
82};
84//-----------------------------------------------------------------------------
86template < class Object >
87class Id_Predicate
89public:
90 Id_Predicate(const std::vector< typename Object::Id_Type >& ids_)
9119004552 q : ids(ids_) {}
92202845353208 q bool match(const Object& obj) const { return std::binary_search(ids.begin(), ids.end(), obj.id); }
93441853508337 q bool match(const Handle< Object >& h) const { return std::binary_search(ids.begin(), ids.end(), h.id()); }
94 bool match(const Handle< Attic< Object > >& h) const { return std::binary_search(ids.begin(), ids.end(), h.id()); }
954673350 q bool possible() const { return !ids.empty(); }
97private:
98 const std::vector< typename Object::Id_Type >& ids;
99};
101//-----------------------------------------------------------------------------
103inline bool has_a_child_with_id
104 (const Relation_Skeleton& relation, const std::vector< Uint64 >& ids, uint32 type)
105164096065 q{
106164096065 q for (std::vector< Relation_Entry >::const_iterator it3(relation.members.begin());
1074464000565 q it3 != relation.members.end(); ++it3)
1084300062065 q {
1094300062065 q if (it3->type == type &&
1104300062065 q std::binary_search(ids.begin(), ids.end(), it3->ref))
111157539 q return true;
1124300062065 q }
113163938565 q return false;
114164096065 q}
117inline bool has_a_child_with_id_and_role
118 (const Relation_Skeleton& relation, const std::vector< Uint64 >& ids, uint32 type, uint32 role_id)
119633307 q{
120633307 q for (std::vector< Relation_Entry >::const_iterator it3(relation.members.begin());
12119317417 q it3 != relation.members.end(); ++it3)
12218684137 q {
12318684137 q if (it3->type == type && it3->role == role_id &&
12418684137 q std::binary_search(ids.begin(), ids.end(), it3->ref))
12521 q return true;
12618684137 q }
127633287 q return false;
128633307 q}
131inline bool has_a_child_with_id
132 (const Way_Skeleton& way, const std::vector< int >* pos, const std::vector< Node::Id_Type >& ids)
13370966556 q{
13470966556 q if (pos)
1350 {
1360 std::vector< int >::const_iterator it3 = pos->begin();
1370 for (; it3 != pos->end() && *it3 < 0; ++it3)
1380 {
1390 if (*it3 + (int)way.nds.size() >= 0 &&
1400 std::binary_search(ids.begin(), ids.end(), way.nds[*it3 + way.nds.size()]))
1410 return true;
1420 }
1430 for (; it3 != pos->end(); ++it3)
1440 {
1450 if (*it3 > 0 && *it3 < (int)way.nds.size()+1 &&
1460 std::binary_search(ids.begin(), ids.end(), way.nds[*it3-1]))
1470 return true;
1480 }
1490 }
15070966556 q else
15170966556 q {
15270966556 q for (std::vector< Node::Id_Type >::const_iterator it3(way.nds.begin());
1531869464356 q it3 != way.nds.end(); ++it3)
1541802385856 q {
1551802385856 q if (std::binary_search(ids.begin(), ids.end(), *it3))
1563888041 q return true;
1571802385856 q }
15870966556 q }
15967078556 q return false;
16070966556 q}
163class Get_Parent_Rels_Predicate
165public:
166 Get_Parent_Rels_Predicate(const std::vector< Uint64 >& ids_, uint32 child_type_)
16730584 q : ids(ids_), child_type(child_type_) {}
168 bool match(const Relation_Skeleton& obj) const
16962066138 q { return has_a_child_with_id(obj, ids, child_type); }
170 bool match(const Handle< Relation_Skeleton >& h) const
171102029939 q { return has_a_child_with_id(h.object(), ids, child_type); }
172 bool match(const Handle< Attic< Relation_Skeleton > >& h) const
1730 { return has_a_child_with_id(h.object(), ids, child_type); }
175private:
176 const std::vector< Uint64 >& ids;
177 uint32 child_type;
178};
181class Get_Parent_Rels_Role_Predicate
183public:
184 Get_Parent_Rels_Role_Predicate(const std::vector< Uint64 >& ids_, uint32 child_type_, uint32 role_id_)
185148 q : ids(ids_), child_type(child_type_), role_id(role_id_) {}
186 bool match(const Relation_Skeleton& obj) const
187394824 q { return has_a_child_with_id_and_role(obj, ids, child_type, role_id); }
188 bool match(const Handle< Relation_Skeleton >& h) const
189238484 q { return has_a_child_with_id_and_role(h.object(), ids, child_type, role_id); }
190 bool match(const Handle< Attic< Relation_Skeleton > >& h) const
1910 { return has_a_child_with_id_and_role(h.object(), ids, child_type, role_id); }
193private:
194 const std::vector< Uint64 >& ids;
195 uint32 child_type;
196 uint32 role_id;
197};
200class Get_Parent_Ways_Predicate
202public:
203 Get_Parent_Ways_Predicate(const std::vector< Node::Id_Type >& ids_, const std::vector< int >* pos_)
20415889 q : ids(ids_), pos(pos_) {}
20522102134 q bool match(const Way_Skeleton& obj) const { return has_a_child_with_id(obj, pos, ids); }
20648864433 q bool match(const Handle< Way_Skeleton >& h) const { return has_a_child_with_id(h.object(), pos, ids); }
2070 bool match(const Handle< Attic< Way_Skeleton > >& h) const { return has_a_child_with_id(h.object(), pos, ids); }
209private:
210 const std::vector< Node::Id_Type >& ids;
211 const std::vector< int >* pos;
212};
215//-----------------------------------------------------------------------------
218template < typename Attic_Skeleton >
219struct Attic_Comparator
221public:
222 bool operator()(const Attic_Skeleton& lhs, const Attic_Skeleton& rhs)
223 {
224 if (lhs.id < rhs.id)
225 return true;
226 if (rhs.id < lhs.id)
227 return false;
228 return (lhs.timestamp < rhs.timestamp);
229 }
230};
233template < class TIndex, class TObject >
234void keep_only_least_younger_than
235 (std::map< TIndex, std::vector< Attic< TObject > > >& attic_result,
236 std::map< TIndex, std::vector< TObject > >& result,
237 uint64 timestamp)
239 std::map< typename TObject::Id_Type, uint64 > timestamp_per_id;
241 for (typename std::map< TIndex, std::vector< Attic< TObject > > >::iterator
242 it = attic_result.begin(); it != attic_result.end(); ++it)
243 {
244 std::sort(it->second.begin(), it->second.end(), Attic_Comparator< Attic< TObject > >());
245 typename std::vector< Attic< TObject > >::iterator it_from = it->second.begin();
246 typename std::vector< Attic< TObject > >::iterator it_to = it->second.begin();
247 while (it_from != it->second.end())
248 {
249 if (it_from->timestamp <= timestamp)
250 ++it_from;
251 else
252 {
253 *it_to = *it_from;
254 if (timestamp_per_id[it_to->id] == 0 || timestamp_per_id[it_to->id] > it_to->timestamp)
255 timestamp_per_id[it_to->id] = it_to->timestamp;
256 ++it_from;
257 while (it_from != it->second.end() && it_from->id == it_to->id)
258 ++it_from;
259 ++it_to;
260 }
261 }
262 it->second.erase(it_to, it->second.end());
263 }
265 for (typename std::map< TIndex, std::vector< Attic< TObject > > >::iterator
266 it = attic_result.begin(); it != attic_result.end(); ++it)
267 {
268 typename std::vector< Attic< TObject > >::iterator it_from = it->second.begin();
269 typename std::vector< Attic< TObject > >::iterator it_to = it->second.begin();
270 while (it_from != it->second.end())
271 {
272 if (timestamp_per_id[it_from->id] == it_from->timestamp)
273 {
274 *it_to = *it_from;
275 ++it_to;
276 }
277 ++it_from;
278 }
279 it->second.erase(it_to, it->second.end());
280 }
282 for (typename std::map< TIndex, std::vector< TObject > >::iterator
283 it = result.begin(); it != result.end(); ++it)
284 {
285 typename std::vector< TObject >::iterator it_from = it->second.begin();
286 typename std::vector< TObject >::iterator it_to = it->second.begin();
287 while (it_from != it->second.end())
288 {
289 if (timestamp_per_id.find(it_from->id) == timestamp_per_id.end())
290 {
291 *it_to = *it_from;
292 ++it_to;
293 }
294 ++it_from;
295 }
296 it->second.erase(it_to, it->second.end());
297 }
301//-----------------------------------------------------------------------------
303template < class TIndex, class TObject, class TPredicate >
304void filter_items(const TPredicate& predicate, std::map< TIndex, std::vector< TObject > >& data)
30621053 q for (typename std::map< TIndex, std::vector< TObject > >::iterator it = data.begin();
3079516753 q it != data.end(); ++it)
3089495729 q {
3099495729 q std::vector< TObject > local_into;
3109495729 q for (typename std::vector< TObject >::const_iterator iit = it->second.begin();
31126178629 q iit != it->second.end(); ++iit)
31216682912 q {
31316682912 q if (predicate.match(*iit))
31416682912 q local_into.push_back(*iit);
31516682912 q }
3169495729 q it->second.swap(local_into);
3179495729 q }
320template< class TIndex, class TObject >
321std::vector< typename TObject::Id_Type > filter_for_ids(const std::map< TIndex, std::vector< TObject > >& elems)
3220{
3230 std::vector< typename TObject::Id_Type > ids;
3240 for (typename std::map< TIndex, std::vector< TObject > >::const_iterator it = elems.begin();
3250 it != elems.end(); ++it)
3260 {
3270 for (typename std::vector< TObject >::const_iterator iit = it->second.begin();
3280 iit != it->second.end(); ++iit)
3290 ids.push_back(iit->id);
3300 }
3310 std::sort(ids.begin(), ids.end());
3330 return ids;
3340}
336//-----------------------------------------------------------------------------
339template< typename TObject >
340struct Compare_By_Id
34243740352 q bool operator()(const TObject& lhs, const TObject& rhs) { return lhs.id < rhs.id; }
343};
346template< class TIndex, class TObject >
347bool indexed_set_union(std::map< TIndex, std::vector< TObject > >& result,
348 const std::map< TIndex, std::vector< TObject > >& summand)
34937776535 q{
35037776535 q bool result_has_grown = false;
35237776535 q for (typename std::map< TIndex, std::vector< TObject > >::const_iterator
353796049535 q it = summand.begin(); it != summand.end(); ++it)
354758273148 q {
355758273148 q if (it->second.empty())
35626787861 q continue;
358490395143 q std::vector< TObject >& target = result[it->first];
359490395143 q if (target.empty())
360464374104 q {
361464374104 q target = it->second;
362464374104 q result_has_grown = true;
363464374104 q continue;
364464374104 q }
3662602191 q if (it->second.size() == 1 && target.size() > 64)
367955 q {
368955 q typename std::vector< TObject >::iterator it_target
369955 q = std::lower_bound(target.begin(), target.end(), it->second.front());
370955 q if (it_target == target.end())
371132 q {
372132 q target.push_back(it->second.front());
373132 q result_has_grown = true;
374132 q }
375825 q else if (!(*it_target == it->second.front()))
376694 q {
377694 q target.insert(it_target, it->second.front());
378694 q result_has_grown = true;
379694 q }
380955 q }
3812592691 q else
3822592691 q {
3832592691 q std::vector< TObject > other;
3842592691 q other.swap(target);
3852592691 q std::set_union(it->second.begin(), it->second.end(), other.begin(), other.end(),
3862592691 q back_inserter(target), Compare_By_Id< TObject >());
3882592691 q result_has_grown |= (target.size() > other.size());
3892592691 q }
3902602191 q }
39237776535 q return result_has_grown;
39337776535 q}
395//-----------------------------------------------------------------------------
397template< class TIndex, class TObject >
398void indexed_set_difference(std::map< TIndex, std::vector< TObject > >& result,
399 const std::map< TIndex, std::vector< TObject > >& to_substract)
40143066 q for (typename std::map< TIndex, std::vector< TObject > >::const_iterator
4021381766 q it = to_substract.begin(); it != to_substract.end(); ++it)
4031338735 q {
4041338735 q std::vector< TObject > other;
4051338735 q other.swap(result[it->first]);
4061338735 q std::sort(other.begin(), other.end());
4071338735 q std::set_difference(other.begin(), other.end(), it->second.begin(), it->second.end(),
4081338735 q back_inserter(result[it->first]));
4091338735 q }
412#endif