slice.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #include <boost/python.hpp>
  2. #include <boost/python/slice.hpp>
  3. #include <boost/python/str.hpp>
  4. #include <vector>
  5. // Copyright (c) 2004 Jonathan Brandmeyer
  6. // Use, modification and distribution are subject to the
  7. // Boost Software License, Version 1.0. (See accompanying file
  8. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. using namespace boost::python;
  10. #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x580)) || BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1500))
  11. # define make_tuple boost::python::make_tuple
  12. #endif
  13. // These checks are only valid under Python 2.3
  14. // (rich slicing wasn't supported for builtins under Python 2.2)
  15. bool check_string_rich_slice()
  16. {
  17. object s("hello, world");
  18. // default slice
  19. if (s[slice()] != "hello, world")
  20. return false;
  21. // simple reverse
  22. if (s[slice(_,_,-1)] != "dlrow ,olleh")
  23. return false;
  24. // reverse with mixed-sign offsets
  25. if (s[slice(-6,1,-1)] != " ,oll")
  26. return false;
  27. // all of the object.cpp check_string_slice() checks should work
  28. // with the form that omits the step argument.
  29. if (s[slice(_,-3)] != "hello, wo")
  30. return false;
  31. if (s[slice(-3,_)] != "rld")
  32. return false;
  33. if (", " != s[slice(5,7)])
  34. return false;
  35. return s[slice(2,-1)][slice(1,-1)] == "lo, wor";
  36. }
  37. // Tried to get more info into the error message (actual array
  38. // contents) but Numeric complains that treating an array as a boolean
  39. // value doesn't make any sense.
  40. #define ASSERT_EQUAL( e1, e2 ) \
  41. if (!all((e1) == (e2))) \
  42. return "assertion failed: " #e1 " == " #e2 "\nLHS:\n%s\nRHS:\n%s" % make_tuple(e1,e2); \
  43. else
  44. // Verify functions accepting a slice argument can be called
  45. bool accept_slice( slice) { return true; }
  46. #if BOOST_WORKAROUND( BOOST_MSVC, BOOST_TESTED_AT(1400)) \
  47. || BOOST_WORKAROUND( BOOST_INTEL_WIN, == 710)
  48. int check_slice_get_indices(slice index);
  49. #endif
  50. int check_slice_get_indices(
  51. #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
  52. const
  53. #endif
  54. slice index)
  55. {
  56. // A vector of integers from [-5, 5].
  57. std::vector<int> coll(11);
  58. typedef std::vector<int>::iterator coll_iterator;
  59. for (coll_iterator i = coll.begin(); i != coll.end(); ++i) {
  60. *i = i - coll.begin() - 5;
  61. }
  62. slice::range<std::vector<int>::iterator> bounds;
  63. try {
  64. bounds = index.get_indices(coll.begin(), coll.end());
  65. }
  66. catch (std::invalid_argument) {
  67. return 0;
  68. }
  69. int sum = 0;
  70. while (bounds.start != bounds.stop) {
  71. sum += *bounds.start;
  72. std::advance( bounds.start, bounds.step);
  73. }
  74. sum += *bounds.start;
  75. return sum;
  76. }
  77. BOOST_PYTHON_MODULE(slice_ext)
  78. {
  79. def( "accept_slice", accept_slice);
  80. def( "check_string_rich_slice", check_string_rich_slice);
  81. def( "check_slice_get_indices", check_slice_get_indices);
  82. }