Sha256: 4ca5156db2652492309af391be94886dbf9d7fad1f89206c3d4fc462389c379e

Contents?: true

Size: 961 Bytes

Versions: 4

Compression:

Stored size: 961 Bytes

Contents

/* PSF copyright: original by Jim Hugunin and Chris Chase. */

int64_t
ndt_slice_adjust_indices(int64_t length, int64_t *start, int64_t *stop, int64_t step)
{
    /* this is harder to get right than you might think */

    assert(step != 0);
    assert(step >= -INT64_MAX);

    if (*start < 0) {
        *start += length;
        if (*start < 0) {
            *start = (step < 0) ? -1 : 0;
        }
    }
    else if (*start >= length) {
        *start = (step < 0) ? length - 1 : length;
    }

    if (*stop < 0) {
        *stop += length;
        if (*stop < 0) {
            *stop = (step < 0) ? -1 : 0;
        }
    }
    else if (*stop >= length) {
        *stop = (step < 0) ? length - 1 : length;
    }

    if (step < 0) {
        if (*stop < *start) {
            return (*start - *stop - 1) / (-step) + 1;
        }
    }
    else {
        if (*start < *stop) {
            return (*stop - *start - 1) / step + 1;
        }
    }
    return 0;
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ndtypes-0.2.0dev8 ext/ruby_ndtypes/ndtypes/libndtypes/slice.h
ndtypes-0.2.0dev6 ext/ruby_ndtypes/ndtypes/libndtypes/slice.h
ndtypes-0.2.0dev5 ext/ruby_ndtypes/ndtypes/libndtypes/slice.h
ndtypes-0.2.0dev4 ext/ruby_ndtypes/ndtypes/libndtypes/slice.h