Lolly 1.4.27
Loading...
Searching...
No Matches
Classes | Macros | Functions
array.hpp File Reference
#include "basic.hpp"
#include "array.ipp"
Include dependency graph for array.hpp:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  array_rep< T >
 A concrete struct representing the implementation of an array. More...
 
class  array< T >
 A template class representing an array. More...
 

Macros

#define TMPL   template <class T>
 

Functions

template<class T >
int N (array< T > a)
 Get the length of the array.
 
template<class T >
T * A (array< T > a)
 Get a pointer to the first element of the array.
 
template<class T >
array< T > copy (array< T > x)
 Make a copy of an array.
 
 CONCRETE_TEMPLATE_CODE (array, class, T)
 
template<class T >
tm_ostreamoperator<< (tm_ostream &out, array< T > a)
 Output the array to an output stream.
 
template<class T >
array< T > & operator<< (array< T > &a, T x)
 Append an element to the end of the array.
 
template<class T >
array< T > & operator<< (array< T > &a, array< T > b)
 Append an array to the end of another array.
 
template<class T >
bool contains (T a, array< T > b)
 Check if an array contains a specified element.
 
template<class T >
array< T > append (T a, array< T > b)
 Append an element to the beginning of an array.
 
template<class T >
array< T > append (array< T > a, array< T > b)
 Append an array to the end of another array.
 
template<class T >
array< T > range (array< T > a, int i, int j)
 Get a subarray of an array.
 
template<class T >
array< T > reverse (array< T > a)
 Reverse an array.
 
template<class T >
bool operator== (array< T > a, array< T > b)
 Check if two arrays are equal.
 
template<class T >
bool operator!= (array< T > a, array< T > b)
 Check if two arrays are not equal.
 
template<class T >
array< T > operator* (array< T > a, T c)
 Multiply an array object by a scalar value of type T.
 
template<class T >
array< T > operator/ (array< T > a, T c)
 Divide an array object by a scalar value of type T.
 
template<class T >
int hash (array< T > a)
 This function computes the hash of an array object a.
 

Detailed Description

defines a templated array class representing a fixed-size array of elements.

Author
Joris van der Hoeven
Date
1999

Definition in file array.hpp.

Macro Definition Documentation

◆ TMPL

#define TMPL   template <class T>

Definition at line 168 of file array.hpp.

Function Documentation

◆ N()

template<class T >
int N ( array< T > a)
inline

Get the length of the array.

Template Parameters
TType of elements in the array.
Parameters
aThe array.
Returns
The length of the array.

Definition at line 170 of file array.hpp.

170 {
171 return a->n;
172}

◆ A()

template<class T >
T * A ( array< T > a)
inline

Get a pointer to the first element of the array.

Template Parameters
TType of elements in the array.
Parameters
aThe array.
Returns
A pointer to the first element of the array.

Definition at line 174 of file array.hpp.

174 {
175 return a->a;
176}

◆ copy()

template<class T >
array< T > copy ( array< T > x)
inline

Make a copy of an array.

Template Parameters
TType of elements in the array.
Parameters
xThe array to be copied.
Returns
A copy of the array.

Definition at line 178 of file array.hpp.

178 {
179 return array<T> (a->a, a->n);
180}
The list class represents a linked list.
Definition list.hpp:48

◆ CONCRETE_TEMPLATE_CODE()

CONCRETE_TEMPLATE_CODE ( array ,
class ,
T  )

◆ operator<<() [1/3]

template<class T >
tm_ostream & operator<< ( tm_ostream & out,
array< T > a )

Output the array to an output stream.

Template Parameters
TType of elements in the array.
Parameters
outThe output stream.
aThe array to be output.
Returns
The output stream.

Definition at line 119 of file array.ipp.

120 {
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}
int N(array< T > a)
Get the length of the array.
Definition array.hpp:170

◆ operator<<() [2/3]

template<class T >
array< T > & operator<< ( array< T > & a,
T x )

Append an element to the end of the array.

Template Parameters
TType of elements in the array.
Parameters
aThe array.
xThe element to be appended.
Returns
The updated array.

Definition at line 119 of file array.ipp.

134 {
135 a->resize (N (a) + 1);
136 a[N (a) - 1]= x;
137 return a;
138}

◆ operator<<() [3/3]

template<class T >
array< T > & operator<< ( array< T > & a,
array< T > b )

Append an array to the end of another array.

Template Parameters
TType of elements in the array.
Parameters
aThe first array.
bThe second array.
Returns
The updated array.

Definition at line 119 of file array.ipp.

142 {
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}
blackbox b[13]

◆ contains()

template<class T >
bool contains ( T a,
array< T > b )

Check if an array contains a specified element.

Template Parameters
TType of elements in the array.
Parameters
aThe array.
bThe element to be checked.
Returns
True if the array contains the element, false otherwise.

◆ append() [1/2]

template<class T >
array< T > append ( T a,
array< T > b )

Append an element to the beginning of an array.

Template Parameters
TType of elements in the array.
Parameters
aThe array.
bThe element to be appended.
Returns
The updated array.

◆ append() [2/2]

template<class T >
array< T > append ( array< T > a,
array< T > b )

Append an array to the end of another array.

Template Parameters
TType of elements in the array.
Parameters
aThe first array.
bThe second array.
Returns
The updated array.

Definition at line 172 of file array.ipp.

172 {
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}

◆ range()

template<class T >
array< T > range ( array< T > a,
int i,
int j )

Get a subarray of an array.

Template Parameters
TType of elements in the array.
Parameters
aThe array.
iThe start index of the subarray.
jThe end index of the subarray.
Returns
The subarray.

Definition at line 184 of file array.ipp.

184 {
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}
#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

◆ reverse()

template<class T >
array< T > reverse ( array< T > a)

Reverse an array.

Template Parameters
TType of elements in the array.
Parameters
aThe array.
Returns
The reversed array.

Definition at line 195 of file array.ipp.

195 {
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}

◆ operator==()

template<class T >
bool operator== ( array< T > a,
array< T > b )

Check if two arrays are equal.

Template Parameters
TType of elements in the arrays.
Parameters
aThe first array.
bThe second array.
Returns
True if the two arrays are equal, false otherwise.

Definition at line 100 of file array.ipp.

100 {
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}

◆ operator!=()

template<class T >
bool operator!= ( array< T > a,
array< T > b )

Check if two arrays are not equal.

Template Parameters
TType of elements in the arrays.
Parameters
aThe first array.
bThe second array.
Returns
True if the two arrays are not equal, false otherwise.

Definition at line 110 of file array.ipp.

110 {
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}

◆ operator*()

template<class T >
array< T > operator* ( array< T > a,
T c )

Multiply an array object by a scalar value of type T.

Parameters
aThe target array.
cThe scalar value.
Returns
Return a new array object whose elements are the elements of a multiplied by c.

Definition at line 214 of file array.ipp.

214 {
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}

◆ operator/()

template<class T >
array< T > operator/ ( array< T > a,
T c )

Divide an array object by a scalar value of type T.

Parameters
aThe target array.
cThe scalar value.
Returns
Return a new array object whose elements are the elements of a divided by c.

Definition at line 224 of file array.ipp.

224 {
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}

◆ hash()

template<class T >
int hash ( array< T > a)

This function computes the hash of an array object a.

Parameters
aThe array object.
Returns
Return an integer hash value.

Definition at line 205 of file array.ipp.

205 {
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}
int hash(array< T > a)
This function computes the hash of an array object a.
Definition array.ipp:205