1 | /************************************************************************* |
---|
2 | * * |
---|
3 | * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. * |
---|
4 | * All rights reserved. Email: russ@q12.org Web: www.q12.org * |
---|
5 | * * |
---|
6 | * This library is free software; you can redistribute it and/or * |
---|
7 | * modify it under the terms of EITHER: * |
---|
8 | * (1) The GNU Lesser General Public License as published by the Free * |
---|
9 | * Software Foundation; either version 2.1 of the License, or (at * |
---|
10 | * your option) any later version. The text of the GNU Lesser * |
---|
11 | * General Public License is included with this library in the * |
---|
12 | * file LICENSE.TXT. * |
---|
13 | * (2) The BSD-style license that is included with this library in * |
---|
14 | * the file LICENSE-BSD.TXT. * |
---|
15 | * * |
---|
16 | * This library is distributed in the hope that it will be useful, * |
---|
17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files * |
---|
19 | * LICENSE.TXT and LICENSE-BSD.TXT for more details. * |
---|
20 | * * |
---|
21 | *************************************************************************/ |
---|
22 | |
---|
23 | /* this comes from the `reuse' library. copy any changes back to the source. |
---|
24 | * |
---|
25 | * Variable sized array template. The array is always stored in a contiguous |
---|
26 | * chunk. The array can be resized. A size increase will cause more memory |
---|
27 | * to be allocated, and may result in relocation of the array memory. |
---|
28 | * A size decrease has no effect on the memory allocation. |
---|
29 | * |
---|
30 | * Array elements with constructors or destructors are not supported! |
---|
31 | * But if you must have such elements, here's what to know/do: |
---|
32 | * - Bitwise copy is used when copying whole arrays. |
---|
33 | * - When copying individual items (via push(), insert() etc) the `=' |
---|
34 | * (equals) operator is used. Thus you should define this operator to do |
---|
35 | * a bitwise copy. You should probably also define the copy constructor. |
---|
36 | */ |
---|
37 | |
---|
38 | |
---|
39 | #ifndef _ODE_ARRAY_H_ |
---|
40 | #define _ODE_ARRAY_H_ |
---|
41 | |
---|
42 | #include <ode/config.h> |
---|
43 | |
---|
44 | |
---|
45 | // this base class has no constructors or destructor, for your convenience. |
---|
46 | |
---|
47 | class dArrayBase { |
---|
48 | protected: |
---|
49 | int _size; // number of elements in `data' |
---|
50 | int _anum; // allocated number of elements in `data' |
---|
51 | void *_data; // array data |
---|
52 | |
---|
53 | void _freeAll (int sizeofT); |
---|
54 | void _setSize (int newsize, int sizeofT); |
---|
55 | // set the array size to `newsize', allocating more memory if necessary. |
---|
56 | // if newsize>_anum and is a power of two then this is guaranteed to |
---|
57 | // set _size and _anum to newsize. |
---|
58 | |
---|
59 | public: |
---|
60 | // not: dArrayBase () { _size=0; _anum=0; _data=0; } |
---|
61 | |
---|
62 | int size() const { return _size; } |
---|
63 | int allocatedSize() const { return _anum; } |
---|
64 | void * operator new (size_t size); |
---|
65 | void operator delete (void *ptr, size_t size); |
---|
66 | |
---|
67 | void constructor() { _size=0; _anum=0; _data=0; } |
---|
68 | // if this structure is allocated with malloc() instead of new, you can |
---|
69 | // call this to set it up. |
---|
70 | |
---|
71 | void constructLocalArray (int __anum); |
---|
72 | // this helper function allows non-reallocating arrays to be constructed |
---|
73 | // on the stack (or in the heap if necessary). this is something of a |
---|
74 | // kludge and should be used with extreme care. this function acts like |
---|
75 | // a constructor - it is called on uninitialized memory that will hold the |
---|
76 | // Array structure and the data. __anum is the number of elements that |
---|
77 | // are allocated. the memory MUST be allocated with size: |
---|
78 | // sizeof(ArrayBase) + __anum*sizeof(T) |
---|
79 | // arrays allocated this way will never try to reallocate or free the |
---|
80 | // memory - that's your job. |
---|
81 | }; |
---|
82 | |
---|
83 | |
---|
84 | template <class T> class dArray : public dArrayBase { |
---|
85 | public: |
---|
86 | void equals (const dArray<T> &x) { |
---|
87 | setSize (x.size()); |
---|
88 | memcpy (_data,x._data,x._size * sizeof(T)); |
---|
89 | } |
---|
90 | |
---|
91 | dArray () { constructor(); } |
---|
92 | dArray (const dArray<T> &x) { constructor(); equals (x); } |
---|
93 | ~dArray () { _freeAll(sizeof(T)); } |
---|
94 | void setSize (int newsize) { _setSize (newsize,sizeof(T)); } |
---|
95 | T *data() const { return (T*) _data; } |
---|
96 | T & operator[] (int i) const { return ((T*)_data)[i]; } |
---|
97 | void operator = (const dArray<T> &x) { equals (x); } |
---|
98 | |
---|
99 | void push (const T item) { |
---|
100 | if (_size < _anum) _size++; else _setSize (_size+1,sizeof(T)); |
---|
101 | memcpy (&(((T*)_data)[_size-1]), &item, sizeof(T)); |
---|
102 | } |
---|
103 | |
---|
104 | void swap (dArray<T> &x) { |
---|
105 | int tmp1; |
---|
106 | void *tmp2; |
---|
107 | tmp1=_size; _size=x._size; x._size=tmp1; |
---|
108 | tmp1=_anum; _anum=x._anum; x._anum=tmp1; |
---|
109 | tmp2=_data; _data=x._data; x._data=tmp2; |
---|
110 | } |
---|
111 | |
---|
112 | // insert the item at the position `i'. if i<0 then add the item to the |
---|
113 | // start, if i >= size then add the item to the end of the array. |
---|
114 | void insert (int i, const T item) { |
---|
115 | if (_size < _anum) _size++; else _setSize (_size+1,sizeof(T)); |
---|
116 | if (i >= (_size-1)) i = _size-1; // add to end |
---|
117 | else { |
---|
118 | if (i < 0) i=0; // add to start |
---|
119 | int n = _size-1-i; |
---|
120 | if (n>0) memmove (((T*)_data) + i+1, ((T*)_data) + i, n*sizeof(T)); |
---|
121 | } |
---|
122 | ((T*)_data)[i] = item; |
---|
123 | } |
---|
124 | |
---|
125 | void remove (int i) { |
---|
126 | if (i >= 0 && i < _size) { // passing this test guarantees size>0 |
---|
127 | int n = _size-1-i; |
---|
128 | if (n>0) memmove (((T*)_data) + i, ((T*)_data) + i+1, n*sizeof(T)); |
---|
129 | _size--; |
---|
130 | } |
---|
131 | } |
---|
132 | }; |
---|
133 | |
---|
134 | |
---|
135 | #endif |
---|