NOMAD Source  Version 4.0.0 Beta
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ArrayOfString.cpp
Go to the documentation of this file.
1 /**
2  \file ArrayOfString.cpp
3  \brief Custom class for array of strings (implementation)
4  \author Viviane Rochon Montplaisir
5  \date February 2018
6  \see ArrayOfString.hpp
7  */
8 #include <sstream>
9 
10 #include "../Util/ArrayOfString.hpp"
11 #include "../Util/Exception.hpp"
12 
13 std::ostream& NOMAD::operator<<(std::ostream& out, const NOMAD::ArrayOfString& arrayOfString)
14 {
15  out << arrayOfString.display();
16 
17  return out;
18 }
19 
20 
21 /*-----------------------------------------------------------*/
22 /* Constructors */
23 /*-----------------------------------------------------------*/
24 NOMAD::ArrayOfString::ArrayOfString(size_t n, const std::string& initString)
25  : _array()
26 {
27  for (size_t i = 0; i < n; i++)
28  {
29  _array.push_back(initString);
30  }
31 }
32 
33 
34 NOMAD::ArrayOfString::ArrayOfString(const std::string & inputString,
35  const std::string & separators)
36  : _array()
37 {
38  _array = splitString(inputString, separators);
39 }
40 
41 
42 /*-----------------------------------------------*/
43 /* destructor */
44 /*-----------------------------------------------*/
45 NOMAD::ArrayOfString::~ArrayOfString()
46 {
47 }
48 
49 
50 /*-----------------------------------------------------------*/
51 /* '[]' operators */
52 /*-----------------------------------------------------------*/
53 const std::string& NOMAD::ArrayOfString::operator[](size_t index) const
54 {
55  if ( index >= _array.size())
56  {
57  std::ostringstream oss;
58  oss << "ArrayOfString: index = " << index << " is out of bounds [0, " << _array.size()-1 << "]";
59  throw NOMAD::Exception(__FILE__, __LINE__, oss.str());
60  }
61 
62  return _array[index];
63 }
64 
65 
66 /*------------------------------------------------*/
67 /* Replace string s at position index. */
68 /* Throw an error if the index is too large. */
69 /* Note: operator[] returns const only, so to */
70 /* modify an array, replace has to be used. */
71 /*------------------------------------------------*/
72 void NOMAD::ArrayOfString::replace(const size_t index, const std::string& s)
73 {
74  if ( index >= _array.size())
75  {
76  std::ostringstream oss;
77  oss << "ArrayOfString: index = " << index << " is out of bounds [0, " << _array.size()-1 << "]";
78  throw NOMAD::Exception(__FILE__, __LINE__, oss.str());
79  }
80  _array[index] = s;
81 }
82 
83 
84 /*------------------------------------------------*/
85 /* Erase element at position index */
86 /* Return true if string was removed from array. */
87 /*------------------------------------------------*/
88 bool NOMAD::ArrayOfString::erase(const size_t index)
89 {
90  size_t k = 0;
91  std::vector<std::string>::const_iterator it;
92 
93  for (it = _array.begin(); it != _array.end(); ++it)
94  {
95  if (index == k)
96  {
97  _array.erase(it);
98  return true;
99  }
100  k++;
101  }
102 
103  return false;
104 }
105 
106 
107 // Return first index of string s in array.
108 // Return -1 if s not found.
109 int NOMAD::ArrayOfString::find(const std::string& s) const
110 {
111  int index = -1;
112 
113  for (size_t i = 0; i < _array.size(); i++)
114  {
115  if (s == _array[i])
116  {
117  index = (int)i;
118  break;
119  }
120  }
121 
122  return index;
123 }
124 
125 
126 /*------------*/
127 /* Comparison */
128 /*------------*/
129 bool NOMAD::ArrayOfString::operator== (const NOMAD::ArrayOfString &array) const
130 {
131  return (_array == array._array);
132 }
133 
134 
135 /*---------------*/
136 /* Display */
137 /*---------------*/
138 std::string NOMAD::ArrayOfString::display() const
139 {
140  std::string s;
141 
142  if ( size() == 0 )
143  s += " - ";
144 
145  for (size_t i = 0; i < size(); i++)
146  {
147  if (i > 0)
148  {
149  s += " ";
150  }
151  s += _array[i];
152  }
153 
154  return s;
155 }
156 
157 
158 /*-----------------------------------*/
159 /* Split */
160 /* - Used as helper for constructor. */
161 /*-----------------------------------*/
162 
163 std::vector<std::string> const
164 NOMAD::ArrayOfString::splitString(const std::string & inputString,
165  const std::string & separators)
166 {
167  std::vector<std::string> array;
168 
169  if (inputString.size() == 0)
170  {
171  return array;
172  }
173 
174  size_t splitIndex = 0, index1 = 0, length;
175 
176  // Fill array with strings found in inputString.
177  // inputString is a list of std::strings, separated by any char in separators.
178  // Sample inputString:
179  // OBJ PB EB
180 
181  while (splitIndex != std::string::npos)
182  {
183  // Find index of first non-separator.
184  index1 = inputString.find_first_not_of(separators, index1);
185  if (index1 == std::string::npos)
186  {
187  break;
188  }
189  splitIndex = inputString.find_first_of(separators, index1+1);
190  if (splitIndex == std::string::npos)
191  {
192  length = inputString.size() - index1;
193  }
194  else
195  {
196  length = splitIndex - index1;
197  }
198 
199  std::string si = inputString.substr(index1, length);
200  array.push_back(si);
201 
202  index1 = splitIndex + 1; // Start looking after separator
203  }
204 
205  return array;
206 }
207 
208 
209 
210 
211 
212 
213 
214 
bool operator==(const Double &d1, const Double &d2)
Comparison operator ==.
Definition: Double.hpp:513
std::ostream & operator<<(std::ostream &os, const Algorithm &algo)
Operator to write parameters used for hot restart.