bpp-seq3  3.0.0
VectorPositionedContainer.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #ifndef BPP_SEQ_CONTAINER_VECTORPOSITIONEDCONTAINER_H
6 #define BPP_SEQ_CONTAINER_VECTORPOSITIONEDCONTAINER_H
7 
8 #include <Bpp/Clonable.h>
9 #include <Bpp/Exceptions.h>
11 
12 #include "PositionedContainer.h"
13 
14 // From the STL library:
15 #include <string>
16 #include <vector>
17 #include <iostream>
18 #include <iterator>
19 
20 namespace bpp
21 {
28 template<class T>
30  public virtual PositionedContainerInterface<T>,
31  public virtual Clonable
32 {
33 protected:
34  std::vector< std::shared_ptr<T>> positions_;
35 
36 public:
42  VectorPositionedContainer(const std::vector<std::shared_ptr<T>>& vs) :
43  positions_(vs)
44  {}
45 
51  VectorPositionedContainer(size_t size = 0) :
52  positions_(size)
53  {}
54 
55 
58  {}
59 
64  {
65  positions_ = vsc.positions_;
66  return *this;
67  }
68 
70  {
71  positions_.clear();
72  }
73 
74 public:
81  {
82  return new VectorPositionedContainer<T>(*this);
83  }
84 
87  size_t getSize() const override
88  {
89  return positions_.size();
90  }
91 
92  bool isAvailablePosition(size_t objectIndex) const
93  {
94  return objectIndex < getSize() && (positions_[objectIndex] == nullptr || positions_[objectIndex]->size() == 0);
95  }
96 
97  bool hasObjectWithPosition(size_t objectIndex) const
98  {
99  return objectIndex < getSize() && (positions_[objectIndex] != nullptr && positions_[objectIndex]->size() > 0);
100  }
101 
102  void setSize(size_t size) override
103  {
104  if (positions_.size() > size)
105  throw Exception("VectorPositionedContainer::setSize : not possible to shorten the vector.");
106 
107  positions_.resize(size);
108  }
109 
113  void clear() override
114  {
115  positions_.clear();
116  }
117 
118 
122  virtual void nullify()
123  {
124  std::fill(positions_.begin(), positions_.end(), nullptr);
125  }
126 
127  const std::shared_ptr<T> getObject(size_t objectIndex) const override
128  {
129  if (objectIndex >= getSize())
130  throw IndexOutOfBoundsException("VectorPositionedContainer::getObject.", objectIndex, 0, getSize());
131  return positions_[objectIndex];
132  }
133 
134  std::shared_ptr<T> getObject(size_t objectIndex) override
135  {
136  if (objectIndex >= getSize())
137  throw IndexOutOfBoundsException("VectorPositionedContainer::getObject.", objectIndex, 0, getSize());
138  return positions_[objectIndex];
139  }
140 
141  const T& object(size_t objectIndex) const override
142  {
143  if (objectIndex >= getSize())
144  throw IndexOutOfBoundsException("VectorPositionedContainer::object.", objectIndex, 0, getSize());
145  return *positions_[objectIndex];
146  }
147 
148  T& object(size_t objectIndex) override
149  {
150  if (objectIndex >= getSize())
151  throw IndexOutOfBoundsException("VectorPositionedContainer::object.", objectIndex, 0, getSize());
152  return *positions_[objectIndex];
153  }
154 
162  void addObject(std::shared_ptr<T> object, size_t objectIndex, bool checkPosition = false)
163  {
164  if (objectIndex >= getSize())
165  throw IndexOutOfBoundsException("VectorPositionedContainer::addObject.", objectIndex, 0, getSize());
166 
167  if (checkPosition && positions_[objectIndex] != nullptr)
168  throw BadIntegerException("VectorPositionedContainer::addObject: object position already occupied in container ", (int)objectIndex);
169 
170  positions_[objectIndex] = object;
171  }
172 
179  void insertObject(std::shared_ptr<T> object, size_t objectIndex)
180  {
181  if (objectIndex > getSize())
182  throw IndexOutOfBoundsException("VectorPositionedContainer::insertObject.", objectIndex, 0, getSize());
183 
184  positions_.insert(positions_.begin() + static_cast<std::ptrdiff_t>(objectIndex), object);
185  }
186 
187 
188  std::shared_ptr<T> removeObject(size_t objectIndex) override
189  {
190  if (objectIndex >= getSize())
191  throw IndexOutOfBoundsException("VectorPositionedContainer::removeObject.", objectIndex, 0, getSize());
192 
193  std::shared_ptr<T> ret = positions_[objectIndex];
194 
195  positions_.erase(positions_.begin() + static_cast<std::ptrdiff_t>(objectIndex));
196 
197  return ret;
198  }
199 
200  void deleteObject(size_t objectIndex) override
201  {
202  if (objectIndex >= getSize())
203  throw IndexOutOfBoundsException("VectorPositionedContainer::deleteObject.", objectIndex, 0, getSize());
204 
205  positions_.erase(positions_.begin() + static_cast<std::ptrdiff_t>(objectIndex));
206  }
207 
208  void deleteObjects(size_t objectIndex, size_t length)
209  {
210  if (objectIndex + length > getSize())
211  throw IndexOutOfBoundsException("VectorPositionedContainer::deleteObjects.", objectIndex + length, 0, getSize());
212 
213  positions_.erase(positions_.begin() + static_cast<std::ptrdiff_t>(objectIndex), positions_.begin() + static_cast<std::ptrdiff_t>(objectIndex + length));
214  }
215 
216  void appendObject(std::shared_ptr<T> object)
217  {
218  positions_.push_back(object);
219  }
220 
221  std::shared_ptr<T> getObject_(size_t objectIndex) const
222  {
223  if (objectIndex >= VectorPositionedContainer<T>::getSize())
224  throw IndexOutOfBoundsException("VectorPositionedContainer::getObject.", objectIndex, 0, VectorPositionedContainer<T>::getSize() - 1);
225  return positions_[objectIndex];
226  }
227 
228  void addObject_(std::shared_ptr<T> object, size_t objectIndex, bool checkPosition = false) const
229  {
230  if (objectIndex >= VectorPositionedContainer<T>::getSize())
231  throw IndexOutOfBoundsException("VectorPositionedContainer::addObject.", objectIndex, 0, VectorPositionedContainer<T>::getSize() - 1);
232 
233  if (checkPosition && positions_[objectIndex] != nullptr)
234  throw BadIntegerException("VectorPositionedContainer::setObject: object position already occupied in container ", (int)objectIndex);
235 
236  const_cast<std::vector<std::shared_ptr<T>>& >(positions_)[objectIndex] = object;
237  }
238 };
239 } // end of namespace bpp.
240 #endif // BPP_SEQ_CONTAINER_VECTORPOSITIONEDCONTAINER_H
The PositionedContainer interface.
The template VectorPositionedContainer class.
const T & object(size_t objectIndex) const override
Get a reference toward an object from the container.
std::shared_ptr< T > removeObject(size_t objectIndex) override
Extract and remove a object from the container.
size_t getSize() const override
the size
const std::shared_ptr< T > getObject(size_t objectIndex) const override
Retrieve an object from the container.
void deleteObject(size_t objectIndex) override
Delete an object from the container.
void appendObject(std::shared_ptr< T > object)
std::vector< std::shared_ptr< T > > positions_
VectorPositionedContainer< T > & operator=(const VectorPositionedContainer< T > &vsc)
copy where shared_ptr elements are shared
VectorPositionedContainer(const VectorPositionedContainer< T > &vsc)
T & object(size_t objectIndex) override
Get a reference toward an object from the container.
bool hasObjectWithPosition(size_t objectIndex) const
VectorPositionedContainer(size_t size=0)
Build a new empty container with specified size.
void addObject(std::shared_ptr< T > object, size_t objectIndex, bool checkPosition=false)
Add an object.
bool isAvailablePosition(size_t objectIndex) const
void insertObject(std::shared_ptr< T > object, size_t objectIndex)
Insert an object.
void addObject_(std::shared_ptr< T > object, size_t objectIndex, bool checkPosition=false) const
std::shared_ptr< T > getObject(size_t objectIndex) override
Retrieve an object from the container.
virtual void nullify()
Nullify all elements.
std::shared_ptr< T > getObject_(size_t objectIndex) const
void deleteObjects(size_t objectIndex, size_t length)
void clear() override
Destroys the vector.
VectorPositionedContainer(const std::vector< std::shared_ptr< T >> &vs)
Build a new container from a set of positions.
VectorPositionedContainer< T > * clone() const override
This alphabet is used to deal NumericAlphabet.