File: simple_vector.cpp

package info (click to toggle)
cppad 2026.00.00.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,584 kB
  • sloc: cpp: 112,960; sh: 6,146; ansic: 179; python: 71; sed: 12; makefile: 10
file content (80 lines) | stat: -rw-r--r-- 2,722 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
// SPDX-FileCopyrightText: Bradley M. Bell <bradbell@seanet.com>
// SPDX-FileContributor: 2003-22 Bradley M. Bell
// ----------------------------------------------------------------------------
/*
{xrst_begin simple_vector.cpp}

Simple Vector Template Class: Example and Test
##############################################

{xrst_literal
   // BEGIN C++
   // END C++
}

{xrst_end simple_vector.cpp}
--------------------------------------------------------------------------
*/
// BEGIN C++
# include <iostream>                   // std::cout and std::endl

# include <vector>                     // std::vector
# include <valarray>                   // std::valarray
# include <cppad/utility/vector.hpp>       // CppAD::vector
# include <cppad/utility/check_simple_vector.hpp>  // CppAD::CheckSimpleVector
namespace {
   template <class Vector>
   bool Ok(void)
   {  // type corresponding to elements of Vector
      typedef typename Vector::value_type Scalar;

      bool ok = true;             // initialize testing flag

      Vector x;                   // use the default constructor
      ok &= (x.size() == 0);      // test size for an empty vector
      Vector y(2);                // use the sizing constructor
      ok &= (y.size() == 2);      // size for an vector with elements

      // non-const access to the elements of y
      size_t i;
      for(i = 0; i < 2; i++)
         y[i] = Scalar(i);

      const Vector z(y);          // copy constructor
      x.resize(2);                // resize
      x = z;                      // vector assignment

      // use the const access to the elements of x
      // and test the values of elements of x, y, z
      for(i = 0; i < 2; i++)
      {  ok &= (x[i] == Scalar(i));
         ok &= (y[i] == Scalar(i));
         ok &= (z[i] == Scalar(i));
      }
      return ok;
   }
}
bool SimpleVector (void)
{  bool ok = true;

   // use routine above to check these cases
   ok &= Ok< std::vector<double> >();
   ok &= Ok< std::valarray<float> >();
   ok &= Ok< CppAD::vector<int> >();
# ifndef _MSC_VER
   // Avoid Microsoft following compiler warning:  'size_t' :
   // forcing value to bool 'true' or 'false' (performance warning)
   ok &= Ok< std::vector<bool> >();
   ok &= Ok< CppAD::vector<bool> >();
# endif
   // use CheckSimpleVector for more extensive testing
   CppAD::CheckSimpleVector<double, std::vector<double>  >();
   CppAD::CheckSimpleVector<float,  std::valarray<float> >();
   CppAD::CheckSimpleVector<int,    CppAD::vector<int>   >();
   CppAD::CheckSimpleVector<bool,   std::vector<bool>    >();
   CppAD::CheckSimpleVector<bool,   CppAD::vector<bool>  >();

   return ok;
}
// END C++