Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _SCL_SECURE_NO_WARNINGS
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define T0 template <typename T>
- #define INITIAL_BUFFER 10
- T0
- class Niz { // array
- T** elementi; // elements
- int** pristupiElementu; // array of counters to element access
- int brElemenata, maksProstora; //number of elements, maxSpace
- int indexTrenutnog; // index of current
- void isprazni(); // empy()
- public:
- Niz() : brElemenata(0), maksProstora(0), elementi(0), pristupiElementu(0), indexTrenutnog(-1) {}
- ~Niz();
- Niz(const Niz&);
- Niz& operator =(const Niz&);
- T& operator[](int);
- T operator[](int) const;
- int brojElemenata() const { return brElemenata; }
- T trenutni() const;
- bool prethodni();
- bool sljedeci();
- void pocetak();
- void kraj();
- void dodajIspred(const T&);
- void dodajIza(const T&);
- void obrisi();
- };
- T0
- Niz<T>:: Niz(const Niz& n) {
- brElemenata = n.brojElemenata(); maksProstora = n.maksProstora; trenutni = n.trenutni;
- elementi = new T*[n.maksProstora];
- copy(&n.elementi[0], &n.elementi[n.brojElemenata()], elementi);
- // uvecavam brojac pristupa n-tom elementu, a onda tu vrijednost kopiram u novi niz brojaca pristupa
- pristupiElementu = new int*[n.maksProstora];
- for(int i(0);i<n.brojElemenata();i++) {
- *(n.pristupiElementu[i])++;
- pristupiElementu[i] = n.pristupiElementu[i]; } // paziti da je i niz brojaca plitka kopija!
- }
- T0
- void Niz<T>:: isprazni() {
- for(int i(0); i<brojElemenata();i++) {
- *(pristupiElementu[i])--;
- if(*(pristupiElementu[i]) == 0) { delete elementi[i]; delete pristupiElementu[i]; } } }
- T0
- Niz<T>:: ~Niz() { isprazni(); delete[] elementi; delete[] pristupiElementu; }
- T0
- Niz<T>& Niz<T>::operator=(const Niz& n) {
- isprazni();
- if(maksProstora < n.maksProstora) { delete[] elementi; delete[] pristupiElementu;
- elementi = new T*[n.maksProstora]; pristupiElementu = new int*[n.maksProstora]; }
- copy(&n.elementi[0], &n.elementi[n.brojElemenata()], elementi);
- for(int i(0);i<n.brojElemenata();i++) *(n.pristupiElementu[i])++;
- copy(&n.pristupiElementu[0], &n.pristupiElementu[n.brojElemenata()], pristupiElementu);
- brElemenata = n.brojElemenata(); maksProstora = n.maksProstora; indexTrenutnog = n.indexTrenutnog;
- return *this; }
- T0
- T Niz<T>:: trenutni() const {
- if(indexTrenutnog < 0 ) throw("Niz je prazan!");
- return *(elementi[indexTrenutnog]); }
- T0
- bool Niz<T>:: prethodni() {
- if(indexTrenutnog == 0 ) return false;
- indexTrenutnog--; return true; }
- T0
- bool Niz<T>:: sljedeci() {
- if(indexTrenutnog == (brojElemenata() - 1) ) return false;
- indexTrenutnog++; return true; }
- T0
- void Niz<T>:: pocetak() { indexTrenutnog = 0; }
- T0
- void Niz<T>:: kraj() { indexTrenutnog = brojElemenata()-1; }
- T0
- void Niz<T>:: dodajIza(const T& el) {
- if (brojElemenata() == 0) {
- elementi = new T*[INITIAL_BUFFER]; pristupiElementu = new int*[INITIAL_BUFFER];
- elementi[0] = new T(el); pristupiElementu[0] = new int(1); // TODO: ostale pokazivac inicijalizirati nulom
- indexTrenutnog = 0; brElemenata++; maksProstora = INITIAL_BUFFER; }
- else if(maksProstora == brojElemenata()) {
- T** noviEl = new T*[(maksProstora*1,5) + 1]; int** noviPristup = new int*[int((maksProstora * 1.5) + 1)];
- if(indexTrenutnog > 0) {
- copy(&elementi[0], &elementi[indexTrenutnog], noviEl);
- copy(&pristupiElementu[0], &pristupiElementu[indexTrenutnog], noviPristup);}
- noviEl[indexTrenutnog] = new T(el); noviPristup[indexTrenutnog] = new int (1);
- if(indexTrenutnog < brojElemenata()-1) {
- copy(&elementi[indexTrenutnog+1], &elementi[brojElemenata()], noviEl[indexTrenutnog+1]);
- copy(&pristupiElementu[indexTrenutnog+1], &pristupiElementu[brojElemenata()], noviPristup[indexTrenutnog+1]); }
- brElemenata++; indexTrenutnog++; maksProstora = (maksProstora * 1.5) + 1;
- delete[] elementi; delete[] pristupiElementu; elementi = noviEl; pristupiElementu = noviPristup;
- }
- else {
- copy(&elementi[indexTrenutnog], &elementi[brojElemenata()], &elementi[indexTrenutnog+1]);
- copy(&pristupiElementu[indexTrenutnog], &pristupiElementu[brojElemenata()], &pristupiElementu[indexTrenutnog+1]);
- elementi[indexTrenutnog] = new T(el);
- pristupiElementu[indexTrenutnog] = new int (1);
- indexTrenutnog++; brElemenata++; }
- }
- T0
- void Niz<T>:: dodajIspred(const T& el) {
- indexTrenutnog++; dodajIza(el); indexTrenutnog -= 2; }
- T0
- void Niz<T>:: obrisi() {
- if(brojElemenata() == 0) throw("Ne moze se nista obrisati iz praznog niza");
- pristupiElementu[indexTrenutnog]--;
- if(*(pristupiElementu[indexTrenutnog]) == 0) {
- delete elementi[indexTrenutnog]; delete pristupiElementu[indexTrenutnog]; }
- copy(&elementi[indexTrenutnog+1], &elementi[brojElemenata()], &elementi[indexTrenutnog]);
- copy(&pristupiElementu[indexTrenutnog+1], &pristupiElementu[brojElemenata()], &pristupiElementu[indexTrenutnog]);
- if(indexTrenutnog == brojElemenata()-1) indexTrenutnog--;
- brElemenata--; }
- T0
- T& Niz<T>::operator[](int i) {
- if(i<1 && i > brojElemenata()+1) throw("Neispravan index");
- pristupiElementu[i-1]--;
- T* dubokaKopija = new T(elementi[i-1]);
- if(pristupiElementu[i-1] == 0) { delete elementi[i-1]; delete pristupiElementu[i-1]; }
- elementi[i-1] = dubokaKopija; pristupiElementu[i-1] = new int (1);
- return *elementi[i-1]; }
- T0
- T Niz<T>:: operator[](int i) const {
- if(i<1 && i > brojElemenata()+1) throw ("Neispravan index");
- return *elementi[i-1]; }
- int main() {
- Niz<int> test;
- test.dodajIspred(1);
- cout << "Sve OK, aBd." << endl; system("pause"); return 0; }
Advertisement
Add Comment
Please, Sign In to add comment