/*_____________________________________
vecteurEntier
par Martin Robinson
_______________________________________
preprocesseur*/

#include "vecteurEntier.hpp"

/*_____________________________________
methode*/

//recherche d'un entier dans le vecteur
int vecteurEntier::cherche(int recherche)const
{
    //encore la magie de STD algorithm
//    int index = int(find(begin(),end(),recherche)-begin()+1);
//    if (index == end()-begin())
//        return -1;
//    return index;
    for (int i=0;i<size();i++)
        if (get(i) == recherche)
            return i;
    return 0;
}

int vecteurEntier::sum()
{
    int accum = 0;
    for (int i=0;i<size();i++)
        accum += get(i);
    return accum;
}

bool vecteurEntier::isAsc()
{
    for (int i=1;i<size();i++)
        if (at(i-1) > get(i))
            return false;
    return true;
}

bool vecteurEntier::isSymetricR(const vecteurEntier& autre, int index)
{
    if(get(index) != autre.get(size()-index-1))
        return false;
    else if (index+1 < size())
        return isSymetricR(autre,index+1);
    return true;
}

bool vecteurEntier::isSymetricR(const vecteurEntier& autre)
{
    if (size() == autre.size())
        return isSymetricR(autre, 0);
    return false;
}

bool vecteurEntier::isAscR(int index)
{
    if (at(index) < get(index-1))
        return false;
    if (index+1 >= size())
        return true;
    return isAscR(index+1);
}

bool vecteurEntier::isAscR()
{
    if (size() > 1)
        return isAscR(1);
}

//triage croissant du vecteur
void vecteurEntier::trie()
{
//    sort(begin(), end());   //std algorithm magic
    //besoin d'un vrai algo,. voici gnomesort
    int j,      //index à rebour
        tmp;    //espace temporaire pour swap
	for (int i=1;i<size();i++)
	{
	    j = i;
	    while (j && get(j) < get(j-1))
	    {
	        tmp = get(j);
	        at(j) = get(j-1);
	        at(j-1)=tmp;
	        j--;
	    }
	}
}

void vecteurEntier::compact()
{
    int tmpSize = size(),
        tmpSwap;
    for (int i=0;i<tmpSize;i++)
        for (int j=i+1;j<tmpSize;j++)
            if (get(i) == get(j))
            {
                tmpSwap = get(tmpSize-1);
                at(tmpSize-1) = get(i);
                at(i) = tmpSwap;
                tmpSize--;
                j=i+1;
            }
    resize(tmpSize);
}

void vecteurEntier::invR(int r)
{
    int tmpSwap;
    if (r > size()/2)
        return;
    tmpSwap = get(r);
    at(r) = get(size()-1-r);
    at(size()-1-r) = tmpSwap;
    invR(r+1);
}

void vecteurEntier::invR()
{
    invR(0);
}

//affichage du contenu du vecteur
void vecteurEntier::affiche(ostream& out)const
{
//    for (int i=0;i<size();i++)
//        out << at(i) << " ";
//    out << endl << endl;
    print(out);
}

//surcharge de cout
ostream& operator<<(ostream& out, const vecteurEntier& v)
{
    v.affiche(out);
    return out;
}

vecteurEntier concatAsc(vecteurEntier& a, vecteurEntier& b)
{
    vecteurEntier vtmp;
    vtmp.resize(a.size()+b.size());
    int i,
        j,
        tmpSwap;
    for (i=0;i<a.size()+b.size();i++)
    {
        if (i < a.size())
            vtmp.at(i) = a.get(i);
        else
            vtmp.at(i) = b.get(i-a.size());
        if (i)
        {
            j = i;
            while (j && vtmp.get(j) < vtmp.get(j-1))
            {
                tmpSwap = vtmp.get(j);
                vtmp.at(j) = vtmp.get(j-1);
                vtmp.at(j-1) = tmpSwap;
                j--;
            }
        }
    }
    return vtmp;
}
