📄 elementrarray.cpp
字号:
/**
*
* @brief Specialisation of CElementList to store the elements in an RArray class
*
* Copyright (c) EMCC Software Ltd 2003
* @version 1.0
*/
// INCLUDE FILES
// Class include
#include "elementrarray.h"
// User includes
#include "chemicalelement.h"
#include "elementcomparisonfunctions.h"
// ================= MEMBER FUNCTIONS =======================
CElementRArray::CElementRArray() :
iElementArray(KElementListGranularity)
{
}
CElementRArray::~CElementRArray()
{
DeleteAllElements();
}
//Getters
TInt CElementRArray::NumberOfElements() const
{
return iElementArray.Count();
}
//Array manipulators and accessors
const CChemicalElement& CElementRArray::At(TInt aPosition) const
{
return *iElementArray[aPosition]; //const overload
}
CChemicalElement& CElementRArray::At(TInt aPosition)
{
return *iElementArray[aPosition]; //non-const overload
}
void CElementRArray::AppendL(const CChemicalElement* aElement)
{
User::LeaveIfError(iElementArray.Append(aElement));
}
void CElementRArray::DeleteAllElements()
{
iElementArray.ResetAndDestroy(); //Deletes all the elements owned by the array
}
TInt CElementRArray::DoFindL(const TIdentityRelation<CChemicalElement>& aRelation, const TDesC& aName,
const TDesC& aSymbol, TInt aAtomicNumber) const
{
//A utility function to wrap up the find operation
CChemicalElement* example = CChemicalElement::NewL(aName, aSymbol, aAtomicNumber, 0); //The example element to search for
TInt foundAt = iElementArray.Find(example, aRelation);
delete example; //No longer need the example element
User::LeaveIfError(foundAt); //Leave if an error (i.e. KErrNotFound)
return foundAt;
}
using namespace NElementComparisonFunctions;
TInt CElementRArray::FindElementByNameL(const TDesC& aName) const
{
//Wrap up a function pointer to the identity function
TIdentityRelation<CChemicalElement> relation(ElementsHaveSameName);
//Find the element using the given name only
return DoFindL(relation, aName, KNullDesC, 0);
}
TInt CElementRArray::FindElementBySymbolL(const TDesC& aSymbol) const
{
//Wrap up a function pointer to the identity function
TIdentityRelation<CChemicalElement> relation(ElementsHaveSameSymbol);
//Find the element using the given symbol only
return DoFindL(relation, KNullDesC, aSymbol, 0);
}
TInt CElementRArray::FindElementByAtomicNumberL(TInt aAtomicNumber) const
{
//Wrap up a function pointer to the identity function
TIdentityRelation<CChemicalElement> relation(ElementsHaveSameAtomicNumber);
//Find the element using the given atomic number only
return DoFindL(relation, KNullDesC, KNullDesC, aAtomicNumber);
}
void CElementRArray::SortByNameL()
{
//Wrap up a function pointer to the comparison function
TLinearOrder<CChemicalElement> order(CompareElementsName);
iElementArray.Sort(order);
}
void CElementRArray::SortBySymbolL()
{
//Wrap up a function pointer to the comparison function
TLinearOrder<CChemicalElement> order(CompareElementsSymbol);
iElementArray.Sort(order);
}
void CElementRArray::SortByAtomicNumberL()
{
//Wrap up a function pointer to the comparison function
TLinearOrder<CChemicalElement> order(CompareElementsAtomicNumber);
iElementArray.Sort(order);
}
void CElementRArray::SortByRelativeAtomicMassL()
{
//Wrap up a function pointer to the comparison function
TLinearOrder<CChemicalElement> order(CompareElementsRelativeAtomicMass);
iElementArray.Sort(order);
}
// End of File
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -