init commit
This commit is contained in:
52
utils/xgboost_stream.h
Normal file
52
utils/xgboost_stream.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#ifndef _XGBOOST_STREAM_H_
|
||||
#define _XGBOOST_STREAM_H_
|
||||
|
||||
#include <cstdio>
|
||||
/*!
|
||||
* \file xgboost_stream.h
|
||||
* \brief general stream interface
|
||||
* \author Tianqi Chen: tianqi.tchen@gmail.com
|
||||
*/
|
||||
namespace xgboost{
|
||||
namespace utils{
|
||||
/*! \brief interface of stream I/O, used to serialize tensor data */
|
||||
class IStream{
|
||||
public:
|
||||
/*!
|
||||
* \brief read data from stream
|
||||
* \param ptr pointer to memory buffer
|
||||
* \param size size of block
|
||||
* \return usually is the size of data readed
|
||||
*/
|
||||
virtual size_t Read( void *ptr, size_t size ) = 0;
|
||||
/*!
|
||||
* \brief write data to stream
|
||||
* \param ptr pointer to memory buffer
|
||||
* \param size size of block
|
||||
*/
|
||||
virtual void Write( const void *ptr, size_t size ) = 0;
|
||||
/*! \brief virtual destructor */
|
||||
virtual ~IStream( void ){}
|
||||
};
|
||||
|
||||
/*! \brief implementation of file i/o stream */
|
||||
class FileStream: public IStream{
|
||||
private:
|
||||
FILE *fp;
|
||||
public:
|
||||
FileStream( FILE *fp ){
|
||||
this->fp = fp;
|
||||
}
|
||||
virtual size_t Read( void *ptr, size_t size ){
|
||||
return fread( ptr, size, 1, fp );
|
||||
}
|
||||
virtual void Write( const void *ptr, size_t size ){
|
||||
fwrite( ptr, size, 1, fp );
|
||||
}
|
||||
inline void Close( void ){
|
||||
fclose( fp );
|
||||
}
|
||||
};
|
||||
};
|
||||
};
|
||||
#endif
|
||||
Reference in New Issue
Block a user