]> https://gitweb.dealii.org/ - dealii.git/commitdiff
Use AlignedVector::size_type in TableBase::position
authorMichael F. Herbst <info@michael-herbst.com>
Mon, 23 Feb 2015 13:59:05 +0000 (14:59 +0100)
committerMichael F. Herbst <info@michael-herbst.com>
Mon, 23 Feb 2015 13:59:05 +0000 (14:59 +0100)
Use AlignedVector<T>::size_type as the integer type to carry out the
calculation of the position of the element indicated by the
TableIndices<N> object.

This has the advantage that a potential integer overflow for Tables
with sizes less than max long int in each dimension, but with more than
max long int entries is prevented.

include/deal.II/base/table.h

index 749808989316b7a1f2a219be85b5b0e5c608307c..9dc7a9c5ec96463ee650cccb4ff5cb67ebda70e4 100644 (file)
@@ -593,7 +593,7 @@ protected:
    * Return the position of the indicated element within the array of elements
    * stored one after the other. This function does no index checking.
    */
-  unsigned int position (const TableIndices<N> &indices) const;
+  typename AlignedVector<T>::size_type position (const TableIndices<N> &indices) const;
 
   /**
    * Return a read-write reference to the indicated element.
@@ -1987,9 +1987,11 @@ TableBase<N,T>::memory_consumption () const
 
 template <int N, typename T>
 inline
-unsigned int
+typename AlignedVector<T>::size_type
 TableBase<N,T>::position (const TableIndices<N> &indices) const
 {
+  typedef typename AlignedVector<T>::size_type size_type;
+
   // specialize this for the
   // different numbers of dimensions,
   // to make the job somewhat easier
@@ -2000,13 +2002,13 @@ TableBase<N,T>::position (const TableIndices<N> &indices) const
     case 1:
       return indices[0];
     case 2:
-      return indices[0]*table_size[1] + indices[1];
+      return size_type(indices[0])*table_size[1] + indices[1];
     case 3:
-      return ((indices[0]*table_size[1] + indices[1])*table_size[2]
+      return ((size_type(indices[0])*table_size[1] + indices[1])*table_size[2]
               + indices[2]);
     default:
     {
-      unsigned int s = indices[0];
+      size_type s = indices[0];
       for (unsigned int n=1; n<N; ++n)
         s = s*table_size[n] + indices[n];
       return s;

In the beginning the Universe was created. This has made a lot of people very angry and has been widely regarded as a bad move.

Douglas Adams


Typeset in Trocchi and Trocchi Bold Sans Serif.