summaryrefslogtreecommitdiffstats
path: root/indexlib/leafdata.h
blob: e499a2e6f203cf8dd2d48721d3a16b5b594f9c90 (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
#ifndef LPC_LEAFDATA_H1102530057_INCLUDE_GUARD_
#define LPC_LEAFDATA_H1102530057_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 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 <iostream>
#include <iterator>
#include <inttypes.h>
#include "pointer.h"
#include "memvector.h"
#include "thing.h"

void  set_leafdata_manager( memory_manager* );
memory_manager* get_leafdata_manager();

struct leafdata_iterator : public std::iterator<STD_NAMESPACE_PREFIX input_iterator_tag,uint32_t> {
	public:
		explicit leafdata_iterator( const unsigned char* d ):
			data_( d ),
			value_( 0 ),
			valid_( true )
			{
			}

		value_type operator *() {
			assert( valid_ );
			valid_ = false;

			uint32_t delta = *data_++;
			if ( delta ) {
				value_ += delta;
			} else {
				value_ = byte_io::read<uint32_t>( data_ );
				data_ += byte_io::byte_lenght<uint32_t>();
			}
			return value_ - 1;
		}

		const unsigned char* raw() const { return data_; }

		leafdata_iterator& operator ++ () {
			valid_ = true;
			return *this;
		}
		bool operator == ( const leafdata_iterator& other ) const {
			return data_ == other.data_;
		}
		bool operator < ( const leafdata_iterator& other ) const {
			return data_ < other.data_;
		}
	private:
		const unsigned char* data_;
		uint32_t value_;
		bool valid_;
};

inline
bool operator != ( const leafdata_iterator& one, const leafdata_iterator& other ) {
	return !( one == other );
}

START_THING( leaf_data, thing< thing_manager<&get_leafdata_manager> > )
	private:
		MEMBER( uint16_t, capacity, 0 )
		MEMBER( uint16_t, usedbytes, 2 )
		friend class leaf_data_pool_traits;
		static const unsigned data_offset = 4;
	public:
		MY_BASE( data_offset )
	public:

	typedef leafdata_iterator iterator;
	leafdata_iterator begin() const { return iterator( const_cast<unsigned char*>( my_base() ) ); }
	leafdata_iterator end() const { return iterator( const_cast<unsigned char*>( my_base() ) + usedbytes() ); }

	uint32_t get_reference( unsigned idx ) const;
	void add_reference( uint32_t );
	void remove_reference( uint32_t );
	
	bool has_reference( uint32_t ) const;
	bool can_add( uint32_t ) const;

	unsigned nelems() const;
	unsigned next_byte_size() const;
	void grow();

	void print( std::ostream& out ) const;

	static void construct( void* );
	static void init( pointer<leaf_data> p ) { construct( p.raw_pointer() ); }
	static unsigned start_bytes() { return 16; }
END_THING( leaf_data )

DO_POINTER_SPECS( leaf_data )


typedef leaf_data leafdata;
typedef leaf_dataptr leafdataptr;

struct leaf_data_pool_traits {
	typedef leaf_data value_type;
	typedef leaf_dataptr pointer;
	
	static bool is_free( pointer p ) { return p->capacity() == 0; }
	static void mark_free( pointer p ) { memset( p.raw_pointer(), 0, p->capacity() ); }
	static unsigned size_of( pointer p ) { return p->capacity() + leafdata::data_offset; }

	static unsigned type_offset() { return 2; }

	static unsigned min_size() { return leaf_data::start_bytes(); }
	static unsigned max_size() { return 1024 * 64; }
	
	static void set_manager( memory_manager* m ) { return set_leafdata_manager( m ); }
	static void print( std::ostream& out, pointer p ) { out << '[' << p << "] leafdata:\n"; p->print( out ); }
};


#endif /* LPC_LEAFDATA_H1102530057_INCLUDE_GUARD_ */