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

overpass_api/statements/for.cc

130 of 138 instrumented lines run by the suite (94.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#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)
365719 q : Statement(line_number_), evaluator(0)
385719 q std::map< std::string, std::string > attributes;
405719 q attributes["from"] = "_";
415719 q attributes["into"] = "_";
435719 q eval_attributes_array(get_name(), attributes, input_attributes);
455719 q input = attributes["from"];
465719 q output = attributes["into"];
50void For_Statement::add_statement(Statement* statement, std::string text)
527519 q assure_no_text(text, this->get_name());
547519 q if (!evaluator)
555719 q {
565719 q evaluator = dynamic_cast< Evaluator* >(statement);
575719 q if (!evaluator)
580 add_static_error("A For statement must have an Evaluator as first sub-statement.");
595719 q }
60186 q else if (statement)
61186 q {
62186 q if (statement->get_name() != "newer")
63186 q substatements.push_back(statement);
640 else
650 add_static_error("\"newer\" can appear only inside \"query\" statements.");
66186 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)
768010 q for (typename std::map< Index, std::vector< Object > >::const_iterator
7758110 q it_idx = container.begin(); it_idx != container.end(); ++it_idx)
785019 q {
795019 q for (typename std::vector< Object >::const_iterator it_elem = it_idx->second.begin();
8045449 q it_elem != it_idx->second.end(); ++it_elem)
8140439 q {
8240439 q std::string valuation = task.eval(context_from.get_context(it_idx->first, *it_elem), 0);
8340439 q target(valuation)[it_idx->first].push_back(*it_elem);
8440439 q }
855019 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)
95567 q for (typename std::map< Index, std::vector< Object > >::const_iterator
961237 q it_idx = container.begin(); it_idx != container.end(); ++it_idx)
97675 q {
98675 q for (typename std::vector< Object >::const_iterator it_elem = it_idx->second.begin();
996645 q it_elem != it_idx->second.end(); ++it_elem)
1005975 q {
1015975 q std::vector< std::string > valuation = task.eval(context_from.get_context(it_idx->first, *it_elem), 0);
1025975 q for (std::vector< std::string >::const_iterator it_val = valuation.begin();
10322985 q it_val != valuation.end(); ++it_val)
10417015 q target(*it_val)[it_idx->first].push_back(*it_elem);
1055975 q }
106675 q }
110struct Base_Valuation_Target
11213617 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
1201717 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
1223173 q { return (*element_groups)[rhs].nodes; }
123};
126struct Attic_Node_Valuation_Target : Base_Valuation_Target
1281717 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
1361717 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
13837675 q { return (*element_groups)[rhs].ways; }
139};
142struct Attic_Way_Valuation_Target : Base_Valuation_Target
1441717 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
14621 q { return (*element_groups)[rhs].attic_ways; }
147};
150struct Relation_Valuation_Target : Base_Valuation_Target
1521717 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
15412715 q { return (*element_groups)[rhs].relations; }
155};
158struct Attic_Relation_Valuation_Target : Base_Valuation_Target
1601717 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
1681717 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
1700 { return (*element_groups)[rhs].areas; }
171};
174struct Derived_Valuation_Target : Base_Valuation_Target
1761717 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
1783862 q { return (*element_groups)[rhs].deriveds; }
179};
182void For_Statement::execute(Resource_Manager& rman)
1841717 q if (!evaluator)
1850 return;
1871717 q Requested_Context requested_context;
1881717 q requested_context.add(evaluator->request_context());
1891717 q requested_context.bind(input);
1911717 q Prepare_Task_Context context(requested_context, *this, rman);
1931717 q Set_With_Context* context_from = context.get_set(input);
1941717 q if (!context_from)
1950 return;
1971717 q Set base_result_set;
1981717 q rman.swap_set(get_result_name(), base_result_set);
2001717 q rman.push_stack_frame();
2021717 q const Set* base_set = (input == get_result_name() ? &base_result_set : rman.get_set(input));
2031717 q if (!base_set)
2040 base_set = &base_result_set;
2061717 q std::map< std::string, Set > element_groups;
2081717 q if (evaluator->return_type() == Statement::string)
2101010 q Owner< Eval_Task > task(evaluator->get_string_task(context, 0));
2121010 q collect_for_targets_by_string(base_set->nodes, Node_Valuation_Target(element_groups),
2131010 q *task, *context_from);
2141010 q collect_for_targets_by_string(base_set->attic_nodes, Attic_Node_Valuation_Target(element_groups),
2151010 q *task, *context_from);
2161010 q collect_for_targets_by_string(base_set->ways, Way_Valuation_Target(element_groups),
2171010 q *task, *context_from);
2181010 q collect_for_targets_by_string(base_set->attic_ways, Attic_Way_Valuation_Target(element_groups),
2191010 q *task, *context_from);
2201010 q collect_for_targets_by_string(base_set->relations, Relation_Valuation_Target(element_groups),
2211010 q *task, *context_from);
2221010 q collect_for_targets_by_string(base_set->attic_relations, Attic_Relation_Valuation_Target(element_groups),
2231010 q *task, *context_from);
2241010 q collect_for_targets_by_string(base_set->areas, Area_Valuation_Target(element_groups),
2251010 q *task, *context_from);
2261010 q collect_for_targets_by_string(base_set->deriveds, Derived_Valuation_Target(element_groups),
2271010 q *task, *context_from);
22977 q else
23177 q Owner< Eval_Container_Task > task(evaluator->get_container_task(context, 0));
23377 q collect_for_targets_by_container(base_set->nodes, Node_Valuation_Target(element_groups),
23477 q *task, *context_from);
23577 q collect_for_targets_by_container(base_set->attic_nodes, Attic_Node_Valuation_Target(element_groups),
23677 q *task, *context_from);
23777 q collect_for_targets_by_container(base_set->ways, Way_Valuation_Target(element_groups),
23877 q *task, *context_from);
23977 q collect_for_targets_by_container(base_set->attic_ways, Attic_Way_Valuation_Target(element_groups),
24077 q *task, *context_from);
24177 q collect_for_targets_by_container(base_set->relations, Relation_Valuation_Target(element_groups),
24277 q *task, *context_from);
24377 q collect_for_targets_by_container(base_set->attic_relations, Attic_Relation_Valuation_Target(element_groups),
24477 q *task, *context_from);
24577 q collect_for_targets_by_container(base_set->areas, Area_Valuation_Target(element_groups),
24677 q *task, *context_from);
24777 q collect_for_targets_by_container(base_set->deriveds, Derived_Valuation_Target(element_groups),
24877 q *task, *context_from);
25142117 q for (std::map< std::string, Set >::iterator it = element_groups.begin(); it != element_groups.end(); ++it)
25240414 q {
25340414 q rman.count_loop();
25440414 q rman.swap_set(get_result_name(), it->second);
25540414 q rman.set_value(get_result_name(), "val", it->first);
25740414 q for (std::vector< Statement* >::iterator it = substatements.begin();
25852614 q it != substatements.end(); ++it)
2591225 q (*it)->execute(rman);
26040414 q }
2621717 q rman.move_all_inward_except(get_result_name());
2631717 q rman.pop_stack_frame();
2651717 q rman.health_check(*this);