#include <amsmathutil25/amsmathutil25.hpp>

namespace ams
{

void amsarray_permutation_identity_tf(
    int threadnum,
    int nthreads,
    amsarray<amsarray_size_t> *ret
)
{
    amsarray_size_t I,I0,I1,Is,N;
    N = ret->length;
    N = (N<=0) ? 0 : N;
    Is = (nthreads>=1) ? N/nthreads : N;
    I0 = Is*(threadnum);
    I1 = (threadnum<nthreads-1) ? Is*(threadnum+1) : N;

    for(I=I0;I<I1;I++)
    {
        ret->data[I] = I;
    }


    return;
}

// {0, 1, 2, .... N}
amsarray<amsarray_size_t> permutation_identity(amsarray_size_t _length)
{
    amsarray<amsarray_size_t> ret;
    int res;
    amsarray_size_t psize = _length;
    amsarray_size_t I;
    
    res = ret.resize(psize);
    if(res!=amsarray_success)
    {
        ret.resize(0);
        return ret;
    }

    if(psize<amsmathutil25_threadpsz)
    {
        for(I=0;I<psize;I++)
        {
            ret[I] = I;
        }
    }
    else
    {
        threaded_execute(
            &amsarray_permutation_identity_tf,
            _length,
            &ret
        );
    }

    return ret;
}

void amsarray_permutation_swap(amsarray<amsarray_size_t> *permutation, amsarray_size_t I, amsarray_size_t J)
{
    amsarray_size_t tmp;
    tmp = permutation->data[I];
    permutation->data[I] = permutation->data[J];
    permutation->data[J] = tmp;
    return;
}

};