gsl_algorithm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_ALGORITHM_H
  17. #define GSL_ALGORITHM_H
  18. #include <gsl/gsl_assert> // for Expects
  19. #include <gsl/span> // for dynamic_extent, span
  20. #include <algorithm> // for copy_n
  21. #include <cstddef> // for ptrdiff_t
  22. #include <type_traits> // for is_assignable
  23. #ifdef _MSC_VER
  24. #pragma warning(push)
  25. // turn off some warnings that are noisy about our Expects statements
  26. #pragma warning(disable : 4127) // conditional expression is constant
  27. #pragma warning(disable : 4996) // unsafe use of std::copy_n
  28. #endif // _MSC_VER
  29. namespace gsl
  30. {
  31. // Note: this will generate faster code than std::copy using span iterator in older msvc+stl
  32. // not necessary for msvc since VS2017 15.8 (_MSC_VER >= 1915)
  33. template <class SrcElementType, std::size_t SrcExtent, class DestElementType,
  34. std::size_t DestExtent>
  35. void copy(span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest)
  36. {
  37. static_assert(std::is_assignable<decltype(*dest.data()), decltype(*src.data())>::value,
  38. "Elements of source span can not be assigned to elements of destination span");
  39. static_assert(SrcExtent == dynamic_extent || DestExtent == dynamic_extent ||
  40. (SrcExtent <= DestExtent),
  41. "Source range is longer than target range");
  42. Expects(dest.size() >= src.size());
  43. GSL_SUPPRESS(stl.1) // NO-FORMAT: attribute
  44. std::copy_n(src.data(), src.size(), dest.data());
  45. }
  46. } // namespace gsl
  47. #ifdef _MSC_VER
  48. #pragma warning(pop)
  49. #endif // _MSC_VER
  50. #endif // GSL_ALGORITHM_H