aboutsummaryrefslogtreecommitdiff
path: root/tool_src/BspLib/Chunk.h
blob: db4fa620d60076576faa1dcdd4535f1e61e2cfdc (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//-----------------------------------------------------------------------------
//	BSPLIB HEADER: Chunk.h
//
//  Copyright (c) 1997 by Markus Hadwiger
//  All Rights Reserved.
//-----------------------------------------------------------------------------

#ifndef _CHUNK_H_
#define _CHUNK_H_

// bsplib header files
#include "BspLibDefs.h"
#include "SystemIO.h"


// if this flag is set storage is increased by using lists
//#define EXPAND_STORAGE_WITH_LISTS

// if this flag is set storage is always increased two-fold
#define EXPAND_EXPONENTIALLY

// if this flag is set assignment operators for elements are not invoked
//#define IGNORE_ELEMENT_ASSIGNMENT_OPERATORS


BSPLIB_NAMESPACE_BEGIN


// added for Visual C++ 6.0
template<class T> class Chunk;


// template for simple chunk class (actual representation) --------------------
//
template<class T>
class ChunkRep : public virtual SystemIO {

	friend class Chunk<T>;

	// error identifiers
	enum {
		E_INVALIDINDX
	};

	void		Error( int err ) const;

private:
	ChunkRep( int chunksize = 0 );
	~ChunkRep();

	int			AddElement( const T& element );
	T&			FetchElement( int index );

	int			getNumElements() const;

private:
	int			ref_count;
	int			numelements;
	int			maxnumelements;
	T*			elements;
	ChunkRep*	next;

	static const int CHUNK_SIZE;
};


// create new chunk -----------------------------------------------------------
//
template<class T>
ChunkRep<T>::ChunkRep( int chunksize ) : ref_count( 0 )
{
	int challocsize	= chunksize > 0 ? chunksize : CHUNK_SIZE;
	elements		= new T[ challocsize ];
	maxnumelements	= challocsize;
	numelements		= 0;
	next			= NULL;
}


// destroy list of chunks recursively -----------------------------------------
//
template<class T>
ChunkRep<T>::~ChunkRep()
{
	delete[] elements;
	delete next;
}


// error message handler ------------------------------------------------------
//
template<class T>
void ChunkRep<T>::Error( int err ) const
{
	{
	StrScratch message;
	sprintf( message, "***ERROR*** in object of a class of template Chunk" );

	switch ( err ) {

	case E_INVALIDINDX:
		sprintf( message + strlen( message ), ": [Invalid index]" );
		break;
	}

	ErrorMessage( message );
	}

	HandleCriticalError();
}


// fetch element corresponding to specific index ------------------------------
//
template<class T>
T& ChunkRep<T>::FetchElement( int index )
{

#ifdef EXPAND_STORAGE_WITH_LISTS

	ChunkRep *vlist = this;
	while ( ( vlist != NULL ) && ( index >= vlist->numelements ) ) {
		index -= vlist->numelements;
		vlist  = vlist->next;
	}

	if ( vlist == NULL )
		Error( E_INVALIDINDX );

	// return element
	return vlist->elements[ index ];

#else

	if ( index >= numelements )
		Error( E_INVALIDINDX );

	// return element
	return elements[ index ];

#endif

}


// insert element into chunk, return index ------------------------------------
//
template<class T>
int ChunkRep<T>::AddElement( const T& element )
{

#ifdef EXPAND_STORAGE_WITH_LISTS

	// if space in head-chunk insert element directly
	if ( numelements < maxnumelements ) {
		elements[ numelements ] = element;
		return numelements++;

	} else {

		int		 numskipped	= numelements;
		ChunkRep *vlist		= this;

		// scan until non-full chunk found
		while ( ( vlist->next != NULL ) && ( vlist->next->numelements == vlist->next->maxnumelements ) ) {
			vlist		= vlist->next;
			numskipped += vlist->numelements;
		}

		// insert new chunk if all existing are full
		if ( vlist->next == NULL ) {

#ifdef EXPAND_EXPONENTIALLY
			vlist->next = new ChunkRep( vlist->maxnumelements * 2 );
#else
			vlist->next = new ChunkRep;
#endif
		}

		// insert new element
		vlist->next->elements[ vlist->next->numelements ] = element;
		return vlist->next->numelements++ + numskipped;
	}

#else

	// expand storage if necessary
	if ( numelements == maxnumelements ) {

#ifdef EXPAND_EXPONENTIALLY
		maxnumelements *= 2;
#else
		maxnumelements += CHUNK_SIZE;
#endif
		T *temp = new T[ maxnumelements ];

#ifdef IGNORE_ELEMENT_ASSIGNMENT_OPERATORS
		memcpy( temp, elements, numelements * sizeof( T ) );
#else
		for ( int i = 0; i < numelements; i++ )
			temp[ i ] = elements[ i ];
#endif
		delete[] elements;
		elements = temp;
	}

	elements[ numelements ] = element;
	return numelements++;

#endif

}


// return number of elements in entire chunk ----------------------------------
//
template<class T>
int ChunkRep<T>::getNumElements() const
{
	int num = 0;
	for ( const ChunkRep *vlist = this; vlist; vlist = vlist->next )
		num += vlist->numelements;
	return num;
}


// template for simple chunk class (handle class) -----------------------------
//
template<class T>
class Chunk {

public:
	Chunk( int chunksize = 0 ) { rep = new ChunkRep<T>( chunksize ); rep->ref_count = 1; }
	~Chunk() { if ( --rep->ref_count == 0 ) delete rep; }

	Chunk( const Chunk& copyobj );
	Chunk& operator =( const Chunk& copyobj );

	T&				operator []( int index ) { return rep->FetchElement( index ); }
	T&				FetchElement( int index ) { return rep->FetchElement( index ); }
	int				AddElement( const T& element ) { return rep->AddElement( element ); }

	int				getNumElements() const { return rep->getNumElements(); }

private:
	ChunkRep<T>*	rep;
};


// copy constructor for a Chunk -----------------------------------------------
//
template<class T>
Chunk<T>::Chunk( const Chunk<T>& copyobj )
{
	rep = copyobj.rep;
	rep->ref_count++;
}


// assignment operator for a Chunk --------------------------------------------
//
template<class T>
Chunk<T>& Chunk<T>::operator =( const Chunk<T>& copyobj )
{
	if ( &copyobj != this ) {
		if ( --rep->ref_count == 0 ) {
			delete rep;
		}
		rep = copyobj.rep;
		rep->ref_count++;
	}
	return *this;
}


BSPLIB_NAMESPACE_END


#endif // _CHUNK_H_