libcrn  3.9.5
A document image processing library
•All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CRNVector.h
Go to the documentation of this file.
1 /* Copyright 2006-2016 Yann LEYDIER, INSA-Lyon, CoReNum, Université Paris Descartes, ENS-Lyon
2  *
3  * This file is part of libcrn.
4  *
5  * libcrn is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Lesser General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * libcrn 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 Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with libcrn. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * file: CRNVector.h
19  * \author Yann LEYDIER
20  */
21 
22 #ifndef CRNVECTOR_HEADER
23 #define CRNVECTOR_HEADER
24 
25 #include <CRNString.h>
26 #include <algorithm>
27 #include <cstddef>
28 #include <CRNData/CRNVectorPtr.h>
29 #include <CRNData/CRNForeach.h>
30 #include <CRNData/CRNData.h>
31 
32 namespace crn
33 {
34  /****************************************************************************/
42  class Vector: public Object
43  {
44  public:
46  Vector();
48  virtual ~Vector() override;
49 
50  Vector(const Vector &);
51  Vector(Vector &&) = default;
52  Vector& operator=(const Vector &);
53  Vector& operator=(Vector &&) = default;
54 
56  size_t Size() const noexcept { return data.size(); }
58  bool IsEmpty() const noexcept { return data.empty(); }
59 
61  SObject& operator[](size_t i) { return data[i]; }
63  SCObject operator[](size_t i) const { return data[i]; }
65  SObject& At(size_t i) { return data[i]; }
67  SCObject At(size_t i) const { return data[i]; }
68 
70  SObject& Front() { return data.front(); }
72  SCObject Front() const { return data.front(); }
74  SObject& Back() { return data.back(); }
76  SCObject Back() const { return data.back(); }
78  bool Contains(const SCObject &o) const;
80  void PushBack(const SObject &d);
82  void Insert(const SObject &d, size_t pos);
83 
85  void ReorderFrom(const std::vector<size_t> &from);
87  void ReorderTo(const std::vector<size_t> &to);
88 
90  void PopBack() { data.pop_back(); }
92  void Remove(size_t index);
94  void Remove(const SCObject &obj);
96  void Clear() noexcept { data.clear(); }
97 
100  iterator begin() { return data.begin(); }
102  iterator end() { return data.end(); }
104  const_iterator begin() const { return data.begin(); }
106  const_iterator end() const { return data.end(); }
108  const_iterator cbegin() const { return data.begin(); }
110  const_iterator cend() const { return data.end(); }
111 
113  void Remove(iterator it);
117  template<class Predicate> void RemoveIf(Predicate pred) { Remove(std::remove_if(begin(), end(), pred), end()); }
118 
120  const_iterator Find(const SCObject &o) const;
122  iterator Find(const SCObject &o);
123 
125  template<typename T> std::vector<std::shared_ptr<T>> ToStd()
126  {
127  std::vector<std::shared_ptr<T>> v;
128  for (iterator it = begin(); it != end(); ++it)
129  v.push_back(std::dynamic_pointer_cast<T>(*it));
130  return v;
131  }
133  template<typename T> std::vector<std::shared_ptr<const T>> ToStd() const
134  {
135  std::vector<std::shared_ptr<const T>> v;
136  for (const_iterator it = begin(); it != end(); ++it)
137  v.push_back(std::dynamic_pointer_cast<const T>(*it));
138  return v;
139  }
141  std::vector<SObject> Std() && { return std::move(data); }
142 
144  void Swap(Vector &other) noexcept;
145 
147  void ShrinkToFit();
148 
150  void Deserialize(xml::Element &el);
152  xml::Element Serialize(xml::Element &parent) const;
153  private:
154  virtual std::string getClassName() const { return "Vector"; }
155 
156  std::vector<SObject> data;
159  public: Vector(xml::Element &el) { Deserialize(el); }
160  };
161  template<> struct IsSerializable<Vector> : public std::true_type {};
162  template<> struct IsClonable<Vector> : public std::true_type {};
163  template<> struct IsMetric<Vector> : public std::true_type {};
164 
166  double Distance(const Vector &v1, const Vector &v2);
167 
169  inline size_t Size(const Vector &v) noexcept { return v.Size(); }
171  inline void Swap(Vector &v1, Vector &v2) noexcept { v1.Swap(v2); }
172 }
175 
176 namespace std
177 {
178  inline void swap(crn::Vector &v1, crn::Vector &v2) noexcept { v1.Swap(v2); }
179 }
180 #endif
void Swap(Vector &other) noexcept
Swaps contents with another vector.
Definition: CRNVector.cpp:326
SCObject operator[](size_t i) const
Returns an object from index (unsafe)
Definition: CRNVector.h:63
std::vector< SObject > Std()&&
Gets the inner data.
Definition: CRNVector.h:141
const_iterator Find(const SCObject &o) const
Finds an object in the container.
Definition: CRNVector.cpp:86
SCObject Back() const
Returns a reference to the last element.
Definition: CRNVector.h:76
#define CRN_ADD_RANGED_FOR_TO_CONST_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Definition: CRNForeach.h:51
XML element.
Definition: CRNXml.h:135
SObject & Back()
Returns a reference to the last element.
Definition: CRNVector.h:74
void ShrinkToFit()
Optimizes the memory usage.
Definition: CRNVector.cpp:332
SCObject At(size_t i) const
Returns an object from index (unsafe)
Definition: CRNVector.h:67
#define CRN_ADD_RANGED_FOR_TO_POINTERS(TYPE)
Enables ranged for for smart pointers on a type.
Definition: CRNForeach.h:37
iterator begin()
Returns an iterator to the first element.
Definition: CRNVector.h:100
void Remove(size_t index)
Removes an element (safe)
Definition: CRNVector.cpp:213
virtual ~Vector() override
Destructor.
Vector & operator=(const Vector &)
Definition: CRNVector.cpp:48
void Insert(const SObject &d, size_t pos)
Inserts an object at a given position.
Definition: CRNVector.cpp:117
size_t Size(const Vector &v) noexcept
Size of a vector.
Definition: CRNVector.h:169
const_iterator end() const
Returns a const iterator to the end of the list.
Definition: CRNVector.h:106
SObject & operator[](size_t i)
Returns an object from index (unsafe)
Definition: CRNVector.h:61
bool Contains(const SCObject &o) const
Checks of the object is in the vector.
Definition: CRNVector.cpp:72
void Clear() noexcept
Empties the vector.
Definition: CRNVector.h:96
double Distance(const Int &i1, const Int &i2) noexcept
Definition: CRNInt.h:78
Data vector class.
Definition: CRNVector.h:42
xml::Element Serialize(xml::Element &parent) const
Dumps to an XML node if applicable.
Definition: CRNVector.cpp:308
const_iterator cbegin() const
Returns a const iterator to the first element.
Definition: CRNVector.h:108
iterator end()
Returns a iterator to the end of the list.
Definition: CRNVector.h:102
void Swap(Map &m1, Map &m2) noexcept
Definition: CRNMap.h:151
std::vector< std::shared_ptr< T > > ToStd()
Converts to a std::vector of pointers.
Definition: CRNVector.h:125
Vector()
Default constructor.
const_iterator begin() const
Returns a const iterator to the first element.
Definition: CRNVector.h:104
void Deserialize(xml::Element &el)
Reads from an XML node if applicable.
Definition: CRNVector.cpp:267
void PopBack()
Removes the last element.
Definition: CRNVector.h:90
void ReorderFrom(const std::vector< size_t > &from)
Reorders the elements.
Definition: CRNVector.cpp:139
std::vector< std::shared_ptr< const T > > ToStd() const
Converts to a std::vector of pointers.
Definition: CRNVector.h:133
#define CRN_DECLARE_CLASS_CONSTRUCTOR(classname)
Declares a class constructor.
Definition: CRNObject.h:173
void RemoveIf(Predicate pred)
Removes elements if predicate is true.
Definition: CRNVector.h:117
SCObject Front() const
Returns a reference to the first element.
Definition: CRNVector.h:72
void PushBack(const SObject &d)
Adds an object at the end of the vector.
Definition: CRNVector.cpp:61
SObject & Front()
Returns a reference to the first element.
Definition: CRNVector.h:70
SObject & At(size_t i)
Returns an object from index (unsafe)
Definition: CRNVector.h:65
size_t Size() const noexcept
Returns the number of data objects in the vector.
Definition: CRNVector.h:56
void ReorderTo(const std::vector< size_t > &to)
Reorders the elements.
Definition: CRNVector.cpp:177
const_iterator cend() const
Returns a const iterator to the end of the list.
Definition: CRNVector.h:110
bool IsEmpty() const noexcept
Tests if the vector is empty.
Definition: CRNVector.h:58