ToPS
Alphabet.cpp
00001 /*
00002  *       Alphabet.cpp
00003  *
00004  *       Copyright 2011 Andre Yoshiaki Kashiwabara <akashiwabara@usp.br>
00005  *                      Ígor Bonádio <ibonadio@ime.usp.br>
00006  *                      Vitor Onuchic <vitoronuchic@gmail.com>
00007  *                      Alan Mitchell Durham <aland@usp.br>
00008  *
00009  *       This program is free software; you can redistribute it and/or modify
00010  *       it under the terms of the GNU  General Public License as published by
00011  *       the Free Software Foundation; either version 3 of the License, or
00012  *       (at your option) any later version.
00013  *
00014  *       This program is distributed in the hope that it will be useful,
00015  *       but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *       GNU General Public License for more details.
00018  *
00019  *       You should have received a copy of the GNU General Public License
00020  *       along with this program; if not, write to the Free Software
00021  *       Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00022  *       MA 02110-1301, USA.
00023  */
00024 
00025 #include "Alphabet.hpp"
00026 #include "Symbol.hpp"
00027 #include "ProbabilisticModelParameter.hpp"
00028 #include <boost/algorithm/string.hpp>
00029 
00030 namespace tops {
00031   bool Alphabet::has (const std::string & s)
00032   {
00033     std::map<std::string, SymbolPtr>::iterator it;
00034     it = _stringToSymbol.find(s);
00035     return it != _stringToSymbol.end();
00036   }
00037 
00038   SymbolPtr Alphabet::getSymbol(const std::string & s)
00039     {
00040       std::map<std::string, SymbolPtr>::iterator it;
00041       it = _stringToSymbol.find(s);
00042       if(it == _stringToSymbol.end())
00043         return SymbolPtr(new Symbol());
00044       else return it->second;
00045     }
00046 
00047   SymbolPtr Alphabet::getSymbol(int k)
00048   {
00049     if((k >= 0) && (k < (int)_pool.size()))
00050       return _pool[k];
00051     else
00052       return SymbolPtr(new Symbol());
00053   }
00054 
00055   unsigned int Alphabet::size ()
00056   {
00057     return _pool.size();
00058   }
00059 
00060   std::string Alphabet::str() const {
00061     std::stringstream out;
00062       std::vector<SymbolPtr, boost::pool_allocator<SymbolPtr> >::const_iterator it;
00063       if(_pool.size() > 0)
00064         {
00065           out << "alphabet = (";
00066 
00067           out << "\"" << _pool[0]->name() << "\"";
00068           for(int k = 1; k < (int)_pool.size(); k++)
00069             {
00070               out << ", \"" << (_pool[k])->name() << "\"";
00071             }
00072           out << ")";
00073         }
00074 
00075       out << std::endl;
00076       return out.str();
00077     }
00078 
00079 
00080   SymbolPtr Alphabet::createSymbol(char * name){
00081       std::string s(name);
00082       return createSymbol(s);
00083     }
00084 
00085   ProbabilisticModelParameterValuePtr Alphabet::getParameterValue(){
00086     ProbabilisticModelParameterValuePtr v;
00087     if(_pool.size() > 0)
00088       {
00089         StringVector strlist;
00090         for(int k = 0; k < (int)_pool.size(); k++)
00091           {
00092             strlist.push_back(_pool[k]->name());
00093           }
00094         v = ProbabilisticModelParameterValuePtr (new StringVectorParameterValue(strlist));
00095       }
00096     return v;
00097   }
00098   SymbolPtr Alphabet::createSymbol (const std::string & name)
00099   {
00100     int i;
00101     for(i = 0; i < (int)_pool.size(); i++)
00102       if(_pool[i]->name() == name)
00103         return getSymbol(i);
00104     SymbolPtr s = SymbolPtr( new Symbol(i, name, this));
00105     _stringToSymbol[name] = s;
00106     _pool.push_back(s);
00107     return getSymbol(i);
00108   }
00109 
00110 
00111   void Alphabet::initializeFromVector(const std::vector<std::string> & str)
00112   {
00113     for(int i = 0; i < (int)str.size(); i++)
00114       createSymbol(str[i]);
00115   }
00116 }