mirror of
https://github.com/Microsoft/calculator.git
synced 2025-07-16 02:02:51 -07:00
Replacing CalculatorVector usage with std::vector (#756)
* Replacing CalculatorVector usage with std::vector Assumptions made here are that memory allocations are not recoverable. If it can be proved that an index will be in range, then the indexing operation is used. If not (without manual checks) the std::vector::at function is used to throw an exception in case of a programmer bug. * Changes based on PR feedback Using auto& in CalculatorCollector::UpdateHistoryExpression so the token.first value is properly updated. Using range for loop to GenerateExpressions. Setting isEditable directly to the result of boolean expression. Using token.second directly instead of creating a separate tokenCommandIndex variable. * Fixing issue with generating expressions strings. A space should not be added before the first item.
This commit is contained in:
parent
25cdca991c
commit
6366e0c535
29 changed files with 244 additions and 480 deletions
|
@ -3,7 +3,6 @@
|
|||
|
||||
#include <string>
|
||||
#include "Header Files/CCommand.h"
|
||||
#include "CalculatorVector.h"
|
||||
#include "ExpressionCommand.h"
|
||||
|
||||
using namespace std;
|
||||
|
@ -35,18 +34,18 @@ void CParentheses::Accept(_In_ ISerializeCommandVisitor& commandVisitor)
|
|||
|
||||
CUnaryCommand::CUnaryCommand(int command)
|
||||
{
|
||||
m_command = make_shared<CalculatorVector<int>>();
|
||||
m_command->Append(command);
|
||||
m_command = make_shared<vector<int>>();
|
||||
m_command->push_back(command);
|
||||
}
|
||||
|
||||
CUnaryCommand::CUnaryCommand(int command1, int command2)
|
||||
{
|
||||
m_command = make_shared<CalculatorVector<int>>();
|
||||
m_command->Append(command1);
|
||||
m_command->Append(command2);
|
||||
m_command = make_shared<vector<int>>();
|
||||
m_command->push_back(command1);
|
||||
m_command->push_back(command2);
|
||||
}
|
||||
|
||||
const shared_ptr<CalculatorVector<int>>& CUnaryCommand::GetCommands() const
|
||||
const shared_ptr<vector<int>>& CUnaryCommand::GetCommands() const
|
||||
{
|
||||
return m_command;
|
||||
}
|
||||
|
@ -58,15 +57,15 @@ CalculationManager::CommandType CUnaryCommand::GetCommandType() const
|
|||
|
||||
void CUnaryCommand::SetCommand(int command)
|
||||
{
|
||||
m_command->Clear();
|
||||
m_command->Append(command);
|
||||
m_command->clear();
|
||||
m_command->push_back(command);
|
||||
}
|
||||
|
||||
void CUnaryCommand::SetCommands(int command1, int command2)
|
||||
{
|
||||
m_command->Clear();
|
||||
m_command->Append(command1);
|
||||
m_command->Append(command2);
|
||||
m_command->clear();
|
||||
m_command->push_back(command1);
|
||||
m_command->push_back(command2);
|
||||
}
|
||||
|
||||
void CUnaryCommand::Accept(_In_ ISerializeCommandVisitor& commandVisitor)
|
||||
|
@ -99,7 +98,7 @@ void CBinaryCommand::Accept(_In_ ISerializeCommandVisitor& commandVisitor)
|
|||
commandVisitor.Visit(*this);
|
||||
}
|
||||
|
||||
COpndCommand::COpndCommand(shared_ptr<CalculatorVector<int>> const& commands, bool fNegative, bool fDecimal, bool fSciFmt)
|
||||
COpndCommand::COpndCommand(shared_ptr<vector<int>> const& commands, bool fNegative, bool fDecimal, bool fSciFmt)
|
||||
: m_commands(commands)
|
||||
, m_fNegative(fNegative)
|
||||
, m_fSciFmt(fSciFmt)
|
||||
|
@ -115,27 +114,25 @@ void COpndCommand::Initialize(Rational const& rat)
|
|||
m_fInitialized = true;
|
||||
}
|
||||
|
||||
const shared_ptr<CalculatorVector<int>>& COpndCommand::GetCommands() const
|
||||
const shared_ptr<vector<int>>& COpndCommand::GetCommands() const
|
||||
{
|
||||
return m_commands;
|
||||
}
|
||||
|
||||
void COpndCommand::SetCommands(shared_ptr<CalculatorVector<int>> const& commands)
|
||||
void COpndCommand::SetCommands(shared_ptr<vector<int>> const& commands)
|
||||
{
|
||||
m_commands = commands;
|
||||
}
|
||||
|
||||
void COpndCommand::AppendCommand(int command)
|
||||
{
|
||||
unsigned int nCommands;
|
||||
m_commands->GetSize(&nCommands);
|
||||
if (m_fSciFmt)
|
||||
{
|
||||
ClearAllAndAppendCommand(static_cast<CalculationManager::Command>(command));
|
||||
}
|
||||
else
|
||||
{
|
||||
m_commands->Append(command);
|
||||
m_commands->push_back(command);
|
||||
}
|
||||
|
||||
if (command == IDC_PNT)
|
||||
|
@ -146,14 +143,8 @@ void COpndCommand::AppendCommand(int command)
|
|||
|
||||
void COpndCommand::ToggleSign()
|
||||
{
|
||||
unsigned int commandCount;
|
||||
m_commands->GetSize(&commandCount);
|
||||
|
||||
for (unsigned int i = 0; i < commandCount; i++)
|
||||
for (int nOpCode : *m_commands)
|
||||
{
|
||||
int nOpCode;
|
||||
m_commands->GetAt(i, &nOpCode);
|
||||
|
||||
if (nOpCode != IDC_0)
|
||||
{
|
||||
m_fNegative = !m_fNegative;
|
||||
|
@ -170,8 +161,7 @@ void COpndCommand::RemoveFromEnd()
|
|||
}
|
||||
else
|
||||
{
|
||||
unsigned int nCommands;
|
||||
m_commands->GetSize(&nCommands);
|
||||
const size_t nCommands = m_commands->size();
|
||||
|
||||
if (nCommands == 1)
|
||||
{
|
||||
|
@ -179,13 +169,14 @@ void COpndCommand::RemoveFromEnd()
|
|||
}
|
||||
else
|
||||
{
|
||||
int nOpCode;
|
||||
m_commands->GetAt(nCommands - 1, &nOpCode);
|
||||
int nOpCode = m_commands->at(nCommands - 1);
|
||||
|
||||
if (nOpCode == IDC_PNT)
|
||||
{
|
||||
m_fDecimal = false;
|
||||
}
|
||||
m_commands->RemoveAt(nCommands - 1);
|
||||
|
||||
m_commands->pop_back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -212,8 +203,8 @@ CalculationManager::CommandType COpndCommand::GetCommandType() const
|
|||
|
||||
void COpndCommand::ClearAllAndAppendCommand(CalculationManager::Command command)
|
||||
{
|
||||
m_commands->Clear();
|
||||
m_commands->Append(static_cast<int>(command));
|
||||
m_commands->clear();
|
||||
m_commands->push_back(static_cast<int>(command));
|
||||
m_fSciFmt = false;
|
||||
m_fNegative = false;
|
||||
m_fDecimal = false;
|
||||
|
@ -223,14 +214,13 @@ const wstring& COpndCommand::GetToken(wchar_t decimalSymbol)
|
|||
{
|
||||
static const wchar_t chZero = L'0';
|
||||
|
||||
unsigned int nCommands;
|
||||
m_commands->GetSize(&nCommands);
|
||||
const size_t nCommands = m_commands->size();
|
||||
m_token.clear();
|
||||
int nOpCode;
|
||||
|
||||
for (unsigned int i = 0; i < nCommands; i++)
|
||||
for (size_t i = 0; i < nCommands; i++)
|
||||
{
|
||||
m_commands->GetAt(i, &nOpCode);
|
||||
int nOpCode = (*m_commands)[i];
|
||||
|
||||
if (nOpCode == IDC_PNT)
|
||||
{
|
||||
m_token.append(wstring{ decimalSymbol });
|
||||
|
@ -238,8 +228,7 @@ const wstring& COpndCommand::GetToken(wchar_t decimalSymbol)
|
|||
else if (nOpCode == IDC_EXP)
|
||||
{
|
||||
m_token.append(&chExp);
|
||||
int nextOpCode;
|
||||
m_commands->GetAt(i + 1, &nextOpCode);
|
||||
int nextOpCode = m_commands->at(i + 1);
|
||||
if (nextOpCode != IDC_SIGN)
|
||||
{
|
||||
m_token.append(&chPlus);
|
||||
|
@ -257,7 +246,7 @@ const wstring& COpndCommand::GetToken(wchar_t decimalSymbol)
|
|||
}
|
||||
|
||||
// Remove zeros
|
||||
for (unsigned int i = 0; i < m_token.size(); i++)
|
||||
for (size_t i = 0; i < m_token.size(); i++)
|
||||
{
|
||||
if (m_token.at(i) != chZero)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue