diff options
| author | 2019-02-14 23:45:18 +0100 | |
|---|---|---|
| committer | 2019-03-23 17:58:32 +0100 | |
| commit | 1d962712fdba24078b6ce35a2d616cd3155cfc31 (patch) | |
| tree | 13d888e3734d44d44b83732bfe35d20bb23a7584 /node | |
| parent | c46ad9d1ceb345f09590ad8ea9a98b33a2a0c31b (diff) | |
| download | OneRoll-1d962712fdba24078b6ce35a2d616cd3155cfc31.tar.gz OneRoll-1d962712fdba24078b6ce35a2d616cd3155cfc31.zip | |
add occurencecountnode
Diffstat (limited to 'node')
| -rw-r--r-- | node/occurencecountnode.cpp | 112 | ||||
| -rw-r--r-- | node/occurencecountnode.h | 11 |
2 files changed, 122 insertions, 1 deletions
diff --git a/node/occurencecountnode.cpp b/node/occurencecountnode.cpp index 6343e8e..8826188 100644 --- a/node/occurencecountnode.cpp +++ b/node/occurencecountnode.cpp @@ -1,6 +1,116 @@ #include "occurencecountnode.h" +#include "result/diceresult.h" +#include "result/stringresult.h" -OccurenceCountNode::OccurenceCountNode() +OccurenceCountNode::OccurenceCountNode() : ExecutionNode() { + m_stringResult= new StringResult(); + m_result= m_stringResult; +} + +void OccurenceCountNode::run(ExecutionNode* previous) +{ + m_previousNode= previous; + std::map<qint64, qint64> mapOccurence; + if(nullptr == m_previousNode) + return; + + DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(m_previousNode->getResult()); + // m_diceResult->setPrevious(previousDiceResult); + if(nullptr == previousDiceResult) + return; + + auto const& diceList= previousDiceResult->getResultList(); + QVector<qint64> vec; + + for(auto dice : diceList) + { + auto val= dice->getValue(); + vec << val; + auto it= mapOccurence.find(val); + if(it == mapOccurence.end()) + mapOccurence[val]= 1; + else + mapOccurence[val]+= 1; + } + + std::sort(vec.begin(), vec.end()); + + QStringList list; + for(auto key : mapOccurence) + { + if(key.first >= m_height && key.second >= m_width) + list << QStringLiteral("%1x%2").arg(key.first).arg(key.second); + } + + QStringList resultList; + std::for_each(vec.begin(), vec.end(), [&resultList](qint64 val) { resultList << QString::number(val); }); + + QString result; + + if(!list.isEmpty()) + result= list.join(','); + else + result= QObject::tr("No matching result"); + + m_stringResult->setText(QStringLiteral("%1 - [%2]").arg(result).arg(resultList.join(','))); + + if(nullptr != m_nextNode) + { + m_nextNode->run(this); + } +} +QString OccurenceCountNode::toString(bool label) const +{ + if(label) + { + return QString("%1 [label=\"OccurenceCountNode %2\"]").arg(m_id); + } + else + { + return m_id; + } +} +ExecutionNode* OccurenceCountNode::getCopy() const +{ + return nullptr; +} +qint64 OccurenceCountNode::getPriority() const +{ + qint64 priority= 0; + if(nullptr != m_previousNode) + { + priority= m_previousNode->getPriority(); + } + return priority; +} + +qint64 OccurenceCountNode::getWidth() const +{ + return m_width; +} + +void OccurenceCountNode::setWidth(const qint64& width) +{ + m_width= width; +} + +qint64 OccurenceCountNode::getHeight() const +{ + return m_height; +} + +void OccurenceCountNode::setHeight(const qint64& height) +{ + m_height= height; +} +Validator* OccurenceCountNode::getValidator() const +{ + return m_validator; +} + +void OccurenceCountNode::setValidator(Validator* validator) +{ + m_validator= validator; } diff --git a/node/occurencecountnode.h b/node/occurencecountnode.h index 9c9aa13..ed7f8b4 100644 --- a/node/occurencecountnode.h +++ b/node/occurencecountnode.h @@ -4,6 +4,7 @@ #include "executionnode.h" #include "validator.h" +class StringResult; class OccurenceCountNode : public ExecutionNode { public: @@ -15,10 +16,20 @@ public: ExecutionNode* getCopy() const; qint64 getPriority() const; + qint64 getWidth() const; + void setWidth(const qint64 &width); + + qint64 getHeight() const; + void setHeight(const qint64 &height); + + Validator *getValidator() const; + void setValidator(Validator *validator); + private: qint64 m_width=0; qint64 m_height=0; Validator* m_validator; + StringResult* m_stringResult; }; #endif // OCCURENCECOUNTNODE_H |