bpp-seq3  3.0.0
SymbolList.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_SYMBOLLIST_H
6 #define BPP_SEQ_SYMBOLLIST_H
7 
8 #include <Bpp/Clonable.h>
9 #include <Bpp/Exceptions.h>
11 
12 #include "Alphabet/Alphabet.h"
13 #include "CoreSymbolList.h"
14 
15 // From the STL:
16 #include <string>
17 #include <vector>
18 #include <random>
19 #include <algorithm>
20 #include <iostream>
21 
22 namespace bpp
23 {
31 template<class T>
33  public virtual TemplateCoreSymbolListInterface<T>
34 {
35 private:
41  std::shared_ptr<const Alphabet> alphabet_;
42 
43 protected:
47  std::vector<T> content_;
48 
49 public:
55  AbstractTemplateSymbolList(std::shared_ptr<const Alphabet> alpha) :
56  alphabet_(alpha), content_() {}
57 
65  AbstractTemplateSymbolList(const std::vector<T>& list, std::shared_ptr<const Alphabet> alpha) :
66  alphabet_(alpha), content_()
67  {
68  setContent(list);
69  }
70 
76  {}
77 
78 
83  alphabet_(list.getAlphabet()), content_(list.size())
84  {
85  for (size_t i = 0; i < list.size(); ++i)
86  {
87  content_[i] = list[i];
88  }
89  }
90 
91 
96  {
97  content_.resize(list.size());
98  for (size_t i = 0; i < list.size(); ++i)
99  {
100  content_[i] = list[i];
101  }
102  alphabet_ = list.getAlphabet();
103  return *this;
104  }
105 
110  {
111  content_ = list.content_;
112  alphabet_ = list.alphabet_;
113  return *this;
114  }
115 
116  // Class destructor
118 
119 public:
120  std::shared_ptr<const Alphabet> getAlphabet() const override { return alphabet_; }
121 
122  const Alphabet& alphabet() const override { return *alphabet_; }
123 
124  size_t size() const override { return content_.size(); }
125 
126  virtual void setContent(const std::vector<T>& list) override
127  {
128  content_ = list;
129  }
130 
131  virtual const std::vector<T>& getContent() const override { return content_; }
132 
133  virtual std::string toString() const override
134  {
135  return "";
136  }
137 
138  void deleteElement(size_t pos) override
139  {
140  if (pos >= content_.size())
141  throw IndexOutOfBoundsException("SymbolList::deleteElement. Invalid position.", pos, 0, size() - 1);
142  content_.erase(content_.begin() + static_cast<std::ptrdiff_t>(pos));
143  }
144 
145  void deleteElements(size_t pos, size_t len) override
146  {
147  if (pos + len > content_.size())
148  throw IndexOutOfBoundsException("SymbolList::deleteElements. Invalid position.", pos + len, 0, size() - 1);
149  content_.erase(content_.begin() + static_cast<std::ptrdiff_t>(pos), content_.begin() + static_cast<std::ptrdiff_t>(pos + len));
150  }
151 
152  void addElement(const T& c) override
153  {
154  content_.push_back(c);
155  }
156 
157  void addElement(size_t pos, const T& c) override
158  {
159  // test:
160  if (pos >= content_.size())
161  throw IndexOutOfBoundsException("SymbolList::addElement. Invalid position.", pos, 0, size() - 1);
162  content_.insert(content_.begin() + static_cast<std::ptrdiff_t>(pos), c);
163  }
164 
165  void setElement(size_t pos, const T& c) override
166  {
167  // test:
168  if (pos >= content_.size())
169  throw IndexOutOfBoundsException("SymbolList::setElement. Invalid position.", pos, 0, size() - 1);
170  content_[pos] = c;
171  }
172 
173  const T& getElement(size_t pos) const override
174  {
175  if (pos >= content_.size())
176  throw IndexOutOfBoundsException("SymbolList::getElement. Invalid position.", pos, 0, size() - 1);
177  return content_[pos];
178  }
179 
180  const T& getValue(size_t pos) const override
181  {
182  if (pos >= content_.size())
183  throw IndexOutOfBoundsException("SymbolList::getValue. Invalid position.", pos, 0, size() - 1);
184  return content_[pos];
185  }
186 
187  const T& operator[](size_t pos) const override { return content_[pos]; }
188 
189  T& operator[](size_t pos) override { return content_[pos]; }
190 
191  void shuffle() override
192  {
193  std::shuffle(content_.begin(), content_.end(), RandomTools::DEFAULT_GENERATOR);
194  }
195 };
196 
197 
205 template<class T>
207  public virtual AbstractTemplateSymbolList<T>,
208  // Note: this needs to be virtual because of diamond inheritance
210 {
211 private:
213 
214 protected:
218  std::vector<std::shared_ptr<CoreSymbolListListener<T>>> listeners_;
219 
225  AbstractTemplateEventDrivenSymbolList(std::shared_ptr<const Alphabet> alpha) :
227 
236  AbstractTemplateEventDrivenSymbolList(const std::vector<T>& list, std::shared_ptr<const Alphabet> alpha) :
237  AbstractTemplateSymbolList<T>(list, alpha), propagateEvents_(true), listeners_()
238  {}
239 
245  propagateEvents_(true),
246  listeners_()
247  {}
248 
249 
256  listeners_(list.listeners_.size())
257  {
258  for (size_t i = 0; i < list.listeners_.size(); ++i)
259  {
260  if (list.listeners_[i]->isShared())
261  {
262  listeners_[i] = list.listeners_[i];
263  }
264  else
265  {
266  listeners_[i] = std::shared_ptr<CoreSymbolListListener<T>>(list.listeners_[i]->clone());
267  }
268  }
269  }
270 
275  {
277  propagateEvents_ = true;
278  listeners_.clear();
279  return *this;
280  }
281 
282 
287  {
290  listeners_.resize(list.listeners_.size());
291  for (size_t i = 0; i < listeners_.size(); ++i)
292  {
293  if (list.listeners_[i]->isShared())
294  {
295  listeners_[i] = list.listeners_[i];
296  }
297  else
298  {
299  listeners_[i] = std::shared_ptr<CoreSymbolListListener<T>>(list.listeners_[i]->clone());
300  }
301  }
302  return *this;
303  }
304 
305  // Class destructor
307 
308 public:
309  virtual void setContent(const std::vector<T>& list) override
310  {
311  CoreSymbolListEditionEvent<T> event(this);
313 
314  // Sequence is valid:
317  }
318 
319  void deleteElement(size_t pos) override
320  {
321  CoreSymbolListDeletionEvent<T> event(this, pos, 1);
325  }
326 
327 
328  void deleteElements(size_t pos, size_t len) override
329  {
330  CoreSymbolListDeletionEvent<T> event(this, pos, len);
334  }
335 
336  virtual void addElement(const T& c) override
337  {
342  }
343 
344  virtual void addElement(size_t pos, const T& c) override
345  {
346  CoreSymbolListInsertionEvent<T> event(this, pos, 1);
350  }
351 
352  virtual void setElement(size_t pos, const T& c) override
353  {
354  CoreSymbolListSubstitutionEvent<T> event(this, pos, pos);
358  }
359 
365  virtual size_t getNumberOfListeners() const override { return listeners_.size(); }
366 
367  virtual const CoreSymbolListListener<T>& listener(size_t i) const override
368  {
369  if (!listeners_[i])
370  throw Exception("EventDrivenSymbolList::listener. The specified listener is missing.");
371  return *listeners_[i];
372  }
373 
374  virtual std::shared_ptr<const CoreSymbolListListener<T>> getListener(size_t i) const override
375  {
376  return listeners_[i];
377  }
378 
379  virtual CoreSymbolListListener<T>& listener(size_t i) override
380  {
381  if (!listeners_[i])
382  throw Exception("EventDrivenSymbolList::listener. The specified listener is missing.");
383  return *listeners_[i];
384  }
385 
386  virtual std::shared_ptr<CoreSymbolListListener<T>> getListener(size_t i) override
387  {
388  return listeners_[i];
389  }
390 
391  virtual void addCoreSymbolListListener(std::shared_ptr<CoreSymbolListListener<T>> listener) override
392  {
393  listeners_.push_back(listener);
394  }
395 
396  virtual void removeCoreSymbolListListener(std::shared_ptr<CoreSymbolListListener<T>> listener) override
397  {
398  if (listener->isRemovable())
399  listeners_.erase(remove(listeners_.begin(), listeners_.end(), listener), listeners_.end());
400  else
401  throw Exception("EventDrivenSymbolList::removeCoreSymbolListListener. Listener is not removable.");
402  }
403 
404 protected:
405  virtual void beforeSequenceChanged(const CoreSymbolListEditionEvent<T>& event) override {}
406  virtual void afterSequenceChanged(const CoreSymbolListEditionEvent<T>& event) override {}
407  virtual void beforeSequenceInserted(const CoreSymbolListInsertionEvent<T>& event) override {}
408  virtual void afterSequenceInserted(const CoreSymbolListInsertionEvent<T>& event) override {}
409  virtual void beforeSequenceDeleted(const CoreSymbolListDeletionEvent<T>& event) override {}
410  virtual void afterSequenceDeleted(const CoreSymbolListDeletionEvent<T>& event) override {}
411  virtual void beforeSequenceSubstituted(const CoreSymbolListSubstitutionEvent<T>& event) override {}
412  virtual void afterSequenceSubstituted(const CoreSymbolListSubstitutionEvent<T>& event) override {}
413 
415  {
416  beforeSequenceChanged(event);
417  if (propagateEvents_)
418  for (size_t i = 0; i < listeners_.size(); ++i)
419  {
420  listeners_[i]->beforeSequenceChanged(event);
421  }
422  }
423 
425  {
426  afterSequenceChanged(event);
427  if (propagateEvents_)
428  for (size_t i = 0; i < listeners_.size(); ++i)
429  {
430  listeners_[i]->afterSequenceChanged(event);
431  }
432  }
433 
435  {
436  beforeSequenceInserted(event);
437  if (propagateEvents_)
438  for (size_t i = 0; i < listeners_.size(); ++i)
439  {
440  listeners_[i]->beforeSequenceInserted(event);
441  }
442  }
443 
445  {
446  afterSequenceInserted(event);
447  if (propagateEvents_)
448  for (size_t i = 0; i < listeners_.size(); ++i)
449  {
450  listeners_[i]->afterSequenceInserted(event);
451  }
452  }
453 
455  {
456  beforeSequenceDeleted(event);
457  if (propagateEvents_)
458  for (size_t i = 0; i < listeners_.size(); ++i)
459  {
460  listeners_[i]->beforeSequenceDeleted(event);
461  }
462  }
463 
465  {
466  afterSequenceDeleted(event);
467  if (propagateEvents_)
468  for (size_t i = 0; i < listeners_.size(); ++i)
469  {
470  listeners_[i]->afterSequenceDeleted(event);
471  }
472  }
473 
475  {
477  if (propagateEvents_)
478  for (size_t i = 0; i < listeners_.size(); ++i)
479  {
480  listeners_[i]->beforeSequenceSubstituted(event);
481  }
482  }
483 
485  {
487  if (propagateEvents_)
488  for (size_t i = 0; i < listeners_.size(); ++i)
489  {
490  listeners_[i]->afterSequenceSubstituted(event);
491  }
492  }
495 protected:
496  void propagateEvents(bool yn) override { propagateEvents_ = yn; }
497  bool propagateEvents() const override { return propagateEvents_; }
498 };
499 } // end of namespace bpp.
500 #endif // BPP_SEQ_SYMBOLLIST_H
A partial implementation of a EventDrivenSymbolList object.
Definition: SymbolList.h:210
virtual void setContent(const std::vector< T > &list) override
Set the whole content of the list.
Definition: SymbolList.h:309
void fireAfterSequenceDeleted(const CoreSymbolListDeletionEvent< T > &event) override
Definition: SymbolList.h:464
void deleteElements(size_t pos, size_t len) override
Remove the elements at position 'pos'.
Definition: SymbolList.h:328
virtual CoreSymbolListListener< T > & listener(size_t i) override
Definition: SymbolList.h:379
AbstractTemplateEventDrivenSymbolList< T > & operator=(const TemplateCoreSymbolListInterface< T > &list)
The generic assignment operator.
Definition: SymbolList.h:274
std::vector< std::shared_ptr< CoreSymbolListListener< T > > > listeners_
Contains the listeners.
Definition: SymbolList.h:218
virtual void beforeSequenceChanged(const CoreSymbolListEditionEvent< T > &event) override
Definition: SymbolList.h:405
AbstractTemplateEventDrivenSymbolList(const AbstractTemplateEventDrivenSymbolList< T > &list)
The copy constructor.
Definition: SymbolList.h:253
AbstractTemplateEventDrivenSymbolList(const TemplateCoreSymbolListInterface< T > &list)
The generic copy constructor.
Definition: SymbolList.h:243
void fireBeforeSequenceInserted(const CoreSymbolListInsertionEvent< T > &event) override
Definition: SymbolList.h:434
virtual std::shared_ptr< const CoreSymbolListListener< T > > getListener(size_t i) const override
Definition: SymbolList.h:374
virtual void beforeSequenceSubstituted(const CoreSymbolListSubstitutionEvent< T > &event) override
Definition: SymbolList.h:411
void fireAfterSequenceSubstituted(const CoreSymbolListSubstitutionEvent< T > &event) override
Definition: SymbolList.h:484
void fireBeforeSequenceChanged(const CoreSymbolListEditionEvent< T > &event) override
Definition: SymbolList.h:414
virtual void addElement(size_t pos, const T &c) override
Add a character at a certain position in the list.
Definition: SymbolList.h:344
virtual ~AbstractTemplateEventDrivenSymbolList()=default
virtual void afterSequenceSubstituted(const CoreSymbolListSubstitutionEvent< T > &event) override
Definition: SymbolList.h:412
virtual size_t getNumberOfListeners() const override
Definition: SymbolList.h:365
virtual void addElement(const T &c) override
Add a character to the end of the list.
Definition: SymbolList.h:336
AbstractTemplateEventDrivenSymbolList< T > & operator=(const AbstractTemplateEventDrivenSymbolList &list)
The assignment operator.
Definition: SymbolList.h:286
virtual void beforeSequenceDeleted(const CoreSymbolListDeletionEvent< T > &event) override
Definition: SymbolList.h:409
virtual void afterSequenceDeleted(const CoreSymbolListDeletionEvent< T > &event) override
Definition: SymbolList.h:410
virtual void beforeSequenceInserted(const CoreSymbolListInsertionEvent< T > &event) override
Definition: SymbolList.h:407
void fireAfterSequenceInserted(const CoreSymbolListInsertionEvent< T > &event) override
Definition: SymbolList.h:444
virtual std::shared_ptr< CoreSymbolListListener< T > > getListener(size_t i) override
Definition: SymbolList.h:386
virtual void afterSequenceInserted(const CoreSymbolListInsertionEvent< T > &event) override
Definition: SymbolList.h:408
virtual const CoreSymbolListListener< T > & listener(size_t i) const override
Definition: SymbolList.h:367
virtual void removeCoreSymbolListListener(std::shared_ptr< CoreSymbolListListener< T >> listener) override
Definition: SymbolList.h:396
void fireAfterSequenceChanged(const CoreSymbolListEditionEvent< T > &event) override
Definition: SymbolList.h:424
void fireBeforeSequenceSubstituted(const CoreSymbolListSubstitutionEvent< T > &event) override
Definition: SymbolList.h:474
virtual void afterSequenceChanged(const CoreSymbolListEditionEvent< T > &event) override
Definition: SymbolList.h:406
AbstractTemplateEventDrivenSymbolList(std::shared_ptr< const Alphabet > alpha)
Build a new void EventDrivenSymbolList object with the specified alphabet.
Definition: SymbolList.h:225
void fireBeforeSequenceDeleted(const CoreSymbolListDeletionEvent< T > &event) override
Definition: SymbolList.h:454
AbstractTemplateEventDrivenSymbolList(const std::vector< T > &list, std::shared_ptr< const Alphabet > alpha)
Build a new EventDrivenSymbolList object with the specified alphabet. The content of the site is init...
Definition: SymbolList.h:236
virtual void addCoreSymbolListListener(std::shared_ptr< CoreSymbolListListener< T >> listener) override
Definition: SymbolList.h:391
virtual void setElement(size_t pos, const T &c) override
Set the element at position 'pos' to character 'c'.
Definition: SymbolList.h:352
void propagateEvents(bool yn) override
Definition: SymbolList.h:496
void deleteElement(size_t pos) override
Remove the element at position 'pos'.
Definition: SymbolList.h:319
A partial implementation of a SymbolList object.
Definition: SymbolList.h:34
const T & getElement(size_t pos) const override
Get the element at position 'pos' as a character.
Definition: SymbolList.h:173
void addElement(const T &c) override
Add a character to the end of the list.
Definition: SymbolList.h:152
virtual void setContent(const std::vector< T > &list) override
Set the whole content of the list.
Definition: SymbolList.h:126
std::shared_ptr< const Alphabet > alphabet_
The Alphabet attribute must be initialized in constructor and then can never be changed.
Definition: SymbolList.h:41
virtual std::string toString() const override
Convert the list as a string.
Definition: SymbolList.h:133
AbstractTemplateSymbolList< T > & operator=(const AbstractTemplateSymbolList< T > &list)
The assignment operator.
Definition: SymbolList.h:109
void deleteElements(size_t pos, size_t len) override
Remove the elements at position 'pos'.
Definition: SymbolList.h:145
T & operator[](size_t pos) override
Operator [] overloaded for quick access to a character in list.
Definition: SymbolList.h:189
AbstractTemplateSymbolList(const std::vector< T > &list, std::shared_ptr< const Alphabet > alpha)
Build a new SymbolList object with the specified alphabet. The content of the site is initialized fro...
Definition: SymbolList.h:65
void shuffle() override
Randomly shuffle the content of the list, with linear complexity.
Definition: SymbolList.h:191
const Alphabet & alphabet() const override
Get the alphabet associated to the list.
Definition: SymbolList.h:122
void deleteElement(size_t pos) override
Remove the element at position 'pos'.
Definition: SymbolList.h:138
void setElement(size_t pos, const T &c) override
Set the element at position 'pos' to character 'c'.
Definition: SymbolList.h:165
virtual const std::vector< T > & getContent() const override
Definition: SymbolList.h:131
const T & getValue(size_t pos) const override
checked access to a character in list.
Definition: SymbolList.h:180
std::shared_ptr< const Alphabet > getAlphabet() const override
Get the alphabet associated to the list.
Definition: SymbolList.h:120
AbstractTemplateSymbolList(const TemplateCoreSymbolListInterface< T > &list)
The copy constructor.
Definition: SymbolList.h:82
size_t size() const override
Get the number of elements in the list.
Definition: SymbolList.h:124
AbstractTemplateSymbolList(const AbstractTemplateSymbolList< T > &list)
The generic copy constructor.
Definition: SymbolList.h:74
void addElement(size_t pos, const T &c) override
Add a character at a certain position in the list.
Definition: SymbolList.h:157
AbstractTemplateSymbolList(std::shared_ptr< const Alphabet > alpha)
Build a new void SymbolList object with the specified alphabet.
Definition: SymbolList.h:55
const T & operator[](size_t pos) const override
Operator [] overloaded for quick access to a character in list.
Definition: SymbolList.h:187
std::vector< T > content_
The list content.
Definition: SymbolList.h:47
AbstractTemplateSymbolList< T > & operator=(const TemplateCoreSymbolListInterface< T > &list)
The generic assignment operator.
Definition: SymbolList.h:95
The Alphabet interface.
Definition: Alphabet.h:99
virtual std::shared_ptr< const Alphabet > getAlphabet() const =0
Get the alphabet associated to the list.
virtual size_t size() const =0
Get the number of elements in the list.
static std::mt19937 DEFAULT_GENERATOR
The CoreSymbolList interface.
Interface for event-driven CoreSymbolList objects.
This alphabet is used to deal NumericAlphabet.