template <class T>
inline void CStackArray <T>::resize (const int NewSize,
const bool NoTrunc = true)
throw (CException)
{
if (NewSize < m_NbreElem && NoTrunc)
throw CException (CstNoTrunc,
"Troncature impossible");
T * Array = new T [NewSize];
// non demandé :
if (!Array) throw CException (CstPbMemoire, "Problème mémoire");
for (unsigned i = m_NbreElem = min (m_NbreElem,
NewSize); i--; )
Array [i] = m_Array
[i];
delete [] m_Array;
m_Array = Array;
m_Size = NewSize;
} // resize()
#include <algorithm> // si utilisation de la fonction générique swap()
template <class T>
inline CStackArray <T> & CStackArray <T>::swap (CStackArray
& AutrePile)
throw ()
{
if (this == &AutrePile) return *this; //
ou return; si type de retour == void
swap (m_Array, AutrePile.m_Array);
swap (m_NbreElem, AutrePile.m_NbreElem);
swap (m_Size, AutrePile.m_Size);
/** / plus long à écrire :
T * Array
= m_Array;
m_Array
= AutrePile.m_Array;
AutrePile.m_Array = Array;
unsigned NbreElem = m_NbreElem;
m_NbreElem
= AutrePile.m_NbreElem;
AutrePile.m_NbreElem = NbreElem;
unsigned Size = m_Size;
m_Size
= AutrePile.m_Size;
AutrePile.m_Size = Size;
return *this; // ou rien si type de retour == void
} // swap()
template <class T>
inline void CStackArray <T>::swap (void) throw (CException)
{
if (m_NbreElem < 2) throw CException (CstNoSwap,
"Swap impossible");
swap (m_Array [m_NbreElem - 1], m_Array [m_NbreElem - 1]);
/** / ou :
T Sommet = m_Array [m_NbreElem - 1];
m_Array [m_NbreElem - 1] = m_Array [m_NbreElem
- 2]
m_Array [m_NbreElem - 2] = Sommet;
/**/
/** / ou, plus long mais plus facile :
T Sommet = Pop ();
T Dessous = Pop ();
Push (Sommet);
Push (Dessous);
/**/
} // swap()