Lolly 1.4.28
Loading...
Searching...
No Matches
array.ipp
Go to the documentation of this file.
1
2/******************************************************************************
3 * MODULE : array.cpp
4 * DESCRIPTION: fixed size arrays with reference counting
5 * COPYRIGHT : (C) 1999 Joris van der Hoeven
6 *******************************************************************************
7 * This software falls under the GNU general public license version 3 or later.
8 * It comes WITHOUT ANY WARRANTY WHATSOEVER. For details, see the file LICENSE
9 * in the root directory or <http://www.gnu.org/licenses/gpl-3.0.html>.
10 ******************************************************************************/
11
12#ifndef ARRAY_CC
13#define ARRAY_CC
14#include "array.hpp"
15
16/******************************************************************************
17 * Routines intern to the array<T> class
18 ******************************************************************************/
19
20inline int
21round_length (int n, size_t s) {
22 (void) s;
23 if (n < 6) return n;
24 int i= 8;
25 while (n > i)
26 i<<= 1;
27 return i;
28}
29
30template <class T>
32 : n (n2), a ((n == 0) ? ((T*) NULL)
33 : (tm_new_array<T> (round_length (n, sizeof (T))))) {}
34
35template <class T>
36void
38 int nn= round_length (n, sizeof (T));
39 int mm= round_length (m, sizeof (T));
40 if (mm != nn) {
41 if (mm != 0) {
42 if (nn != 0) {
43 a= tm_resize_array<T> (mm, a);
44 }
45 else {
47 }
48 }
49 else {
50 if (nn != 0) tm_delete_array (a);
51 a= NULL;
52 }
53 }
54 n= m;
55}
56
57template <class T> array<T>::array (T* a, int n) {
58 int i;
60 for (i= 0; i < n; i++)
61 rep->a[i]= a[i];
62}
63
64template <class T> array<T>::array (T x1, T x2) {
66 rep->a[0]= x1;
67 rep->a[1]= x2;
68}
69
70template <class T> array<T>::array (T x1, T x2, T x3) {
72 rep->a[0]= x1;
73 rep->a[1]= x2;
74 rep->a[2]= x3;
75}
76
77template <class T> array<T>::array (T x1, T x2, T x3, T x4) {
79 rep->a[0]= x1;
80 rep->a[1]= x2;
81 rep->a[2]= x3;
82 rep->a[3]= x4;
83}
84
85template <class T> array<T>::array (T x1, T x2, T x3, T x4, T x5) {
87 rep->a[0]= x1;
88 rep->a[1]= x2;
89 rep->a[2]= x3;
90 rep->a[3]= x4;
91 rep->a[4]= x5;
92}
93
94/******************************************************************************
95 * Other routines on arrays
96 ******************************************************************************/
97
98template <class T>
99bool
101 int i;
102 if (N (a) != N (b)) return false;
103 for (i= 0; i < N (a); i++)
104 if (a[i] != b[i]) return false;
105 return true;
106}
107
108template <class T>
109bool
111 int i;
112 if (N (a) != N (b)) return true;
113 for (i= 0; i < N (a); i++)
114 if (a[i] != b[i]) return true;
115 return false;
116}
117
118template <class T>
121 int i;
122
123 if (N (a) == 0) return out << "[ ]";
124 out << "[ ";
125 for (i= 0; i < N (a) - 1; i++)
126 out << a[i] << ", ";
127 if (N (a) != 0) out << a[i];
128 out << " ]";
129 return out;
130}
131
132template <class T>
134operator<< (array<T>& a, T x) {
135 a->resize (N (a) + 1);
136 a[N (a) - 1]= x;
137 return a;
138}
139
140template <class T>
143 int i, k= N (a);
144 a->resize (N (a) + N (b));
145 for (i= 0; i < N (b); i++)
146 a[i + k]= b[i];
147 return a;
148}
149
150template <class t>
151bool
153 int i, l= N (b);
154 for (i= 0; i < l; i++)
155 if (a == b[i]) return true;
156 return false;
157}
158
159template <class t>
162 int i, l= N (b);
163 array<t> c (l + 1);
164 c[0]= a;
165 for (i= 0; i < l; i++)
166 c[i + 1]= b[i];
167 return c;
168}
169
170template <class T>
173 int i, k= N (a), l= N (b);
174 array<T> c (k + l);
175 for (i= 0; i < k; i++)
176 c[i]= a[i];
177 for (i= 0; i < l; i++)
178 c[i + k]= b[i];
179 return c;
180}
181
182template <class T>
184range (array<T> a, int i, int j) {
185 int k;
186 ASSERT (i >= 0 && j <= N (a), "out of range");
187 array<T> r (j - i);
188 for (k= i; k < j; k++)
189 r[k - i]= a[k];
190 return r;
191}
192
193template <class T>
196 int i, n= N (a);
197 array<T> r (n);
198 for (i= 0; i < n; i++)
199 r[i]= a[n - 1 - i];
200 return r;
201}
202
203template <class T>
204int
206 int i, n= N (a), h= 0;
207 for (i= 0; i < n; i++)
208 h= hash (a[i]) ^ ((h << 7) + (h >> 25));
209 return h;
210}
211
212template <class T>
215 int i, n= N (a);
216 array<T> r (n);
217 for (i= 0; i < n; i++)
218 r[i]= a[i] * c;
219 return r;
220}
221
222template <class T>
225 int i, n= N (a);
226 array<T> r (n);
227 for (i= 0; i < n; i++)
228 r[i]= a[i] / c;
229 return r;
230}
231
232#endif // defined ARRAY_CC
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170
int round_length(int n, size_t s)
Definition array.ipp:21
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
array< t > append(t a, array< t > b)
Definition array.ipp:161
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
bool contains(t a, array< t > b)
Definition array.ipp:152
tm_ostream & operator<<(tm_ostream &out, array< T > a)
Output the array to an output stream.
Definition array.ipp:120
array< T > operator/(array< T > a, T c)
Divide an array object by a scalar value of type T.
Definition array.ipp:224
#define ASSERT(cond, msg)
Macro used to assert that a condition is true, and throw an exception with an error message if the co...
Definition basic.hpp:85
blackbox b[13]
blackbox t[13]
array_rep()
Construct a new array representation object with length 0.
Definition array.hpp:61
void resize(int n)
Resize the array length to n.
Definition array.ipp:37
array(int n=0)
Construct a new array object with length 0.
Definition array.hpp:101
The list class represents a linked list.
Definition list.hpp:48
list(T item)
Construct a new list object with a single item.
Definition list.hpp:137
C * tm_new_array(int n)
void tm_delete_array(C *Ptr)
base class of resources
Definition resource.hpp:23