ATLAS Offline Software
Loading...
Searching...
No Matches
CaloRec::Arrayrep Class Reference

Representation class for Array's. More...

#include <Arrayrep.h>

Inheritance diagram for CaloRec::Arrayrep:
Collaboration diagram for CaloRec::Arrayrep:

Public Member Functions

 Arrayrep ()
 Default constructor.
 Arrayrep (const std::string &str, const std::string &context="")
 Construct from a string.
 Arrayrep (const std::vector< unsigned int > &shape)
 Construct an empty array of a given shape.
 Arrayrep (const unsigned int shape[], unsigned int n)
 Construct an empty array of a given shape.
void init_sizes (bool resize_data=false)
 Initialize the m_sizes vector from the m_shape vector.
void write_array (std::ostream &stream) const
 Creates a text representation of the array content.
void write_subarray (std::ostream &stream, std::vector< Arrayelt >::size_type &idx, unsigned dimIndex) const
 Helper function for write_array.

Public Attributes

std::vector< Arrayeltm_data
 The array data, stored using the C array ordering.
std::vector< unsigned int > m_shape
 The array shape.
std::vector< unsigned int > m_sizes
 Subarray sizes, for faster access.

Detailed Description

Representation class for Array's.

This class is used for the implementation of multidimensional array constants. The user interface is provided by the Array template class. Given an Array<N> a, we want a[i] to be an Array<N-1>. However, we don't want to have to copy the array data. Thus, the Array class just holds a reference to the actual array data. Those data are stored in an instance of Arrayrep.

An Arrayrep is defined by two vectors: one giving all the array elements, and the second giving the shape of the array. (A shape is a vector with one integer per array dimension, the integers giving the size of the dimensions.)

We maintain one additional array as an optimization. m_sizes[0] contains the total size in elements of the array resulting from a single indexing operation. m_sizes[1] contains the size of the array resulting from two indexing operations, and so on. m_sizes has the same length as m_shape, but the last element is always 1. The function init_sizes will initialize the m_sizes array from the contents of m_shape.

A scalar (0-dimensional array) is represented by m_shape (and m_sizes) being empty, and m_data having a single element.

If both m_shape and m_data are empty, then the representation is uninitialized.

Definition at line 61 of file Control/CxxUtils/CxxUtils/Arrayrep.h.

Constructor & Destructor Documentation

◆ Arrayrep() [1/4]

CaloRec::Arrayrep::Arrayrep ( )
inline

Default constructor.

This makes an uninitialized Arrayrep.

Definition at line 68 of file Control/CxxUtils/CxxUtils/Arrayrep.h.

68{}

◆ Arrayrep() [2/4]

CaloRec::Arrayrep::Arrayrep ( const std::string & str,
const std::string & context = "" )
explicit

Construct from a string.

Parameters
strThe string to convert.
contextAn optional string to use for error reporting.

Parse the string and initialize the array. This string should be like ‘[[1, 2], [3, 4]]’.

Definition at line 202 of file Arrayrep.cxx.

203{
204 // Special cases for True and False.
205 if (str == "True")
206 m_data.push_back (1);
207 else if (str == "False")
208 m_data.push_back (0);
209 else {
210 // Make the token stream from which we're reading.
211 std::istringstream is (str);
212 ArrayScanner s (is);
213
214 // If we're looking at an open bracket, consume it, and proceed
215 // to read an array.
216 if (s.at_open())
217 read_array (*this, s, 0, context);
218 else {
219 // Otherwise, we're reading a scalar. So read a single number.
220 Arrayelt elt;
221 if (!s.at_num (elt))
222 error (context, "Number expected");
223 m_data.push_back (elt);
224 }
225
226 // We should now be at the end of the stream.
227 if (!s.at_end())
228 error (context, "End of vector before end of string");
229 }
230
231 // Set up the m_sizes vector.
232 init_sizes();
233}
float Arrayelt
The type of an element of an Array.
std::vector< Arrayelt > m_data
The array data, stored using the C array ordering.
void init_sizes(bool resize_data=false)
Initialize the m_sizes vector from the m_shape vector.
Definition Arrayrep.cxx:34

◆ Arrayrep() [3/4]

CaloRec::Arrayrep::Arrayrep ( const std::vector< unsigned int > & shape)
explicit

Construct an empty array of a given shape.

Parameters
shapeThe shape of the array.

Initialize an array of a given shape. The array will contain all 0's.

Definition at line 69 of file Arrayrep.cxx.

70 : m_shape (shape)
71{
72 // Set up m_sizes.
73 init_sizes (true);
74}
std::vector< unsigned int > m_shape
The array shape.

◆ Arrayrep() [4/4]

CaloRec::Arrayrep::Arrayrep ( const unsigned int shape[],
unsigned int n )
explicit

Construct an empty array of a given shape.

Parameters
shapeThe shape of the array.
nThe length of the shape array.

Initialize an array of a given shape. The array will contain all 0's. This version is more convenient to call with a constant shape.

Definition at line 86 of file Arrayrep.cxx.

87{
88 // Copy the data into m_shape.
89 m_shape.reserve (n);
90 for (unsigned int i=0; i < n; i++)
91 m_shape.push_back (shape[i]);
92
93 // Set up m_sizes.
94 init_sizes (true);
95}

Member Function Documentation

◆ init_sizes()

void CaloRec::Arrayrep::init_sizes ( bool resize_data = false)

Initialize the m_sizes vector from the m_shape vector.

Parameters
resize_dataShould m_data be resized appropriately?

The contents of the m_sizes vector are initialized from the contents of the m_shape vector. If resize_data is true, then the size of m_data is changed to the total size indicated by m_shape. Otherwise, we verify that m_data has the correct size, and raise an assertion if not.

Definition at line 34 of file Arrayrep.cxx.

35{
36 // Don't do anything if we've already done this, or if the array
37 // is empty.
38 if (m_sizes.size() < m_shape.size())
39 {
40 // Calculate the m_sizes array.
41 unsigned int sz = 1;
42 unsigned int dim = m_shape.size();
43 m_sizes.resize (dim);
44 m_sizes[0] = 1;
45 for (unsigned int i=0; i < dim; i++) {
46 m_sizes[i] = sz;
47 sz *= m_shape[dim-1 - i];
48 }
49
50 if (resize_data) {
51 // Resize m_data to the proper size.
52 m_data.resize (sz);
53 }
54 else {
55 // Check that m_data has the correct size.
56 assert (sz == m_data.size());
57 }
58 }
59}
static Double_t sz
std::vector< unsigned int > m_sizes
Subarray sizes, for faster access.

◆ write_array()

void CaloRec::Arrayrep::write_array ( std::ostream & stream) const

Creates a text representation of the array content.

Helper function for write_array.

Parameters
std::ostreamwhere the text should be written

Writes the content of the array to a ostream. The sub-arrays are enclosed by square brackets and separated by commas.

Parameters
streamwhere the array should be written
idxCurrent index in m_data
dimIndexCurrent index in m_shapes

Calls itself recursively with dimIndex-1

Definition at line 245 of file Arrayrep.cxx.

245 {
246 if (!m_data.size()) {//Empty array
247 stream << "[ ]" << std::endl;
248 return;
249 }
250
251 if (!m_shape.size()) {//Single element
252 stream << m_data[0] << std::endl;
253 return;
254 }
255
256 //All other cases: Array of dimension>=1
257 //check consistency of Array
258 unsigned totSize=m_shape[0];
259 for (unsigned i=1;i<m_shape.size();i++)
260 totSize=totSize*m_shape[i];
261 if (totSize!=m_data.size())
262 error("","Array is inconsistent!");
263
264 std::vector<Arrayelt>::size_type dataIndex=0;
265 write_subarray(stream,dataIndex,0);
266 stream << "]" << std::endl;
267 return;
268}
void write_subarray(std::ostream &stream, std::vector< Arrayelt >::size_type &idx, unsigned dimIndex) const
Helper function for write_array.
Definition Arrayrep.cxx:279

◆ write_subarray()

void CaloRec::Arrayrep::write_subarray ( std::ostream & stream,
std::vector< Arrayelt >::size_type & idx,
unsigned dimIndex ) const

Helper function for write_array.

Creates a text representation of the array content.

Parameters
streamwhere the array should be written
idxCurrent index in m_data
dimIndexCurrent index in m_shapes

Calls itself recursively with dimIndex-1

Parameters
std::ostreamwhere the text should be written

Writes the content of the array to a ostream. The sub-arrays are enclosed by square brackets and separated by commas.

Definition at line 279 of file Arrayrep.cxx.

279 {
280 if (dimIndex<(m_shape.size()-1)) {
281 stream << "[\n ";
282 for (unsigned i=0;i<m_shape[dimIndex];i++) {
283 write_subarray(stream,idx,dimIndex+1);
284 if (i==m_shape[dimIndex]-1)
285 stream << "]\n ";
286 else
287 stream << "],\n ";
288 }
289 }
290 else { // last dimension
291 stream << "[" << m_data[idx++];
292 for (unsigned i=1;i<m_shape[dimIndex];i++)
293 stream << ", " << m_data[idx++];
294 }
295 return;
296}

Member Data Documentation

◆ m_data

std::vector<Arrayelt> CaloRec::Arrayrep::m_data

The array data, stored using the C array ordering.

Definition at line 102 of file Control/CxxUtils/CxxUtils/Arrayrep.h.

◆ m_shape

std::vector<unsigned int> CaloRec::Arrayrep::m_shape

The array shape.

One entry per dimension, giving the size of each dimension.

Definition at line 106 of file Control/CxxUtils/CxxUtils/Arrayrep.h.

◆ m_sizes

std::vector<unsigned int> CaloRec::Arrayrep::m_sizes

Subarray sizes, for faster access.

See above. This member could be considered transient.

Definition at line 110 of file Control/CxxUtils/CxxUtils/Arrayrep.h.


The documentation for this class was generated from the following files: