span_ext 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
  4. //
  5. // This code is licensed under the MIT License (MIT).
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  8. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  9. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  10. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  11. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  12. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  13. // THE SOFTWARE.
  14. //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. #ifndef GSL_SPAN_EXT_H
  17. #define GSL_SPAN_EXT_H
  18. ///////////////////////////////////////////////////////////////////////////////
  19. //
  20. // File: span_ext
  21. // Purpose: continue offering features that have been cut from the official
  22. // implementation of span.
  23. // While modernizing gsl::span a number of features needed to be removed to
  24. // be compliant with the design of std::span
  25. //
  26. ///////////////////////////////////////////////////////////////////////////////
  27. #include <gsl/gsl_util> // for narrow_cast, narrow
  28. #include <gsl/span> // for span
  29. #include <algorithm> // for lexicographical_compare
  30. #include <cstddef> // for ptrdiff_t, size_t
  31. #include <utility>
  32. namespace gsl
  33. {
  34. // [span.comparison], span comparison operators
  35. template <class ElementType, std::size_t FirstExtent, std::size_t SecondExtent>
  36. constexpr bool operator==(span<ElementType, FirstExtent> l, span<ElementType, SecondExtent> r)
  37. {
  38. return std::equal(l.begin(), l.end(), r.begin(), r.end());
  39. }
  40. template <class ElementType, std::size_t Extent>
  41. constexpr bool operator!=(span<ElementType, Extent> l, span<ElementType, Extent> r)
  42. {
  43. return !(l == r);
  44. }
  45. template <class ElementType, std::size_t Extent>
  46. constexpr bool operator<(span<ElementType, Extent> l, span<ElementType, Extent> r)
  47. {
  48. return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
  49. }
  50. template <class ElementType, std::size_t Extent>
  51. constexpr bool operator<=(span<ElementType, Extent> l, span<ElementType, Extent> r)
  52. {
  53. return !(l > r);
  54. }
  55. template <class ElementType, std::size_t Extent>
  56. constexpr bool operator>(span<ElementType, Extent> l, span<ElementType, Extent> r)
  57. {
  58. return r < l;
  59. }
  60. template <class ElementType, std::size_t Extent>
  61. constexpr bool operator>=(span<ElementType, Extent> l, span<ElementType, Extent> r)
  62. {
  63. return !(l < r);
  64. }
  65. //
  66. // make_span() - Utility functions for creating spans
  67. //
  68. template <class ElementType>
  69. constexpr span<ElementType> make_span(ElementType* ptr, typename span<ElementType>::size_type count)
  70. {
  71. return span<ElementType>(ptr, count);
  72. }
  73. template <class ElementType>
  74. constexpr span<ElementType> make_span(ElementType* firstElem, ElementType* lastElem)
  75. {
  76. return span<ElementType>(firstElem, lastElem);
  77. }
  78. template <class ElementType, std::size_t N>
  79. constexpr span<ElementType, N> make_span(ElementType (&arr)[N]) noexcept
  80. {
  81. return span<ElementType, N>(arr);
  82. }
  83. template <class Container>
  84. constexpr span<typename Container::value_type> make_span(Container& cont)
  85. {
  86. return span<typename Container::value_type>(cont);
  87. }
  88. template <class Container>
  89. constexpr span<const typename Container::value_type> make_span(const Container& cont)
  90. {
  91. return span<const typename Container::value_type>(cont);
  92. }
  93. template <class Ptr>
  94. constexpr span<typename Ptr::element_type> make_span(Ptr& cont, std::size_t count)
  95. {
  96. return span<typename Ptr::element_type>(cont, count);
  97. }
  98. template <class Ptr>
  99. constexpr span<typename Ptr::element_type> make_span(Ptr& cont)
  100. {
  101. return span<typename Ptr::element_type>(cont);
  102. }
  103. // Specialization of gsl::at for span
  104. template <class ElementType, std::size_t Extent>
  105. constexpr ElementType& at(span<ElementType, Extent> s, index i)
  106. {
  107. // No bounds checking here because it is done in span::operator[] called below
  108. Ensures(i >= 0);
  109. return s[narrow_cast<std::size_t>(i)];
  110. }
  111. // [span.obs] Free observer functions
  112. template <class ElementType, std::size_t Extent>
  113. constexpr std::ptrdiff_t ssize(const span<ElementType, Extent>& s) noexcept
  114. {
  115. return static_cast<std::ptrdiff_t>(s.size());
  116. }
  117. // [span.iter] Free functions for begin/end functions
  118. template <class ElementType, std::size_t Extent>
  119. constexpr typename span<ElementType, Extent>::iterator
  120. begin(const span<ElementType, Extent>& s) noexcept
  121. {
  122. return s.begin();
  123. }
  124. template <class ElementType, std::size_t Extent = dynamic_extent>
  125. constexpr typename span<ElementType, Extent>::iterator
  126. end(const span<ElementType, Extent>& s) noexcept
  127. {
  128. return s.end();
  129. }
  130. template <class ElementType, std::size_t Extent>
  131. constexpr typename span<ElementType, Extent>::reverse_iterator
  132. rbegin(const span<ElementType, Extent>& s) noexcept
  133. {
  134. return s.rbegin();
  135. }
  136. template <class ElementType, std::size_t Extent>
  137. constexpr typename span<ElementType, Extent>::reverse_iterator
  138. rend(const span<ElementType, Extent>& s) noexcept
  139. {
  140. return s.rend();
  141. }
  142. template <class ElementType, std::size_t Extent>
  143. constexpr typename span<ElementType, Extent>::iterator
  144. cbegin(const span<ElementType, Extent>& s) noexcept
  145. {
  146. return s.begin();
  147. }
  148. template <class ElementType, std::size_t Extent = dynamic_extent>
  149. constexpr typename span<ElementType, Extent>::iterator
  150. cend(const span<ElementType, Extent>& s) noexcept
  151. {
  152. return s.end();
  153. }
  154. template <class ElementType, std::size_t Extent>
  155. constexpr typename span<ElementType, Extent>::reverse_iterator
  156. crbegin(const span<ElementType, Extent>& s) noexcept
  157. {
  158. return s.rbegin();
  159. }
  160. template <class ElementType, std::size_t Extent>
  161. constexpr typename span<ElementType, Extent>::reverse_iterator
  162. crend(const span<ElementType, Extent>& s) noexcept
  163. {
  164. return s.rend();
  165. }
  166. } // namespace gsl
  167. #endif // GSL_SPAN_EXT_H