#include "Slicer.h"

Slicer::Slicer():
    _idxValid(false)
  , _atEnd(false)
{}

void Slicer::setOverlap(uint32_t overlap)
{
    _idxValid = false;
    _overlap = overlap;
}

void Slicer::setN(uint32_t N)
{
    _idxValid = false;
    _N = N > 0 ? N : 1;
}

void Slicer::setStartIndex(int idxStart)
{
    _idxValid = false;
    _idxStart = idxStart;
}

uint32_t Slicer::overlap()
{
    return _overlap;
}

uint32_t Slicer::N()
{
    return _N;
}

int Slicer::startIndex()
{
    return _idxStart;
}

void Slicer::_updateIdx(std::size_t size)
{
    _idxValid = true;
    _atEnd    = false;
    if ( _idxStart >= long(size)-1 ) {
        _idxStart   = long(size)-1;
        _idxEnd     = _idxStart;
        _idxNext    = _idxStart;
        _atEnd      = true;
        return;
    }

    _idxStart = _idxStart < 0 ? 0 : _idxStart;

    _idxEnd = _idxStart + _N - 1;
    _idxEnd = _idxEnd < long(size) ? _idxEnd : size-1;

    _idxNext = _idxEnd + 1 - _overlap;
    _idxNext = _idxNext < 0    ? 0        : _idxNext;
    _idxNext = _idxNext < long(size) ? _idxNext : size-1;

    _idxPrevious = _idxStart - 1 + _overlap;
    _idxPrevious = _idxPrevious < 0    ? 0            : _idxPrevious;
    _idxPrevious = _idxPrevious < long(size) ? _idxPrevious : size-1;
}
