CppAD: A C++ Algorithmic Differentiation Package 20110419
|
00001 /* $Id$ */ 00002 # ifndef CPPAD_CHECK_SIMPLE_VECTOR_INCLUDED 00003 # define CPPAD_CHECK_SIMPLE_VECTOR_INCLUDED 00004 00005 /* -------------------------------------------------------------------------- 00006 CppAD: C++ Algorithmic Differentiation: Copyright (C) 2003-10 Bradley M. Bell 00007 00008 CppAD is distributed under multiple licenses. This distribution is under 00009 the terms of the 00010 Common Public License Version 1.0. 00011 00012 A copy of this license is included in the COPYING file of this distribution. 00013 Please visit http://www.coin-or.org/CppAD/ for information on other licenses. 00014 -------------------------------------------------------------------------- */ 00015 /* 00016 $begin CheckSimpleVector$$ 00017 $spell 00018 const 00019 cppad.hpp 00020 CppAD 00021 $$ 00022 00023 $section Check Simple Vector Concept$$ 00024 00025 $index simple, vector check$$ 00026 $index vector, simple check$$ 00027 $index check, simple vector$$ 00028 $index concept, check simple vector$$ 00029 00030 $head Syntax$$ 00031 $code # include <cppad/check_simple_vector.hpp>$$ 00032 $pre 00033 $$ 00034 $codei%CheckSimpleVector<%Scalar%, %Vector%>()%$$ 00035 $pre 00036 $$ 00037 $codei%CheckSimpleVector<%Scalar%, %Vector%>(%x%, %y%)%$$ 00038 00039 00040 $head Purpose$$ 00041 Preforms compile and run time checks that the type specified 00042 by $icode Vector$$ satisfies all the requirements for 00043 a $xref/SimpleVector/$$ class with 00044 $xref/SimpleVector/Elements of Specified Type/elements of type/$$ 00045 $icode Scalar$$. 00046 If a requirement is not satisfied, 00047 a an error message makes it clear what condition is not satisfied. 00048 00049 $head x, y$$ 00050 If the arguments $icode x$$ and $icode y$$ are present, 00051 they have prototype 00052 $codei% 00053 const %Scalar%& %x% 00054 const %Scalar%& %y% 00055 %$$ 00056 In addition, the check 00057 $code% 00058 %x% == %x% 00059 %$$ 00060 will return the boolean value $code true$$, and 00061 $code% 00062 %x% == %y% 00063 %$$ 00064 will return $code false$$. 00065 00066 $head Restrictions$$ 00067 If the arguments $icode x$$ and $icode y$$ are not present, 00068 the following extra assumption is made by $code CheckSimpleVector$$: 00069 If $icode x$$ is a $icode Scalar$$ object 00070 $codei% 00071 %x% = 0 00072 %y% = 1 00073 %$$ 00074 assigns values to the objects $icode x$$ and $icode y$$. 00075 In addition, 00076 $icode%x% == %x%$$ would return the boolean value $code true$$ and 00077 $icode%x% == %y%$$ would return $code false$$. 00078 00079 $head Include$$ 00080 The file $code cppad/check_simple_vector.hpp$$ is included by $code cppad/cppad.hpp$$ 00081 but it can also be included separately with out the rest 00082 if the CppAD include files. 00083 00084 $head Example$$ 00085 $children% 00086 example/check_simple_vector.cpp 00087 %$$ 00088 The file $xref/CheckSimpleVector.cpp/$$ 00089 contains an example and test of this function where $icode S$$ 00090 is the same as $icode T$$. 00091 It returns true, if it succeeds an false otherwise. 00092 The comments in this example suggest a way to change the example 00093 so $icode S$$ is not the same as $icode T$$. 00094 00095 $end 00096 --------------------------------------------------------------------------- 00097 */ 00098 00099 # include <cppad/local/cppad_assert.hpp> 00100 00101 namespace CppAD { 00102 00103 # ifdef NDEBUG 00104 template <class Scalar, class Vector> 00105 inline void CheckSimpleVector(const Scalar& x, const Scalar& y) 00106 { } 00107 template <class Scalar, class Vector> 00108 inline void CheckSimpleVector(void) 00109 { } 00110 # else 00111 template <class S, class T> 00112 struct ok_if_S_same_as_T { }; 00113 00114 template <class T> 00115 struct ok_if_S_same_as_T<T,T> { typedef T ok; }; 00116 00117 template <class Scalar, class Vector> 00118 void CheckSimpleVector(const Scalar& x, const Scalar& y) 00119 { // only need execute once per value Scalar, Vector pair 00120 static bool runOnce = false; 00121 if( runOnce ) 00122 return; 00123 runOnce = true; 00124 00125 // value_type must be type of elements of Vector 00126 typedef typename Vector::value_type value_type; 00127 00128 // check that elements of Vector have type Scalar 00129 typedef typename ok_if_S_same_as_T<Scalar, value_type>::ok ok; 00130 00131 // check default constructor 00132 Vector d; 00133 00134 // size member function 00135 CPPAD_ASSERT_KNOWN( 00136 d.size() == 0, 00137 "default construtor result does not have size zero" 00138 ); 00139 00140 // resize to same size as other vectors in test 00141 d.resize(1); 00142 00143 // check sizing constructor 00144 Vector s(1); 00145 00146 // check element assignment 00147 s[0] = y; 00148 CPPAD_ASSERT_KNOWN( 00149 s[0] == y, 00150 "element assignment failed" 00151 ); 00152 00153 // check copy constructor 00154 s[0] = x; 00155 const Vector c(s); 00156 s[0] = y; 00157 CPPAD_ASSERT_KNOWN( 00158 c[0] == x, 00159 "copy constructor is shallow" 00160 ); 00161 00162 // vector assignment operator 00163 d[0] = x; 00164 s = d; 00165 s[0] = y; 00166 CPPAD_ASSERT_KNOWN( 00167 d[0] == x, 00168 "assignment operator is shallow" 00169 ); 00170 00171 // element access, right side const 00172 // element assignment, left side not const 00173 d[0] = c[0]; 00174 CPPAD_ASSERT_KNOWN( 00175 d[0] == x, 00176 "element assignment from const failed" 00177 ); 00178 } 00179 template <class Scalar, class Vector> 00180 void CheckSimpleVector(void) 00181 { Scalar x; 00182 Scalar y; 00183 00184 // use assignment and not constructor 00185 x = 0; 00186 y = 1; 00187 00188 CheckSimpleVector<Scalar, Vector>(x, y); 00189 } 00190 00191 # endif 00192 00193 } // end namespace CppAD 00194 00195 # endif