Overpass API regression suite coverage (Nova Scotia) / overpass_api/statements/for.cc

overpass_api/statements/for.cc

131 of 138 instrumented lines run by the suite (94.9%) 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#include <cctype>
20#include <iostream>
21#include <map>
22#include <set>
23#include <sstream>
24#include <string>
25#include <stdlib.h>
26#include <vector>
28#include "for.h"
31Generic_Statement_Maker< For_Statement > For_Statement::statement_maker("for");
34For_Statement::For_Statement
35 (int line_number_, const std::map< std::string, std::string >& input_attributes, Parsed_Query& global_settings)
366020 q : Statement(line_number_), evaluator(0)
386020 q std::map< std::string, std::string > attributes;
406020 q attributes["from"] = "_";
416020 q attributes["into"] = "_";
436020 q eval_attributes_array(get_name(), attributes, input_attributes);
456020 q input = attributes["from"];
466020 q output = attributes["into"];
50void For_Statement::add_statement(Statement* statement, std::string text)
527520 q assure_no_text(text, this->get_name());
547520 q if (!evaluator)
556020 q {
566020 q evaluator = dynamic_cast< Evaluator* >(statement);
576020 q if (!evaluator)
580 add_static_error("A For statement must have an Evaluator as first sub-statement.");
596020 q }
60155 q else if (statement)
61155 q {
62155 q if (statement->get_name() != "newer")
63155 q substatements.push_back(statement);
640 else
650 add_static_error("\"newer\" can appear only inside \"query\" statements.");
66155 q }
70template< typename Index, typename Object, typename Valuation_Target >
71void collect_for_targets_by_string(
72 const std::map< Index, std::vector< Object > >& container,
73 const Valuation_Target& target,
74 const Eval_Task& task, Set_With_Context& context_from)
7511213 q{
7611213 q for (typename std::map< Index, std::vector< Object > >::const_iterator
77114513 q it_idx = container.begin(); it_idx != container.end(); ++it_idx)
78103312 q {
79103312 q for (typename std::vector< Object >::const_iterator it_elem = it_idx->second.begin();
80230312 q it_elem != it_idx->second.end(); ++it_elem)
81127012 q {
82127012 q std::string valuation = task.eval(context_from.get_context(it_idx->first, *it_elem), 0);
83127012 q target(valuation)[it_idx->first].push_back(*it_elem);
84127012 q }
85103312 q }
8611213 q}
89template< typename Index, typename Object, typename Valuation_Target >
90void collect_for_targets_by_container(
91 const std::map< Index, std::vector< Object > >& container,
92 const Valuation_Target& target,
93 const Eval_Container_Task& task, Set_With_Context& context_from)
95405 q for (typename std::map< Index, std::vector< Object > >::const_iterator
9639585 q it_idx = container.begin(); it_idx != container.end(); ++it_idx)
9739185 q {
9839185 q for (typename std::vector< Object >::const_iterator it_elem = it_idx->second.begin();
99121215 q it_elem != it_idx->second.end(); ++it_elem)
10082035 q {
10182035 q std::vector< std::string > valuation = task.eval(context_from.get_context(it_idx->first, *it_elem), 0);
10282035 q for (std::vector< std::string >::const_iterator it_val = valuation.begin();
103343845 q it_val != valuation.end(); ++it_val)
104261815 q target(*it_val)[it_idx->first].push_back(*it_elem);
10582035 q }
10639185 q }
110struct Base_Valuation_Target
11215218 q Base_Valuation_Target(std::map< std::string, Set >& element_groups_) : element_groups(&element_groups_) {}
113protected:
114 std::map< std::string, Set >* element_groups;
115};
118struct Node_Valuation_Target : Base_Valuation_Target
1201918 q Node_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
121 std::map< Uint32_Index, std::vector< Node_Skeleton > >& operator()(const std::string& rhs) const
12212215 q { return (*element_groups)[rhs].nodes; }
123};
126struct Attic_Node_Valuation_Target : Base_Valuation_Target
1281918 q Attic_Node_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
129 std::map< Uint32_Index, std::vector< Attic< Node_Skeleton > > >& operator()(const std::string& rhs) const
1300 { return (*element_groups)[rhs].attic_nodes; }
131};
134struct Way_Valuation_Target : Base_Valuation_Target
1361918 q Way_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
137 std::map< Uint31_Index, std::vector< Way_Skeleton > >& operator()(const std::string& rhs) const
138188249 q { return (*element_groups)[rhs].ways; }
139};
142struct Attic_Way_Valuation_Target : Base_Valuation_Target
1441918 q Attic_Way_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
145 std::map< Uint31_Index, std::vector< Attic< Way_Skeleton > > >& operator()(const std::string& rhs) const
146542 q { return (*element_groups)[rhs].attic_ways; }
147};
150struct Relation_Valuation_Target : Base_Valuation_Target
1521918 q Relation_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
153 std::map< Uint31_Index, std::vector< Relation_Skeleton > >& operator()(const std::string& rhs) const
15472397 q { return (*element_groups)[rhs].relations; }
155};
158struct Attic_Relation_Valuation_Target : Base_Valuation_Target
1601918 q Attic_Relation_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
161 std::map< Uint31_Index, std::vector< Attic< Relation_Skeleton > > >& operator()(const std::string& rhs) const
16211 q { return (*element_groups)[rhs].attic_relations; }
163};
166struct Area_Valuation_Target : Base_Valuation_Target
1681918 q Area_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
169 std::map< Uint31_Index, std::vector< Area_Skeleton > >& operator()(const std::string& rhs) const
17021 q { return (*element_groups)[rhs].areas; }
171};
174struct Derived_Valuation_Target : Base_Valuation_Target
1761918 q Derived_Valuation_Target(std::map< std::string, Set >& rhs) : Base_Valuation_Target(rhs) {}
177 std::map< Uint31_Index, std::vector< Derived_Structure > >& operator()(const std::string& rhs) const
1781102 q { return (*element_groups)[rhs].deriveds; }
179};
182void For_Statement::execute(Resource_Manager& rman)
1841918 q if (!evaluator)
1850 return;
1871918 q Requested_Context requested_context;
1881918 q requested_context.add(evaluator->request_context());
1891918 q requested_context.bind(input);
1911918 q Prepare_Task_Context context(requested_context, *this, rman);
1931918 q Set_With_Context* context_from = context.get_set(input);
1941918 q if (!context_from)
1950 return;
1971918 q Set base_result_set;
1981918 q rman.swap_set(get_result_name(), base_result_set);
2001918 q rman.push_stack_frame();
2021918 q const Set* base_set = (input == get_result_name() ? &base_result_set : rman.get_set(input));
2031918 q if (!base_set)
2040 base_set = &base_result_set;
2061918 q std::map< std::string, Set > element_groups;
2081918 q if (evaluator->return_type() == Statement::string)
2101413 q Owner< Eval_Task > task(evaluator->get_string_task(context, 0));
2121413 q collect_for_targets_by_string(base_set->nodes, Node_Valuation_Target(element_groups),
2131413 q *task, *context_from);
2141413 q collect_for_targets_by_string(base_set->attic_nodes, Attic_Node_Valuation_Target(element_groups),
2151413 q *task, *context_from);
2161413 q collect_for_targets_by_string(base_set->ways, Way_Valuation_Target(element_groups),
2171413 q *task, *context_from);
2181413 q collect_for_targets_by_string(base_set->attic_ways, Attic_Way_Valuation_Target(element_groups),
2191413 q *task, *context_from);
2201413 q collect_for_targets_by_string(base_set->relations, Relation_Valuation_Target(element_groups),
2211413 q *task, *context_from);
2221413 q collect_for_targets_by_string(base_set->attic_relations, Attic_Relation_Valuation_Target(element_groups),
2231413 q *task, *context_from);
2241413 q collect_for_targets_by_string(base_set->areas, Area_Valuation_Target(element_groups),
2251413 q *task, *context_from);
2261413 q collect_for_targets_by_string(base_set->deriveds, Derived_Valuation_Target(element_groups),
2271413 q *task, *context_from);
22955 q else
23155 q Owner< Eval_Container_Task > task(evaluator->get_container_task(context, 0));
23355 q collect_for_targets_by_container(base_set->nodes, Node_Valuation_Target(element_groups),
23455 q *task, *context_from);
23555 q collect_for_targets_by_container(base_set->attic_nodes, Attic_Node_Valuation_Target(element_groups),
23655 q *task, *context_from);
23755 q collect_for_targets_by_container(base_set->ways, Way_Valuation_Target(element_groups),
23855 q *task, *context_from);
23955 q collect_for_targets_by_container(base_set->attic_ways, Attic_Way_Valuation_Target(element_groups),
24055 q *task, *context_from);
24155 q collect_for_targets_by_container(base_set->relations, Relation_Valuation_Target(element_groups),
24255 q *task, *context_from);
24355 q collect_for_targets_by_container(base_set->attic_relations, Attic_Relation_Valuation_Target(element_groups),
24455 q *task, *context_from);
24555 q collect_for_targets_by_container(base_set->areas, Area_Valuation_Target(element_groups),
24655 q *task, *context_from);
24755 q collect_for_targets_by_container(base_set->deriveds, Derived_Valuation_Target(element_groups),
24855 q *task, *context_from);
25143018 q for (std::map< std::string, Set >::iterator it = element_groups.begin(); it != element_groups.end(); ++it)
25241117 q {
25341117 q rman.count_loop();
25441117 q rman.swap_set(get_result_name(), it->second);
25541117 q rman.set_value(get_result_name(), "val", it->first);
25741117 q for (std::vector< Statement* >::iterator it = substatements.begin();
25851817 q it != substatements.end(); ++it)
2591075 q (*it)->execute(rman);
26041117 q }
2621918 q rman.move_all_inward_except(get_result_name());
2631918 q rman.pop_stack_frame();
2651918 q rman.health_check(*this);