File indexing completed on 2024-05-12 05:39:29
0001 /*************************************************************************** 0002 * Copyright (C) 2014 by Renaud Guezennec * 0003 * https://rolisteam.org/contact * 0004 * * 0005 * This file is part of DiceParser * 0006 * * 0007 * DiceParser is free software; you can redistribute it and/or modify * 0008 * it under the terms of the GNU General Public License as published by * 0009 * the Free Software Foundation; either version 2 of the License, or * 0010 * (at your option) any later version. * 0011 * * 0012 * This program is distributed in the hope that it will be useful, * 0013 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0015 * GNU General Public License for more details. * 0016 * * 0017 * You should have received a copy of the GNU General Public License * 0018 * along with this program; if not, write to the * 0019 * Free Software Foundation, Inc., * 0020 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 0021 ***************************************************************************/ 0022 #include "sortresult.h" 0023 0024 #include "die.h" 0025 0026 SortResultNode::SortResultNode() : m_diceResult(new DiceResult) 0027 { 0028 m_ascending= true; 0029 m_result= m_diceResult; 0030 } 0031 void SortResultNode::run(ExecutionNode* previous) 0032 { 0033 m_previousNode= previous; 0034 if(nullptr == previous) 0035 { 0036 return; 0037 } 0038 DiceResult* previousDiceResult= dynamic_cast<DiceResult*>(previous->getResult()); 0039 m_diceResult->setPrevious(previousDiceResult); 0040 if(nullptr == previousDiceResult) 0041 return; 0042 0043 auto const& diceList= previousDiceResult->getResultList(); 0044 QList<Die*> diceList2= m_diceResult->getResultList(); 0045 0046 /* const auto& asce = [](const Die* a,const Die* b){ 0047 return a->getValue() < b->getValue(); 0048 }; 0049 const auto& desc = [](const Die* a,const Die* b){ 0050 return a->getValue() > b->getValue(); 0051 }; 0052 0053 for(auto const dice : diceList) 0054 { 0055 Die* tmp1 = new Die(*dice); 0056 diceList2.append(tmp1); 0057 } 0058 if(m_ascending) 0059 std::sort(diceList2.begin(), diceList2.end(), asce); 0060 else 0061 std::sort(diceList2.begin(), diceList2.end(), desc);*/ 0062 0063 // half-interval search sorting 0064 for(int i= 0; i < diceList.size(); ++i) 0065 { 0066 Die* tmp1= new Die(*diceList[i]); 0067 //qDebug() << tmp1->getColor() << diceList[i]->getColor(); 0068 //*tmp1=*diceList[i]; 0069 diceList[i]->displayed(); 0070 0071 int j= 0; 0072 bool found= false; 0073 int start= 0; 0074 int end= diceList2.size(); 0075 Die* tmp2= nullptr; 0076 while(!found) 0077 { 0078 int distance= end - start; 0079 j= (start + end) / 2; 0080 if(distance == 0) 0081 { 0082 j= end; 0083 found= true; 0084 } 0085 else 0086 { 0087 tmp2= diceList2[j]; 0088 if(tmp1->getValue() < tmp2->getValue()) 0089 { 0090 end= j; 0091 } 0092 else 0093 { 0094 start= j + 1; 0095 } 0096 } 0097 } 0098 diceList2.insert(j, tmp1); 0099 } 0100 0101 if(!m_ascending) 0102 { 0103 for(int i= 0; i < diceList2.size() / 2; ++i) 0104 { 0105 diceList2.swapItemsAt(i, diceList2.size() - (1 + i)); 0106 } 0107 } 0108 m_diceResult->setResultList(diceList2); 0109 if(nullptr != m_nextNode) 0110 { 0111 m_nextNode->run(this); 0112 } 0113 } 0114 void SortResultNode::setSortAscending(bool asc) 0115 { 0116 m_ascending= asc; 0117 } 0118 QString SortResultNode::toString(bool wl) const 0119 { 0120 if(wl) 0121 { 0122 auto order= m_ascending ? QStringLiteral("Ascending") : QStringLiteral("Descending"); 0123 return QString("%1 [label=\"SortResultNode %2\"]").arg(m_id, order); 0124 } 0125 else 0126 { 0127 return m_id; 0128 } 0129 } 0130 qint64 SortResultNode::getPriority() const 0131 { 0132 qint64 priority= 0; 0133 if(nullptr != m_previousNode) 0134 { 0135 priority= m_previousNode->getPriority(); 0136 } 0137 return priority; 0138 } 0139 ExecutionNode* SortResultNode::getCopy() const 0140 { 0141 SortResultNode* node= new SortResultNode(); 0142 node->setSortAscending(m_ascending); 0143 if(nullptr != m_nextNode) 0144 { 0145 node->setNextNode(m_nextNode->getCopy()); 0146 } 0147 return node; 0148 }