Wired2Code

Just another WordPress.com site

N-dimension array initialization

In the previous article Container assignment list I show how to assign items to STL containers using a list of items separated by a comma.

This example will show how to do the same with a N-dimensional array, with debug build bounds checking. First an example which shows how to declare and initialize a N-dimensional array of type int or type double with a comma separated list of items.

  1. // define a 3 dimensional array of int's
  2. // and initialize.
  3. MyArray<int, 3> intArray3D;
  4. intArray3D = 1, 2, 3;
  5.  
  6. // define a 2 diemsional array of double's
  7. // and initialize.
  8. MyArray<double, 2> doubleArray2D;
  9. doubleArray2D = 10.0, 20.0;
  10.  
  11. // define a 3 x 3 matrix of double and initialize
  12. MyArray<double, 9> doubleMatrix3x3;
  13. doubleMatrix3x3 = 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0;

The N-Dimensional array class implementation

The array class code below only shows enough code to get the assignment and comma operator overloaded. Other useful code and overloads that this class would have is not shown, in order to keep the discussion simple and on topic.

MyArray Class
  1. template<class T, int ArraySize>
  2. class MyArray
  3. {
  4. public:
  5.     MyArray()
  6.     {
  7.         assert(ArraySize != 0);
  8.         // Set items in array to 0
  9.         for(size_t i = 0; i < ArraySize; ++i)
  10.             m_array[i] = 0;
  11.     }
  12.  
  13.     Initializer<T, ArraySize> operator=(T val)
  14.     {
  15.         // store RHS of = to first array item location
  16.         m_array[0] = val;
  17.  
  18.         // Call Initializer construct with address of m_array[1],
  19.         // return Initializer object.
  20.         // Allows the overloaded comma operator for Initializer
  21.         // to be called repeatedly.
  22.         return Initializer<T, ArraySize>(&m_array[1]);
  23.     }
  24. private:
  25.     T m_array[ArraySize];
  26. };

MyArray is a template class that needs 2 template parameters, the first is the type of array to create, and the second is the number of elements the array can store.

The default constructor assigns 0 to each element in the array, if this does not make sense for the type object being stored in the array then modify as needed.

The overloaded assignment operator takes the first item in the list, right of the = sign (RHS), and assigns it to first position in the array. It then calls Initializer’s constructor with the address of the second element in the array, and returns the Initializer object.

This sets up Initializer, which overloads the comma operator, to handle the rest of the assignment list. Overloading the assignment and comma operators is discussed in the article Container assignment list.

The Initializer class is simple and documented below.

Initializer Class for MyArray
  1. // Initializer class. Helper class for MyArray,
  2. // keeping track of the current element being
  3. // assigned, with the overloaded comma operator,
  4. template<class T, int ArraySize>
  5. class Initializer
  6. {
  7. public:
  8.     Initializer() : m_ptr(0)
  9.     {
  10.         #ifndef NDEBUG
  11.             m_nCount = 0;
  12.         #endif
  13.     }
  14.  
  15.     // Constructor called by MyArray operator=
  16.     // ptr will equal MyArray::m_array[1]. Since
  17.     // For debug builds set m_nCount to 1 since
  18.     // MyArray already assigned element [0].
  19.     Initializer(T * ptr) : m_ptr(ptr)
  20.     {
  21.         #ifndef NDEBUG
  22.             m_nCount = 1;
  23.         #endif
  24.     }
  25.  
  26.     // Overloaded comma operator
  27.     Initializer & operator,(T val)
  28.     {
  29.         #ifndef NDEBUG
  30.             // Debug assert for array overrun
  31.             assert(m_nCount < ArraySize);
  32.             ++m_nCount;
  33.         #endif
  34.  
  35.         // store value in pointer to array, then
  36.         // increment pointer to array to next location
  37.         *m_ptr++ = val;
  38.         return *this;
  39.     }
  40. private:
  41.     // Pointer to array location
  42.     T * m_ptr;
  43.     #ifndef NDEBUG
  44.         int m_nCount;
  45.     #endif
  46. };

July 12, 2010 Posted by | Arrays, C++ Programming, Templates | , , , , , | Leave a comment