[1097] | 1 | #ifndef MXBLOCK_INCLUDED // -*- C++ -*-
|
---|
| 2 | #define MXBLOCK_INCLUDED
|
---|
| 3 | #if !defined(__GNUC__)
|
---|
| 4 | # pragma once
|
---|
| 5 | #endif
|
---|
| 6 |
|
---|
| 7 | /************************************************************************
|
---|
| 8 |
|
---|
| 9 | MxBlock provides typed access a contiguous block of elements.
|
---|
| 10 |
|
---|
| 11 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
| 12 |
|
---|
| 13 | $Id: MxBlock.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
| 14 |
|
---|
| 15 | ************************************************************************/
|
---|
| 16 | #include "stdmix.h"
|
---|
| 17 | template<class T>
|
---|
| 18 | class MxBlock : public MxEQ
|
---|
| 19 | {
|
---|
| 20 | private:
|
---|
| 21 | uint N;
|
---|
| 22 | T *block;
|
---|
| 23 |
|
---|
| 24 | protected:
|
---|
| 25 | MxBlock() { }
|
---|
| 26 | void init_block(uint n)
|
---|
| 27 | {
|
---|
| 28 | // Allocate memory for block
|
---|
| 29 | N=n; block = (T *)malloc(sizeof(T)*n);
|
---|
| 30 |
|
---|
| 31 | // Initialize each entry
|
---|
| 32 | for(uint i=0; i<n; i++) new((void *)&block[i], MX_ALLOC_INPLACE) T;
|
---|
| 33 | }
|
---|
| 34 | void resize_block(uint n)
|
---|
| 35 | {
|
---|
| 36 | T *old = block;
|
---|
| 37 |
|
---|
| 38 | // Allocate new block
|
---|
| 39 | block = (T *)realloc(old, sizeof(T)*n);
|
---|
| 40 |
|
---|
| 41 | // Initialize all the newly allocated entries
|
---|
| 42 | for(uint i=N; i<n; i++) new((void *)&block[i], MX_ALLOC_INPLACE) T;
|
---|
| 43 |
|
---|
| 44 | N = n;
|
---|
| 45 | }
|
---|
| 46 | void free_block()
|
---|
| 47 | {
|
---|
| 48 | #ifdef WIN32
|
---|
| 49 | // The Microsoft compiler has a problem with the
|
---|
| 50 | // standard syntax below. For some reason,
|
---|
| 51 | // expanding it into the following pointer-based
|
---|
| 52 | // version makes it work. Don't ask me why.
|
---|
| 53 | //
|
---|
| 54 | for(uint i=0; i<N; i++) { T *p = &block[i]; p->~T(); }
|
---|
| 55 | free(block);
|
---|
| 56 | #else
|
---|
| 57 | // Call the relevant destructors for each element before
|
---|
| 58 | // freeing the block. Has now effect for types like 'int'.
|
---|
| 59 | //
|
---|
| 60 | for(uint i=0; i<N; i++) block[i].~T();
|
---|
| 61 | free(block);
|
---|
| 62 | #endif
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | T& raw(uint i) { return block[i]; }
|
---|
| 66 | const T& raw(uint i) const { return block[i]; }
|
---|
| 67 |
|
---|
| 68 |
|
---|
| 69 | public:
|
---|
| 70 | MxBlock(uint n) { init_block(n); }
|
---|
| 71 | ~MxBlock() { free_block(); }
|
---|
| 72 |
|
---|
| 73 | // Standard accessors
|
---|
| 74 | //
|
---|
| 75 | T& ref(uint i) { AssertBound(i<N);return block[i]; }
|
---|
| 76 | const T& ref(uint i) const { AssertBound(i<N);return block[i]; }
|
---|
| 77 | T& operator()(uint i) { return ref(i); }
|
---|
| 78 | const T& operator()(uint i) const { return ref(i); }
|
---|
| 79 | #ifdef __GNUC__
|
---|
| 80 | // Most compilers can apply the casting operators and then use the
|
---|
| 81 | // standard array []. GCC (2.7.2) seems to need explicit operators.
|
---|
| 82 | //
|
---|
| 83 | T& operator[](uint i) { return block[i]; }
|
---|
| 84 | const T& operator[](uint i) const { return block[i]; }
|
---|
| 85 | #endif
|
---|
| 86 |
|
---|
| 87 | operator const T*() const { return block; }
|
---|
| 88 | operator T*() { return block; }
|
---|
| 89 | uint length() const { return N; }
|
---|
| 90 |
|
---|
| 91 | // Primitive methods for altering the data block
|
---|
| 92 | //
|
---|
| 93 | void resize(uint n) { resize_block(n); }
|
---|
| 94 | void bitcopy(const T *array, uint n) // copy bits directly
|
---|
| 95 | { memcpy(block, array, MIN(n,N)*sizeof(T)); }
|
---|
| 96 | void copy(const T *array, const uint n) // copy using assignment operator
|
---|
| 97 | { for(uint i=0; i<MIN(n,N); i++) block[i] = array[i]; }
|
---|
| 98 | void bitcopy(const MxBlock<T>& b) { bitcopy(b, b.length()); }
|
---|
| 99 | void copy(const MxBlock<T>& b) { copy(b, b.length()); }
|
---|
| 100 | };
|
---|
| 101 |
|
---|
| 102 | // MXBLOCK_INCLUDED
|
---|
| 103 | #endif
|
---|