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

overpass_api/statements/set_list_operators.cc

159 of 163 instrumented lines run by the suite (97.5%) 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 "../data/tag_store.h"
20#include "../data/utils.h"
21#include "set_list_operators.h"
24std::string trim(const std::string& input)
25740431864 q{
26740431864 q std::string::size_type from = 0;
27740431864 q while (from < input.size() && isspace(input[from]))
280 ++from;
30740431864 q if (from == input.size())
31282204 q return " ";
33737609864 q std::string::size_type to = input.size()-1;
34737609864 q while (isspace(input[to]))
350 --to;
37737609864 q return input.substr(from, to+1);
38740431864 q}
41std::vector< std::string > members(const std::string& lrs)
42567351244 q{
43567351244 q std::vector< std::string > result;
45567351244 q if (lrs.empty())
46214346739 q return result;
48353004544 q std::string::size_type from = 0;
49721057244 q while (from < lrs.size())
50721057244 q {
51721057244 q std::string::size_type to = lrs.find(';', from);
52721057244 q if (to == std::string::npos)
53353004544 q break;
55368052724 q result.push_back(trim(lrs.substr(from, to - from)));
57368052724 q from = to + 1;
58368052724 q }
59353004544 q result.push_back(trim(lrs.substr(from)));
61353004544 q std::sort(result.begin(), result.end());
62353004544 q result.erase(std::unique(result.begin(), result.end()), result.end());
63353004544 q return result;
64567351244 q}
67//-----------------------------------------------------------------------------
70Binary_Set_List_Operator_Statement_Maker< Evaluator_Lrs_In > Evaluator_Lrs_In::statement_maker;
71Binary_Set_List_Operator_Evaluator_Maker< Evaluator_Lrs_In > Evaluator_Lrs_In::evaluator_maker;
74std::string Evaluator_Lrs_In::process(const std::string& first_s, const std::string& second_s) const
753196915 q{
763196915 q if (second_s.empty())
7737166 q return "0";
79282539 q std::string first = trim(first_s);
81282539 q std::string::size_type from = 0;
82347429 q while (from < second_s.size())
83347429 q {
84347429 q std::string::size_type to = second_s.find(';', from);
85347429 q if (to == std::string::npos)
86282368 q break;
8865066 q if (first == trim(second_s.substr(from, to - from)))
89171 q return "1";
9164895 q from = to + 1;
9264895 q }
94282368 q if (first == trim(second_s.substr(from)))
950 return "1";
97282368 q return "0";
98282368 q}
101//-----------------------------------------------------------------------------
104Binary_Set_List_Operator_Statement_Maker< Evaluator_Lrs_Isect > Evaluator_Lrs_Isect::statement_maker;
105Binary_Set_List_Operator_Evaluator_Maker< Evaluator_Lrs_Isect > Evaluator_Lrs_Isect::evaluator_maker;
108std::string Evaluator_Lrs_Isect::process(const std::string& first_s, const std::string& second_s) const
10975544823 q{
11075544823 q std::vector< std::string > first;
11175544823 q members(first_s).swap(first);
11275544823 q std::vector< std::string > second;
11375544823 q members(second_s).swap(second);
11475544823 q std::vector< std::string > result(std::min(first.size(), second.size()));
11675544823 q result.erase(std::set_intersection(first.begin(), first.end(), second.begin(), second.end(), result.begin()),
11775544823 q result.end());
11975544823 q std::string result_s;
12075544823 q if (!result.empty())
12167038 q result_s = result[0];
12275677123 q for (unsigned int i = 1; i < result.size(); ++i)
12313233 q result_s += ";" + result[i];
12575544823 q return result_s;
12675544823 q}
129//-----------------------------------------------------------------------------
132Binary_Set_List_Operator_Statement_Maker< Evaluator_Lrs_Union > Evaluator_Lrs_Union::statement_maker;
133Binary_Set_List_Operator_Evaluator_Maker< Evaluator_Lrs_Union > Evaluator_Lrs_Union::evaluator_maker;
136std::string Evaluator_Lrs_Union::process(const std::string& first_s, const std::string& second_s) const
137208130842 q{
138208130842 q std::vector< std::string > first;
139208130842 q members(first_s).swap(first);
140208130842 q std::vector< std::string > second;
141208130842 q members(second_s).swap(second);
142208130842 q std::vector< std::string > result(first.size() + second.size());
144208130842 q result.erase(std::set_union(first.begin(), first.end(), second.begin(), second.end(), result.begin()),
145208130842 q result.end());
147208130842 q std::string result_s;
148208130842 q if (!result.empty())
149208129942 q result_s = result[0];
150566306242 q for (unsigned int i = 1; i < result.size(); ++i)
151358175426 q result_s += ";" + result[i];
153208130842 q return result_s;
154208130842 q}
157//-----------------------------------------------------------------------------
160enum Type_Indicator { type_void = 0, type_int64 = 1, type_double = 2, type_string = 3 };
163Unary_Set_List_Operator_Statement_Maker< Evaluator_Lrs_Max > Evaluator_Lrs_Max::statement_maker;
164Unary_Set_List_Operator_Evaluator_Maker< Evaluator_Lrs_Max > Evaluator_Lrs_Max::evaluator_maker;
167void update_maximum(const std::string& elem, Type_Indicator& relevant_type,
168 int64& result_l, double& result_d, std::string& result_s)
1693654312 q{
1703654312 q if (relevant_type <= type_int64)
1713566012 q {
1723566012 q int64 rhs_l = 0;
1733566012 q if (try_int64(elem, rhs_l))
174317204 q result_l = std::max(result_l, rhs_l);
17539409 q else
17639409 q relevant_type = type_double;
1773566012 q }
1793654312 q if (relevant_type <= type_double)
1803566012 q {
1813566012 q double rhs_d = 0;
1823566012 q if (try_double(elem, rhs_d))
183317204 q result_d = std::max(result_d, rhs_d);
18439409 q else
18539409 q relevant_type = type_string;
1863566012 q }
1883654312 q if (!elem.empty())
1893654312 q result_s = (result_s.empty() ? elem : std::max(result_s, elem));
1903654312 q}
193std::string Evaluator_Lrs_Max::process(const std::string& rhs_s) const
1942008313 q{
1952008313 q if (rhs_s.empty())
1961152 q return "";
1981996812 q Type_Indicator relevant_type = type_int64;
1991996812 q int64 result_l = std::numeric_limits< int64 >::min();
2001996812 q double result_d = -std::numeric_limits< double >::max();
2011996812 q std::string result_s;
2031996812 q std::string::size_type from = 0;
2043654312 q while (from < rhs_s.size())
2053654312 q {
2063654312 q std::string::size_type to = rhs_s.find(';', from);
2073654312 q if (to == std::string::npos)
2081996812 q break;
210165756 q update_maximum(trim(rhs_s.substr(from, to - from)), relevant_type, result_l, result_d, result_s);
212165756 q from = to + 1;
213165756 q }
2151996812 q update_maximum(trim(rhs_s.substr(from)), relevant_type, result_l, result_d, result_s);
2171996812 q if (relevant_type == type_int64)
218160284 q return to_string(result_l);
21939409 q else if (relevant_type == type_double)
2200 return to_string(result_d);
22239409 q return result_s;
2231996812 q}
226//-----------------------------------------------------------------------------
229Unary_Set_List_Operator_Statement_Maker< Evaluator_Lrs_Min > Evaluator_Lrs_Min::statement_maker;
230Unary_Set_List_Operator_Evaluator_Maker< Evaluator_Lrs_Min > Evaluator_Lrs_Min::evaluator_maker;
233void update_minimum(const std::string& elem, Type_Indicator& relevant_type,
234 int64& result_l, double& result_d, std::string& result_s)
2359420833 q{
2369420833 q if (relevant_type <= type_int64)
2378705733 q {
2388705733 q int64 rhs_l = 0;
2398705733 q if (try_int64(elem, rhs_l))
2406358112 q result_l = std::min(result_l, rhs_l);
2412347623 q else
2422347623 q relevant_type = type_double;
2438705733 q }
2459420833 q if (relevant_type <= type_double)
2468705733 q {
2478705733 q double rhs_d = 0;
2488705733 q if (try_double(elem, rhs_d))
2496358213 q result_d = std::min(result_d, rhs_d);
2502347522 q else
2512347522 q relevant_type = type_string;
2528705733 q }
2549420833 q if (!elem.empty())
2559420833 q result_s = (result_s.empty() ? elem : std::min(result_s, elem));
2569420833 q}
259std::string Evaluator_Lrs_Min::process(const std::string& rhs_s) const
26012019444 q{
26112019444 q if (rhs_s.empty())
2626460417 q return "";
2645559033 q Type_Indicator relevant_type = type_int64;
2655559033 q int64 result_l = std::numeric_limits< int64 >::max();
2665559033 q double result_d = std::numeric_limits< double >::max();
2675559033 q std::string result_s;
2695559033 q std::string::size_type from = 0;
2709420833 q while (from < rhs_s.size())
2719420833 q {
2729420833 q std::string::size_type to = rhs_s.find(';', from);
2739420833 q if (to == std::string::npos)
2745559033 q break;
2763861820 q update_minimum(trim(rhs_s.substr(from, to - from)), relevant_type, result_l, result_d, result_s);
2783861820 q from = to + 1;
2793861820 q }
2815559033 q update_minimum(trim(rhs_s.substr(from)), relevant_type, result_l, result_d, result_s);
2835559033 q if (relevant_type == type_int64)
2843211412 q return to_string(result_l);
2852347623 q else if (relevant_type == type_double)
28611 q return to_string(result_d);
2882347522 q return result_s;
2895559033 q}