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.
tdepim/indexlib/memvector.tcc

81 lines
2.1 KiB

#include "format.h"
#include "mmap_manager.h"
#include <cstring>
#include <cstring>
template <typename T>
memvector<T>::memvector( std::string fname ):
data_( new mmap_manager( fname ) )
{
if ( !data_->size() ) {
13 years ago
data_->resize( byte_io::byte_length<unsigned>() );
byte_io::write<unsigned>( data_->rw_base( 0 ), 0 );
}
}
template <typename T>
memvector<T>::~memvector() {
}
template <typename T>
void memvector<T>::print( std::ostream& out ) const {
out << format( "size(): %s\n" ) % size();
for ( const_iterator first = begin(), past = end(); first != past; ++first ) {
out << *first << std::endl;
}
}
template <typename T>
void memvector<T>::resize( size_type n_s ) {
if ( size() >= n_s ) return;
using namespace byte_io;
13 years ago
data_->resize( n_s * byte_length<value_type>() + byte_length<unsigned>() );
iterator p_end = end();
write<unsigned>( data_->rw_base( 0 ), n_s );
while ( p_end != end() ) {
*p_end = value_type();
++p_end;
}
}
template<typename T>
void memvector<T>::insert( const_iterator where, const value_type v ) {
assert( !( where < begin() ) );
assert( where <= end() );
const unsigned to_idx = where.raw() - data_->ronly_base( 0 );
13 years ago
data_->resize( ( size() + 1 ) * byte_io::byte_length<value_type>() + byte_io::byte_length<unsigned>() );
unsigned char* to = data_->rw_base( to_idx );
// make space:
13 years ago
std::memmove( to + byte_io::byte_length<value_type>(), to, end().raw() - to );
byte_io::write<value_type>( to, v );
byte_io::write<unsigned>( data_->rw_base( 0 ), size() + 1 );
}
template <typename T>
void memvector<T>::erase( iterator where ) {
assert( size() );
assert( !( where < begin() ) );
assert( where < end() );
iterator next = where;
++next;
std::memmove( const_cast<unsigned char*>( where.raw() ), next.raw(), end().raw() - next.raw() );
byte_io::write<uint32_t>( data_->rw_base( 0 ), size() - 1 );
}
template <typename T>
void memvector<T>::clear() {
13 years ago
data_->resize( byte_io::byte_length<uint32_t>() );
byte_io::write<uint32_t>( data_->rw_base( 0 ), 0 );
}
template<typename T>
void memvector<T>::remove( std::string fname ) {
::unlink( fname.c_str() );
}