29 #include <boost/bind.hpp>
33 #ifdef ENABLE_MASSPP_PARALLEL_TBB_MODE
34 #include "tbb/spin_mutex.h"
40 #define new new( _NORMAL_BLOCK, __FILE__, __LINE__ )
41 #define malloc( s ) _malloc_dbg( s, _NORMAL_BLOCK, __FILE__, __LINE__ )
47 typedef spin_mutex XYDATA_ParallelMutexType;
48 XYDATA_ParallelMutexType XYDATA_ParallelMutex_01;
49 XYDATA_ParallelMutexType XYDATA_ParallelMutex_02;
51 #endif // ENABLE_MASSPP_PARALLEL_TBB_MODE
54 using namespace kome::core;
57 const unsigned int XYData::m_cacheSize = 10000;
60 unsigned long XYData::m_currentVersion = 0;
84 #ifdef ENABLE_MASSPP_PARALLEL_TBB_MODE
85 XYDATA_ParallelMutexType::scoped_lock lock( XYDATA_ParallelMutex_01 );
86 #endif // ENABLE_MASSPP_PARALLEL_TBB_MODE
90 std::list< XYData* >& cacheList = getCacheList();
91 std::list< XYData* >::iterator it = cacheList.begin();
92 while( it != cacheList.end() ) {
94 cacheList.erase( it );
103 if( !m_tmpDataFile.empty() ) {
105 std::string filePath =
getpath( msppMgr.
getTmpDir(), m_tmpDataFile.c_str() );
113 double XYData::getMinX() {
119 double XYData::getMaxX() {
125 double XYData::getMinY() {
131 double XYData::getMaxY() {
137 void XYData::clearPoints() {
149 void XYData::addPoint(
const double x,
const double y ) {
161 void XYData::insertPoint(
const unsigned int index,
const double x,
const double y ) {
166 onInsertPoint( std::min( index, getLength() ), x, y );
173 void XYData::updatePoint(
const unsigned int index,
const double x,
const double y ) {
178 if( index >= getLength() ) {
183 deletePoint( index );
186 int idx = searchIndex( x );
192 onInsertPoint( idx, x, y );
199 void XYData::deletePoint(
const unsigned int index ) {
204 if( index >= getLength() ) {
209 onDeletePoint( index );
216 unsigned int XYData::getLength() {
220 return onGetLength();
224 double XYData::getX(
const unsigned int index ) {
228 if( index >= getLength() ) {
229 std::string msg = FMT(
230 "The point index(%d) is larger than max index(%d).",
239 return onGetX( index );
243 double XYData::getY(
const unsigned int index ) {
247 if( index >= getLength() ) {
248 std::string msg = FMT(
249 "The point index(%d) is larger than max index(%d).",
258 return onGetY( index );
262 void XYData::reserve(
const unsigned int num ) {
270 unsigned long XYData::getVersion() {
276 int XYData::searchIndex(
const double x ) {
284 static int compare(
double x0,
double x1 ) {
295 static double getX(
int index,
XYData* xyData ) {
296 return xyData->
onGetX( index );
302 idx = SearchTool::binarySearch< double, double >(
304 boost::bind( SearchHelper::getX, _1,
this ),
305 SearchHelper::compare,
313 int XYData::getNearestIndex(
const double x ) {
315 int len = (int)getLength();
321 int idx = searchIndex( x );
327 int idx0 = - idx - 2;
328 int idx1 = - idx - 1;
338 double d0 = fabs( getX( idx0 ) - x );
339 double d1 = fabs( getX( idx1 ) - x );
341 return ( d1 < d0 ? idx1 : idx0 );
345 void XYData::filter(
const double absY,
const double relY ) {
350 int len = (int)getLength();
356 double minY = std::max( fabs( m_minY ), fabs( m_maxY ) ) * relY / 100.0;
357 minY = std::max( absY, minY );
360 for(
int i = len - 1; i >= 0; i-- ) {
361 double y = getY( i );
372 void XYData::getPoints( std::vector<
Point< double > >& points,
const bool ySort,
const bool desc ) {
383 unsigned int len = getLength();
387 points.resize( len );
389 for(
unsigned int i = 0; i < len; i++ ) {
390 points[ i ].px = onGetX( i );
391 points[ i ].py = onGetY( i );
398 ySort ? ( desc ? pt::greaterY : pt::lessY ) : ( desc ? pt::greaterX : pt::lessX )
403 bool XYData::importData( boost::function<
int (
void*,
int ) > readFun ) {
408 bool ret = onLoadData( readFun );
414 bool XYData::exportData( boost::function<
int (
void*,
int ) > writeFun ) {
419 bool ret = onSaveData( writeFun );
438 void XYData::updateRange() {
440 if( m_updated || m_version == 0 ) {
441 m_currentVersion = m_currentVersion + 1;
442 m_version = m_currentVersion;
457 unsigned int len = getLength();
458 for(
unsigned int i = 0; i < len; i++ ) {
459 const double x = getX( i );
460 const double y = getY( i );
462 if( i == 0 || x < m_minX ) {
465 if( i == 0 || x > m_maxX ) {
468 if( i == 0 || y < m_minY ) {
471 if( i == 0 || y > m_maxY ) {
481 bool XYData::onLoadData( boost::function<
int (
void*,
int ) > readFun ) {
483 unsigned long len = 0;
484 readFun( &len,
sizeof( len ) );
488 double* arr =
new double[ len * 2 ];
489 readFun( arr,
sizeof(
double ) * len * 2 );
492 for(
unsigned int i = 0; i < len; i++ ) {
493 onAddPoint( arr[ i * 2 ], arr[ i * 2 + 1 ] );
503 bool XYData::onSaveData( boost::function<
int (
void*,
int ) > writeFun ) {
505 unsigned long len = onGetLength();
506 writeFun( &len,
sizeof( len ) );
510 double* arr =
new double[ len * 2 ];
511 for(
unsigned int i = 0; i < len; i++ ) {
512 arr[ i * 2 ] = onGetX( i );
513 arr[ i * 2 + 1 ] = onGetY( i );
516 writeFun( arr,
sizeof(
double ) * len * 2 );
525 void XYData::releaseData() {
543 exportData( boost::bind( &DataAccessor::write, &buffer, _1, _2 ) );
545 static int tmpDirCnt = 0;
547 if( m_tmpDataFile.empty() ) {
549 std::string dir =
getpath(
"xy_data", FMT(
"%02x", tmpDirCnt ).c_str() );
550 tmpDirCnt = ( tmpDirCnt + 1 ) % 0x100;
554 FMT(
"xy_data%04d", ( 0xffff & (
long long)
this ) ).c_str(),
558 m_tmpDataFile =
getpath( dir.c_str(), fileName.c_str() );
563 FILE* fp =
fileopen( path.c_str(),
"wb" );
566 exportData( boost::bind( &DataAccessor::write, &acc, _1, _2 ) );
583 void XYData::recoverData() {
600 #ifdef ENABLE_MASSPP_PARALLEL_TBB_MODE
602 XYDATA_ParallelMutexType::scoped_lock lock( XYDATA_ParallelMutex_01 );
603 #endif // ENABLE_MASSPP_PARALLEL_TBB_MODE
609 std::list< XYData* >& cacheList = getCacheList();
615 while( (
int)cacheList.size() > (int)m_cacheSize - 1 ) {
616 cacheList.front()->releaseData();
617 cacheList.pop_front();
623 cacheList.push_back(
this );
628 #ifdef ENABLE_MASSPP_PARALLEL_TBB_MODE
630 #endif // ENABLE_MASSPP_PARALLEL_TBB_MODE
637 if( !m_tmpDataFile.empty() ) {
641 #ifdef ENABLE_MASSPP_PARALLEL_TBB_MODE
642 XYDATA_ParallelMutexType::scoped_lock lock( XYDATA_ParallelMutex_02 );
643 #endif // ENABLE_MASSPP_PARALLEL_TBB_MODE
651 FILE* fp =
fileopen( path.c_str(),
"rb" );
655 importData( boost::bind( &DataAccessor::read, &acc, _1, _2 ) );
674 std::list< XYData* >& XYData::getCacheList() {
676 static std::list< XYData* > cl;
abstraction class of two dimention coordinate data
memory buffer management class
virtual double onGetX(const unsigned int index)=0
This method is called by getX method. (abstract method)
const char * getTmpDir()
gets temporary file directory name
interfaces of XYData class
implements of MsppManager class
FILE * fileopen(const char *path, const char *mode)
opens file
interfarces of FileAccessor class
bool removefile(const char *path)
removes file
interfarces of Buffer class
std::string getTmpFileName(const char *prefix, const char *suffix, const char *dir=NULL)
gets temporary file name
std::string getpath(const char *dir, const char *file)
get file path