Commit a69f997b authored by Davis King's avatar Davis King

Added the promote template which is a tool for converting from a smaller

scalar type to a bigger one.  The change in vector.h is just renaming to
avoid a naming conflict.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403968
parent c2ed90ec
......@@ -597,6 +597,27 @@ namespace dlib
template <> struct is_built_in_scalar_type<wchar_t> { const static bool value = true; };
#endif
// ----------------------------------------------------------------------------------------
/*!A promote
This is a template that takes one of the built in scalar types and give you another
scalar type that should be big enough to hold sums of values from the original scalar
type. The new scalar type will also always be signed.
For example, promote<uint16>::type == int32
!*/
template <typename T, size_t s = sizeof(T)> struct promote;
template <typename T> struct promote<T,1> { typedef int16 type; };
template <typename T> struct promote<T,2> { typedef int32 type; };
template <typename T> struct promote<T,4> { typedef int64 type; };
template <typename T> struct promote<T,8> { typedef int64 type; };
template <> struct promote<float,sizeof(float)> { typedef double type; };
template <> struct promote<double,sizeof(double)> { typedef double type; };
template <> struct promote<long double,sizeof(long double)> { typedef long double type; };
// ----------------------------------------------------------------------------------------
/*!A assign_zero_if_built_in_scalar_type
......
......@@ -32,7 +32,7 @@ namespace dlib
// ----------------------------------------------------------------------------------------
template <typename T, typename U, typename enabled = void>
struct promote;
struct vect_promote;
template <typename T, typename U, bool res = (sizeof(T) <= sizeof(U))>
struct largest_type
......@@ -46,15 +46,15 @@ namespace dlib
};
template <typename T, typename U>
struct promote<T,U, typename enable_if_c<std::numeric_limits<T>::is_integer == std::numeric_limits<U>::is_integer>::type>
struct vect_promote<T,U, typename enable_if_c<std::numeric_limits<T>::is_integer == std::numeric_limits<U>::is_integer>::type>
{
// If both T and U are both either integeral or non-integral then just
// If both T and U are both either integral or non-integral then just
// use the biggest one
typedef typename largest_type<T,U>::type type;
};
template <typename T, typename U>
struct promote<T,U, typename enable_if_c<std::numeric_limits<T>::is_integer != std::numeric_limits<U>::is_integer>::type>
struct vect_promote<T,U, typename enable_if_c<std::numeric_limits<T>::is_integer != std::numeric_limits<U>::is_integer>::type>
{
typedef double type;
};
......@@ -73,7 +73,7 @@ namespace dlib
template <typename T, typename U, long N>
struct vc_rebind_promote
{
typedef vector<typename promote<T,U>::type,N> type;
typedef vector<typename vect_promote<T,U>::type,N> type;
};
// ----------------------------------------------------------------------------------------
......@@ -208,7 +208,7 @@ namespace dlib
template <typename V, typename U, long N>
struct vc_rebind_promote
{
typedef vector<typename promote<V,U>::type,N> type;
typedef vector<typename vect_promote<V,U>::type,N> type;
};
public:
......@@ -415,7 +415,7 @@ namespace dlib
// ---------------------------------------
template <typename U, long N>
typename promote<T,U>::type dot (
typename vect_promote<T,U>::type dot (
const vector<U,N>& rhs
) const
{
......@@ -429,7 +429,7 @@ namespace dlib
const vector<U,N>& rhs
) const
{
typedef vector<typename promote<T,U>::type,3> ret_type;
typedef vector<typename vect_promote<T,U>::type,3> ret_type;
return ret_type (
y()*rhs.z() - z()*rhs.y(),
......@@ -501,7 +501,7 @@ namespace dlib
const U& val
) const
{
typedef vector<typename promote<T,U>::type,3> ret_type;
typedef vector<typename vect_promote<T,U>::type,3> ret_type;
return ret_type(x()/val, y()/val, z()/val);
}
......@@ -565,7 +565,7 @@ namespace dlib
template <typename V, typename U, long N>
struct vc_rebind_promote
{
typedef vector<typename promote<V,U>::type,N> type;
typedef vector<typename vect_promote<V,U>::type,N> type;
};
......@@ -759,7 +759,7 @@ namespace dlib
// ---------------------------------------
template <typename U, long N>
typename promote<T,U>::type dot (
typename vect_promote<T,U>::type dot (
const vector<U,N>& rhs
) const
{
......@@ -825,7 +825,7 @@ namespace dlib
const U& val
) const
{
typedef vector<typename promote<T,U>::type,2> ret_type;
typedef vector<typename vect_promote<T,U>::type,2> ret_type;
return ret_type(x()/val, y()/val);
}
......@@ -884,7 +884,7 @@ namespace dlib
const vector<U,N>& rhs
) const
{
typedef vector<typename promote<T,U>::type,3> ret_type;
typedef vector<typename vect_promote<T,U>::type,3> ret_type;
return ret_type (
y()*rhs.z(),
- x()*rhs.z(),
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment