|
///////////////////////////////////////////////////////////////////////////
// CExample -- Implementation for CExample object
#include "stdafx.h"
#include "Example.h"
IMPLEMENT_SERIAL(CExample, CObject, 0);
////////////////////////////////////////////////////////////
// CExampleList class members
CExample* CExampleList::FindExample(COleDateTime dtDate, CString strLocationID, int nOrder)
{
POSITION Pos = FindExamplePos(dtDate, strLocationID, nOrder);
if (Pos == NULL) return NULL;
return (CExample*)GetAt(Pos);
}
POSITION CExampleList::FindExamplePos(COleDateTime dtDate, CString strLocationID, int nOrder)
{
POSITION Pos;
CExample* pExample;
div_t divresult;
int nCurrent, nHigh, nLow, nCompareResult, nLastCurrent = -1;
nLow = 0;
nHigh = GetCount();
divresult = div(nHigh - nLow, 2);
nCurrent = nLow + divresult.quot;
if (nHigh <= 0) goto l_NotFound; // no items in the list
while (TRUE)
{
Pos = FindIndex(nCurrent);
pExample = (CExample*)GetAt(Pos);
nCompareResult = pExample->Compare(dtDate, strLocationID, nOrder);
if (nCompareResult == 0)
{
return Pos;
}
if (nCompareResult > 0) // we are in lower half of test range
{
nHigh = nCurrent;
divresult = div(nHigh - nLow, 2);
nCurrent = nLow + divresult.quot;
}
else // we are in upper half of test range
{
nLow = nCurrent;
divresult = div(nHigh - nLow, 2);
nCurrent = nLow + divresult.quot;
}
if (nCurrent == nLastCurrent) goto l_NotFound;
nLastCurrent = nCurrent;
}
l_NotFound:;
#ifdef _DEBUG
Pos = FindExamplePosBruteForce(dtDate, strLocationID, nOrder);
if (Pos != NULL) TRACE("Searching algorithm failed\n");
#endif
return NULL;
} // end of FindExamplePos
POSITION CExampleList::FindExamplePosBruteForce(COleDateTime dtDate, CString strLocationID, int nOrder)
{
CExample* pExample;
POSITION Pos = GetHeadPosition();
while (Pos)
{
pExample = (CExample*)GetNext(Pos);
if (pExample->Compare(dtDate, strLocationID, nOrder) == 0)
{
return Pos;
}
}
return NULL;
} // end of FindExamplePosBruteForce
void CExampleList::AddExample(CExample* pNew)
{
CExample* pExample;
int nCompareResult;
POSITION Pos;
// need to search through list and add in the proper sorted order
ASSERT_VALID(pNew);
ASSERT(Find(pNew) == NULL);
// start from end because it is more likely to be added to the end
Pos = GetTailPosition();
while (Pos)
{
pExample = (CExample*)GetAt(Pos);
nCompareResult = pExample->Compare(pNew);
ASSERT(nCompareResult != 0);
if (nCompareResult == 0) return;
if (nCompareResult == -1)
{
InsertAfter(Pos, pNew);
return;
}
GetPrev(Pos);
}
AddHead(pNew);
return;
}
void CExampleList::RemoveExample(COleDateTime dtDate, CString strLocationID, int nOrder)
{
POSITION Pos = FindExamplePos(dtDate, strLocationID, nOrder);
if (Pos) RemoveAt(Pos);
}
void CExampleList::Serialize(CArchive& ar)
{
// NOTE: Do not call the base class!
DWORD dwVersion = 0x00000000;
int n, nCount;
POSITION Pos;
CExample* pExample;
if (ar.IsStoring())
{
ar<<dwVersion;
nCount = GetCount();
ar<<nCount;
Pos = GetHeadPosition();
while (Pos)
{
pExample = (CExample*)GetNext(Pos);
pExample->Serialize(ar);
}
}
else
{
ar>>dwVersion;
ASSERT(GetCount() == 0);
ar>>nCount;
for (n = 0; n < nCount; ++n)
{
pExample = new CExample();
if (pExample == NULL)
THROW(new CMemoryException());
pExample->Serialize(ar);
AddTail(pExample);
}
}
} // end of Serialize
void CExampleList::ClearAndDelete()
{
CExample* pExample;
POSITION Pos = GetHeadPosition();
while (Pos)
{
pExample = (CExample*)GetNext(Pos);
ASSERT_VALID(pExample);
delete pExample;
}
RemoveAll();
}
////////////////////////////////////////////////////////////
// CExample class members
////////////////////////////////////////////
// CExample construction/destruction
// Construction
CExample::CExample()
{
Clear();
m_dtCreated = COleDateTime::GetCurrentTime();
}
CExample::CExample(COleDateTime dtDate, CString strLocationID, int nOrder)
{
Clear();
m_dtCreated = COleDateTime::GetCurrentTime();
m_dtDate = dtDate;
m_strLocationID = strLocationID;
m_nOrder = nOrder;
}
// Initialization
void CExample::Clear()
{
m_dtDate = 0.0;
m_strLocationID = _T("");
m_nOrder = 0;
m_strCustomerName = _T("");
m_dtBirthDate = 0.0;
m_rgbColor = 0;
m_dtCreated = 0.0;
m_dtLastModified = 0.0;
}
// Destruction
CExample::~CExample()
{
}
////////////////////////////////////////////
// CExample Diagnostics
#ifdef _DEBUG
void CExample::Dump(CDumpContext& dc) const
{
dc.SetDepth(1);
dc <<"Date = " << m_dtDate;
dc <<"LocationID = " << m_strLocationID;
dc <<"Order = " << m_nOrder;
dc <<"CustomerName = " << m_strCustomerName;
dc <<"BirthDate = " << m_dtBirthDate;
dc <<"Color = " << m_rgbColor;
dc <<"Created = " << m_dtCreated;
dc <<"LastModified = " << m_dtLastModified;
}
void CExample::AssertValid() const
{
CObject::AssertValid();
// TODO: Add validity checking here
}
#endif
////////////////////////////////////////////
// CExample operations
void CExample::Serialize(CArchive& ar)
{
DWORD dwVersion = 0x00000000;
if (ar.IsStoring())
{
ar<<dwVersion;
ar<<m_dtDate<<m_strLocationID<<m_nOrder<<m_strCustomerName<<m_dtBirthDate<<m_rgbColor<<m_dtCreated<<m_dtLastModified;
}
else
{
ar>>dwVersion;
ar>>m_dtDate>>m_strLocationID>>m_nOrder>>m_strCustomerName>>m_dtBirthDate>>m_rgbColor>>m_dtCreated>>m_dtLastModified;
}
CObject::Serialize(ar);
} // end of Serialize
void CExample::Duplicate(CExample* pSource)
{
m_dtDate = pSource->m_dtDate;
m_strLocationID = pSource->m_strLocationID;
m_nOrder = pSource->m_nOrder;
m_strCustomerName = pSource->m_strCustomerName;
m_dtBirthDate = pSource->m_dtBirthDate;
m_rgbColor = pSource->m_rgbColor;
m_dtCreated = pSource->m_dtCreated;
m_dtLastModified = pSource->m_dtLastModified;
} // end of Duplicate
int CExample::Compare(COleDateTime dtDate, CString strLocationID, int nOrder)
{
int nCompare;
if (dtDate.m_status != COleDateTime::valid) return -1;
if (m_dtDate < dtDate) return -1;
if (m_dtDate > dtDate) return 1;
nCompare = m_strLocationID.Compare(strLocationID);
if (nCompare < 0) return -1;
if (nCompare > 0) return 1;
if (m_nOrder < nOrder) return -1;
if (m_nOrder > nOrder) return 1;
return 0;
}
int CExample::Compare(CExample* pTest)
{
return Compare(pTest->GetDate(), pTest->GetLocationID(), pTest->GetOrder());
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Bob Pittenger is founder and President of Starpoint Software Inc. He holds a B.A. degree from Miami University, M.S. and Ph.D. degrees from Purdue University, and an MBA from Xavier University. He has been programming since 1993, starting with Windows application development in C++/MFC and moving to C# and .NET around 2005 and is a .NET Microsoft Certified Professional Developer.
Bob is the author of two books:
Billionaire: How the Ultra-Rich Built Their Fortunes Through Good and Evil and What You Can Learn from Them
and
Wealthonomics: The Most Important Economic and Financial Concepts that Can Make You Rich Fast.
Visit http://www.billionairebook.net for more information.