Update of /cvsroot/cpptool/rfta/bin
In directory sc8-pr-cvs1:/tmp/cvs-serv22533/bin
Added Files:
KeyedString.h
Log Message:
-- additional test file for ASTDump
--- NEW FILE: KeyedString.h ---
// //////////////////////////////////////////////////////////////////////////
// (c)Copyright 2003, Andre Baresel
// Created: 2003/04/28
// //////////////////////////////////////////////////////////////////////////
#ifndef RFTA_KEYEDSTRING_H
#define RFTA_KEYEDSTRING_H
#include <vector>
#include <map>
#include <string>
namespace Refactoring { namespace Testing
{
class KeyedString {
public:
KeyedString& operator<< (std::string characters);
operator std::string () const;
KeyedString& addKeyed (std::string key , std::string word );
std::string asString() const;
std::vector<int> getKeyedIndex(std::string key) const;
std::vector<int> getKeyedLen(std::string key) const;
int getKeyedIndex(std::string key,int index) const;
int getKeyedLen(std::string key,int index) const;
std::string getWord(std::string key, int index) const;
int length() const;
KeyedString& setKeyStart(std::string key);
KeyedString& setKeyEnd(std::string key);
protected:
std::string sequence_;
std::map<std::string,std::vector<int> > wordIndex_;
std::map<std::string,std::vector<int> > wordLen_;
std::map<std::string,std::vector<std::string> > word_;
};
// INLINED MEMBERS:
inline KeyedString& KeyedString::operator<<(std::string characters)
{
sequence_.append(characters);
return *this;
}
inline KeyedString::operator std::string() const
{
return sequence_;
}
inline std::string KeyedString::asString() const
{
return sequence_;
}
inline std::vector<int> KeyedString::getKeyedIndex(std::string key) const
{
std::map<std::string,std::vector<int> >::const_iterator it;
it = wordIndex_.find(key);
if (it == wordIndex_.end()) return std::vector<int>();
return (*it).second;
}
inline std::vector<int> KeyedString::getKeyedLen(std::string key) const
{
std::map<std::string,std::vector<int> >::const_iterator it;
it = wordLen_.find(key);
if (it == wordLen_.end()) return std::vector<int>();
return (*it).second;
}
inline int KeyedString::getKeyedIndex(std::string key, int index) const
{
std::map<std::string,std::vector<int> >::const_iterator it;
it = wordIndex_.find(key);
if (it == wordIndex_.end()) throw std::logic_error("No string registered for this key.");
if ((*it).second.size()<index) throw std::logic_error("Index out of range.");
return (*it).second[index];
}
inline int KeyedString::getKeyedLen(std::string key, int index) const
{
std::map<std::string,std::vector<int> >::const_iterator it;
it = wordLen_.find(key);
if (it == wordLen_.end()) throw std::logic_error("No string registered for this key.");
if ((*it).second.size()<index) throw std::logic_error("Index out of range.");
return (*it).second[index];
}
inline std::string KeyedString::getWord(std::string key, int index) const
{
std::map<std::string,std::vector<std::string> >::const_iterator it;
it = word_.find(key);
if (it == word_.end())
throw std::logic_error("No string registered for this key.");
if ((*it).second.size() < index)
throw std::logic_error("Index out of range.");
return ((*it).second)[index];
}
inline KeyedString& KeyedString::addKeyed(std::string key,std::string word)
{
wordIndex_[key].push_back(sequence_.length());
wordLen_[key].push_back(word.length());
word_[key].push_back(word);
sequence_.append(word);
return *this;
}
inline int KeyedString::length() const
{
return sequence_.length();
}
inline KeyedString& KeyedString::setKeyStart(std::string key)
{
wordIndex_[key].push_back(sequence_.length());
wordLen_[key].push_back(0);
word_[key].push_back("");
return *this;
}
inline KeyedString& KeyedString::setKeyEnd(std::string key)
{
std::vector<int>& v1_ref = wordIndex_[key];
std::vector<int>& v2_ref = wordLen_[key];
if ( v1_ref.empty() || v2_ref.empty() || v2_ref.size() != v1_ref.size() )
throw std::logic_error("Key was not initialized correctly.");
int idx = v1_ref.size()-1;
v2_ref[idx] = sequence_.length() - v1_ref[idx];
return *this;
}
} // namespace Testing
} // namespace Refactoring
#endif // RFTA_KEYEDSTRING_H
|