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::vector< unsigned int > &shape)
 Construct from a string.
 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

*The array data
*The array shape *One entry per dimension
*The array shape *One entry per giving the size of each dimension std::vector< unsigned int > m_shape
*Subarray sizes
*Subarray for faster access See above *This member could be considered transient std::vector< unsigned int > m_sizes

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/3]

Default constructor.

This makes an uninitialized Arrayrep.

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

68{}

◆ Arrayrep() [2/3]

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

Construct from a string.

Construct an empty array of a given shape.

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]]'. */ explicit Arrayrep (const std::string& str, const std::string& context = "");

/**

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.

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}
*The array shape *One entry per giving the size of each dimension std::vector< unsigned int > m_shape

◆ Arrayrep() [3/3]

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
*Subarray for faster access See above *This member could be considered transient std::vector< unsigned int > m_sizes

◆ 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

◆ data

◆ dimension

* The array shape* One entry per CaloRec::Arrayrep::dimension

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

◆ m_shape

* The array shape* One entry per giving the size of each dimension std::vector<unsigned int> CaloRec::Arrayrep::m_shape

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

◆ m_sizes

* Subarray for faster access See above* This member could be considered transient std::vector<unsigned int> CaloRec::Arrayrep::m_sizes

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

◆ sizes

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


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