35#ifndef _CHOMP_STRUCT_MULTITAB_H_
36#define _CHOMP_STRUCT_MULTITAB_H_
56#define DEFAULTPIECESIZE 32
63template <
class element>
125template <
class element>
133 while ((1 << shiftbits) < piecesize)
135 offsetmask = (1 << shiftbits) - 1;
139template <
class element>
141 npieces (m. npieces), shiftbits (m. shiftbits),
142 offsetmask (m. offsetmask)
147 throw "Cannot alloc mem in copying constructor of a table.";
152 tab [i] =
new element [piecesize];
154 throw "No memory in copying constr.";
155 for (
int j = 0; j < piecesize; ++ j)
156 tab [i] [j] = m.
tab [i] [j];
166template <
class element>
178 if (shiftbits != m. shiftbits)
181 for (
int_t i = 0; i < npieces; ++ i)
190 shiftbits = m. shiftbits;
191 offsetmask = m. offsetmask;
197 if (npieces != m. npieces)
200 element **newtab = (m. npieces) ?
201 (
new element * [m. npieces]) : 0;
202 if (!newtab && m. npieces)
203 throw "No memory for a table in operator =.";
214 while (i < m. npieces)
217 while ((j < npieces) && !tab [j])
221 newtab [i ++] = tab [j ++];
225 while (i < m. npieces)
239 for (
int_t i = 0; i < m. npieces; ++ i)
246 npieces = m. npieces;
255 int_t first_nonempty = 0;
256 int_t first_empty = 0;
258 int piecesize = 1 << shiftbits;
261 while ((first_nonempty < npieces) && !tab [first_nonempty])
263 while ((first_empty < npieces) && tab [first_empty])
267 while (pos < npieces)
273 if (first_nonempty < npieces)
275 tab [pos] = tab [first_nonempty];
276 tab [first_nonempty ++] = 0;
280 tab [pos] =
new element [piecesize];
282 throw "Error in operator =.";
292 for (
int i = 0; i < piecesize; ++ i)
293 tab [pos] [i] = m. tab [pos] [i];
297 if (first_empty < npieces)
299 tab [first_empty] = tab [pos];
316 while ((first_nonempty < npieces) && !tab [first_nonempty])
318 while ((first_empty < npieces) && tab [first_empty])
325template <
class element>
330 for (
int_t i = 0; i < npieces; ++ i)
339template <
class element>
343 throw "Negative index of an element in a table used.";
346 int_t piece = n >> shiftbits;
349 if (piece >= npieces)
351 int_t newnpieces = (npieces << 1) + 1;
352 if (newnpieces <= piece)
353 newnpieces = piece + 1;
354 increase (newnpieces);
360 tab [piece] =
new element [1 << shiftbits];
362 throw "Cannot allocate a piece of a table";
365 return tab [piece] [n & offsetmask];
368template <
class element>
372 throw "Negative index of an element in a table used.";
375 int_t piece = n >> shiftbits;
377 if ((piece >= npieces) || (!tab [piece]))
378 throw "Non-existent table entry requested.";
380 return tab [piece] [n & offsetmask];
383template <
class element>
389template <
class element>
394 int piecesize = 1 << shiftbits;
395 int_t necessarypieces = (n + piecesize - 1) / piecesize;
398 if (necessarypieces > npieces)
399 increase (necessarypieces);
402 for (
int_t i = necessarypieces; i < npieces; ++ i)
413template <
class element>
418 int piecesize = 1 << shiftbits;
419 int_t maxpiece = (n + piecesize - 1) / piecesize;
420 if (maxpiece > npieces)
422 for (
int_t piece = 0; piece < maxpiece; ++ piece)
426 tab [piece] =
new element [piecesize];
428 throw "Too little mem for a piece.";
431 if ((piece == maxpiece - 1) && (n & offsetmask))
432 piecesize = n & offsetmask;
433 for (
int i = 0; i < piecesize; ++ i)
439template <
class element>
444 std::swap (offsetmask, other. offsetmask);
449template <
class element>
456 throw "Trying to increase a multitable incorrectly.";
457 element **newtab =
new element * [n];
459 throw "Cannot increase a table.";
460 for (
int_t i = 0; i < npieces; ++ i)
461 newtab [i] = tab [i];
462 for (
int_t i = npieces; i < n; ++ i)
A container for elements placed in a table (like a vector) that is actually built of many smaller tab...
element & operator[](int_t n)
Returns a reference of an element for reading/writing.
void swap(multitable< element > &other)
Swaps data with another multitable object.
void increase(int_t n)
Increases the number of pieces to the desired one.
multitable(const multitable< element > &m)
The copy constructor.
void fill(const element &e, int_t n)
Fills the table from 0 to the given index with the given element.
void allocate(int_t n)
Allocates the table for holding 'n' elements.
int offsetmask
The mask to get the offset of an element in a table piece.
int shiftbits
The number of bits to shift the index of an element in the table.
const element & operator()(int_t n) const
Returns a reference of an element for reading only.
element ** tab
The actual tables.
int_t npieces
The number of pieces ready to allocate.
multitable(int piecesize=0)
The default constructor for a table with the given size of each piece (should be a power of 2 or is r...
multitable< element > & operator=(const multitable< element > &m)
The assignment operator.
~multitable()
The destructor.
This file contains some precompiler definitions which indicate the operating system and/or compiler u...
int int_t
Index type for indexing arrays, counting cubes, etc.
#define DEFAULTPIECESIZE
The default size of a piece used in the multi-table.
void swap(mwWorkerData &data1, mwWorkerData &data2)
This namespace contains the entire CHomP library interface.