NOMAD Source  Version 4.0.0 Beta
ArrayOfString.cpp
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------------*/
2 /* NOMAD - Nonlinear Optimization by Mesh Adaptive Direct Search - */
3 /* */
4 /* NOMAD - Version 4.0.0 has been created by */
5 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
6 /* Christophe Tribes - Polytechnique Montreal */
7 /* */
8 /* The copyright of NOMAD - version 4.0.0 is owned by */
9 /* Charles Audet - Polytechnique Montreal */
10 /* Sebastien Le Digabel - Polytechnique Montreal */
11 /* Viviane Rochon Montplaisir - Polytechnique Montreal */
12 /* Christophe Tribes - Polytechnique Montreal */
13 /* */
14 /* NOMAD v4 has been funded by Rio Tinto, Hydro-Québec, NSERC (Natural */
15 /* Sciences and Engineering Research Council of Canada), InnovÉÉ (Innovation */
16 /* en Énergie Électrique) and IVADO (The Institute for Data Valorization) */
17 /* */
18 /* NOMAD v3 was created and developed by Charles Audet, Sebastien Le Digabel, */
19 /* Christophe Tribes and Viviane Rochon Montplaisir and was funded by AFOSR */
20 /* and Exxon Mobil. */
21 /* */
22 /* NOMAD v1 and v2 were created and developed by Mark Abramson, Charles Audet, */
23 /* Gilles Couture, and John E. Dennis Jr., and were funded by AFOSR and */
24 /* Exxon Mobil. */
25 /* */
26 /* Contact information: */
27 /* Polytechnique Montreal - GERAD */
28 /* C.P. 6079, Succ. Centre-ville, Montreal (Quebec) H3C 3A7 Canada */
29 /* e-mail: nomad@gerad.ca */
30 /* */
31 /* This program is free software: you can redistribute it and/or modify it */
32 /* under the terms of the GNU Lesser General Public License as published by */
33 /* the Free Software Foundation, either version 3 of the License, or (at your */
34 /* option) any later version. */
35 /* */
36 /* This program is distributed in the hope that it will be useful, but WITHOUT */
37 /* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or */
38 /* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License */
39 /* for more details. */
40 /* */
41 /* You should have received a copy of the GNU Lesser General Public License */
42 /* along with this program. If not, see <http://www.gnu.org/licenses/>. */
43 /* */
44 /* You can find information on the NOMAD software at www.gerad.ca/nomad */
45 /*---------------------------------------------------------------------------------*/
46 /**
47  \file ArrayOfString.cpp
48  \brief Custom class for array of strings (implementation)
49  \author Viviane Rochon Montplaisir
50  \date February 2018
51  \see ArrayOfString.hpp
52  */
53 #include <sstream>
54 
55 #include "../Util/ArrayOfString.hpp"
56 #include "../Util/Exception.hpp"
57 
58 std::ostream& NOMAD::operator<<(std::ostream& out, const NOMAD::ArrayOfString& arrayOfString)
59 {
60  out << arrayOfString.display();
61 
62  return out;
63 }
64 
65 
66 /*-----------------------------------------------------------*/
67 /* Constructors */
68 /*-----------------------------------------------------------*/
69 NOMAD::ArrayOfString::ArrayOfString(size_t n, const std::string& initString)
70  : _array()
71 {
72  for (size_t i = 0; i < n; i++)
73  {
74  _array.push_back(initString);
75  }
76 }
77 
78 
79 NOMAD::ArrayOfString::ArrayOfString(const std::string & inputString,
80  const std::string & separators)
81  : _array()
82 {
83  _array = splitString(inputString, separators);
84 }
85 
86 
87 /*-----------------------------------------------*/
88 /* destructor */
89 /*-----------------------------------------------*/
90 NOMAD::ArrayOfString::~ArrayOfString()
91 {
92 }
93 
94 
95 /*-----------------------------------------------------------*/
96 /* '[]' operators */
97 /*-----------------------------------------------------------*/
98 const std::string& NOMAD::ArrayOfString::operator[](size_t index) const
99 {
100  if ( index >= _array.size())
101  {
102  std::ostringstream oss;
103  oss << "ArrayOfString: index = " << index << " is out of bounds [0, " << _array.size()-1 << "]";
104  throw NOMAD::Exception(__FILE__, __LINE__, oss.str());
105  }
106 
107  return _array[index];
108 }
109 
110 
111 /*------------------------------------------------*/
112 /* Replace string s at position index. */
113 /* Throw an error if the index is too large. */
114 /* Note: operator[] returns const only, so to */
115 /* modify an array, replace has to be used. */
116 /*------------------------------------------------*/
117 void NOMAD::ArrayOfString::replace(const size_t index, const std::string& s)
118 {
119  if ( index >= _array.size())
120  {
121  std::ostringstream oss;
122  oss << "ArrayOfString: index = " << index << " is out of bounds [0, " << _array.size()-1 << "]";
123  throw NOMAD::Exception(__FILE__, __LINE__, oss.str());
124  }
125  _array[index] = s;
126 }
127 
128 
129 /*------------------------------------------------*/
130 /* Erase element at position index */
131 /* Return true if string was removed from array. */
132 /*------------------------------------------------*/
133 bool NOMAD::ArrayOfString::erase(const size_t index)
134 {
135  size_t k = 0;
136  std::vector<std::string>::const_iterator it;
137 
138  for (it = _array.begin(); it != _array.end(); ++it)
139  {
140  if (index == k)
141  {
142  _array.erase(it);
143  return true;
144  }
145  k++;
146  }
147 
148  return false;
149 }
150 
151 
152 // Return first index of string s in array.
153 // Return -1 if s not found.
154 int NOMAD::ArrayOfString::find(const std::string& s) const
155 {
156  int index = -1;
157 
158  for (size_t i = 0; i < _array.size(); i++)
159  {
160  if (s == _array[i])
161  {
162  index = (int)i;
163  break;
164  }
165  }
166 
167  return index;
168 }
169 
170 
171 /*------------*/
172 /* Comparison */
173 /*------------*/
174 bool NOMAD::ArrayOfString::operator== (const NOMAD::ArrayOfString &array) const
175 {
176  return (_array == array._array);
177 }
178 
179 
180 /*---------------*/
181 /* Display */
182 /*---------------*/
183 std::string NOMAD::ArrayOfString::display() const
184 {
185  std::string s;
186 
187  if ( size() == 0 )
188  s += " - ";
189 
190  for (size_t i = 0; i < size(); i++)
191  {
192  if (i > 0)
193  {
194  s += " ";
195  }
196  s += _array[i];
197  }
198 
199  return s;
200 }
201 
202 
203 /*-----------------------------------*/
204 /* Split */
205 /* - Used as helper for constructor. */
206 /*-----------------------------------*/
207 
208 std::vector<std::string> const
209 NOMAD::ArrayOfString::splitString(const std::string & inputString,
210  const std::string & separators)
211 {
212  std::vector<std::string> array;
213 
214  if (inputString.size() == 0)
215  {
216  return array;
217  }
218 
219  size_t splitIndex = 0, index1 = 0, length;
220 
221  // Fill array with strings found in inputString.
222  // inputString is a list of std::strings, separated by any char in separators.
223  // Sample inputString:
224  // OBJ PB EB
225 
226  while (splitIndex != std::string::npos)
227  {
228  // Find index of first non-separator.
229  index1 = inputString.find_first_not_of(separators, index1);
230  if (index1 == std::string::npos)
231  {
232  break;
233  }
234  splitIndex = inputString.find_first_of(separators, index1+1);
235  if (splitIndex == std::string::npos)
236  {
237  length = inputString.size() - index1;
238  }
239  else
240  {
241  length = splitIndex - index1;
242  }
243 
244  std::string si = inputString.substr(index1, length);
245  array.push_back(si);
246 
247  index1 = splitIndex + 1; // Start looking after separator
248  }
249 
250  return array;
251 }
252 
253 
254 NOMAD::ArrayOfString NOMAD::ArrayOfString::combineAndAddPadding(const NOMAD::ArrayOfString & s1, const NOMAD::ArrayOfString & s2)
255 {
256  // Add a padding on the first string of each pair of string. The padding must assure that the second strings are all aligned.
257  // Also add a return at the end of the second string.
258 
259  size_t sizeS1 = s1.size();
260  if (sizeS1 != s2.size() )
261  {
262  throw NOMAD::Exception(__FILE__, __LINE__, "s1 and s2 must have the same of number of elements.");
263  }
264 
265  // Max length of first+second elements in s
266  size_t maxL = 0;
267  for (size_t e = 0 ; e < sizeS1 ; e++)
268  {
269  maxL = std::max(maxL,s1[e].length()+s2[e].length());
270  }
271 
272  // Pad the first element to get the same overall length for all + combine first and second + add return
273  NOMAD::ArrayOfString paddedString("\n");
274  for (size_t e = 0 ; e < sizeS1 ; e++)
275  {
276  size_t padL = 1 + maxL - s1[e].length() - s2[e].length(); // Add one ' ' at least (for space after colon)
277  std::string padS = s1[e];
278  padS.insert(s1[e].length(), padL,' ');
279  padS += s2[e]+'\n';
280  paddedString.add(padS);
281  }
282 
283  return paddedString;
284 }
285 
286 
287 
288 
289 
max
Double max(const Double d1, const Double d2)
Largest of two values >=.
Definition: Double.hpp:600
operator==
bool operator==(const Double &d1, const Double &d2)
Comparison operator ==.
Definition: Double.hpp:524
operator<<
std::ostream & operator<<(std::ostream &os, const Algorithm &algo)
Operator to write parameters used for hot restart.