functions.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. // Debugging support implementation -*- C++ -*-
  2. // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
  3. // Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // Under Section 7 of GPL version 3, you are granted additional
  15. // permissions described in the GCC Runtime Library Exception, version
  16. // 3.1, as published by the Free Software Foundation.
  17. // You should have received a copy of the GNU General Public License and
  18. // a copy of the GCC Runtime Library Exception along with this program;
  19. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file debug/functions.h
  22. * This file is a GNU debug extension to the Standard C++ Library.
  23. */
  24. #ifndef _GLIBCXX_DEBUG_FUNCTIONS_H
  25. #define _GLIBCXX_DEBUG_FUNCTIONS_H 1
  26. #include <bits/c++config.h>
  27. #include <cstddef> // for ptrdiff_t
  28. #include <bits/stl_iterator_base_types.h> // for iterator_traits, categories
  29. #include <bits/cpp_type_traits.h> // for __is_integer
  30. namespace __gnu_debug
  31. {
  32. template<typename _Iterator, typename _Sequence>
  33. class _Safe_iterator;
  34. // An arbitrary iterator pointer is not singular.
  35. inline bool
  36. __check_singular_aux(const void*) { return false; }
  37. // We may have an iterator that derives from _Safe_iterator_base but isn't
  38. // a _Safe_iterator.
  39. template<typename _Iterator>
  40. inline bool
  41. __check_singular(_Iterator& __x)
  42. { return __check_singular_aux(&__x); }
  43. /** Non-NULL pointers are nonsingular. */
  44. template<typename _Tp>
  45. inline bool
  46. __check_singular(const _Tp* __ptr)
  47. { return __ptr == 0; }
  48. /** Safe iterators know if they are singular. */
  49. template<typename _Iterator, typename _Sequence>
  50. inline bool
  51. __check_singular(const _Safe_iterator<_Iterator, _Sequence>& __x)
  52. { return __x._M_singular(); }
  53. /** Assume that some arbitrary iterator is dereferenceable, because we
  54. can't prove that it isn't. */
  55. template<typename _Iterator>
  56. inline bool
  57. __check_dereferenceable(_Iterator&)
  58. { return true; }
  59. /** Non-NULL pointers are dereferenceable. */
  60. template<typename _Tp>
  61. inline bool
  62. __check_dereferenceable(const _Tp* __ptr)
  63. { return __ptr; }
  64. /** Safe iterators know if they are singular. */
  65. template<typename _Iterator, typename _Sequence>
  66. inline bool
  67. __check_dereferenceable(const _Safe_iterator<_Iterator, _Sequence>& __x)
  68. { return __x._M_dereferenceable(); }
  69. /** If the distance between two random access iterators is
  70. * nonnegative, assume the range is valid.
  71. */
  72. template<typename _RandomAccessIterator>
  73. inline bool
  74. __valid_range_aux2(const _RandomAccessIterator& __first,
  75. const _RandomAccessIterator& __last,
  76. std::random_access_iterator_tag)
  77. { return __last - __first >= 0; }
  78. /** Can't test for a valid range with input iterators, because
  79. * iteration may be destructive. So we just assume that the range
  80. * is valid.
  81. */
  82. template<typename _InputIterator>
  83. inline bool
  84. __valid_range_aux2(const _InputIterator&, const _InputIterator&,
  85. std::input_iterator_tag)
  86. { return true; }
  87. /** We say that integral types for a valid range, and defer to other
  88. * routines to realize what to do with integral types instead of
  89. * iterators.
  90. */
  91. template<typename _Integral>
  92. inline bool
  93. __valid_range_aux(const _Integral&, const _Integral&, std::__true_type)
  94. { return true; }
  95. /** We have iterators, so figure out what kind of iterators that are
  96. * to see if we can check the range ahead of time.
  97. */
  98. template<typename _InputIterator>
  99. inline bool
  100. __valid_range_aux(const _InputIterator& __first,
  101. const _InputIterator& __last, std::__false_type)
  102. {
  103. typedef typename std::iterator_traits<_InputIterator>::iterator_category
  104. _Category;
  105. return __valid_range_aux2(__first, __last, _Category());
  106. }
  107. /** Don't know what these iterators are, or if they are even
  108. * iterators (we may get an integral type for InputIterator), so
  109. * see if they are integral and pass them on to the next phase
  110. * otherwise.
  111. */
  112. template<typename _InputIterator>
  113. inline bool
  114. __valid_range(const _InputIterator& __first, const _InputIterator& __last)
  115. {
  116. typedef typename std::__is_integer<_InputIterator>::__type _Integral;
  117. return __valid_range_aux(__first, __last, _Integral());
  118. }
  119. /** Safe iterators know how to check if they form a valid range. */
  120. template<typename _Iterator, typename _Sequence>
  121. inline bool
  122. __valid_range(const _Safe_iterator<_Iterator, _Sequence>& __first,
  123. const _Safe_iterator<_Iterator, _Sequence>& __last)
  124. { return __first._M_valid_range(__last); }
  125. /* Checks that [first, last) is a valid range, and then returns
  126. * __first. This routine is useful when we can't use a separate
  127. * assertion statement because, e.g., we are in a constructor.
  128. */
  129. template<typename _InputIterator>
  130. inline _InputIterator
  131. __check_valid_range(const _InputIterator& __first,
  132. const _InputIterator& __last
  133. __attribute__((__unused__)))
  134. {
  135. _GLIBCXX_DEBUG_ASSERT(__valid_range(__first, __last));
  136. return __first;
  137. }
  138. /** Checks that __s is non-NULL or __n == 0, and then returns __s. */
  139. template<typename _CharT, typename _Integer>
  140. inline const _CharT*
  141. __check_string(const _CharT* __s,
  142. const _Integer& __n __attribute__((__unused__)))
  143. {
  144. #ifdef _GLIBCXX_DEBUG_PEDANTIC
  145. _GLIBCXX_DEBUG_ASSERT(__s != 0 || __n == 0);
  146. #endif
  147. return __s;
  148. }
  149. /** Checks that __s is non-NULL and then returns __s. */
  150. template<typename _CharT>
  151. inline const _CharT*
  152. __check_string(const _CharT* __s)
  153. {
  154. #ifdef _GLIBCXX_DEBUG_PEDANTIC
  155. _GLIBCXX_DEBUG_ASSERT(__s != 0);
  156. #endif
  157. return __s;
  158. }
  159. // Can't check if an input iterator sequence is sorted, because we
  160. // can't step through the sequence.
  161. template<typename _InputIterator>
  162. inline bool
  163. __check_sorted_aux(const _InputIterator&, const _InputIterator&,
  164. std::input_iterator_tag)
  165. { return true; }
  166. // Can verify if a forward iterator sequence is in fact sorted using
  167. // std::__is_sorted
  168. template<typename _ForwardIterator>
  169. inline bool
  170. __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
  171. std::forward_iterator_tag)
  172. {
  173. if (__first == __last)
  174. return true;
  175. _ForwardIterator __next = __first;
  176. for (++__next; __next != __last; __first = __next, ++__next)
  177. if (*__next < *__first)
  178. return false;
  179. return true;
  180. }
  181. // Can't check if an input iterator sequence is sorted, because we can't step
  182. // through the sequence.
  183. template<typename _InputIterator, typename _Predicate>
  184. inline bool
  185. __check_sorted_aux(const _InputIterator&, const _InputIterator&,
  186. _Predicate, std::input_iterator_tag)
  187. { return true; }
  188. // Can verify if a forward iterator sequence is in fact sorted using
  189. // std::__is_sorted
  190. template<typename _ForwardIterator, typename _Predicate>
  191. inline bool
  192. __check_sorted_aux(_ForwardIterator __first, _ForwardIterator __last,
  193. _Predicate __pred, std::forward_iterator_tag)
  194. {
  195. if (__first == __last)
  196. return true;
  197. _ForwardIterator __next = __first;
  198. for (++__next; __next != __last; __first = __next, ++__next)
  199. if (__pred(*__next, *__first))
  200. return false;
  201. return true;
  202. }
  203. // Determine if a sequence is sorted.
  204. template<typename _InputIterator>
  205. inline bool
  206. __check_sorted(const _InputIterator& __first, const _InputIterator& __last)
  207. {
  208. typedef typename std::iterator_traits<_InputIterator>::iterator_category
  209. _Category;
  210. // Verify that the < operator for elements in the sequence is a
  211. // StrictWeakOrdering by checking that it is irreflexive.
  212. _GLIBCXX_DEBUG_ASSERT(__first == __last || !(*__first < *__first));
  213. return __check_sorted_aux(__first, __last, _Category());
  214. }
  215. template<typename _InputIterator, typename _Predicate>
  216. inline bool
  217. __check_sorted(const _InputIterator& __first, const _InputIterator& __last,
  218. _Predicate __pred)
  219. {
  220. typedef typename std::iterator_traits<_InputIterator>::iterator_category
  221. _Category;
  222. // Verify that the predicate is StrictWeakOrdering by checking that it
  223. // is irreflexive.
  224. _GLIBCXX_DEBUG_ASSERT(__first == __last || !__pred(*__first, *__first));
  225. return __check_sorted_aux(__first, __last, __pred, _Category());
  226. }
  227. template<typename _InputIterator>
  228. inline bool
  229. __check_sorted_set_aux(const _InputIterator& __first,
  230. const _InputIterator& __last,
  231. std::__true_type)
  232. { return __check_sorted(__first, __last); }
  233. template<typename _InputIterator>
  234. inline bool
  235. __check_sorted_set_aux(const _InputIterator&,
  236. const _InputIterator&,
  237. std::__false_type)
  238. { return true; }
  239. template<typename _InputIterator, typename _Predicate>
  240. inline bool
  241. __check_sorted_set_aux(const _InputIterator& __first,
  242. const _InputIterator& __last,
  243. _Predicate __pred, std::__true_type)
  244. { return __check_sorted(__first, __last, __pred); }
  245. template<typename _InputIterator, typename _Predicate>
  246. inline bool
  247. __check_sorted_set_aux(const _InputIterator&,
  248. const _InputIterator&, _Predicate,
  249. std::__false_type)
  250. { return true; }
  251. // ... special variant used in std::merge, std::includes, std::set_*.
  252. template<typename _InputIterator1, typename _InputIterator2>
  253. inline bool
  254. __check_sorted_set(const _InputIterator1& __first,
  255. const _InputIterator1& __last,
  256. const _InputIterator2&)
  257. {
  258. typedef typename std::iterator_traits<_InputIterator1>::value_type
  259. _ValueType1;
  260. typedef typename std::iterator_traits<_InputIterator2>::value_type
  261. _ValueType2;
  262. typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
  263. _SameType;
  264. return __check_sorted_set_aux(__first, __last, _SameType());
  265. }
  266. template<typename _InputIterator1, typename _InputIterator2,
  267. typename _Predicate>
  268. inline bool
  269. __check_sorted_set(const _InputIterator1& __first,
  270. const _InputIterator1& __last,
  271. const _InputIterator2&, _Predicate __pred)
  272. {
  273. typedef typename std::iterator_traits<_InputIterator1>::value_type
  274. _ValueType1;
  275. typedef typename std::iterator_traits<_InputIterator2>::value_type
  276. _ValueType2;
  277. typedef typename std::__are_same<_ValueType1, _ValueType2>::__type
  278. _SameType;
  279. return __check_sorted_set_aux(__first, __last, __pred, _SameType());
  280. }
  281. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  282. // 270. Binary search requirements overly strict
  283. // Determine if a sequence is partitioned w.r.t. this element.
  284. template<typename _ForwardIterator, typename _Tp>
  285. inline bool
  286. __check_partitioned_lower(_ForwardIterator __first,
  287. _ForwardIterator __last, const _Tp& __value)
  288. {
  289. while (__first != __last && *__first < __value)
  290. ++__first;
  291. while (__first != __last && !(*__first < __value))
  292. ++__first;
  293. return __first == __last;
  294. }
  295. template<typename _ForwardIterator, typename _Tp>
  296. inline bool
  297. __check_partitioned_upper(_ForwardIterator __first,
  298. _ForwardIterator __last, const _Tp& __value)
  299. {
  300. while (__first != __last && !(__value < *__first))
  301. ++__first;
  302. while (__first != __last && __value < *__first)
  303. ++__first;
  304. return __first == __last;
  305. }
  306. // Determine if a sequence is partitioned w.r.t. this element.
  307. template<typename _ForwardIterator, typename _Tp, typename _Pred>
  308. inline bool
  309. __check_partitioned_lower(_ForwardIterator __first,
  310. _ForwardIterator __last, const _Tp& __value,
  311. _Pred __pred)
  312. {
  313. while (__first != __last && bool(__pred(*__first, __value)))
  314. ++__first;
  315. while (__first != __last && !bool(__pred(*__first, __value)))
  316. ++__first;
  317. return __first == __last;
  318. }
  319. template<typename _ForwardIterator, typename _Tp, typename _Pred>
  320. inline bool
  321. __check_partitioned_upper(_ForwardIterator __first,
  322. _ForwardIterator __last, const _Tp& __value,
  323. _Pred __pred)
  324. {
  325. while (__first != __last && !bool(__pred(__value, *__first)))
  326. ++__first;
  327. while (__first != __last && bool(__pred(__value, *__first)))
  328. ++__first;
  329. return __first == __last;
  330. }
  331. } // namespace __gnu_debug
  332. #endif