Skip to content

Commit

Permalink
Merge pull request #21 from Xaunther/improve-tableau-state-performance
Browse files Browse the repository at this point in the history
Improve tableau state performance
  • Loading branch information
Alejandro Alfonso authored Dec 4, 2023
2 parents 42f891e + 9a590ae commit 3c7aefb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
4 changes: 3 additions & 1 deletion include/CTableauState.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <deque>

#include "CTableau.h"

namespace blacksmith
Expand All @@ -22,7 +24,7 @@ class CTableauState

private:
// Current piece sequence
std::vector<piece_type> mPieceSequence;
std::deque<piece_type> mPieceSequence;
// True if current set is chess, false if it is a number set. Undefined if any.
std::optional<bool> mChessSet;
// Sets completed
Expand Down
43 changes: 21 additions & 22 deletions src/CTableauState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,39 +25,38 @@ 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 )
{
// 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();
Expand Down

0 comments on commit 3c7aefb

Please sign in to comment.