?? container.cpp
字號(hào):
/*
Cross Platform Core Code.
Copyright(R) 2001-2002 Balang Software.
All rights reserved.
Using:
class CStockInfoArray;
class CStockContainer;
class CDomain;
class CDomainContainer;
*/
#include "stdafx.h"
#include "Container.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// class CStockContainer
CStockContainer * CStockContainer::m_pSortContainer = NULL;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CStockContainer::CStockContainer()
{
m_nType = typeNone;
m_dwDate = -1;
m_nCurrentStock = -1;
m_nSortVariantID = SLH_INVALID;
m_bSortAscend = FALSE;
}
CStockContainer::~CStockContainer()
{
Clear( );
}
BOOL CStockContainer::Lock()
{
return m_mutex.Lock();
}
BOOL CStockContainer::UnLock()
{
return m_mutex.UnLock();
}
int CStockContainer::Add( CStockInfo &newElement )
{
CSPMutex::Scoped l(m_mutex);
int nIndex = CStockInfoArray::Add(newElement);
m_map.SetAt( newElement.GetStockCode(), (void *)nIndex );
return nIndex;
}
BOOL CStockContainer::GetCurrentType( int *pType, CSPString * pDomain, DWORD *pdwDate )
{
CSPMutex::Scoped l(m_mutex);
if( pType )
*pType = m_nType;
if( pDomain )
*pDomain = m_strDomain;
if( pdwDate )
*pdwDate = m_dwDate;
return TRUE;
}
BOOL CStockContainer::GetPioneerTechDate( DWORD * pdwDate )
{
CStockInfo info;
if( GetStockInfo( STKLIB_CODE_MAIN, &info )
&& info.m_kdata.GetSize() > 0 )
{
*pdwDate = info.m_kdata.ElementAt(0).m_date;
return TRUE;
}
return FALSE;
}
BOOL CStockContainer::GetLatestTechDate( DWORD * pdwDate )
{
CStockInfo info;
if( GetStockInfo( STKLIB_CODE_MAIN, &info )
&& info.m_kdata.GetSize() > 0 )
{
*pdwDate = info.m_kdata.ElementAt(info.m_kdata.GetSize()-1).m_date;
return TRUE;
}
return FALSE;
}
BOOL CStockContainer::GetTechDateArray( CSPDWordArray & dwArray )
{
CStockInfo info;
if( GetStockInfo( STKLIB_CODE_MAIN, &info ) )
{
dwArray.SetSize( 0, info.m_kdata.GetSize()+10 );
for( int i=0; i<info.m_kdata.GetSize(); i++ )
dwArray.Add( info.m_kdata.ElementAt(i).m_date );
return TRUE;
}
return FALSE;
}
BOOL CStockContainer::RetrieveFromStatic( int nType, LPCTSTR lpszDomain, CStrategy * pStrategy, DWORD dwDate )
{
CSPMutex::Scoped l(m_mutex);
Clear( );
if( nType == typeNone )
{
m_nType = typeNone;
return TRUE;
}
if( nType < typeMin || nType > typeMax )
return FALSE;
m_nType = nType;
m_dwDate = dwDate;
CSPStringArray astrSpecify;
switch( nType )
{
case typeAll:
CopyData( AfxGetStockContainer() );
break;
case typeIndex:
case typeA:
case typeBond:
case typeFund:
case typeClassShaa:
case typeClassShab:
case typeClassSzna:
case typeClassSznb:
case typeClassShabond:
case typeClassSznbond:
case typeClassMsmall:
case typeRight:
{
int nCount = 0;
SetSize( 0, AfxGetStockContainer().GetSize() );
for( int i=0; i<AfxGetStockContainer().GetSize(); i++ )
{
CStockInfo & info = AfxGetStockContainer().ElementAt(i);
LONG stocktype = info.GetType();
if( ( typeIndex == nType && CStock::typeshIndex == stocktype )
|| ( typeIndex == nType && CStock::typeszIndex == stocktype )
|| ( typeA == nType && (CStock::typeshA == stocktype || CStock::typeszA == stocktype) && !info.IsFund() )
|| ( typeBond == nType && (CStock::typeshBond == stocktype || CStock::typeszBond == stocktype) )
|| ( typeFund == nType && info.IsFund() )
|| ( typeClassShaa == nType && CStock::typeshA == stocktype )
|| ( typeClassShab == nType && CStock::typeshB == stocktype )
|| ( typeClassSzna == nType && CStock::typeszA == stocktype )
|| ( typeClassSznb == nType && CStock::typeszB == stocktype )
|| ( typeClassShabond == nType && CStock::typeshBond == stocktype )
|| ( typeClassSznbond == nType && CStock::typeszBond == stocktype )
|| ( typeClassMsmall == nType && CStock::typeszMsmall == stocktype )
|| ( typeRight == nType && ( CStock::typeshRight == stocktype || CStock::typeszRight == stocktype) ) )
{
Add( info );
nCount ++;
}
}
SetSize( nCount );
}
break;
case typeStrategy:
{
if( pStrategy )
RetrieveSpecify( pStrategy->GetStocks() );
}
break;
case typeGroup:
{
if( lpszDomain && strlen(lpszDomain) > 0 )
{
AfxGetGroupContainer().GetDomainStocks( lpszDomain, astrSpecify );
RetrieveSpecify( astrSpecify );
m_strDomain = lpszDomain;
}
else
return AfxGetGroupContainer().GetAllDomainInfo( this, m_dwDate );
}
break;
case typeDomain:
{
if( lpszDomain && strlen(lpszDomain) > 0 )
{
AfxGetDomainContainer().GetDomainStocks( lpszDomain, astrSpecify );
RetrieveSpecify( astrSpecify );
m_strDomain = lpszDomain;
}
else
return AfxGetDomainContainer().GetAllDomainInfo( this, m_dwDate );
}
break;
default:
SP_ASSERT( FALSE );
SetMap( );
SetAverage( );
return FALSE;
}
SetMap( );
DWORD dwMainDate = -1;
AfxGetStockContainer().GetCurrentType( NULL, NULL, &dwMainDate );
if( m_dwDate != dwMainDate )
OnDataChanged();
else
SetAverage( );
return TRUE;
}
BOOL CStockContainer::ReRetrieveFromStatic( CStrategy * pStrategy, BOOL bUpToDate )
{
CSPMutex::Scoped l(m_mutex);
int type;
CSPString strDomain;
DWORD dwDate;
GetCurrentType( &type, &strDomain, &dwDate );
if( bUpToDate )
dwDate = -1;
return RetrieveFromStatic( type, strDomain, pStrategy, dwDate );
}
BOOL CStockContainer::RetrieveSpecify( CSPStringArray & astr )
{
CSPMutex::Scoped l(m_mutex);
SetSize( 0, astr.GetSize() );
int nCount = 0;
for( int i=0; i<astr.GetSize(); i++ )
{
CSPString string = astr.ElementAt(i);
CStockInfo info;
if( AfxGetStockContainer().GetStockInfo( string, &info ) )
{
Add( info );
nCount ++;
}
}
SetSize( nCount );
return TRUE;
}
BOOL CStockContainer::GetStockInfo( const char * szCode, CStockInfo * pInfo, int * pid )
{
CSPMutex::Scoped l(m_mutex);
if( NULL == szCode || strlen(szCode) <= 0 )
return FALSE;
void * pArrayID = NULL;
if( m_map.Lookup( szCode, pArrayID ) )
{
if( ((int)pArrayID) >= 0 && ((int)pArrayID) < GetSize() )
{
CStockInfo & info = ElementAt((int)pArrayID);
if( pInfo )
*pInfo = info;
if( pid )
*pid = (int)pArrayID;
return TRUE;
}
}
return FALSE;
}
BOOL CStockContainer::SetCurrentStock( const char * szCode )
{
CSPMutex::Scoped l(m_mutex);
int nStockIndex = -1;
CStockInfo info;
if( GetStockInfo( szCode, &info, &nStockIndex ) )
return SetCurrentStock( nStockIndex );
else
{
m_nCurrentStock = -1;
m_strCurrentStockCode = szCode;
return FALSE;
}
}
BOOL CStockContainer::SetCurrentStock( int nStockIndex )
{
CSPMutex::Scoped l(m_mutex);
if( nStockIndex >= 0 && nStockIndex < GetSize() )
{
m_nCurrentStock = nStockIndex;
return TRUE;
}
return FALSE;
}
BOOL CStockContainer::GetCurrentStock( CStockInfo * pInfo )
{
CSPMutex::Scoped l(m_mutex);
SP_ASSERT( pInfo );
if( m_nCurrentStock >= 0 && m_nCurrentStock < GetSize() )
{
CStockInfo & info = ElementAt(m_nCurrentStock);
if( pInfo )
* pInfo = info;
return TRUE;
}
else if( m_strCurrentStockCode.GetLength() > 0 )
{
CStockInfo info;
info.SetStockCode( CStock::marketUnknown, m_strCurrentStockCode );
if( pInfo )
*pInfo = info;
return TRUE;
}
return FALSE;
}
BOOL CStockContainer::GetPrevStock( CStockInfo * pInfo )
{
CSPMutex::Scoped l(m_mutex);
SP_ASSERT( pInfo );
if( GetSize() == 0 )
return FALSE;
if( m_nCurrentStock < 0 || m_nCurrentStock >= GetSize() )
m_nCurrentStock = -1;
if( m_auidSort.GetSize() == GetSize() )
{
if( m_nCurrentStock == -1 )
m_nCurrentStock = m_auidSort[m_auidSort.GetSize()-1];
else
{
int i;
for( i=0; i<m_auidSort.GetSize(); i++ )
{
if( m_nCurrentStock == (int)m_auidSort[i] )
break;
}
if( i >= m_auidSort.GetSize() || i == 0 )
{
m_nCurrentStock = -1;
return FALSE;
}
i --;
m_nCurrentStock = m_auidSort[i];
}
}
else
{
if( m_nCurrentStock < 0 )
m_nCurrentStock = GetSize()-1;
else
m_nCurrentStock --;
if( m_nCurrentStock < 0 )
{
m_nCurrentStock = -1;
return FALSE;
}
}
if( m_nCurrentStock >= 0 && m_nCurrentStock < GetSize() )
{
CStockInfo & info = ElementAt(m_nCurrentStock);
if( pInfo )
* pInfo = info;
return TRUE;
}
else
m_nCurrentStock = -1;
return FALSE;
}
BOOL CStockContainer::GetNextStock( CStockInfo * pInfo )
{
CSPMutex::Scoped l(m_mutex);
SP_ASSERT( pInfo );
if( GetSize() == 0 )
return FALSE;
if( m_nCurrentStock < 0 || m_nCurrentStock >= GetSize() )
m_nCurrentStock = -1;
if( m_auidSort.GetSize() == GetSize() )
{
if( m_nCurrentStock == -1 )
m_nCurrentStock = m_auidSort[0];
else
{
int i;
for( i=0; i<m_auidSort.GetSize(); i++ )
{
if( m_nCurrentStock == (int)m_auidSort[i] )
break;
}
i ++;
if( i >= m_auidSort.GetSize() )
{
m_nCurrentStock = -1;
return FALSE;
}
m_nCurrentStock = m_auidSort[i];
}
}
else
{
if( m_nCurrentStock < 0 )
m_nCurrentStock = 0;
else
m_nCurrentStock ++;
if( m_nCurrentStock >= GetSize() )
{
m_nCurrentStock = -1;
return FALSE;
}
}
if( m_nCurrentStock >= 0 && m_nCurrentStock < GetSize() )
{
CStockInfo & info = ElementAt(m_nCurrentStock);
if( pInfo )
* pInfo = info;
return TRUE;
}
else
m_nCurrentStock = -1;
return FALSE;
}
CStockInfo & CStockContainer::GetStockInfoByID( int nID )
{
CSPMutex::Scoped l(m_mutex);
if( ID_STOCKCNTN_AVERAGE == nID )
return m_infoAverage;
if( ID_STOCKCNTN_WEIGHTAVERAGE == nID )
return m_infoWeightAverage;
if( nID >= 0 && nID < GetSize() )
return ElementAt( nID );
SP_ASSERT( FALSE );
return m_infoNull;
}
CStockInfo & CStockContainer::GetStockInfoByIDSort( int nID )
{
CSPMutex::Scoped l(m_mutex);
if( ID_STOCKCNTN_AVERAGE == nID )
return m_infoAverage;
if( ID_STOCKCNTN_WEIGHTAVERAGE == nID )
return m_infoWeightAverage;
if( m_auidSort.GetSize() == GetSize()
&& nID >= 0 && nID < GetSize() )
{
if( m_auidSort[nID] >= 0 && (int)m_auidSort[nID] < GetSize() )
return ElementAt(m_auidSort[nID]);
}
else if( nID >= 0 && nID < GetSize() )
return ElementAt( nID );
SP_ASSERT( FALSE );
return m_infoNull;
}
CStockInfo & CStockContainer::GetAverage( )
{
return m_infoAverage;
}
CStockInfo & CStockContainer::GetWeightAverage( )
{
return m_infoWeightAverage;
}
BOOL CStockContainer::Load( CStDatabase * pDB, PROGRESS_CALLBACK fnCallback, void *cookie, int nProgStart, int nProgEnd )
{
CSPMutex::Scoped l(m_mutex);
SP_ASSERT( nProgStart <= nProgEnd );
SP_ASSERT( pDB );
if( nProgStart > nProgEnd || NULL == pDB )
return FALSE;
Clear( );
int nLen = pDB->LoadCodetable( *this );
SetMap( );
pDB->LoadBasetable( *this );
// pDB->LoadKDataCache( *this, fnCallback, cookie, nProgStart, nProgEnd );
m_nType = typeAll;
m_dwDate = -1;
OnDataChanged( );
return TRUE;
}
void CStockContainer::OnDataChanged( )
{
CSPMutex::Scoped l(m_mutex);
DWORD dateLatest = m_dwDate;
if( -1 == dateLatest )
AfxGetStockContainer().GetLatestTechDate( &dateLatest );
for( int i=0; i<GetSize(); i++ )
{
CStockInfo & info = ElementAt(i);
info.StatBaseIndex( dateLatest );
info.StatTechIndex( dateLatest );
}
SetAverage( );
}
BOOL CStockContainer::ReloadBase( CStDatabase * pDB )
{
CSPMutex::Scoped l(m_mutex);
SP_ASSERT( pDB );
if( GetSize() <= 0 || NULL == pDB )
return Load( NULL, NULL );
pDB->LoadBasetable( *this );
OnDataChanged();
return TRUE;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -