array.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /* The following code declares class array,
  2. * an STL container (as wrapper) for arrays of constant size.
  3. *
  4. * See
  5. * http://www.boost.org/libs/array/
  6. * for documentation.
  7. *
  8. * The original author site is at: http://www.josuttis.com/
  9. *
  10. * (C) Copyright Nicolai M. Josuttis 2001.
  11. *
  12. * Distributed under the Boost Software License, Version 1.0. (See
  13. * accompanying file LICENSE_1_0.txt or copy at
  14. * http://www.boost.org/LICENSE_1_0.txt)
  15. *
  16. * 14 Apr 2012 - (mtc) Added support for boost::hash
  17. * 28 Dec 2010 - (mtc) Added cbegin and cend (and crbegin and crend) for C++Ox compatibility.
  18. * 10 Mar 2010 - (mtc) fill method added, matching resolution of the standard library working group.
  19. * See <http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#776> or Trac issue #3168
  20. * Eventually, we should remove "assign" which is now a synonym for "fill" (Marshall Clow)
  21. * 10 Mar 2010 - added workaround for SUNCC and !STLPort [trac #3893] (Marshall Clow)
  22. * 29 Jan 2004 - c_array() added, BOOST_NO_PRIVATE_IN_AGGREGATE removed (Nico Josuttis)
  23. * 23 Aug 2002 - fix for Non-MSVC compilers combined with MSVC libraries.
  24. * 05 Aug 2001 - minor update (Nico Josuttis)
  25. * 20 Jan 2001 - STLport fix (Beman Dawes)
  26. * 29 Sep 2000 - Initial Revision (Nico Josuttis)
  27. *
  28. * Jan 29, 2004
  29. */
  30. #ifndef BOOST_ARRAY_HPP
  31. #define BOOST_ARRAY_HPP
  32. #include <boost/detail/workaround.hpp>
  33. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  34. # pragma warning(push)
  35. # pragma warning(disable:4996) // 'std::equal': Function call with parameters that may be unsafe
  36. # pragma warning(disable:4510) // boost::array<T,N>' : default constructor could not be generated
  37. # pragma warning(disable:4610) // warning C4610: class 'boost::array<T,N>' can never be instantiated - user defined constructor required
  38. #endif
  39. #include <cstddef>
  40. #include <stdexcept>
  41. #include <boost/assert.hpp>
  42. #include <boost/swap.hpp>
  43. // Handles broken standard libraries better than <iterator>
  44. #include <boost/detail/iterator.hpp>
  45. #include <boost/throw_exception.hpp>
  46. #include <boost/functional/hash_fwd.hpp>
  47. #include <algorithm>
  48. // FIXES for broken compilers
  49. #include <boost/config.hpp>
  50. namespace boost {
  51. template<class T, std::size_t N>
  52. class array {
  53. public:
  54. T elems[N]; // fixed-size array of elements of type T
  55. public:
  56. // type definitions
  57. typedef T value_type;
  58. typedef T* iterator;
  59. typedef const T* const_iterator;
  60. typedef T& reference;
  61. typedef const T& const_reference;
  62. typedef std::size_t size_type;
  63. typedef std::ptrdiff_t difference_type;
  64. // iterator support
  65. iterator begin() { return elems; }
  66. const_iterator begin() const { return elems; }
  67. const_iterator cbegin() const { return elems; }
  68. iterator end() { return elems+N; }
  69. const_iterator end() const { return elems+N; }
  70. const_iterator cend() const { return elems+N; }
  71. // reverse iterator support
  72. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
  73. typedef std::reverse_iterator<iterator> reverse_iterator;
  74. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  75. #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
  76. // workaround for broken reverse_iterator in VC7
  77. typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
  78. reference, iterator, reference> > reverse_iterator;
  79. typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
  80. const_reference, iterator, reference> > const_reverse_iterator;
  81. #elif defined(_RWSTD_NO_CLASS_PARTIAL_SPEC)
  82. typedef std::reverse_iterator<iterator, std::random_access_iterator_tag,
  83. value_type, reference, iterator, difference_type> reverse_iterator;
  84. typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag,
  85. value_type, const_reference, const_iterator, difference_type> const_reverse_iterator;
  86. #else
  87. // workaround for broken reverse_iterator implementations
  88. typedef std::reverse_iterator<iterator,T> reverse_iterator;
  89. typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
  90. #endif
  91. reverse_iterator rbegin() { return reverse_iterator(end()); }
  92. const_reverse_iterator rbegin() const {
  93. return const_reverse_iterator(end());
  94. }
  95. const_reverse_iterator crbegin() const {
  96. return const_reverse_iterator(end());
  97. }
  98. reverse_iterator rend() { return reverse_iterator(begin()); }
  99. const_reverse_iterator rend() const {
  100. return const_reverse_iterator(begin());
  101. }
  102. const_reverse_iterator crend() const {
  103. return const_reverse_iterator(begin());
  104. }
  105. // operator[]
  106. reference operator[](size_type i)
  107. {
  108. BOOST_ASSERT_MSG( i < N, "out of range" );
  109. return elems[i];
  110. }
  111. const_reference operator[](size_type i) const
  112. {
  113. BOOST_ASSERT_MSG( i < N, "out of range" );
  114. return elems[i];
  115. }
  116. // at() with range check
  117. reference at(size_type i) { rangecheck(i); return elems[i]; }
  118. const_reference at(size_type i) const { rangecheck(i); return elems[i]; }
  119. // front() and back()
  120. reference front()
  121. {
  122. return elems[0];
  123. }
  124. const_reference front() const
  125. {
  126. return elems[0];
  127. }
  128. reference back()
  129. {
  130. return elems[N-1];
  131. }
  132. const_reference back() const
  133. {
  134. return elems[N-1];
  135. }
  136. // size is constant
  137. static size_type size() { return N; }
  138. static bool empty() { return false; }
  139. static size_type max_size() { return N; }
  140. enum { static_size = N };
  141. // swap (note: linear complexity)
  142. void swap (array<T,N>& y) {
  143. for (size_type i = 0; i < N; ++i)
  144. boost::swap(elems[i],y.elems[i]);
  145. }
  146. // direct access to data (read-only)
  147. const T* data() const { return elems; }
  148. T* data() { return elems; }
  149. // use array as C array (direct read/write access to data)
  150. T* c_array() { return elems; }
  151. // assignment with type conversion
  152. template <typename T2>
  153. array<T,N>& operator= (const array<T2,N>& rhs) {
  154. std::copy(rhs.begin(),rhs.end(), begin());
  155. return *this;
  156. }
  157. // assign one value to all elements
  158. void assign (const T& value) { fill ( value ); } // A synonym for fill
  159. void fill (const T& value)
  160. {
  161. std::fill_n(begin(),size(),value);
  162. }
  163. // check range (may be private because it is static)
  164. static void rangecheck (size_type i) {
  165. if (i >= size()) {
  166. std::out_of_range e("array<>: index out of range");
  167. boost::throw_exception(e);
  168. }
  169. }
  170. };
  171. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION)
  172. template< class T >
  173. class array< T, 0 > {
  174. public:
  175. // type definitions
  176. typedef T value_type;
  177. typedef T* iterator;
  178. typedef const T* const_iterator;
  179. typedef T& reference;
  180. typedef const T& const_reference;
  181. typedef std::size_t size_type;
  182. typedef std::ptrdiff_t difference_type;
  183. // iterator support
  184. iterator begin() { return iterator( reinterpret_cast< T * >( this ) ); }
  185. const_iterator begin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
  186. const_iterator cbegin() const { return const_iterator( reinterpret_cast< const T * >( this ) ); }
  187. iterator end() { return begin(); }
  188. const_iterator end() const { return begin(); }
  189. const_iterator cend() const { return cbegin(); }
  190. // reverse iterator support
  191. #if !defined(BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION) && !defined(BOOST_MSVC_STD_ITERATOR) && !defined(BOOST_NO_STD_ITERATOR_TRAITS)
  192. typedef std::reverse_iterator<iterator> reverse_iterator;
  193. typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
  194. #elif defined(_MSC_VER) && (_MSC_VER == 1300) && defined(BOOST_DINKUMWARE_STDLIB) && (BOOST_DINKUMWARE_STDLIB == 310)
  195. // workaround for broken reverse_iterator in VC7
  196. typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, iterator,
  197. reference, iterator, reference> > reverse_iterator;
  198. typedef std::reverse_iterator<std::_Ptrit<value_type, difference_type, const_iterator,
  199. const_reference, iterator, reference> > const_reverse_iterator;
  200. #elif defined(_RWSTD_NO_CLASS_PARTIAL_SPEC)
  201. typedef std::reverse_iterator<iterator, std::random_access_iterator_tag,
  202. value_type, reference, iterator, difference_type> reverse_iterator;
  203. typedef std::reverse_iterator<const_iterator, std::random_access_iterator_tag,
  204. value_type, const_reference, const_iterator, difference_type> const_reverse_iterator;
  205. #else
  206. // workaround for broken reverse_iterator implementations
  207. typedef std::reverse_iterator<iterator,T> reverse_iterator;
  208. typedef std::reverse_iterator<const_iterator,T> const_reverse_iterator;
  209. #endif
  210. reverse_iterator rbegin() { return reverse_iterator(end()); }
  211. const_reverse_iterator rbegin() const {
  212. return const_reverse_iterator(end());
  213. }
  214. const_reverse_iterator crbegin() const {
  215. return const_reverse_iterator(end());
  216. }
  217. reverse_iterator rend() { return reverse_iterator(begin()); }
  218. const_reverse_iterator rend() const {
  219. return const_reverse_iterator(begin());
  220. }
  221. const_reverse_iterator crend() const {
  222. return const_reverse_iterator(begin());
  223. }
  224. // operator[]
  225. reference operator[](size_type /*i*/)
  226. {
  227. return failed_rangecheck();
  228. }
  229. const_reference operator[](size_type /*i*/) const
  230. {
  231. return failed_rangecheck();
  232. }
  233. // at() with range check
  234. reference at(size_type /*i*/) { return failed_rangecheck(); }
  235. const_reference at(size_type /*i*/) const { return failed_rangecheck(); }
  236. // front() and back()
  237. reference front()
  238. {
  239. return failed_rangecheck();
  240. }
  241. const_reference front() const
  242. {
  243. return failed_rangecheck();
  244. }
  245. reference back()
  246. {
  247. return failed_rangecheck();
  248. }
  249. const_reference back() const
  250. {
  251. return failed_rangecheck();
  252. }
  253. // size is constant
  254. static size_type size() { return 0; }
  255. static bool empty() { return true; }
  256. static size_type max_size() { return 0; }
  257. enum { static_size = 0 };
  258. void swap (array<T,0>& /*y*/) {
  259. }
  260. // direct access to data (read-only)
  261. const T* data() const { return 0; }
  262. T* data() { return 0; }
  263. // use array as C array (direct read/write access to data)
  264. T* c_array() { return 0; }
  265. // assignment with type conversion
  266. template <typename T2>
  267. array<T,0>& operator= (const array<T2,0>& ) {
  268. return *this;
  269. }
  270. // assign one value to all elements
  271. void assign (const T& value) { fill ( value ); }
  272. void fill (const T& ) {}
  273. // check range (may be private because it is static)
  274. static reference failed_rangecheck () {
  275. std::out_of_range e("attempt to access element of an empty array");
  276. boost::throw_exception(e);
  277. #if defined(BOOST_NO_EXCEPTIONS) || (!defined(BOOST_MSVC) && !defined(__PATHSCALE__))
  278. //
  279. // We need to return something here to keep
  280. // some compilers happy: however we will never
  281. // actually get here....
  282. //
  283. static T placeholder;
  284. return placeholder;
  285. #endif
  286. }
  287. };
  288. #endif
  289. // comparisons
  290. template<class T, std::size_t N>
  291. bool operator== (const array<T,N>& x, const array<T,N>& y) {
  292. return std::equal(x.begin(), x.end(), y.begin());
  293. }
  294. template<class T, std::size_t N>
  295. bool operator< (const array<T,N>& x, const array<T,N>& y) {
  296. return std::lexicographical_compare(x.begin(),x.end(),y.begin(),y.end());
  297. }
  298. template<class T, std::size_t N>
  299. bool operator!= (const array<T,N>& x, const array<T,N>& y) {
  300. return !(x==y);
  301. }
  302. template<class T, std::size_t N>
  303. bool operator> (const array<T,N>& x, const array<T,N>& y) {
  304. return y<x;
  305. }
  306. template<class T, std::size_t N>
  307. bool operator<= (const array<T,N>& x, const array<T,N>& y) {
  308. return !(y<x);
  309. }
  310. template<class T, std::size_t N>
  311. bool operator>= (const array<T,N>& x, const array<T,N>& y) {
  312. return !(x<y);
  313. }
  314. // global swap()
  315. template<class T, std::size_t N>
  316. inline void swap (array<T,N>& x, array<T,N>& y) {
  317. x.swap(y);
  318. }
  319. #if defined(__SUNPRO_CC)
  320. // Trac ticket #4757; the Sun Solaris compiler can't handle
  321. // syntax like 'T(&get_c_array(boost::array<T,N>& arg))[N]'
  322. //
  323. // We can't just use this for all compilers, because the
  324. // borland compilers can't handle this form.
  325. namespace detail {
  326. template <typename T, std::size_t N> struct c_array
  327. {
  328. typedef T type[N];
  329. };
  330. }
  331. // Specific for boost::array: simply returns its elems data member.
  332. template <typename T, std::size_t N>
  333. typename detail::c_array<T,N>::type& get_c_array(boost::array<T,N>& arg)
  334. {
  335. return arg.elems;
  336. }
  337. // Specific for boost::array: simply returns its elems data member.
  338. template <typename T, std::size_t N>
  339. typename const detail::c_array<T,N>::type& get_c_array(const boost::array<T,N>& arg)
  340. {
  341. return arg.elems;
  342. }
  343. #else
  344. // Specific for boost::array: simply returns its elems data member.
  345. template <typename T, std::size_t N>
  346. T(&get_c_array(boost::array<T,N>& arg))[N]
  347. {
  348. return arg.elems;
  349. }
  350. // Const version.
  351. template <typename T, std::size_t N>
  352. const T(&get_c_array(const boost::array<T,N>& arg))[N]
  353. {
  354. return arg.elems;
  355. }
  356. #endif
  357. #if 0
  358. // Overload for std::array, assuming that std::array will have
  359. // explicit conversion functions as discussed at the WG21 meeting
  360. // in Summit, March 2009.
  361. template <typename T, std::size_t N>
  362. T(&get_c_array(std::array<T,N>& arg))[N]
  363. {
  364. return static_cast<T(&)[N]>(arg);
  365. }
  366. // Const version.
  367. template <typename T, std::size_t N>
  368. const T(&get_c_array(const std::array<T,N>& arg))[N]
  369. {
  370. return static_cast<T(&)[N]>(arg);
  371. }
  372. #endif
  373. template<class T, std::size_t N>
  374. std::size_t hash_value(const array<T,N>& arr)
  375. {
  376. return boost::hash_range(arr.begin(), arr.end());
  377. }
  378. } /* namespace boost */
  379. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  380. # pragma warning(pop)
  381. #endif
  382. #endif /*BOOST_ARRAY_HPP*/