/usr/include/openturns/Collection.hxx is in libopenturns-dev 1.9-5.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | // -*- C++ -*-
/**
* @brief Collection defines top-most collection strategies
*
* Copyright 2005-2017 Airbus-EDF-IMACS-Phimeca
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef OPENTURNS_COLLECTION_HXX
#define OPENTURNS_COLLECTION_HXX
#include <vector>
#include <algorithm> // for std::copy
#include "openturns/OTprivate.hxx"
#include "openturns/ResourceMap.hxx"
#include "openturns/OSS.hxx"
#include "openturns/OStream.hxx"
#include "openturns/Exception.hxx"
#include "openturns/ResourceMap.hxx"
BEGIN_NAMESPACE_OPENTURNS
/**
* @class Collection
*
* Collection defines top-most collection strategies
*/
/* Forward declaration */
template <class T> class Collection;
/** Comparison operator (friend) */
template <class T> inline
Bool operator == (const Collection<T> & lhs,
const Collection<T> & rhs)
{
return lhs.coll__ == rhs.coll__;
}
/** Comparison operator (friend) */
template <class T> inline
Bool operator != (const Collection<T> & lhs,
const Collection<T> & rhs)
{
return !(lhs == rhs);
}
/** Ordering operator (friend) */
template <class T> inline
Bool operator < (const Collection<T> & lhs,
const Collection<T> & rhs)
{
return lhs.coll__ < rhs.coll__;
}
/** Stream operator */
template <class T> inline
std::ostream & operator << (std::ostream & os,
const Collection<T> & collection)
{
return os << collection.__repr__();
}
template <class T> inline
OStream & operator << (OStream & OS,
const Collection<T> & collection)
{
return OS << collection.__str__();
}
template <class T>
class Collection
{
public:
typedef T ElementType;
typedef T ValueType;
typedef typename std::vector<T> InternalType;
typedef typename InternalType::iterator iterator;
typedef typename InternalType::const_iterator const_iterator;
typedef typename InternalType::reverse_iterator reverse_iterator;
typedef typename InternalType::const_reverse_iterator const_reverse_iterator;
protected:
/* The actual collection is a STL vector */
InternalType coll__;
public:
/** Default constructor */
Collection()
: coll__()
{
// Nothing to do
}
/** Constructor that pre-allocate size elements */
Collection(const UnsignedInteger size)
: coll__(size)
{
// Nothing to do
}
/** Constructor that pre-allocate size elements with value */
Collection(const UnsignedInteger size,
const T & value)
: coll__(size, value)
{
// Nothing to do
}
/* Virtual destructor to allow dynamic polymorphism*/
virtual ~Collection()
{
// Nothing to do
}
/** Constructor from a range of elements */
template <typename InputIterator>
Collection(const InputIterator first,
const InputIterator last)
: coll__(first, last)
{
// Nothing to do
}
#ifndef SWIG
/** Erase the elements between first and last */
inline
virtual iterator erase(const iterator first,
const iterator last)
{
if ( (first < begin()) ||
(first > end()) ||
(last < begin()) ||
(last > end()) )
throw OutOfBoundException(HERE) << "Can NOT erase value outside of collection";
return coll__.erase(first, last);
}
/** Erase the elements pointed by position */
inline
virtual iterator erase(iterator position)
{
if ( (position < begin()) ||
(position > end()) )
throw OutOfBoundException(HERE) << "Can NOT erase value outside of collection";
return coll__.erase(position);
}
#endif
/** Clear all elements of the collection */
inline
virtual void clear()
{
coll__.clear();
}
/** Assign elements to the collection */
template <typename InputIterator>
inline
void assign(const InputIterator first,
const InputIterator last)
{
coll__.assign(first, last);
}
#ifndef SWIG
/** Operator[]() gives access to the elements of the collection */
inline
virtual T & operator [] (const UnsignedInteger i)
{
return coll__[i];
}
/** Operator[]() gives access to the elements of the const collection */
inline
virtual const T & operator [] (const UnsignedInteger i) const
{
return coll__[i];
}
#endif
/* Method __len__() is for Python */
inline
UnsignedInteger __len__() const
{
return coll__.size();
}
/* Method __eq__() is for Python */
inline
Bool __eq__(const Collection & rhs) const
{
return (*this == rhs);
}
/* Whether the list contains the value val */
inline
Bool contains(T val) const
{
for (UnsignedInteger i = 0; i < coll__.size(); ++i) if ( coll__[i] == val ) return true;
return false;
}
/* Method __getitem__() is for Python */
inline
T __getitem__(SignedInteger i) const
{
if (i < 0)
{
i += coll__.size();
}
return coll__.at(i);
}
/* Method __setitem__() is for Python */
inline
virtual void __setitem__(SignedInteger i,
const T & val)
{
if (i < 0)
{
i += coll__.size();
}
coll__.at(i) = val;
}
/* Method __delitem__() is for Python */
inline
virtual void __delitem__(const UnsignedInteger i)
{
if (i < coll__.size())
coll__.erase( coll__.begin() + i );
else
throw OutOfBoundException(HERE) << "Index i is out of range. Got " << i << " (size=" << coll__.size() << ")";
}
/** At() gives access to the elements of the collection but throws an exception if bounds are overcome */
inline
virtual T & at(const UnsignedInteger i)
{
return coll__.at(i);
}
/** At() gives access to the elements of the const collection but throws an exception if bounds are overcome */
inline
virtual const T & at(const UnsignedInteger i) const
{
return coll__.at(i);
}
/** Method add() appends an element to the collection */
inline
virtual void add(const T & elt)
{
coll__.push_back(elt);
}
/** Method add() appends a collection to the collection */
inline
virtual void add(const Collection< T > & coll)
{
coll__.insert(coll__.end(), coll.begin(), coll.end());
}
/** Method getSize() returns the number of elements of the collection */
inline
UnsignedInteger getSize() const
{
return static_cast<UnsignedInteger>(coll__.size());
}
/** Method resize() changes the size of the Collection. If the new size is smaller than the older one, the last elements are thrown away, else the new elements are setted to the default value of the element type */
inline
virtual void resize(const UnsignedInteger newSize)
{
coll__.resize(newSize);
}
/** Method empty() returns true if there is no element in the collection */
inline
Bool isEmpty() const
{
return coll__.empty();
}
#ifndef SWIG
/** Method begin() points to the first element of the collection */
inline
iterator begin()
{
return coll__.begin();
}
inline
const_iterator begin() const
{
return coll__.begin();
}
/** Method end() points beyond the last element of the collection */
inline
iterator end()
{
return coll__.end();
}
inline
const_iterator end() const
{
return coll__.end();
}
/** Method rbegin() points to the last element of the collection */
inline
reverse_iterator rbegin()
{
return coll__.rbegin();
}
inline
const_reverse_iterator rbegin() const
{
return coll__.rbegin();
}
/** Method rend() points before the first element of the collection */
inline
reverse_iterator rend()
{
return coll__.rend();
}
inline
const_reverse_iterator rend() const
{
return coll__.rend();
}
#endif
protected:
inline
String toString(Bool full) const
{
OSS oss(full);
String separator;
oss << "[";
std::copy( begin(), end(), OSS_iterator<T>(oss, ",") );
oss << "]";
return oss;
}
public:
/** String converter */
inline String __repr__() const
{
return toString(true);
}
inline String __str__(const String & offset = "") const
{
OSS oss;
oss << toString(false);
if (getSize() >= ResourceMap::GetAsUnsignedInteger("Collection-size-visible-in-str-from"))
oss << "#" << getSize();
return oss;
}
#ifndef SWIG
/* Friend operator */
template <class U> friend inline
Bool operator == (const Collection<U> & lhs,
const Collection<U> & rhs);
/* Friend operator */
template <class U> friend inline
Bool operator != (const Collection<U> & lhs,
const Collection<U> & rhs);
/* Friend operator */
template <class U> friend inline
Bool operator < (const Collection<U> & lhs,
const Collection<U> & rhs);
#endif
}; /* class Collection */
END_NAMESPACE_OPENTURNS
#endif /* OPENTURNS_COLLECTION_HXX */
|