Sei sulla pagina 1di 11

#include <stdio.

h>
#define INC_CAPACITY 10
using namespace std;
template<class T>
class Array
{
private:
T** List; // lista cu pointeri la obiecte de tipul T*
int Capacity; // dimensiunea listei de pointeri
int Size; // cate elemente sunt in lista
public:
class ArrayIterator
{
private:
int Current; //indicele elementului curent
(Array<T>)* MyArray; // pointer catre obiectul de tip Array
public:
ArrayIterator()
{
Current = -1;
MyArray = NULL;
}
ArrayIterator(int Index, Array<T> * ParamArray)
{
Current = Index;
MyArray = ParamArray;
}
//prefix
ArrayIterator& operator ++ ()
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//postfix
ArrayIterator& operator ++ (int val)
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//prefix

ArrayIterator& operator -- ()
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
//postfix
ArrayIterator& operator -- (int val)
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
bool operator= (ArrayIterator & ParamIterator)
{
//if (ParamIterator.Current = -1 || ParamIterator.MyArra
y == NULL)return false;
this->Current = ParamIterator.Current;
this->MyArray = ParamIterator.MyArray;
return true;
}
bool operator!=(ArrayIterator & ParamIterator)
{
if (this->Current <= ParamIterator.Current || this->MyAr
ray != ParamIterator.MyArray)return true;
return false;
}
T* GetElement()
{
if (!this->MyArray || this->Current == -1) {
printf("Err: Iterator neinitializat!\n");
return NULL;
}
return &(MyArray->operator[](Current));
}
};
// Lista nu e alocata, Capacity si Size = 0
Array()
{
Capacity = 0;
Size = 0;
List = NULL;
}
// destructor
~Array(){};

// Lista e alocata cu 'capacity' elemente


Array(int capacity)
{
Size = 0;
Capacity = capacity;
List = new T*[capacity];
}
// constructor de copiere
Array(const Array<T>& ParamList)
{
Capacity = ParamList.Capacity;
Size = ParamList.Size;
List = new T*[Size];
for (int i = 0; i < Size; i++)
{
List[i] = ParamList.List[i];
}
}
// arunca exceptie daca index este out of range
T& operator[] (int index)
{
if (index < 0) printf("Err: Invalid Index!");
if (index > this->Size) printf("Err: Index out of SIZE!\n");
if (index > this->Capacity) printf("Err: Index out of CAPACITY!\
n");
return *(List[index]);
}
// daca dimensiunea maxima a fost atinsa creste dimensiunea listei
void IncCapacity()
{
T** Temp = new T*[Capacity+INC_CAPACITY];
for (int i = 0; i < Capacity; i++)
{
Temp [i] = List[i];
}
delete[] List;
List = Temp;
Capacity += INC_CAPACITY;
}
// adauga un element de tipul T la sfarsitul listei si returneaza this
const Array<T>& operator+=(const T& ParamElement)
{
if (Size >= Capacity)this->IncCapacity();
T * Temp = new T;
*Temp = ParamElement;
List[Size++] = Temp;
return (*this);
}
// adauga un element pe pozitia index, retureaza this. Daca index e inva
lid arunca o exceptie
const Array<T>& Insert(int index, const T& ParamElement)
{
if (index < 0)printf("Err: Could not insert, invalid index (too
smal)!\n");

if (index > Size)printf("Err: Could not insert, index out of SIZ


E!\n");
if (Size == Capacity)this->IncCapacity();
Size++;
for (int i=Size; i >index; i--)
{
List[i] = List[i - 1];
}
T * Temp = new T;
*Temp = ParamElement;
List[index] = Temp;
return (*this);
}
// adauga o lista pe pozitia index, retureaza this. Daca index e invalid
arunca o exceptie
const Array<T>& Insert(int index, const Array<T> ParamList)
{
if (index < 0)printf("Err: Could not insert 'List', invalid inde
x (too smal)!\n");
if (index > Size)printf("Err: Could not insert 'List', index out
of SIZE!\n");
for (int i = 0; i < ParamList.Size; i++)
{
this->Insert(index + i, *ParamList.List[i]);
}
return (*this);
}
// sterge un element de pe pozitia index, returneaza this. Daca index e
invalid arunca o exceptie
const Array<T>& Delete(int index)
{
if (index < 0)printf("Err: Could not delete, invalid index (too
smal)!\n");
if (index >= Size)printf("Err: Could not delete, index out of SI
ZE!\n");
for (int i = index; i < Size - 1; i++)
{
List[index] = List[index + 1];
}
Size--;
return (*this);
}
//operator de copiere
bool operator=(const Array<T> & ParamList)
{
this->Size = ParamList.Size;
this->Capacity = ParamList.Capacity;
T** Temp = new T*[Capacity];
List = Temp;
for (int i = 0; i < Size; i++)
{
List[i] = ParamList.List[i];
}
return true;
}

// sorteaza folosind comparatia intre elementele din T


void Sort()
{
for (int i = 0; i < Size - 1; i++)
{
for (int j = i + 1; j < Size; j++)
{
if (*List[i]>*List[j]) {
T* aux = List[i];
List[i] = List[j];
List[j] = aux;
}
}
}
}
//returneaza marimea array-ului
int GetSize()
{
return Size;
}
//returneaza capacitatea array-ului
int GetCapacity()
{
return Capacity;
}
// cauta un element folosind binary search in Array
int BinarySearch(const T& ParamElement)
{
this->Sort();
int Low = 0, Up = Size - 1;
while (Low < Up)
{
if (*List[(Low + Up) / 2] == ParamElement)return (Low +
Up) / 2;
if (ParamElement < *List[(Low + Up) / 2]){
Up = (Low + Up + 1) / 2;
}
else{
Low = (Low + Up + 1) / 2;
}
}
if (ParamElement == *List[Low]) return Low;
return -1;
}
// cauta un element in Array
int Find(const T& ParamElement)
{
for (int i = 0; i < Size; i++){
if (ParamElement == *List[i]) return i;
}
return -1;
}
//returneaza Begin Iterator

ArrayIterator GetBeginIterator()
{
ArrayIterator *Temp = new ArrayIterator(0,this);
return *Temp;
}
//returneaza End Iterator
ArrayIterator GetEndIterator()
{
ArrayIterator *Temp = new ArrayIterator(this->GetSize()-1, this)
;
return *Temp;
}
};
________________________________________________
Lucaniuc Dragos Lucian
________________________________________________
________________________________________________
________________________________________________
#include <stdio.h>
#define INC_CAPACITY 10
using namespace std;
template<class T>
class Array
{
private:
T** List; // lista cu pointeri la obiecte de tipul T*
int Capacity; // dimensiunea listei de pointeri
int Size; // cate elemente sunt in lista
public:
class ArrayIterator
{
private:
int Current; //indicele elementului curent
(Array<T>)* MyArray; // pointer catre obiectul de tip Array
public:
ArrayIterator()
{
Current = -1;
MyArray = NULL;
}
ArrayIterator(int Index, Array<T> * ParamArray)
{
Current = Index;
MyArray = ParamArray;
}

//prefix
ArrayIterator& operator ++ ()
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//postfix
ArrayIterator& operator ++ (int val)
{
if (Current >= MyArray->GetSize()){
printf("Err: Could not [++] \"end\" iterator!\n"
);
return (*this);
}
Current++;
return (*this);
}
//prefix
ArrayIterator& operator -- ()
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
//postfix
ArrayIterator& operator -- (int val)
{
if (MyArray->GetSize() == 0 || Current <= 0){
printf("Err: Could not [--] \"begin\" iterator!\
n");
return (*this);
}
Current--;
return (*this);
}
bool operator= (ArrayIterator & ParamIterator)
{
//if (ParamIterator.Current = -1 || ParamIterator.MyArra
y == NULL)return false;
this->Current = ParamIterator.Current;
this->MyArray = ParamIterator.MyArray;
return true;
}
bool operator!=(ArrayIterator & ParamIterator)
{

if (this->Current <= ParamIterator.Current || this->MyAr


ray != ParamIterator.MyArray)return true;
return false;
}
T* GetElement()
{
if (!this->MyArray || this->Current == -1) {
printf("Err: Iterator neinitializat!\n");
return NULL;
}
return &(MyArray->operator[](Current));
}
};
// Lista nu e alocata, Capacity si Size = 0
Array()
{
Capacity = 0;
Size = 0;
List = NULL;
}
// destructor
~Array(){};
// Lista e alocata cu 'capacity' elemente
Array(int capacity)
{
Size = 0;
Capacity = capacity;
List = new T*[capacity];
}
// constructor de copiere
Array(const Array<T>& ParamList)
{
Capacity = ParamList.Capacity;
Size = ParamList.Size;
List = new T*[Size];
for (int i = 0; i < Size; i++)
{
List[i] = ParamList.List[i];
}
}
// arunca exceptie daca index este out of range
T& operator[] (int index)
{
if (index < 0) printf("Err: Invalid Index!");
if (index > this->Size) printf("Err: Index out of SIZE!\n");
if (index > this->Capacity) printf("Err: Index out of CAPACITY!\
n");
return *(List[index]);
}
// daca dimensiunea maxima a fost atinsa creste dimensiunea listei
void IncCapacity()
{

T** Temp = new T*[Capacity+INC_CAPACITY];


for (int i = 0; i < Capacity; i++)
{
Temp [i] = List[i];
}
delete[] List;
List = Temp;
Capacity += INC_CAPACITY;
}
// adauga un element de tipul T la sfarsitul listei si returneaza this
const Array<T>& operator+=(const T& ParamElement)
{
if (Size >= Capacity)this->IncCapacity();
T * Temp = new T;
*Temp = ParamElement;
List[Size++] = Temp;
return (*this);
}
// adauga un element pe pozitia index, retureaza this. Daca index e inva
lid arunca o exceptie
const Array<T>& Insert(int index, const T& ParamElement)
{
if (index < 0)printf("Err: Could not insert, invalid index (too
smal)!\n");
if (index > Size)printf("Err: Could not insert, index out of SIZ
E!\n");
if (Size == Capacity)this->IncCapacity();
Size++;
for (int i=Size; i >index; i--)
{
List[i] = List[i - 1];
}
T * Temp = new T;
*Temp = ParamElement;
List[index] = Temp;
return (*this);
}
// adauga o lista pe pozitia index, retureaza this. Daca index e invalid
arunca o exceptie
const Array<T>& Insert(int index, const Array<T> ParamList)
{
if (index < 0)printf("Err: Could not insert 'List', invalid inde
x (too smal)!\n");
if (index > Size)printf("Err: Could not insert 'List', index out
of SIZE!\n");
for (int i = 0; i < ParamList.Size; i++)
{
this->Insert(index + i, *ParamList.List[i]);
}
return (*this);
}
// sterge un element de pe pozitia index, returneaza this. Daca index e
invalid arunca o exceptie
const Array<T>& Delete(int index)
{
if (index < 0)printf("Err: Could not delete, invalid index (too

smal)!\n");
if (index >= Size)printf("Err: Could not delete, index out of SI
ZE!\n");
for (int i = index; i < Size - 1; i++)
{
List[index] = List[index + 1];
}
Size--;
return (*this);
}
//operator de copiere
bool operator=(const Array<T> & ParamList)
{
this->Size = ParamList.Size;
this->Capacity = ParamList.Capacity;
T** Temp = new T*[Capacity];
List = Temp;
for (int i = 0; i < Size; i++)
{
List[i] = ParamList.List[i];
}
return true;
}
// sorteaza folosind comparatia intre elementele din T
void Sort()
{
for (int i = 0; i < Size - 1; i++)
{
for (int j = i + 1; j < Size; j++)
{
if (*List[i]>*List[j]) {
T* aux = List[i];
List[i] = List[j];
List[j] = aux;
}
}
}
}
//returneaza marimea array-ului
int GetSize()
{
return Size;
}
//returneaza capacitatea array-ului
int GetCapacity()
{
return Capacity;
}
// cauta un element folosind binary search in Array
int BinarySearch(const T& ParamElement)
{
this->Sort();
int Low = 0, Up = Size - 1;

while (Low < Up)


{
if (*List[(Low + Up) / 2] == ParamElement)return (Low +
Up) / 2;
if (ParamElement < *List[(Low + Up) / 2]){
Up = (Low + Up + 1) / 2;
}
else{
Low = (Low + Up + 1) / 2;
}
}
if (ParamElement == *List[Low]) return Low;
return -1;
}
// cauta un element in Array
int Find(const T& ParamElement)
{
for (int i = 0; i < Size; i++){
if (ParamElement == *List[i]) return i;
}
return -1;
}
//returneaza Begin Iterator
ArrayIterator GetBeginIterator()
{
ArrayIterator *Temp = new ArrayIterator(0,this);
return *Temp;
}
//returneaza End Iterator
ArrayIterator GetEndIterator()
{
ArrayIterator *Temp = new ArrayIterator(this->GetSize()-1, this)
;
return *Temp;
}
};
________________________________________________
Lucaniuc Dragos Lucian

Potrebbero piacerti anche