summaryrefslogtreecommitdiffstats
path: root/indexlib/memvector.h
blob: 446fddf9206f2dd0ff2645c0ab861e414ce0e904 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#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 <luis@luispedro.org>
 *
 * 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 TQt library by Trolltech AS, Norway (or with modified versions
 * of TQt that use the same license as TQt), 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
 * TQt.  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 <boost/type_traits/is_convertible.hpp>
#endif
#include <iostream>
#include <iterator>
#include <string>
#include <cstddef>
#include <cstring>
#include <assert.h>

template <typename> class memory_iterator;
template <typename> class memvector;

template <typename T>
struct memory_iterator : public std::iterator<STD_NAMESPACE_PREFIX random_access_iterator_tag,T> {
	private:
	public:
		template <typename U>
		memory_iterator( const memory_iterator<U>& other ):
			data_( const_cast<unsigned char*>( other.raw() ) )
			{
				BOOST_STATIC_ASSERT( (boost::is_convertible<U, T>::value ) );
			}
		explicit memory_iterator( unsigned char* d ):
			data_( d )
			{
			}
		T operator* () const {
			return byte_io::read<T>( data_ );
		}
		memory_reference<T> operator* () {
			return memory_reference<T>( data_ );
		}

		memory_iterator& operator ++() {
			data_ += byte_io::byte_length<T>();
			return *this;
		}

		memory_iterator& operator --() {
			data_ -= byte_io::byte_length<T>();
			return *this;
		}

		memory_iterator& operator += ( ptrdiff_t dif ) {
			data_ += dif * byte_io::byte_length<T>();
			return *this;
		}

		ptrdiff_t operator - ( const memory_iterator<T>& other ) const {
			assert( !( ( raw() - other.raw() )%byte_io::byte_length<T>() ) );
			return ( raw() - other.raw() )/byte_io::byte_length<T>();
		}

		bool operator < ( const memory_iterator<T>& other ) const {
			return ( this->raw() - other.raw() ) < 0;
		}
		const unsigned char* raw() const { return data_; }
	private:
		unsigned char* data_;
};

template <typename T, typename U>
inline
bool operator == ( const memory_iterator<T>& a, const memory_iterator<U>& b ) {
	return a.raw() == b.raw();
}

template <typename T, typename U>
inline
bool operator != ( const memory_iterator<T>& a, const memory_iterator<U>& b ) {
	return !( a == b );
}

template <typename T, typename U>
inline
bool operator <= ( const memory_iterator<T>& a, const memory_iterator<U>& b ) {
	return !( b < a );
}



template <typename T>
inline
memory_iterator<T> operator + ( memory_iterator<T> iter, typename memory_iterator<T>::difference_type dif ) {
	iter += dif;
	return iter;
}

template <typename T>
inline
memory_iterator<T>& operator -= ( memory_iterator<T>& iter, typename memory_iterator<T>::difference_type dif ) {
	iter += -dif;
	return iter;
}

template <typename T>
inline
memory_iterator<T> operator - ( memory_iterator<T> iter, typename memory_iterator<T>::difference_type  dif ) {
	iter -= dif;
	return iter;
}


template <typename T>
inline
memory_iterator<T> operator -- ( memory_iterator<T>& ref, int ) {
	memory_iterator<T> copy = ref;
	--ref;
	return copy;
}

template<typename T>
inline
memory_iterator<T> operator ++ ( memory_iterator<T>& ref, int ) {
	memory_iterator<T> copy = ref;
	++ref;
	return copy;
}

/**
 * A vector of T kept on disk.
 *
 * The interface is a subset of std::vector<T>'s interface.
 */
template <typename T>
struct memvector {
	public:
		memvector( std::string );
		~memvector();

		typedef T value_type;
		typedef unsigned size_type;
		typedef memory_iterator<T> iterator;
		typedef memory_iterator<const T> 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<T>( address_of( idx ) );
		}

		memory_reference<T> operator[] ( unsigned idx ) {
			assert( idx < size() );
			return memory_reference<T>( address_of( idx ) );
		}

		/**
		 * For debugging, nothing else
		 */
		void print( std::ostream& ) const;
		size_type size() const { return byte_io::read<uint32_t>( 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<memory_manager> data_;
		unsigned char* address_of( unsigned i ) {
			return 	data_->rw_base(
				byte_io::byte_length<unsigned>() +
				i * byte_io::byte_length<T>() );
		}
		const unsigned char* address_of( unsigned i ) const {
			return const_cast<memvector*>( this )->address_of( i );
		}
};

#include "memvector.tcc"


#endif /* LPC_MEMVECTOR_H1105049836_INCLUDE_GUARD_ */