Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef LineBuffer_h__
- #define LineBuffer_h__
- #include "common_header.h"
- namespace FileMerger
- {
- struct StringView
- {
- int size;
- char* str;
- StringView( char* s, int sz ) : str( s ), size( sz )
- {
- DEBUG_ASSERT( s );
- DEBUG_ASSERT( sz > 0 );
- }
- };
- class LineBuffer : public std::vector< StringView >
- {
- public:
- typedef vector< string > StringList;
- static float rangeRandom( float min, float max )
- {
- return ((float)rand()/(float)RAND_MAX)*(max-min) + min;
- }
- LineBuffer() :
- mTotalSize( 0 )
- {
- mLineBuf.reserve( 1024*1024 );
- }
- ~LineBuffer()
- {
- }
- void addFile( const string& path )
- {
- FILE* file = fopen( path.c_str(), "r" );
- if( !file )
- {
- cout << "Error: cannot find file \"" << path << "\"" << endl;
- return;
- }
- //create a new file buffer
- fseek( file, 0, SEEK_END);
- mLoadedFiles.push_back( "" );
- mLoadedFiles.back().resize( ftell( file ) );
- fseek( file, 0, SEEK_SET );
- //load the file into the buffer
- char* buf = (char*)mLoadedFiles.back().c_str();
- char* endPtr = buf + mLoadedFiles.back().size();
- fread( buf, 1, mLoadedFiles.back().size(), file );
- fclose( file );
- char lastChar = 0;
- size_t lineSize = 0;
- while( buf < endPtr )
- {
- //find next line start
- for( ; buf < endPtr && *buf == ' ' && *buf == '\n'; ++buf );
- char* start = buf;
- //find next line end
- for( ; buf < endPtr && *buf != '\n'; ++buf );
- char* end = buf++;
- if( start == end )
- continue;
- //save the slice
- *end = 0;
- size_t size = end - start;
- push_back( StringView( start, size ) );
- mTotalSize += size;
- }
- }
- ///shuffles n loaded lines selected at random
- void shuffle()
- {
- for( int i = 0; i < size(); ++i )
- {
- int first = randomIndex();
- int second = randomIndex();
- if( first == second )
- continue;
- auto temp = at(first);
- at(first) = at( second );
- at( second ) = temp;
- }
- }
- inline int randomIndex()
- {
- return rangeRandom( 0, (int)size()-1 );
- }
- inline size_t getTotalSize() const
- {
- return mTotalSize;
- }
- protected:
- size_t mTotalSize;
- string mLineBuf;
- StringList mLoadedFiles;
- private:
- };
- }
- #endif // LineBuffer_h__
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement