43 #include <Bpp/Text/TextTools.h> 44 #include <Bpp/Utils/MapTools.h> 58 if (letters_.find(st.
getLetter()) == letters_.end())
61 throw Exception(
"AbstractAlphabet::updateMaps_. A state with the same character code already exists! " + st.
getLetter() +
".");
62 if (nums_.find(st.
getNum()) == nums_.end())
72 alphabet_.push_back(st);
74 updateMaps_(alphabet_.size() - 1, *st);
80 throw (Exception, IndexOutOfBoundsException) {
81 if (pos > alphabet_.size())
82 throw IndexOutOfBoundsException(
"AbstractAlphabet::setState: incorect position", pos, 0, alphabet_.size());
84 if (alphabet_[pos] != 0)
85 delete alphabet_[pos];
89 updateMaps_(pos, *st);
95 map<string, size_t>::const_iterator it = letters_.find(letter);
96 if (it == letters_.end())
97 throw BadCharException(letter,
"AbstractAlphabet::getState(string): Specified base unknown",
this);
98 return * (alphabet_[it->second]);
104 map<string, size_t>::const_iterator it = letters_.find(letter);
105 if (it == letters_.end())
106 throw BadCharException(letter,
"AbstractAlphabet::getStateIndex(string): Specified base unknown",
this);
113 map<int, size_t>::const_iterator it = nums_.find(num);
114 if (it == nums_.end())
115 throw BadIntException(num,
"AbstractAlphabet::getState(int): Specified base unknown",
this);
116 return *(alphabet_[it->second]);
122 map<int, size_t>::const_iterator it = nums_.find(num);
123 if (it == nums_.end())
124 throw BadIntException(num,
"AbstractAlphabet::getStateIndex(int): Specified base unknown",
this);
131 map<string, size_t>::iterator it = letters_.find(letter);
132 if (it == letters_.end())
133 throw BadCharException(letter,
"AbstractAlphabet::getState(string): Specified base unknown",
this);
134 return * (alphabet_[it->second]);
140 map<int, size_t>::iterator it = nums_.find(num);
141 if (it == nums_.end())
142 throw BadIntException(num,
"AbstractAlphabet::getState(int): Specified base unknown",
this);
143 return * (alphabet_[it->second]);
149 if (pos > alphabet_.size())
150 throw IndexOutOfBoundsException(
"AbstractAlphabet::getStateAt: incorect position", pos, 0, alphabet_.size());
151 return * (alphabet_[pos]);
157 if (pos > alphabet_.size())
158 throw IndexOutOfBoundsException(
"AbstractAlphabet::getStateAt: incorect position", pos, 0, alphabet_.size());
159 return * (alphabet_[pos]);
166 return (getState(state)).getName();
173 return (getState(state)).getName();
180 return getState(state).getNum();
187 return (getState(state)).getLetter();
194 map<int, size_t>::const_iterator it = nums_.find(state);
195 if (it != nums_.end())
204 map<string, size_t>::const_iterator it = letters_.find(state);
205 if (it != letters_.end())
214 if (!isIntInAlphabet(state))
throw BadIntException(state,
"AbstractAlphabet::getAlias(int): Specified base unknown.");
224 if (!isCharInAlphabet(state))
throw BadCharException(state,
"AbstractAlphabet::getAlias(char): Specified base unknown.");
234 for (
unsigned int i = 0 ; i < states.size() ; ++i) {
235 vector<int> tmp_s = this->getAlias(states[i]);
236 for (
unsigned int j = 0 ; j < tmp_s.size() ; ++j) {
240 vector<int> ve = MapTools::getKeys(m);
243 for (
unsigned int i = 0 ; i < ve.size() ; ++i) {
244 if (!isIntInAlphabet(ve[i]))
throw BadIntException(ve[i],
"AbstractAlphabet::getGeneric(const vector<int>): Specified base unknown.");
245 key +=
"_" + TextTools::toString(ve[i]);
248 if (ve.size() == 1) {
251 v = this->getUnknownCharacterCode();
260 for (
unsigned int i = 0 ; i < states.size() ; ++i) {
261 vector<string> tmp_s = this->getAlias(states[i]);
262 for (
unsigned int j = 0 ; j < tmp_s.size() ; ++j) {
266 vector<string> ve = MapTools::getKeys(m);
269 for (
unsigned int i = 0 ; i < ve.size() ; ++i) {
270 if (!isCharInAlphabet(ve[i]))
throw BadCharException(ve[i],
"AbstractAlphabet::getAlias(const vector<string>): Specified base unknown.");
271 key += TextTools::toString(ve[i]);
274 if (ve.size() == 1) {
286 if(intList_.size() != alphabet_.size())
288 intList_.resize(alphabet_.size());
289 charList_.resize(alphabet_.size());
290 for (
size_t i = 0; i < alphabet_.size(); ++i)
292 intList_[i] = alphabet_[i]->getNum();
293 charList_[i] = alphabet_[i]->getLetter();
303 if(charList_.size() != alphabet_.size())
305 intList_.resize(alphabet_.size());
306 charList_.resize(alphabet_.size());
307 for (
size_t i = 0; i < alphabet_.size(); ++i)
309 intList_[i] = alphabet_[i]->getNum();
310 charList_[i] = alphabet_[i]->getLetter();
This is the base class to describe states in an Alphabet.
An alphabet exception thrown when trying to specify a bad char to the alphabet.
int getNum() const
Get the state's number.
This alphabet is used to deal NumericAlphabet.
void updateMaps_(size_t pos, const AlphabetState &st)
Update the private maps letters_ and nums_ when adding a state.
int charToInt(const std::string &state) const
Give the int description of a state given its string description.
bool isIntInAlphabet(int state) const
Tell if a state (specified by its int description) is allowed by the the alphabet.
const std::vector< std::string > & getSupportedChars() const
const AlphabetState & getState(const std::string &letter) const
Get a state by its letter.
virtual void setState(size_t pos, AlphabetState *st)
Set a state in the Alphabet.
The alphabet exception base class.
std::vector< int > getAlias(int state) const
Get all resolved states that match a generic state.
const std::string & getLetter() const
Get the letter(s) corresponding to the state.
std::string getName(const std::string &state) const
Get the complete name of a state given its string description.
bool isCharInAlphabet(const std::string &state) const
Tell if a state (specified by its string description) is allowed by the the alphabet.
Exception thrown in case no character is available for a certain state in an alphabet.
int getGeneric(const std::vector< int > &states) const
Get the generic state that match a set of states.
An alphabet exception thrown when trying to specify a bad int to the alphabet.
virtual AlphabetState & getStateAt(size_t stateIndex)
Get a state at a position in the alphabet_ vector.
virtual void registerState(AlphabetState *st)
Add a state to the Alphabet.
const std::vector< int > & getSupportedInts() const
std::string intToChar(int state) const
Give the string description of a state given its int description.
size_t getStateIndex(int state) const