You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
513 lines
12 KiB
513 lines
12 KiB
//----------------------------------------------------------------------------
|
|
// Laser Guard Ltd.
|
|
// Copyright © 2000-2002. All Rights Reserved.
|
|
// Version 1.0 01/01/2000
|
|
// Last modification: 08/10/2003
|
|
// PROJECT:
|
|
// SUBSYSTEM:
|
|
// FILE: Stream.h
|
|
// AUTHOR: Sergey Chizhevskiy
|
|
// OVERVIEW
|
|
// ~~~~~~~~
|
|
// Header file of Stream.h
|
|
//----------------------------------------------------------------------------
|
|
#ifndef __STREAM__H__INCLUDED__
|
|
#define __STREAM__H__INCLUDED__
|
|
|
|
#include "stdafx.h"
|
|
/*
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
class CStream
|
|
{
|
|
public:
|
|
CStream();
|
|
~CStream();
|
|
|
|
public:
|
|
bool Create();
|
|
void Destroy();
|
|
|
|
public:
|
|
bool Put( TYPE byte );
|
|
bool Get( TYPE *pByte );
|
|
|
|
int16_t Get( TYPE *pByte, int16_t nCount );
|
|
int16_t Put( TYPE *pByte, int16_t nCount );
|
|
|
|
int16_t GetCount() { return m_nCount; }
|
|
int16_t GetOverflow();
|
|
void Clear();
|
|
|
|
private:
|
|
#ifndef INCLUDE_WINDOWS_DEFINITIONS
|
|
TYPE m_pBuffer[ MAX_CAPACITY ];
|
|
int16_t m_nCount;
|
|
int16_t m_nHead;
|
|
int16_t m_nTail;
|
|
int16_t m_nOverflow;
|
|
#else
|
|
volatile TYPE m_pBuffer[ MAX_CAPACITY ];
|
|
volatile int16_t m_nCount;
|
|
volatile int16_t m_nHead;
|
|
volatile int16_t m_nTail;
|
|
volatile int16_t m_nOverflow;
|
|
#endif
|
|
};
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
CStream<TYPE,MAX_CAPACITY,OVERFLOW>::CStream()
|
|
{
|
|
m_nCount = 0;
|
|
m_nHead = 0;
|
|
m_nTail = 0;
|
|
m_nOverflow = 0;
|
|
}
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
CStream<TYPE,MAX_CAPACITY,OVERFLOW>::~CStream()
|
|
{
|
|
Destroy();
|
|
}
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
bool CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Create()
|
|
{
|
|
Destroy();
|
|
|
|
m_nCount = 0;
|
|
m_nHead = 0;
|
|
m_nTail = 0;
|
|
m_nOverflow = 0;
|
|
|
|
return true;
|
|
}
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
void CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Destroy()
|
|
{
|
|
m_nCount = 0;
|
|
m_nHead = 0;
|
|
m_nTail = 0;
|
|
m_nOverflow = 0;
|
|
}
|
|
|
|
#ifdef __BCPLUSPLUS__
|
|
#pragma warn -rch
|
|
#pragma warn -ccc
|
|
#endif
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
bool CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Put( TYPE byte )
|
|
{
|
|
if( OVERFLOW )
|
|
{
|
|
m_pBuffer[ m_nHead ] = byte;
|
|
|
|
m_nHead = (m_nHead + 1) % MAX_CAPACITY;
|
|
|
|
if( m_nCount >= MAX_CAPACITY )
|
|
{
|
|
m_nTail = ( m_nTail + 1 ) % MAX_CAPACITY;
|
|
m_nOverflow++;
|
|
}
|
|
else
|
|
m_nCount++;
|
|
}
|
|
else
|
|
{
|
|
if( m_nCount >= MAX_CAPACITY )
|
|
return false;
|
|
|
|
m_pBuffer[ m_nHead ] = byte;
|
|
|
|
m_nHead = (m_nHead + 1) % MAX_CAPACITY;
|
|
|
|
m_nCount ++;
|
|
}
|
|
return true;
|
|
}
|
|
#ifdef __BCPLUSPLUS__
|
|
#pragma warn .ccc
|
|
#pragma warn .rch
|
|
#endif
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
bool CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Get( TYPE *pByte )
|
|
{
|
|
if( m_nCount <= 0 )
|
|
return false;
|
|
|
|
*pByte = m_pBuffer[ m_nTail ];
|
|
|
|
m_nTail = (m_nTail + 1) % MAX_CAPACITY;
|
|
|
|
m_nCount --;
|
|
|
|
return true;
|
|
}
|
|
|
|
#ifdef __BCPLUSPLUS__
|
|
#pragma warn -rch
|
|
#pragma warn -ccc
|
|
#endif
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
int16_t CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Put( TYPE *pByte, int16_t nCount )
|
|
{
|
|
if( OVERFLOW )
|
|
{
|
|
for( int16_t i = 0; i < nCount; i++ )
|
|
{
|
|
m_pBuffer[ m_nHead ] = *pByte++;
|
|
m_nHead = (m_nHead + 1) % MAX_CAPACITY;
|
|
if( m_nCount >= MAX_CAPACITY )
|
|
{
|
|
m_nTail = ( m_nTail + 1 ) % MAX_CAPACITY;
|
|
m_nOverflow++;
|
|
}
|
|
else
|
|
m_nCount++;
|
|
}
|
|
return nCount;
|
|
}
|
|
else
|
|
{
|
|
for( int16_t i = 0; (i < nCount) && (m_nCount < MAX_CAPACITY); i++ )
|
|
{
|
|
m_pBuffer[ m_nHead ] = *pByte++;
|
|
m_nHead = (m_nHead + 1) % MAX_CAPACITY;
|
|
m_nCount ++;
|
|
}
|
|
return i;
|
|
}
|
|
return 0;
|
|
}
|
|
#ifdef __BCPLUSPLUS__
|
|
#pragma warn .ccc
|
|
#pragma warn .rch
|
|
#endif
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
int16_t CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Get( TYPE *pByte, int16_t nCount )
|
|
{
|
|
for( int16_t i = 0; (i < nCount) && m_nCount; i++ )
|
|
{
|
|
*pByte++ = m_pBuffer[ m_nTail ];
|
|
m_nTail = (m_nTail + 1) % MAX_CAPACITY;
|
|
m_nCount --;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
int16_t CStream<TYPE,MAX_CAPACITY,OVERFLOW>::RemoveLast( int16_t nCount )
|
|
{
|
|
for( int16_t i = 0; (i < nCount) && m_nCount; i++ )
|
|
{
|
|
|
|
m_nHead = ( m_nHead - 1 ) % MAX_CAPACITY;
|
|
m_nCount --;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
void CStream<TYPE,MAX_CAPACITY,OVERFLOW>::Clear()
|
|
{
|
|
m_nCount = 0;
|
|
m_nTail = 0;
|
|
m_nHead = 0;
|
|
m_nOverflow = 0;
|
|
}
|
|
|
|
template< class TYPE, int16_t MAX_CAPACITY, bool OVERFLOW >
|
|
int16_t CStream<TYPE,MAX_CAPACITY,OVERFLOW>::GetOverflow()
|
|
{
|
|
int16_t nOverflow = m_nOverflow;
|
|
m_nOverflow = 0;
|
|
return nOverflow;
|
|
}
|
|
*/
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// fast stream without overflow
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
template< class TYPE, const int16_t MAX_CAPACITY > class CFastStream {
|
|
public:
|
|
CFastStream();
|
|
virtual ~CFastStream();
|
|
|
|
public:
|
|
virtual bool Create();
|
|
virtual void Destroy();
|
|
|
|
public:
|
|
virtual bool Put( TYPE byte );
|
|
bool RemoveLast( void );
|
|
bool Get( TYPE *pByte );
|
|
|
|
virtual int16_t Put( TYPE *pByte, int16_t nCount );
|
|
int16_t RemoveLast( int16_t nCount );
|
|
int16_t Get( TYPE *pByte, int16_t nCount );
|
|
|
|
int16_t GetCount() { return m_nCount; }
|
|
virtual void Clear();
|
|
|
|
protected:
|
|
// #ifndef INCLUDE_WINDOWS_DEFINITIONS
|
|
TYPE m_pBuffer[ 1 << MAX_CAPACITY ];
|
|
int16_t m_nCount;
|
|
int16_t m_nHead;
|
|
int16_t m_nTail;
|
|
//#else
|
|
// volatile TYPE m_pBuffer[ 1 << MAX_CAPACITY ];
|
|
// volatile int16_t m_nCount;
|
|
// volatile int16_t m_nHead;
|
|
// volatile int16_t m_nTail;
|
|
//#endif
|
|
};
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > CFastStream<TYPE,MAX_CAPACITY>::CFastStream(){
|
|
m_nCount = m_nHead = m_nTail = 0;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > CFastStream<TYPE,MAX_CAPACITY>::~CFastStream(){
|
|
Destroy();
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > bool CFastStream<TYPE,MAX_CAPACITY>::Create() {
|
|
Destroy();
|
|
m_nCount = m_nHead = m_nTail = 0;
|
|
return true;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > void CFastStream<TYPE,MAX_CAPACITY>::Destroy(){
|
|
m_nCount = m_nHead = m_nTail = 0;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > bool CFastStream<TYPE,MAX_CAPACITY>::Put( TYPE byte ){
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
|
|
if( m_nCount >= cMaxCapacity )
|
|
return false;
|
|
|
|
m_pBuffer[ m_nHead ] = byte;
|
|
|
|
m_nHead = (m_nHead + 1) & cCapacityMask;
|
|
|
|
m_nCount ++;
|
|
|
|
return true;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > bool CFastStream<TYPE,MAX_CAPACITY>::Get( TYPE *pByte ) {
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
|
|
if( m_nCount <= 0 )
|
|
return false;
|
|
|
|
*pByte = m_pBuffer[ m_nTail ];
|
|
|
|
m_nTail = (m_nTail + 1) & cCapacityMask;
|
|
|
|
m_nCount --;
|
|
|
|
return true;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > bool CFastStream<TYPE,MAX_CAPACITY>::RemoveLast( void ){
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
|
|
if( m_nCount > 0 ){
|
|
m_nHead = ( m_nHead - 1 ) & cCapacityMask;
|
|
m_nCount --;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > int16_t CFastStream<TYPE,MAX_CAPACITY>::Put( TYPE *pByte, int16_t nCount ){
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
int16_t i = 0;
|
|
for(; (i < nCount) && (m_nCount < cMaxCapacity); i++ ){
|
|
m_pBuffer[ m_nHead ] = *pByte++;
|
|
m_nHead = (m_nHead + 1) & cCapacityMask;
|
|
m_nCount ++;
|
|
};
|
|
return i;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > int16_t CFastStream<TYPE,MAX_CAPACITY>::Get( TYPE *pByte, int16_t nCount ){
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
int16_t i = 0;
|
|
for(; (i < nCount) && m_nCount; i++ ){
|
|
*pByte++ = m_pBuffer[ m_nTail ];
|
|
m_nTail = (m_nTail + 1) & cCapacityMask;
|
|
m_nCount --;
|
|
};
|
|
return i;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > int16_t CFastStream<TYPE,MAX_CAPACITY>::RemoveLast( int16_t nCount ){
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
int16_t i = 0;
|
|
for(; (i < nCount) && m_nCount; i++ ){
|
|
|
|
m_nHead = ( m_nHead - 1 ) & cCapacityMask;
|
|
m_nCount --;
|
|
}
|
|
|
|
return i;
|
|
}
|
|
|
|
template< class TYPE, const int16_t MAX_CAPACITY > void CFastStream<TYPE,MAX_CAPACITY>::Clear(){
|
|
m_nCount = m_nTail = m_nHead = 0;
|
|
}
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
// fast stream with overflow
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
template < class TYPE, const int16_t MAX_CAPACITY > class CFastStreamOverflow : public CFastStream<TYPE,MAX_CAPACITY>{
|
|
public:
|
|
CFastStreamOverflow() { m_nOverflow = 0; }
|
|
virtual ~CFastStreamOverflow() { Destroy(); }
|
|
|
|
public:
|
|
virtual bool Create(){
|
|
if( !CFastStream<TYPE,MAX_CAPACITY>::Create() ) return false;
|
|
m_nOverflow = 0;
|
|
return true;
|
|
}
|
|
|
|
virtual void Destroy(){
|
|
CFastStream<TYPE,MAX_CAPACITY>::Destroy();
|
|
m_nOverflow = 0;
|
|
}
|
|
|
|
public:
|
|
virtual bool Put( TYPE byte ){
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_pBuffer[ CFastStream<TYPE,MAX_CAPACITY>::m_nHead ] = byte;
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_nHead = (CFastStream<TYPE,MAX_CAPACITY>::m_nHead + 1) & cCapacityMask;
|
|
|
|
if( CFastStream<TYPE,MAX_CAPACITY>::m_nCount >= cMaxCapacity )
|
|
{
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_nTail = ( CFastStream<TYPE,MAX_CAPACITY>::m_nTail + 1 ) & cCapacityMask;
|
|
m_nOverflow++;
|
|
}
|
|
else
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_nCount++;
|
|
|
|
return true;
|
|
}
|
|
|
|
virtual int16_t Put( TYPE *pByte, int16_t nCount );
|
|
|
|
virtual void Clear(){
|
|
CFastStream<TYPE,MAX_CAPACITY>::Clear();
|
|
m_nOverflow = 0;
|
|
}
|
|
|
|
int16_t GetOverflow(){
|
|
int16_t nOverflow = m_nOverflow;
|
|
m_nOverflow = 0;
|
|
return nOverflow;
|
|
}
|
|
|
|
protected:
|
|
volatile int16_t m_nOverflow;
|
|
};
|
|
|
|
// in Borland C++ this function can't be inline
|
|
template< class TYPE, const int16_t MAX_CAPACITY > int16_t CFastStreamOverflow<TYPE,MAX_CAPACITY>::Put( TYPE *pByte, int16_t nCount ){
|
|
const int16_t cMaxCapacity = (1<<MAX_CAPACITY);
|
|
const int16_t cCapacityMask = cMaxCapacity-1;
|
|
int16_t i = 0;
|
|
for(; i < nCount; i++ ){
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_pBuffer[ CFastStream<TYPE,MAX_CAPACITY>::m_nHead ] = *pByte++;
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_nHead = (CFastStream<TYPE,MAX_CAPACITY>::m_nHead + 1) & cCapacityMask;
|
|
if( CFastStream<TYPE,MAX_CAPACITY>::m_nCount >= cMaxCapacity ){
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_nTail = ( CFastStream<TYPE,MAX_CAPACITY>::m_nTail + 1 ) & cCapacityMask;
|
|
m_nOverflow++;
|
|
}
|
|
else
|
|
CFastStream<TYPE,MAX_CAPACITY>::m_nCount++;
|
|
}
|
|
return nCount;
|
|
}
|
|
|
|
// NOTE: remember about interger overflow !!!
|
|
template <class VALTYPE, class SUMTYPE, const int16_t LENGTH> class CFastHistorySum {
|
|
public:
|
|
CFastHistorySum()
|
|
{
|
|
memset( m_pHistory, 0, sizeof(m_pHistory) );
|
|
|
|
Clear();
|
|
}
|
|
|
|
public:
|
|
void Clear()
|
|
{
|
|
m_nSum = 0;
|
|
m_nCount = 0;
|
|
m_nHead = 0;
|
|
m_nTail = 0;
|
|
}
|
|
|
|
void Put( VALTYPE value )
|
|
{
|
|
const int16_t nMaxCount = 1 << LENGTH;
|
|
const int16_t nMask = nMaxCount - 1;
|
|
if( m_nCount < nMaxCount )
|
|
{
|
|
// accumulate sum
|
|
m_nSum += value;
|
|
// store in history
|
|
m_pHistory[ m_nHead ] = value;
|
|
// update head pointer
|
|
m_nHead = (m_nHead + 1) & nMask;
|
|
// increase count
|
|
m_nCount++;
|
|
}
|
|
else
|
|
{
|
|
// update "floating" medium
|
|
m_nSum = m_nSum - m_pHistory[ m_nTail ] + value;
|
|
// store new value in history
|
|
m_pHistory[ m_nHead ] = value;
|
|
// move pointers
|
|
m_nHead = (m_nTail + 1) & nMask;
|
|
m_nTail = (m_nTail + 1) & nMask;
|
|
}
|
|
}
|
|
|
|
const int16_t GetTotalLength() const { return 1 << LENGTH; }
|
|
|
|
bool Get( SUMTYPE *pSum )
|
|
{
|
|
const int16_t nMaxCount = 1 << LENGTH;
|
|
const int16_t nMask = nMaxCount - 1;
|
|
// if history not comlette yet
|
|
if( m_nCount < nMaxCount )
|
|
return false;
|
|
|
|
*pSum = m_nSum;
|
|
return true;
|
|
}
|
|
|
|
public:
|
|
VALTYPE m_pHistory[ 1 << LENGTH ];
|
|
SUMTYPE m_nSum;
|
|
int16_t m_nCount;
|
|
int16_t m_nHead;
|
|
int16_t m_nTail;
|
|
};
|
|
|
|
#endif
|