Added ability to check index of keyword from Deck.
This commit is contained in:
parent
7bd566d6a6
commit
b02e536ad8
@ -27,11 +27,17 @@ namespace Opm {
|
||||
Deck::Deck() {
|
||||
}
|
||||
|
||||
bool Deck::hasKeyword(DeckKeywordConstPtr keyword) const {
|
||||
return (m_keywordIndex.find( const_cast<DeckKeyword *>(keyword.get()) ) != m_keywordIndex.end());
|
||||
}
|
||||
|
||||
|
||||
bool Deck::hasKeyword(const std::string& keyword) const {
|
||||
return (m_keywordMap.find(keyword) != m_keywordMap.end());
|
||||
}
|
||||
|
||||
void Deck::addKeyword( DeckKeywordConstPtr keyword) {
|
||||
m_keywordIndex[ keyword.get() ] = m_keywordList.size();
|
||||
m_keywordList.push_back(keyword);
|
||||
|
||||
if (!hasKeyword(keyword->name())) {
|
||||
@ -42,9 +48,17 @@ namespace Opm {
|
||||
std::vector<DeckKeywordConstPtr>& keywordList = m_keywordMap[keyword->name()];
|
||||
keywordList.push_back(keyword);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
size_t Deck::getKeywordIndex(DeckKeywordConstPtr keyword) const {
|
||||
auto iter = m_keywordIndex.find( const_cast<DeckKeyword *>(keyword.get()));
|
||||
if (iter == m_keywordIndex.end())
|
||||
throw std::invalid_argument("Keyword not in deck.");
|
||||
|
||||
return (*iter).second;
|
||||
}
|
||||
|
||||
|
||||
DeckKeywordConstPtr Deck::getKeyword(const std::string& keyword, size_t index) const {
|
||||
const std::vector<DeckKeywordConstPtr>& keywordList = getKeywordList( keyword );
|
||||
if (index < keywordList.size())
|
||||
|
@ -32,12 +32,16 @@ namespace Opm {
|
||||
class Deck {
|
||||
public:
|
||||
Deck();
|
||||
bool hasKeyword(DeckKeywordConstPtr keyword) const;
|
||||
bool hasKeyword( const std::string& keyword ) const;
|
||||
void addKeyword( DeckKeywordConstPtr keyword);
|
||||
DeckKeywordConstPtr getKeyword(const std::string& keyword , size_t index) const;
|
||||
DeckKeywordConstPtr getKeyword(const std::string& keyword) const;
|
||||
DeckKeywordConstPtr getKeyword(size_t index) const;
|
||||
|
||||
size_t getKeywordIndex(DeckKeywordConstPtr keyword) const;
|
||||
|
||||
|
||||
size_t numKeywords(const std::string& keyword) const;
|
||||
const std::vector<DeckKeywordConstPtr>& getKeywordList(const std::string& keyword) const;
|
||||
size_t size() const;
|
||||
@ -53,6 +57,7 @@ namespace Opm {
|
||||
|
||||
std::vector<DeckKeywordConstPtr> m_keywordList;
|
||||
std::map<std::string, std::vector<DeckKeywordConstPtr> > m_keywordMap;
|
||||
std::map<const DeckKeyword *, size_t> m_keywordIndex;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<Deck> DeckPtr;
|
||||
|
@ -212,6 +212,22 @@ BOOST_AUTO_TEST_CASE(keywordList_getbyindex_correctkeywordreturned) {
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(KeywordIndexCorrect) {
|
||||
Deck deck;
|
||||
DeckKeywordPtr keyword1 = DeckKeywordPtr(new DeckKeyword("TRULS"));
|
||||
DeckKeywordPtr keyword2 = DeckKeywordPtr(new DeckKeyword("TRULS"));
|
||||
DeckKeywordPtr keyword3 = DeckKeywordPtr(new DeckKeyword("TRULS"));
|
||||
DeckKeywordPtr keyword4 = DeckKeywordPtr(new DeckKeyword("TRULS4"));
|
||||
deck.addKeyword(keyword1);
|
||||
deck.addKeyword(keyword2);
|
||||
deck.addKeyword(keyword3);
|
||||
|
||||
BOOST_CHECK_THROW( deck.getKeywordIndex( keyword4 ) , std::invalid_argument);
|
||||
|
||||
BOOST_CHECK_EQUAL(0U , deck.getKeywordIndex(keyword1));
|
||||
BOOST_CHECK_EQUAL(1U , deck.getKeywordIndex(keyword2));
|
||||
BOOST_CHECK_EQUAL(2U , deck.getKeywordIndex(keyword3));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user