1 | #ifndef MXDYNBLOCK_INCLUDED // -*- C++ -*-
|
---|
2 | #define MXDYNBLOCK_INCLUDED
|
---|
3 | #if !defined(__GNUC__)
|
---|
4 | # pragma once
|
---|
5 | #endif
|
---|
6 |
|
---|
7 | /************************************************************************
|
---|
8 |
|
---|
9 | MxDynBlocks are blocks that automatically grow to fit the data added
|
---|
10 | to them.
|
---|
11 |
|
---|
12 | Copyright (C) 1998 Michael Garland. See "COPYING.txt" for details.
|
---|
13 |
|
---|
14 | $Id: MxDynBlock.h,v 1.1 2002/09/24 16:53:54 wimmer Exp $
|
---|
15 |
|
---|
16 | ************************************************************************/
|
---|
17 |
|
---|
18 | #include "MxBlock.h"
|
---|
19 |
|
---|
20 | template<class T>
|
---|
21 | class MxDynBlock : public MxBlock<T>
|
---|
22 | {
|
---|
23 | private:
|
---|
24 | uint fill;
|
---|
25 |
|
---|
26 | public:
|
---|
27 | MxDynBlock() { init_block(8); fill=0; }
|
---|
28 | MxDynBlock(uint n) { init_block(n); fill=0; }
|
---|
29 |
|
---|
30 | T& operator()(uint i) { AssertBound(i<fill); return ref(i); }
|
---|
31 | const T& operator()(uint i) const { AssertBound(i<fill); return ref(i); }
|
---|
32 |
|
---|
33 | uint length() const { return fill; }
|
---|
34 | uint total_space() const { return MxBlock<T>::length(); }
|
---|
35 | T& last() { AssertBound(fill>0); return raw(fill-1); }
|
---|
36 | const T& last() const { AssertBound(fill>0); return raw(fill-1); }
|
---|
37 |
|
---|
38 | uint add()
|
---|
39 | {
|
---|
40 | if( length()==total_space() ) resize(total_space() * 2);
|
---|
41 | return fill++;
|
---|
42 | }
|
---|
43 | uint add(const T& t)
|
---|
44 | {
|
---|
45 | uint i=add();
|
---|
46 | raw(i) = t;
|
---|
47 | return i;
|
---|
48 | }
|
---|
49 |
|
---|
50 | void reset() { fill = 0; }
|
---|
51 | T& drop() { return raw(--fill); }
|
---|
52 | void drop(uint d) { fill -= d; }
|
---|
53 | bool find(const T& t, uint *index=NULL)
|
---|
54 | {
|
---|
55 | for(uint i=0; i<fill; i++)
|
---|
56 | if( raw(i) == t )
|
---|
57 | {
|
---|
58 | if( index ) *index = i;
|
---|
59 | return true;
|
---|
60 | }
|
---|
61 | return false;
|
---|
62 | }
|
---|
63 | void remove(uint i)
|
---|
64 | { AssertBound(i<fill); fill--; raw(i) = raw(fill); }
|
---|
65 | void remove_inorder(uint i)
|
---|
66 | { memmove(&raw(i), &raw(i+1), (--fill - i)*sizeof(T)); }
|
---|
67 | };
|
---|
68 |
|
---|
69 | template<class T, int T_SIZE>
|
---|
70 | class MxSizedDynBlock : public MxDynBlock<T>
|
---|
71 | {
|
---|
72 | public: MxSizedDynBlock(uint n=T_SIZE) : MxDynBlock<T>(n) { }
|
---|
73 | };
|
---|
74 |
|
---|
75 | // MXDYNBLOCK_INCLUDED
|
---|
76 | #endif
|
---|