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