Lolly 1.4.27
Loading...
Searching...
No Matches
array.hpp
Go to the documentation of this file.
1
2/** \file array.hpp
3 * \copyright GPLv3
4 * \details defines a templated array class representing a fixed-size array of
5 elements.
6 * \author Joris van der Hoeven
7 * \date 1999
8 */
9
10#ifndef ARRAY_H
11#define ARRAY_H
12#include "basic.hpp"
13
14/**
15 * @brief A template class representing an array.
16 *
17 * @tparam T Type of elements in the array.
18 */
19template <class T> class array;
20
21/**
22 * @brief Get the length of the array.
23 *
24 * @tparam T Type of elements in the array.
25 * @param a The array.
26 * @return The length of the array.
27 */
28template <class T> int N (array<T> a);
29
30/**
31 * @brief Get a pointer to the first element of the array.
32 *
33 * @tparam T Type of elements in the array.
34 * @param a The array.
35 * @return A pointer to the first element of the array.
36 */
37template <class T> T* A (array<T> a);
38
39/**
40 * @brief Make a copy of an array.
41 *
42 * @tparam T Type of elements in the array.
43 * @param x The array to be copied.
44 * @return A copy of the array.
45 */
46template <class T> array<T> copy (array<T> x);
47
48/**
49 * @brief A concrete struct representing the implementation of an array.
50 *
51 * @tparam T Type of elements in the array.
52 */
53template <class T> class array_rep : concrete_struct {
54 int n;
55 T* a;
56
57public:
58 /**
59 * @brief Construct a new array representation object with length 0.
60 */
61 inline array_rep () : n (0), a (NULL) {}
62
63 /**
64 * @brief Construct a new array representation object with specified length.
65 *
66 * @param n The length of the array.
67 */
68 array_rep (int n);
69
70 /**
71 * @brief Destroy the array representation object.
72 */
73 inline ~array_rep () {
74 if (n != 0) tm_delete_array (a);
75 }
76
77 /**
78 * @brief Resize the array length to n
79 *
80 * @param n
81 */
82 void resize (int n);
83
84 friend class array<T>;
85 friend int N LESSGTR (array<T> a);
86 friend T* A LESSGTR (array<T> a);
88};
89
90/**
91 * @brief A template class representing an array.
92 *
93 * @tparam T Type of elements in the array.
94 */
95template <class T> class array {
97
98 /**
99 * @brief Construct a new array object with length 0.
100 */
101 inline array (int n= 0) : rep (tm_new<array_rep<T>> (n)) {}
102
103 /**
104 * @brief Construct a new array object from a C array with specified length.
105 *
106 * @tparam T Type of elements in the array.
107 * @param a The C array.
108 * @param n The length of the C array.
109 */
110 array (T* a, int n);
111
112 /**
113 * @brief Construct a new array object with two elements.
114 *
115 * @tparam T Type of elements in the array.
116 * @param x1 The first element.
117 * @param x2 The second element.
118 */
119 array (T x1, T x2);
120
121 /**
122 * @brief Construct a new array object with three elements.
123 *
124 * @tparam T Type of elements in the array.
125 * @param x1 The first element.
126 * @param x2 The second element.
127 * @param x3 The third element.
128 */
129 array (T x1, T x2, T x3);
130
131 /**
132 * @brief Construct a new array object with four elements.
133 *
134 * @tparam T Type of elements in the array.
135 * @param x1 The first element.
136 * @param x2 The second element.
137 * @param x3 The third element.
138 * @param x4 The fourth element.
139 */
140 array (T x1, T x2, T x3, T x4);
141
142 /**
143 * @brief Construct a new array object with five elements.
144 *
145 * @tparam T Type of elements in the array.
146 * @param x1 The first element.
147 * @param x2 The second element.
148 * @param x3 The third element.
149 * @param x4 The fourth element.
150 * @param x5 The fifth element.
151 */
152 array (T x1, T x2, T x3, T x4, T x5);
153
154 /**
155 * @brief Get a reference to the element atthe specified index.
156 *
157 * @tparam T Type of elements in the array.
158 * @param i The index of the element.
159 * @return A reference to the element at the specified index.
160 */
161 inline T& operator[] (int i) { return rep->a[i]; }
162
163 T* begin () { return rep->a; }
164 T* end () { return rep->a + rep->n; }
165};
167
168#define TMPL template <class T>
169TMPL inline int
171 return a->n;
172}
173TMPL inline T*
175 return a->a;
176}
177TMPL inline array<T>
179 return array<T> (a->a, a->n);
180}
181
182/**
183 * @brief Output the array to an output stream.
184 *
185 * @tparam T Type of elements in the array.
186 * @param out The output stream.
187 * @param a The array to be output.
188 * @return The output stream.
189 */
191
192/**
193 * @brief Append an element to the end of the array.
194 *
195 * @tparam T Type of elements in the array.
196 * @param a The array.
197 * @param x The element to be appended.
198 * @return The updated array.
199 */
201
202/**
203 * @brief Append an array to the end of another array.
204 *
205 * @tparam T Type of elements in the array.
206 * @param a The first array.
207 * @param b The second array.
208 * @return The updated array.
209 */
211
212/**
213 * @brief Check if an array contains a specified element.
214 *
215 * @tparam T Type of elements in the array.
216 * @param a The array.
217 * @param b The element to be checked.
218 * @return True if the array contains the element, false otherwise.
219 */
221
222/**
223 * @brief Append an element to the beginning of an array.
224 *
225 * @tparam T Type of elements in the array.
226 * @param a The array.
227 * @param b The element to be appended.
228 * @return The updated array.
229 */
231
232/**
233 * @brief Append an array to the end of another array.
234 *
235 * @tparam T Type of elements in the array.
236 * @param a The first array.
237 * @param b The second array.
238 * @return The updated array.
239 */
241
242/**
243 * @brief Get a subarray of an array.
244 *
245 * @tparam T Type of elements in the array.
246 * @param a The array.
247 * @param i The start index of the subarray.
248 * @param j The end index of the subarray.
249 * @return The subarray.
250 */
251TMPL array<T> range (array<T> a, int i, int j);
252
253/**
254 * @brief Reverse an array.
255 *
256 * @tparam T Type of elements in the array.
257 * @param a The array.
258 * @return The reversed array.
259 */
261
262/**
263 * @brief Check if two arrays are equal.
264 *
265 * @tparam T Type of elements in the arrays.
266 * @param a The first array.
267 * @param b The second array.
268 * @return True if the two arrays are equal, false otherwise.
269 */
271
272/**
273 * @brief Check if two arrays are not equal.
274 *
275 * @tparam T Type of elements in the arrays.
276 * @param a The first array.
277 * @param b The second array.
278 * @return True if the two arrays are not equal, false otherwise.
279 */
281
282/**
283 * @brief Multiply an array object by a scalar value of type T.
284 *
285 * @param a The target array.
286 * @param c The scalar value.
287 * @return Return a new array object whose elements are the elements of a
288 * multiplied by c.
289 */
291
292/**
293 * @brief Divide an array object by a scalar value of type T.
294 *
295 * @param a The target array.
296 * @param c The scalar value.
297 * @return Return a new array object whose elements are the elements of a
298 * divided by c.
299 */
301
302/**
303 * @brief This function computes the hash of an array object a.
304 *
305 * @param a The array object.
306 * @return Return an integer hash value.
307 */
308TMPL int hash (array<T> a);
309#undef TMPL
310
311#ifdef STRING_H
312// Function parameters participating in template signature resolution are never
313// cast implicitely.
314inline array<string>&
315operator<< (array<string>& a, char* x) {
316 return a << string (x);
317}
318#endif
319
320#include "array.ipp"
321
322#endif // defined ARRAY_H
T * A(array< T > a)
Get a pointer to the first element of the array.
Definition array.hpp:174
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
array< T > reverse(array< T > a)
Reverse an array.
Definition array.ipp:195
array< T > operator*(array< T > a, T c)
Multiply an array object by a scalar value of type T.
Definition array.ipp:214
int hash(array< T > a)
This function computes the hash of an array object a.
Definition array.ipp:205
array< T > range(array< T > a, int i, int j)
Get a subarray of an array.
Definition array.ipp:184
bool contains(T a, array< T > b)
Check if an array contains a specified element.
bool operator!=(array< T > a, array< T > b)
Check if two arrays are not equal.
Definition array.ipp:110
bool operator==(array< T > a, array< T > b)
Check if two arrays are equal.
Definition array.ipp:100
array< T > copy(array< T > x)
Make a copy of an array.
Definition array.hpp:178
#define TMPL
Definition array.hpp:168
tm_ostream & operator<<(tm_ostream &out, array< T > a)
Output the array to an output stream.
Definition array.ipp:120
array< T > append(T a, array< T > b)
Append an element to the beginning of an array.
array< T > operator/(array< T > a, T c)
Divide an array object by a scalar value of type T.
Definition array.ipp:224
blackbox b[13]
A concrete struct representing the implementation of an array.
Definition array.hpp:53
friend array< T > copy LESSGTR(array< T > a)
friend T *A LESSGTR(array< T > a)
array_rep()
Construct a new array representation object with length 0.
Definition array.hpp:61
int n
Definition array.hpp:54
~array_rep()
Destroy the array representation object.
Definition array.hpp:73
T * a
Definition array.hpp:55
friend int N LESSGTR(array< T > a)
void resize(int n)
Resize the array length to n.
Definition array.ipp:37
A template class representing an array.
Definition array.hpp:95
T & operator[](int i)
Get a reference to the element atthe specified index.
Definition array.hpp:161
array(int n=0)
Construct a new array object with length 0.
Definition array.hpp:101
CONCRETE_TEMPLATE(array, T)
T * end()
Definition array.hpp:164
T * begin()
Definition array.hpp:163
#define CONCRETE_TEMPLATE_CODE(PTR, TT, T)
Macro used to define the implementation of a concrete smart pointer with reference counting for a sin...
Definition classdef.hpp:215
The list class represents a linked list.
Definition list.hpp:48
C * tm_new()
void tm_delete_array(C *Ptr)
Structure representing a concrete object with a reference count.
Definition classdef.hpp:24
base class of resources
Definition resource.hpp:23