3 * Iter Vehemens ad Necem (IVAN)
4 * Copyright (C) Timo Kiviluoto
5 * Released under the GNU General
8 * See LICENSING which should be included
9 * along with this file for more details
12 #ifndef __FELIB_FEARRAY_H__
13 #define __FELIB_FEARRAY_H__
18 template <class type
> struct fearray
{
19 typedef uInt sizetype
;
20 fearray () : Data(0), Size(0) {}
21 fearray (const fearray
&);
22 fearray (const type
*, sizetype
);
24 fearray
&operator = (const fearray
&);
25 type
&operator [] (sizetype I
) { return Data
[I
]; }
26 const type
&operator [] (sizetype I
) const { return Data
[I
]; }
27 void Allocate (sizetype
);
28 void Add (const type
&);
30 const type
&GetRandomElement () const { return Data
[RAND_N(Size
)]; }
36 template <class type
> inline fearray
<type
>::fearray (const fearray
<type
>& A
) : Data(A
.Data
), Size(A
.Size
) {
37 if (Data
) ++REFS(Data
);
41 template <class type
> inline fearray
<type
>::fearray (const type
*Array
, sizetype Size
) : Size(Size
) {
42 char *Ptr
= new char[Size
*sizeof(type
)+sizeof(feuLong
)];
43 *reinterpret_cast<feuLong
*>(Ptr
) = 0;
44 Data
= reinterpret_cast<type
*>(Ptr
+sizeof(feuLong
));
45 for (sizetype c
= 0; c
< Size
; ++c
) new (&Data
[c
])type(Array
[c
]);
49 template <class type
> inline fearray
<type
>::~fearray
<type
> () {
51 if (Ptr
&& !REFS(Ptr
)--) {
52 type
*TempPtr
= Ptr
, *EndPtr
= Ptr
+Size
;
53 for (; TempPtr
!= EndPtr
; ++TempPtr
) TempPtr
->~type();
59 template <class type
> inline fearray
<type
> &fearray
<type
>::operator = (const fearray
<type
> &A
) {
60 if (A
.Data
) ++REFS(Data
= A
.Data
);
66 template <class type
> inline void fearray
<type
>::Clear () {
70 for (sizetype c
= 0; c
< Size
; ++c
) Ptr
[c
].~type();
79 template <class type
> inline void fearray
<type
>::Allocate (sizetype What
) {
80 char *Ptr
= new char[What
*sizeof(type
)+sizeof(feuLong
)];
81 *reinterpret_cast<feuLong
*>(Ptr
) = 0;
82 Data
= reinterpret_cast<type
*>(Ptr
+sizeof(feuLong
));
84 for (sizetype c
= 0; c
< What
; ++c
) new (&Data
[c
])type
;
88 /* Don't use unless necessary */
89 template <class type
> inline void fearray
<type
>::Add (const type
&Type
) {
92 sizetype Size
= this->Size
++;
93 char* NewPtr
= new char[(Size
+1)*sizeof(type
)+sizeof(feuLong
)];
94 *reinterpret_cast<feuLong
*>(NewPtr
) = 0;
95 type
* NewData
= reinterpret_cast<type
*>(NewPtr
+sizeof(feuLong
));
97 for (sizetype c
= 0; c
< Size
; ++c
) {
98 new (&NewData
[c
])type(Ptr
[c
]);
101 delete [] REFSA(Ptr
);
103 for(sizetype c
= 0; c
< Size
; ++c
) new (&NewData
[c
])type(Ptr
[c
]);
106 new(&NewData
[Size
]) type(Type
);
108 char *NewPtr
= new char[sizeof(type
)+sizeof(feuLong
)];
109 *reinterpret_cast<feuLong
*>(NewPtr
) = 0;
110 Data
= reinterpret_cast<type
*>(NewPtr
+sizeof(feuLong
));
112 new(Data
) type(Type
);
117 template <class type1
, class type2
> inline void ArrayToVector (const fearray
<type1
> &Array
, std::vector
<type2
> &Vect
) {
118 Vect
.resize(Array
.Size
, type2());
119 for (typename fearray
<type1
>::sizetype c
= 0; c
< Array
.Size
; ++c
) Vect
[c
] = Array
.Data
[c
];