From f1548bcbd65ad79b5a9961ee7fac1a66dab38529 Mon Sep 17 00:00:00 2001 From: Alejandro Alfonso Date: Sat, 2 Dec 2023 10:49:17 +0100 Subject: [PATCH 1/2] Reduce number of operations when updating --- src/CTableauState.cpp | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/src/CTableauState.cpp b/src/CTableauState.cpp index de15ceb..f550b3f 100644 --- a/src/CTableauState.cpp +++ b/src/CTableauState.cpp @@ -28,34 +28,34 @@ CTableauState::CSetState::CSetState() : unsigned short CTableauState::CSetState::Update( const piece_type& aPiece ) { - // If it is a wildcard, just keep it going! - if( aPiece == CTableau::E_PIECE_TYPE::WILDCARD ) - mPieceSequence.push_back( aPiece ); - // If it continues the sequence - else if( !mChessSet || *mChessSet == CTableau::CHESS_PIECES().contains( aPiece ) ) + if( aPiece != CTableau::E_PIECE_TYPE::WILDCARD ) { - const auto& found = std::find( mPieceSequence.begin(), mPieceSequence.end(), aPiece ); - // If repeated, delete that portion and reset count before adding new - if( found != mPieceSequence.end() ) + const auto pieceSet = CTableau::CHESS_PIECES().contains( aPiece ); + if( !mChessSet ) + mChessSet = pieceSet; + // If it continues the sequence + else if( *mChessSet == pieceSet ) { - mPieceSequence.erase( mPieceSequence.begin(), found + 1 ); + auto found = std::find( mPieceSequence.cbegin(), mPieceSequence.cend(), aPiece ); + // If repeated, delete that portion and reset count before adding new + if( found != mPieceSequence.cend() ) + { + mPieceSequence.erase( mPieceSequence.cbegin(), ++found ); + mCount = 0; + } + } + // Otherwise, reset + else + { + mPieceSequence.clear(); + mChessSet = !*mChessSet; mCount = 0; } - mPieceSequence.push_back( aPiece ); - mChessSet = CTableau::CHESS_PIECES().contains( aPiece ); - - } - // Otherwise, reset - else - { - mPieceSequence.clear(); - mPieceSequence.push_back( aPiece ); - mChessSet = !*mChessSet; - mCount = 0; } + mPieceSequence.push_back( aPiece ); // Detect score given by this piece - if( mPieceSequence.size() == CTableau::CHESS_PIECES().size() ) + if( mChessSet && mPieceSequence.size() >= CTableau::CHESS_PIECES().size() ) { ++mCount; mPieceSequence.clear(); From 9a590ae7ea323202cec5590b00e60e46bd238018 Mon Sep 17 00:00:00 2001 From: Alejandro Alfonso Date: Sat, 2 Dec 2023 21:51:13 +0100 Subject: [PATCH 2/2] Use deque instead of vector --- include/CTableauState.h | 4 +++- src/CTableauState.cpp | 1 - 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/include/CTableauState.h b/include/CTableauState.h index 0eeb865..f3c9f99 100644 --- a/include/CTableauState.h +++ b/include/CTableauState.h @@ -1,5 +1,7 @@ #pragma once +#include + #include "CTableau.h" namespace blacksmith @@ -22,7 +24,7 @@ class CTableauState private: // Current piece sequence - std::vector mPieceSequence; + std::deque mPieceSequence; // True if current set is chess, false if it is a number set. Undefined if any. std::optional mChessSet; // Sets completed diff --git a/src/CTableauState.cpp b/src/CTableauState.cpp index f550b3f..bdd16a7 100644 --- a/src/CTableauState.cpp +++ b/src/CTableauState.cpp @@ -23,7 +23,6 @@ constexpr unsigned short CTableauState::RB_DESTINATIONS_COUNT() CTableauState::CSetState::CSetState() : mCount( 0 ) { - mPieceSequence.reserve( CTableau::CHESS_PIECES().size() ); } unsigned short CTableauState::CSetState::Update( const piece_type& aPiece )