Guest User

Untitled

a guest
May 25th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.14 KB | None | 0 0
  1. /***************************************************************************
  2. utilities.h - description
  3. -------------------
  4. begin : Sat Apr 26 2003
  5. copyright : (C) 2003 by Blake Madden
  6. ***************************************************************************/
  7.  
  8. /***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the BSD License. *
  12. * *
  13. ***************************************************************************/
  14.  
  15. #ifndef __UTILITIES_H__
  16. #define __UTILITIES_H__
  17.  
  18. #include <algorithm>
  19. #include <functional>
  20. #include <math.h>
  21. #include <cassert>
  22. #include <iterator>
  23.  
  24. #ifndef M_PI
  25. #define M_PI 3.1415926535897932384626433832795
  26. #endif
  27.  
  28. //returns the byte size of an array
  29. #define size_of_array(x) (sizeof(x)/sizeof(x[0]))
  30.  
  31. inline double within_range(size_t start, size_t end, double value)
  32. {
  33. return ( (value >= start) && (value <= end) ) ? value :
  34. (value < start) ? start :
  35. (value > end) ? end : /*never reaches this branch*/ value;
  36. }
  37.  
  38. inline float degrees_to_radians(const float degrees)
  39. { return degrees * (M_PI/static_cast<float>(180)); }
  40.  
  41. //Opposite side is the side going upwards of a right triangle
  42. inline float calc_opposite_side(const float hypontenuse, const float angleInDegrees)
  43. {
  44. return (sin(degrees_to_radians(angleInDegrees)) * hypontenuse);
  45. }
  46.  
  47. //Adjacent side is the side at the bottom of a right triangle
  48. inline float calc_adjacent_side(const float hypontenuse, const float angleInDegrees)
  49. {
  50. return (cos(degrees_to_radians(angleInDegrees)) * hypontenuse);
  51. }
  52.  
  53. //Function to see if a number is even
  54. template<typename T>
  55. inline bool is_even(T value)
  56. { return (value%2) == 0; }
  57. //specialized version of is_even for floating point value types that need to be "floored" first
  58. inline bool is_even(double value)
  59. { return (static_cast<long>(floor(abs(value)))%2) == 0; }
  60. inline bool is_even(float value)
  61. { return (static_cast<long>(floor(abs(value)))%2) == 0; }
  62.  
  63. ///integer rounding function
  64. template<typename T>
  65. inline double round(T x)
  66. { return (floor(x+0.5f)); }
  67.  
  68. //class that remembers its original value from constuction
  69. template <typename T>
  70. class backup_variable
  71. {
  72. public:
  73. backup_variable(const T& value) : m_originalValue(value), m_value(value)
  74. {}
  75. void operator=(const T& value)
  76. { m_value = value; }
  77. bool operator==(const T& value) const
  78. { return m_value == value; }
  79. bool operator<(const T& value) const
  80. { return m_value < value; }
  81. bool operator<=(const T& value) const
  82. { return m_value <= value; }
  83. bool operator>(const T& value) const
  84. { return m_value > value; }
  85. bool operator>=(const T& value) const
  86. { return m_value >= value; }
  87. void operator+(const T& value)
  88. { m_value + value; }
  89. void operator+=(const T& value)
  90. { m_value += value; }
  91. void operator-(const T& value)
  92. { m_value - value; }
  93. void operator-=(const T& value)
  94. { m_value -= value; }
  95. operator const T() const
  96. { return m_value; }
  97. T* operator&()
  98. { return &m_value; }
  99. T get_value() const
  100. { return m_value; }
  101. bool has_changed() const
  102. { return m_value != m_originalValue; }
  103. private:
  104. T m_originalValue;
  105. T m_value;
  106. };
  107.  
  108. template<typename T>
  109. inline bool is_either(T value, T first, T second)
  110. {
  111. return (value == first || value == second);
  112. }
  113.  
  114. template<typename T>
  115. inline bool is_neither(T value, T first, T second)
  116. {
  117. assert(first != second);
  118. return (value != first && value != second);
  119. }
  120.  
  121. template<typename T>
  122. inline bool is_within(T value, T first, T second)
  123. {
  124. assert(first <= second);
  125. return (value >= first && value <= second);
  126. }
  127.  
  128. /*calls a member function of elements in a container for each
  129. elelement in another container*/
  130. template<typename inT, typename outT, typename member_extract_functorT>
  131. inline outT copy_member(inT begin, inT end, outT dest, member_extract_functorT get_value)
  132. {
  133. for (; begin != end; ++dest, ++begin)
  134. *dest = get_value(*begin);
  135. return (dest);
  136. }
  137.  
  138. template<typename inT, typename outT,
  139. typename _Pr,
  140. typename member_extract_functorT>
  141. inline outT copy_member_if(inT begin, inT end, outT dest,
  142. _Pr meets_criteria,
  143. member_extract_functorT get_value)
  144. {
  145. for (; begin != end; ++begin)
  146. {
  147. if (meets_criteria(*begin))
  148. {
  149. *dest = get_value(*begin);
  150. ++dest;
  151. }
  152. }
  153. return (dest);
  154. }
  155.  
  156. template<typename _InIt, typename _Pr, typename member_extract_functorT>
  157. inline typename std::iterator_traits<_InIt>::difference_type
  158. count_member_if(_InIt _First, _InIt _Last,
  159. _Pr _Pred, member_extract_functorT get_value)
  160. {
  161. //count elements satisfying _Pred
  162. typename std::iterator_traits<_InIt>::difference_type _Count = 0;
  163.  
  164. for (; _First != _Last; ++_First)
  165. if (_Pred(get_value(*_First)) )
  166. ++_Count;
  167. return (_Count);
  168. }
  169.  
  170. //determines if number is even
  171. template <typename T>
  172. class even : public std::unary_function<T, bool>
  173. {
  174. public:
  175. inline bool operator()(const T& val) const
  176. { return val%2==0; }
  177. };
  178.  
  179. //determines if a value is within a given range
  180. template<typename T>
  181. class within : public std::unary_function<T, bool>
  182. {
  183. public:
  184. within(T range_begin, T range_end)
  185. : m_range_begin(range_begin), m_range_end(range_end)
  186. {}
  187. inline bool operator()(T value) const
  188. { return (value >= m_range_begin && value <= m_range_end); }
  189. private:
  190. T m_range_begin;
  191. T m_range_end;
  192. };
  193.  
  194. #endif //__UTILITIES_H__
Advertisement
Add Comment
Please, Sign In to add comment