1 /***************************************************************************
2 *
3 * Copyright 2012 BMW Car IT GmbH
4 *
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 ****************************************************************************/
19
20 #include "LMControl.h"
21
22 #include <cmath>
23 using std::min;
24 using std::max;
25 using std::pow;
26
inside(tuple4 A,tuple4 B)27 t_ilm_bool inside(tuple4 A, tuple4 B)
28 {
29 if (A.x >= B.x && A.y >= B.y && A.w <= B.w && A.z <= B.z)
30 return ILM_TRUE;
31 return ILM_FALSE;
32 }
33
between(int b1,int a,int b2)34 t_ilm_bool between(int b1, int a, int b2)
35 {
36 return a >= b1 && a <= b2;
37 }
38
intersect(tuple4 A,tuple4 B)39 t_ilm_bool intersect(tuple4 A, tuple4 B)
40 {
41 if (between(B.x, A.x, B.z) || between(B.x, A.z, B.z))
42 {
43 if (between(A.y, B.y, A.w) || between(A.y, B.w, A.w))
44 return ILM_TRUE;
45 }
46
47 if (between(B.y, A.y, B.w) || between(B.y, A.w, B.w))
48 {
49 if (between(A.x, B.x, A.z) || between(A.x, B.z, A.z))
50 return ILM_TRUE;
51 }
52 return inside(A, B) || inside(B, A);
53 }
54
rtrim(string s)55 string rtrim(string s)
56 {
57 s = s.substr(s.find_first_not_of(" \r\n\t\b"));
58 return s;
59 }
60
replaceAll(string s,string a,string b)61 string replaceAll(string s, string a, string b)
62 {
63 std::size_t index = -b.size();
64
65 while ((index = s.find(a, index + b.size())) != string::npos)
66 {
67 s.replace(index, a.size(), b);
68 }
69
70 return s;
71 }
72
replaceAll(string s,map<string,string> replacements)73 string replaceAll(string s, map<string, string> replacements)
74 {
75 for (std::size_t i = 0; i < s.size(); ++i)
76 {
77 for (map<string, string>::iterator it = replacements.begin(); it != replacements.end(); ++it)
78 {
79 if (s.size() - i >= it->first.size() && s.substr(i, it->first.size()) == it->first)
80 {
81 s.replace(i, it->first.size(), it->second);
82 i += it->second.size();
83 }
84 }
85 }
86
87 return s;
88 }
89
replaceAll(string s,char a,char b)90 string replaceAll(string s, char a, char b)
91 {
92 std::size_t index = -1;
93 while ((index = s.find(a, index + 1)) != string::npos)
94 {
95 s.replace(index, 1, string(1, b));
96 }
97
98 return s;
99 }
100
split(string s,char d)101 set<string> split(string s, char d)
102 {
103 set<string> result;
104 std::size_t start = 0;
105
106 while (start < s.size() && start != string::npos)
107 {
108 start = s.find_first_not_of(d, start);
109 std::size_t end = s.find(d, start);
110 result.insert(s.substr(start, end - start));
111
112 start = end;
113 }
114
115 return result;
116 }
117