From 460c52653ab0dcca6f19a4f492ed2c5e4e963ab0 Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdepim@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- indexlib/memvector.h | 224 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 224 insertions(+) create mode 100644 indexlib/memvector.h (limited to 'indexlib/memvector.h') diff --git a/indexlib/memvector.h b/indexlib/memvector.h new file mode 100644 index 00000000..e251c447 --- /dev/null +++ b/indexlib/memvector.h @@ -0,0 +1,224 @@ +#ifndef LPC_MEMVECTOR_H1105049836_INCLUDE_GUARD_ +#define LPC_MEMVECTOR_H1105049836_INCLUDE_GUARD_ + +/* This file is part of indexlib. + * Copyright (C) 2005 Luís Pedro Coelho + * + * Indexlib is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License, version 2, as + * published by the Free Software Foundation and available as file + * GPL_V2 which is distributed along with indexlib. + * + * Indexlib is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA + * + * In addition, as a special exception, the copyright holders give + * permission to link the code of this program with any edition of + * the Qt library by Trolltech AS, Norway (or with modified versions + * of Qt that use the same license as Qt), and distribute linked + * combinations including the two. You must obey the GNU General + * Public License in all respects for all of the code used other than + * Qt. If you modify this file, you may extend this exception to + * your version of the file, but you are not obligated to do so. If + * you do not wish to do so, delete this exception statement from + * your version. + */ + +#include "memreference.h" +#include "bitio.h" +#include "compat.h" +#include "manager.h" +#include "boost-compat/static_assert.hpp" +#include "boost-compat/scoped_ptr.hpp" +#ifdef HAVE_BOOST +#include +#endif +#include +#include +#include +#include +#include + +template class memory_iterator; +template class memvector; + +template +struct memory_iterator : public std::iterator { + private: + public: + template + memory_iterator( const memory_iterator& other ): + data_( const_cast( other.raw() ) ) + { + BOOST_STATIC_ASSERT( (boost::is_convertible::value ) ); + } + explicit memory_iterator( unsigned char* d ): + data_( d ) + { + } + T operator* () const { + return byte_io::read( data_ ); + } + memory_reference operator* () { + return memory_reference( data_ ); + } + + memory_iterator& operator ++() { + data_ += byte_io::byte_lenght(); + return *this; + } + + memory_iterator& operator --() { + data_ -= byte_io::byte_lenght(); + return *this; + } + + memory_iterator& operator += ( ptrdiff_t dif ) { + data_ += dif * byte_io::byte_lenght(); + return *this; + } + + ptrdiff_t operator - ( const memory_iterator& other ) const { + assert( !( ( raw() - other.raw() )%byte_io::byte_lenght() ) ); + return ( raw() - other.raw() )/byte_io::byte_lenght(); + } + + bool operator < ( const memory_iterator& other ) const { + return ( this->raw() - other.raw() ) < 0; + } + const unsigned char* raw() const { return data_; } + private: + unsigned char* data_; +}; + +template +inline +bool operator == ( const memory_iterator& a, const memory_iterator& b ) { + return a.raw() == b.raw(); +} + +template +inline +bool operator != ( const memory_iterator& a, const memory_iterator& b ) { + return !( a == b ); +} + +template +inline +bool operator <= ( const memory_iterator& a, const memory_iterator& b ) { + return !( b < a ); +} + + + +template +inline +memory_iterator operator + ( memory_iterator iter, typename memory_iterator::difference_type dif ) { + iter += dif; + return iter; +} + +template +inline +memory_iterator& operator -= ( memory_iterator& iter, typename memory_iterator::difference_type dif ) { + iter += -dif; + return iter; +} + +template +inline +memory_iterator operator - ( memory_iterator iter, typename memory_iterator::difference_type dif ) { + iter -= dif; + return iter; +} + + +template +inline +memory_iterator operator -- ( memory_iterator& ref, int ) { + memory_iterator copy = ref; + --ref; + return copy; +} + +template +inline +memory_iterator operator ++ ( memory_iterator& ref, int ) { + memory_iterator copy = ref; + ++ref; + return copy; +} + +/** + * A vector of T kept on disk. + * + * The interface is a subset of std::vector's interface. + */ +template +struct memvector { + public: + memvector( std::string ); + ~memvector(); + + typedef T value_type; + typedef unsigned size_type; + typedef memory_iterator iterator; + typedef memory_iterator const_iterator; + + iterator begin() { return iterator( address_of( 0 ) ); } + iterator end() { return iterator( address_of( size() ) ); } + + const_iterator begin() const { return const_iterator( address_of( 0 ) ); } + const_iterator end() const { return const_iterator( address_of( size() ) ); } + + value_type operator[] ( unsigned idx ) const { + assert( idx < size() ); + return byte_io::read( address_of( idx ) ); + } + + memory_reference operator[] ( unsigned idx ) { + assert( idx < size() ); + return memory_reference( address_of( idx ) ); + } + + /** + * For debugging, nothing else + */ + void print( std::ostream& ) const; + size_type size() const { return byte_io::read( data_->ronly_base( 0 ) ); } + bool empty() const { return !size(); } + void resize( size_type ); + + void insert( const_iterator, const value_type ); + void erase( iterator ); + void clear(); + void push_back( value_type v ) { insert( end(), v ); } + + /** + * Removes from disk + */ + static void remove( std::string ); + + private: + boost::scoped_ptr data_; + unsigned char* address_of( unsigned i ) { + return data_->rw_base( + byte_io::byte_lenght() + + i * byte_io::byte_lenght() ); + } + const unsigned char* address_of( unsigned i ) const { + return const_cast( this )->address_of( i ); + } +}; + +#include "memvector.tcc" + + +#endif /* LPC_MEMVECTOR_H1105049836_INCLUDE_GUARD_ */ -- cgit v1.2.3