bpp-seq3  3.0.0
IntSymbolList.cpp
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: The Bio++ Development Group
2 //
3 // SPDX-License-Identifier: CECILL-2.1
4 
5 #include "IntSymbolList.h"
6 #include "StringSequenceTools.h"
7 
8 using namespace bpp;
9 
10 using namespace std;
11 
12 
13 /****************************************************************************************/
14 
15 void IntSymbolList::setContent(const vector<string>& list)
16 {
17  // Check list for incorrect characters
18  vector<int> coded(list.size());
19  for (auto i : list)
20  {
21  if (!getAlphabet()->isCharInAlphabet(i))
22  throw BadCharException(i, "IntSymbolList::setContent", getAlphabet());
23  }
24 
25  size_t j = 0;
26  for (auto i : list)
27  {
28  coded[j++] = getAlphabet()->charToInt(i);
29  }
30 
31  // BasicIntSymbolList is valid:
33 }
34 
35 /****************************************************************************************/
36 
37 void IntSymbolList::setContent(const vector<int>& list)
38 {
39  // Check list for incorrect characters
40  for (auto i : list)
41  {
42  if (!getAlphabet()->isIntInAlphabet(i))
43  throw BadIntException(i, "IntSymbolList::setContent", getAlphabet());
44  }
45 
46  // Sequence is valid:
48 }
49 
50 /****************************************************************************************/
51 
52 string IntSymbolList::getChar(size_t pos) const
53 {
54  if (pos >= content_.size())
55  throw IndexOutOfBoundsException("BasicIntSymbolList::getChar. Invalid position.", pos, 0, size() - 1);
56  string c = "";
57  try
58  {
59  c = getAlphabet()->intToChar(content_[pos]);
60  }
61  catch (BadIntException& bie)
62  {
63  // This should never happen!
64  }
65  return c;
66 }
67 
68 /****************************************************************************************/
69 
70 void EventDrivenIntSymbolList::setContent(const vector<string>& list)
71 {
72  // Check list for incorrect characters
73  vector<int> coded(list.size());
74  for (auto i : list)
75  {
76  if (!getAlphabet()->isCharInAlphabet(i))
77  throw BadCharException(i, "EventDrivenIntSymbolList::setContent", getAlphabet());
78  }
79 
80  size_t j = 0;
81  for (auto i : list)
82  {
83  coded[j++] = getAlphabet()->charToInt(i);
84  }
85 
87 }
88 
89 /****************************************************************************************/
90 
91 void EventDrivenIntSymbolList::setContent(const vector<int>& list)
92 {
93  // Check list for incorrect characters
94  for (auto i : list)
95  {
96  if (!getAlphabet()->isIntInAlphabet(i))
97  throw BadIntException(i, "EventDrivenIntSymbolList::setContent", getAlphabet());
98  }
99 
101 }
102 
103 /****************************************************************************************/
104 
106 {
107  auto alphaPtr = getAlphabet();
108  return StringSequenceTools::decodeSequence(content_, alphaPtr);
109 }
110 
111 /****************************************************************************************/
112 
114 {
115  AbstractTemplateEventDrivenSymbolList<int>::addElement(getAlphabet()->charToInt(c));
116 }
117 
118 /****************************************************************************************/
119 
120 void EventDrivenIntSymbolList::addElement(size_t pos, const string& c)
121 {
122  AbstractTemplateEventDrivenSymbolList<int>::addElement(pos, getAlphabet()->charToInt(c));
123 }
124 
125 /****************************************************************************************/
126 
127 void EventDrivenIntSymbolList::setElement(size_t pos, const string& c)
128 {
129  AbstractTemplateEventDrivenSymbolList<int>::setElement(pos, getAlphabet()->charToInt(c));
130 }
131 
132 /****************************************************************************************/
133 
134 string EventDrivenIntSymbolList::getChar(size_t pos) const
135 {
136  if (pos >= content_.size())
137  throw IndexOutOfBoundsException("EventDrivenIntSymbolList::getChar. Invalid position.", pos, 0, size() - 1);
138  string c = "";
139  try
140  {
141  c = getAlphabet()->intToChar(content_[pos]);
142  }
143  catch (BadIntException& bie)
144  {
145  // This should never happen!
146  }
147  return c;
148 }
virtual void setContent(const std::vector< T > &list) override
Set the whole content of the list.
Definition: SymbolList.h:309
virtual void addElement(const T &v) override
Add a character to the end of the list.
Definition: SymbolList.h:336
virtual void setElement(size_t pos, const T &v) override
Set the element at position 'pos' to character 'c'.
Definition: SymbolList.h:352
virtual void setContent(const std::vector< T > &list) override
Set the whole content of the list.
Definition: SymbolList.h:126
An alphabet exception thrown when trying to specify a bad char to the alphabet.
An alphabet exception thrown when trying to specify a bad int to the alphabet.
virtual std::string toString() const override
Convert the list as a string.
virtual std::string getChar(size_t pos) const override
Get the element at position 'pos' as a character.
virtual void setContent(const std::vector< T > &list)=0
Set the whole content of the list.
std::string getChar(size_t pos) const override
Get the element at position 'pos' as a character.
void addElement(const T &v) override
Definition: SymbolList.h:152
void setElement(size_t pos, const T &v) override
Definition: SymbolList.h:165
static std::string decodeSequence(const std::vector< int > &sequence, std::shared_ptr< const Alphabet > &alphabet)
Convert a sequence to its string representation.
This alphabet is used to deal NumericAlphabet.