<commit_before>

#ifndef EGE_GRAPHIC_BUFFER_STREAM_MAPPER_HXX
#define EGE_GRAPHIC_BUFFER_STREAM_MAPPER_HXX


#include <ege/graphic/buffer/mapper.hxx>


namespace ege
{
        namespace graphic
        {
                namespace buffer
                {
                        template < typename unit >
                        class StreamMapper: public Mapper< unit >
                        {
                                private:
                                        size_t flushed_units;
                                        size_t next_index;

                                        void prepareSector( size_t index );

                                protected:
                                        void performFlush();

                                public:
                                        StreamMapper( size_t sectorSize = 32768, size_t sectorsCount = 2 );
                                        virtual ~StreamMapper();
                                        unit* mapNext();
                                        void reset();
                        };
                }
        }
}


template < typename unit >
ege::graphic::buffer::StreamMapper< unit >::StreamMapper( size_t sectorSize, size_t sectorsCount ): Mapper< unit >( BufferUsage::STREAM, sectorSize, sectorsCount )
{
        prepareSector( 0 );
}


template < typename unit >
ege::graphic::buffer::StreamMapper< unit >::~StreamMapper()
{

}


template < typename unit >
void ege::graphic::buffer::StreamMapper< unit >::prepareSector( size_t index )
{
        this->mapSector( index, { BufferMapAccess::INVALIDATE_RANGE, BufferMapAccess::UNSYNCHRONIZED, BufferMapAccess::FLUSH_EXPLICIT } );
        flushed_units = 0;
        next_index = 0;
}


template < typename unit >
void ege::graphic::buffer::StreamMapper< unit >::performFlush()
{
        this->buffer->flushRange( flushed_units * sizeof( unit ), ( next_index - flushed_units ) * sizeof( unit ) );
        flushed_units = next_index;
}


template < typename unit >
unit* ege::graphic::buffer::StreamMapper< unit >::mapNext()
{
        if ( next_index == this->unitsPerSector )
        {
                if ( this->mappedSector + 1 == this->sectorsCount )
                        return nullptr;
                else
                        prepareSector( this->mappedSector + 1 );
        }

        return &this->mappedArea[ next_index++ ];
}


template < typename unit >
void ege::graphic::buffer::StreamMapper< unit >::reset()
{
        this->unmap();
        this->buffer->orphan();
        prepareSector( 0 );
}


#endif<commit_msg>buffer stream mapper can now map also multiple units at once even if those overlaps a sector (that's f***ing awesome)<commit_after>

#ifndef EGE_GRAPHIC_BUFFER_STREAM_MAPPER_HXX
#define EGE_GRAPHIC_BUFFER_STREAM_MAPPER_HXX


#include <ege/graphic/buffer/mapper.hxx>


namespace ege
{
        namespace graphic
        {
                namespace buffer
                {
                        template < typename unit >
                        class StreamMapper: public Mapper< unit >
                        {
                                private:
                                        size_t flushed_units;
                                        size_t next_index;

                                        void prepareSector( size_t , size_t includeNPrevUnits = 0 );

                                protected:
                                        void performFlush();

                                public:
                                        StreamMapper( size_t sectorSize = 32768, size_t sectorsCount = 2 );
                                        virtual ~StreamMapper();
                                        unit* mapNext();
                                        unit* mapNext( size_t nUnits );
                                        void reset();
                        };
                }
        }
}


template < typename unit >
ege::graphic::buffer::StreamMapper< unit >::StreamMapper( size_t sectorSize, size_t sectorsCount ): Mapper< unit >( BufferUsage::STREAM, sectorSize, sectorsCount )
{
        prepareSector( 0 );
}


template < typename unit >
ege::graphic::buffer::StreamMapper< unit >::~StreamMapper()
{

}


template < typename unit >
void ege::graphic::buffer::StreamMapper< unit >::prepareSector( size_t index, size_t includeNPrevUnits )
{
        this->mapSector( index, { BufferMapAccess::INVALIDATE_RANGE, BufferMapAccess::UNSYNCHRONIZED, BufferMapAccess::FLUSH_EXPLICIT }, includeNPrevUnits );
        flushed_units = 0;
        next_index = 0;
}


template < typename unit >
void ege::graphic::buffer::StreamMapper< unit >::performFlush()
{
        this->buffer->flushRange( flushed_units * sizeof( unit ), ( next_index - flushed_units ) * sizeof( unit ) );
        flushed_units = next_index;
}


template < typename unit >
unit* ege::graphic::buffer::StreamMapper< unit >::mapNext()
{
        if ( next_index == this->unitsPerSector )
        {
                if ( this->mappedSector + 1 == this->sectorsCount )
                        return nullptr;
                else
                        prepareSector( this->mappedSector + 1 );
        }

        return &this->mappedArea[ next_index++ ];
}


template < typename unit >
unit* ege::graphic::buffer::StreamMapper< unit >::mapNext( size_t nUnits )
{
        if ( ( next_index + nUnits - 1 ) >= this->unitsPerSector )
        {
                if ( this->mappedSector + 1 == this->sectorsCount )
                        return nullptr;
                else
                        prepareSector( this->mappedSector + 1, this->unitsPerSector - next_index );
        }

        unit* mappedUnits = &this->mappedArea[ next_index ];
        next_index += nUnits;
        return mappedUnits;
}


template < typename unit >
void ege::graphic::buffer::StreamMapper< unit >::reset()
{
        this->unmap();
        this->buffer->orphan();
        prepareSector( 0 );
}


#endif
{'fraction_non_alphanumeric': 0.09679037111334002, 'fraction_numerical': 0.004680708793045804, 'mean_word_length': 1.5956616052060737, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 66, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2548656', 'n_tokens_mistral': 1603, 'n_tokens_neox': 1486, 'n_words': 341}