1 /*************************************************************************
3 * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
4 * All rights reserved. Email: russ@q12.org Web: www.q12.org *
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 *
13 * (2) The BSD-style license that is included with this library in *
14 * the file LICENSE-BSD.TXT. *
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. *
21 *************************************************************************/
23 /* this comes from the `reuse' library. copy any changes back to the source.
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.
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.
42 #include <ode/odeconfig.h>
46 // this base class has no constructors or destructor, for your convenience.
50 int _size
; // number of elements in `data'
51 int _anum
; // allocated number of elements in `data'
52 void *_data
; // array data
54 void _freeAll (int sizeofT
);
55 void _setSize (int newsize
, int sizeofT
);
56 // set the array size to `newsize', allocating more memory if necessary.
57 // if newsize>_anum and is a power of two then this is guaranteed to
58 // set _size and _anum to newsize.
61 // not: dArrayBase () { _size=0; _anum=0; _data=0; }
63 int size() const { return _size
; }
64 int allocatedSize() const { return _anum
; }
65 void * operator new (size_t size
);
66 void operator delete (void *ptr
, size_t size
);
68 void constructor() { _size
=0; _anum
=0; _data
=0; }
69 // if this structure is allocated with malloc() instead of new, you can
70 // call this to set it up.
72 void constructLocalArray (int __anum
);
73 // this helper function allows non-reallocating arrays to be constructed
74 // on the stack (or in the heap if necessary). this is something of a
75 // kludge and should be used with extreme care. this function acts like
76 // a constructor - it is called on uninitialized memory that will hold the
77 // Array structure and the data. __anum is the number of elements that
78 // are allocated. the memory MUST be allocated with size:
79 // sizeof(ArrayBase) + __anum*sizeof(T)
80 // arrays allocated this way will never try to reallocate or free the
81 // memory - that's your job.
85 template <class T
> class dArray
: public dArrayBase
{
87 void equals (const dArray
<T
> &x
) {
89 memcpy (_data
,x
._data
,x
._size
* sizeof(T
));
92 dArray () { constructor(); }
93 dArray (const dArray
<T
> &x
) { constructor(); equals (x
); }
94 ~dArray () { _freeAll(sizeof(T
)); }
95 void setSize (int newsize
) { _setSize (newsize
,sizeof(T
)); }
96 T
*data() const { return (T
*) _data
; }
97 T
& operator[] (int i
) const { return ((T
*)_data
)[i
]; }
98 void operator = (const dArray
<T
> &x
) { equals (x
); }
100 void push (const T item
) {
101 if (_size
< _anum
) _size
++; else _setSize (_size
+1,sizeof(T
));
102 memcpy (&(((T
*)_data
)[_size
-1]), &item
, sizeof(T
));
105 void swap (dArray
<T
> &x
) {
108 tmp1
=_size
; _size
=x
._size
; x
._size
=tmp1
;
109 tmp1
=_anum
; _anum
=x
._anum
; x
._anum
=tmp1
;
110 tmp2
=_data
; _data
=x
._data
; x
._data
=tmp2
;
113 // insert the item at the position `i'. if i<0 then add the item to the
114 // start, if i >= size then add the item to the end of the array.
115 void insert (int i
, const T item
) {
116 if (_size
< _anum
) _size
++; else _setSize (_size
+1,sizeof(T
));
117 if (i
>= (_size
-1)) i
= _size
-1; // add to end
119 if (i
< 0) i
=0; // add to start
121 if (n
>0) memmove (((T
*)_data
) + i
+1, ((T
*)_data
) + i
, n
*sizeof(T
));
123 ((T
*)_data
)[i
] = item
;
126 void remove (int i
) {
127 if (i
>= 0 && i
< _size
) { // passing this test guarantees size>0
129 if (n
>0) memmove (((T
*)_data
) + i
, ((T
*)_data
) + i
+1, n
*sizeof(T
));