bpp-seq3  3.0.0
VectorMappedContainer.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_VECTORMAPPEDCONTAINER_H
6 #define BPP_SEQ_CONTAINER_VECTORMAPPEDCONTAINER_H
7 
8 
9 #include "MappedNamedContainer.h"
12 
13 // From the STL library:
14 #include <string>
15 #include <vector>
16 #include <iostream>
17 
18 namespace bpp
19 {
30 template<class T>
32  public virtual PositionedNamedContainerInterface<T>,
33  public MappedNamedContainer<T>,
35 {
36 private:
40  std::vector<std::string> vNames_;
41 
45  std::map<std::string, size_t> mNames_;
46 
47 public:
51  vNames_(),
52  mNames_()
53  {}
54 
56  MappedNamedContainer<T>(vsc),
58  vNames_(vsc.vNames_),
59  mNames_(vsc.mNames_)
60  {}
61 
63  {
66  vNames_ = vsc.vNames_;
67  mNames_ = vsc.mNames_;
68 
69  return *this;
70  }
71 
73 
74 public:
80  VectorMappedContainer<T>* clone() const override
81  {
82  return new VectorMappedContainer(*this);
83  }
84 
87  /*
88  * @brief size of the position vector
89  * !! may be different than the actual number of objects
90  *
91  */
92  size_t getSize() const override
93  {
95  }
96 
97  /*
98  * @brief real number of objects
99  *
100  */
101  size_t getNumberOfObjects() const
102  {
104  }
105 
106  size_t getObjectPosition(const std::string& name) const override
107  {
108  auto it = mNames_.find(name);
109  if (it == mNames_.end())
110  throw Exception("VectorMappedContainer::getObjectPosition : Not found object with name " + name);
111 
112  return it->second;
113  }
114 
115  const std::string& getObjectName(size_t objectIndex) const override
116  {
117  if (objectIndex >= getSize())
118  throw IndexOutOfBoundsException("VectorMappedContainer::getObjectName.", objectIndex, 0, getSize() - 1);
119 
120  return vNames_[objectIndex];
121  }
122 
124 
126 
128 
130 
132 
133  std::vector<std::string> getObjectNames() const override
134  {
135  return vNames_;
136  }
137 
138  void setObjectNames(const std::vector<std::string>& names)
139  {
140  if (names.size() != vNames_.size())
141  throw BadSizeException("VectorMappedContainer::setObjectNames: bad number of new names", vNames_.size(), names.size());
142 
143  mNames_.clear();
144 
145  for (size_t i = 0; i < names.size(); i++)
146  {
148  mNames_[names[i]] = i;
149  }
150 
151  vNames_ = names;
152  }
153 
154  void setObjectName(size_t pos, const std::string& name)
155  {
157  mNames_[name] = pos;
158  vNames_[pos] = name;
159  }
160 
161  void addObject(std::shared_ptr<T> newObject, size_t objectIndex, const std::string& name, bool check = false) override
162  {
163  VectorPositionedContainer<T>::addObject(newObject, objectIndex, check);
164  MappedNamedContainer<T>::addObject(newObject, name, check);
165  vNames_[objectIndex] = name;
166  mNames_[name] = objectIndex;
167  }
168 
170 
171  void insertObject(std::shared_ptr<T> newObject, size_t objectIndex, const std::string& name) override
172  {
173  MappedNamedContainer<T>::addObject(newObject, name, true);
174  VectorPositionedContainer<T>::insertObject(newObject, objectIndex);
175  vNames_.insert(vNames_.begin() + static_cast<std::ptrdiff_t>(objectIndex), name);
176  for (auto it : mNames_)
177  {
178  if (it.second >= objectIndex)
179  it.second++;
180  }
181 
182  mNames_[name] = objectIndex;
183  }
184 
185  virtual void appendObject(std::shared_ptr<T> newObject, const std::string& name, bool checkNames = true)
186  {
187  MappedNamedContainer<T>::addObject(newObject, name, checkNames);
189  vNames_.push_back(name);
190  mNames_[name] = vNames_.size() - 1;
191  }
192 
193  std::shared_ptr<T> removeObject(size_t objectIndex) override
194  {
195  std::shared_ptr<T> obj = VectorPositionedContainer<T>::removeObject(objectIndex);
197  mNames_.erase(vNames_[objectIndex]);
198  for (auto it : mNames_)
199  {
200  if (it.second > objectIndex)
201  it.second--;
202  }
203  vNames_.erase(vNames_.begin() + static_cast<std::ptrdiff_t>(objectIndex));
204  return obj;
205  }
206 
207  void deleteObject(size_t objectIndex) override
208  {
211 
212  mNames_.erase(vNames_[objectIndex]);
213  for (auto it : mNames_)
214  {
215  if (it.second > objectIndex)
216  it.second--;
217  }
218  vNames_.erase(vNames_.begin() + static_cast<std::ptrdiff_t>(objectIndex));
219  }
220 
221 
222  std::shared_ptr<T> removeObject(const std::string& name) override
223  {
224  return removeObject(mNames_[name]);
225  }
226 
227  void deleteObject(const std::string& name) override
228  {
229  deleteObject(mNames_[name]);
230  }
231 
232  void addObject_(std::shared_ptr<T> newObject, size_t objectIndex, const std::string& name, bool check = false) const
233  {
234  VectorPositionedContainer<T>::addObject_(newObject, objectIndex, check);
235  MappedNamedContainer<T>::addObject_(newObject, name, check);
236  const_cast<std::vector<std::string>& >(vNames_)[objectIndex] = name;
237  const_cast<std::map<std::string, size_t>&>(mNames_)[name] = objectIndex;
238  }
239 
240  void clear() override
241  {
244  vNames_.clear();
245  mNames_.clear();
246  }
247 
248  void nullify() override
249  {
252  }
253 };
254 } // end of namespace bpp.
255 
256 #endif // BPP_SEQ_CONTAINER_VECTORMAPPEDCONTAINER_H
MappedNamedContainer class.
void addObject(std::shared_ptr< T > newObject, const std::string &name, bool checkName=false)
Set an object.
void changeName(const std::string &okey, const std::string &nkey)
change the key of an object.
std::shared_ptr< T > removeObject(const std::string &name) override
Remove and returns an object.
void clear() override
Delete all objects in the container.
virtual void nullify()
Nullify all elements.
void deleteObject(const std::string &name) override
Remove an object.
size_t getSize() const override
Get the number of objects in the container.
MappedNamedContainer & operator=(const MappedNamedContainer &msc)
void addObject_(std::shared_ptr< T > newObject, const std::string &name, bool checkName=false) const
The template PositionedNamedContainer interface, that links position & name containers.
The template VectorMappedContainer class.
void addObject(std::shared_ptr< T > newObject, size_t objectIndex, const std::string &name, bool check=false) override
void insertObject(std::shared_ptr< T > newObject, size_t objectIndex, const std::string &name) override
VectorMappedContainer(const VectorMappedContainer &vsc)
void deleteObject(size_t objectIndex) override
Delete an object from the container.
virtual void appendObject(std::shared_ptr< T > newObject, const std::string &name, bool checkNames=true)
void clear() override
Delete all objects in the container.
void addObject_(std::shared_ptr< T > newObject, size_t objectIndex, const std::string &name, bool check=false) const
const std::string & getObjectName(size_t objectIndex) const override
std::map< std::string, size_t > mNames_
map <string, size_t> for the positions of the names
VectorMappedContainer< T > * clone() const override
std::vector< std::string > getObjectNames() const override
void nullify() override
Nullify all elements.
std::shared_ptr< T > removeObject(const std::string &name) override
Remove and returns an object.
size_t getObjectPosition(const std::string &name) const override
Link between position & name.
void deleteObject(const std::string &name) override
Remove an object.
VectorMappedContainer< T > & operator=(const VectorMappedContainer &vsc)
void setObjectNames(const std::vector< std::string > &names)
size_t getSize() const override
Get the number of objects in the container.
std::vector< std::string > vNames_
vector of the names, in same order as objects
void setObjectName(size_t pos, const std::string &name)
std::shared_ptr< T > removeObject(size_t objectIndex) override
Extract and remove a object from the container.
The template VectorPositionedContainer class.
std::shared_ptr< T > removeObject(size_t objectIndex) override
Extract and remove a object from the container.
size_t getSize() const override
the size
void deleteObject(size_t objectIndex) override
Delete an object from the container.
void appendObject(std::shared_ptr< T > object)
VectorPositionedContainer< T > & operator=(const VectorPositionedContainer< T > &vsc)
copy where shared_ptr elements are shared
void addObject(std::shared_ptr< T > object, size_t objectIndex, bool checkPosition=false)
Add an object.
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
virtual void nullify()
Nullify all elements.
void clear() override
Destroys the vector.
This alphabet is used to deal NumericAlphabet.