Esempio n. 1
0
		static ImmutableTreeVector<T> slice(ImmutableTreeVector<T>& in, boost::python::slice s)
			{
			int32_t start = boost::python::extract<int32_t>(s.start());
			if (start < 0)
				start = in.size() - start;

			int32_t stop = boost::python::extract<int32_t>(s.stop());
			if (stop < 0)
				stop = in.size() - stop;

			return in.slice(start, stop);
			}
Esempio n. 2
0
static boost::python::list getSlice( Set &s, boost::python::slice sl )
{
	Py_ssize_t start, stop, step, length;
	if( PySlice_GetIndicesEx( (PySliceObject *)sl.ptr(), s.size(), &start, &stop, &step, &length ) )
	{
		boost::python::throw_error_already_set();
	}

	boost::python::list result;
	for( Py_ssize_t i = start; i < stop; i++ )
	{
		result.append( Set::MemberPtr( s.member( i ) ) );
	}
	return result;
}
Esempio n. 3
0
  adapted_slice::adapted_slice(boost::python::slice const& sl, std::size_t sz)
  :
    step(1),
    size(0)
  {
    static const boost::python::slice_nil slice_nil = boost::python::slice_nil();

    long signed_sz = static_cast<long>(sz);
    if (sl.step() != slice_nil) {
      step = boost::python::extract<long>(sl.step());
    }
    if (sl.start() == slice_nil) {
      start = step < 0 ? signed_sz-1 : 0;
    }
    else {
      start = boost::python::extract<long>(sl.start());
      if (start < 0) start += signed_sz;
    }
    if (sl.stop() == slice_nil) {
      stop = step < 0 ? -1 : signed_sz;
    }
    else {
      stop = boost::python::extract<long>(sl.stop());
      if (stop < 0) stop += signed_sz;
    }
    if (start > signed_sz-1) start = signed_sz;
    if (start < 0) start = 0;
    if      (stop < -1) stop = -1;
    else if (stop > signed_sz) stop = signed_sz;
    SCITBX_ASSERT(step != 0 || stop == start);
    long signed_size = stop - start + step;
    if      (step < 0) signed_size++;
    else if (step > 0) signed_size--;
    else {
      signed_size = 0;
      step = 1;
    }
    signed_size /= step;
    if (signed_size < 0) signed_size = 0;
    size = static_cast<std::size_t>(signed_size);
    stop = start + step * size;
  }