Guest User

Untitled

a guest
Oct 3rd, 2013
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.43 KB | None | 0 0
  1. #define _SCL_SECURE_NO_WARNINGS
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. #define T0 template <typename T>
  6. #define INITIAL_BUFFER 10
  7.  
  8. T0
  9. class Niz { // array
  10.     T** elementi; // elements
  11.     int** pristupiElementu; // array of counters to element access
  12.     int brElemenata, maksProstora; //number of elements, maxSpace
  13.     int indexTrenutnog; // index of current
  14.     void isprazni(); // empy()
  15. public:
  16.     Niz() : brElemenata(0), maksProstora(0), elementi(0), pristupiElementu(0), indexTrenutnog(-1) {}
  17.     ~Niz();
  18.     Niz(const Niz&);
  19.     Niz& operator =(const Niz&);
  20.     T& operator[](int);
  21.     T operator[](int) const;
  22.     int brojElemenata() const { return brElemenata; }
  23.     T trenutni() const;
  24.     bool prethodni();
  25.     bool sljedeci();
  26.     void pocetak();
  27.     void kraj();
  28.     void dodajIspred(const T&);
  29.     void dodajIza(const T&);
  30.     void obrisi();
  31. };
  32.  
  33. T0
  34. Niz<T>:: Niz(const Niz& n) {
  35.     brElemenata = n.brojElemenata(); maksProstora = n.maksProstora; trenutni = n.trenutni;
  36.         elementi = new T*[n.maksProstora];
  37.         copy(&n.elementi[0], &n.elementi[n.brojElemenata()], elementi);
  38.     // uvecavam brojac pristupa n-tom elementu, a onda tu vrijednost kopiram u novi niz brojaca pristupa
  39.     pristupiElementu = new int*[n.maksProstora];
  40.     for(int i(0);i<n.brojElemenata();i++)  {
  41.         *(n.pristupiElementu[i])++;
  42.         pristupiElementu[i] = n.pristupiElementu[i]; } // paziti da je i niz brojaca plitka kopija!
  43. }
  44.  
  45. T0
  46. void Niz<T>:: isprazni() {
  47.     for(int i(0); i<brojElemenata();i++) {
  48.     *(pristupiElementu[i])--;
  49.         if(*(pristupiElementu[i]) == 0) { delete elementi[i]; delete pristupiElementu[i]; } } }
  50.  
  51. T0
  52. Niz<T>:: ~Niz() { isprazni(); delete[] elementi; delete[] pristupiElementu; }
  53.  
  54. T0
  55. Niz<T>& Niz<T>::operator=(const Niz& n) {
  56.     isprazni();
  57.     if(maksProstora < n.maksProstora) { delete[] elementi; delete[] pristupiElementu;
  58.         elementi = new T*[n.maksProstora]; pristupiElementu = new int*[n.maksProstora]; }
  59.     copy(&n.elementi[0], &n.elementi[n.brojElemenata()], elementi);
  60.     for(int i(0);i<n.brojElemenata();i++) *(n.pristupiElementu[i])++;
  61.     copy(&n.pristupiElementu[0], &n.pristupiElementu[n.brojElemenata()], pristupiElementu);
  62.     brElemenata = n.brojElemenata(); maksProstora = n.maksProstora; indexTrenutnog = n.indexTrenutnog;
  63.     return *this; }
  64. T0
  65. T Niz<T>:: trenutni() const {
  66.     if(indexTrenutnog < 0 ) throw("Niz je prazan!");
  67.     return *(elementi[indexTrenutnog]); }
  68. T0
  69. bool Niz<T>:: prethodni() {
  70.     if(indexTrenutnog == 0 ) return false;
  71.     indexTrenutnog--; return true; }
  72. T0
  73. bool Niz<T>:: sljedeci() {
  74.     if(indexTrenutnog == (brojElemenata() - 1) ) return false;
  75.     indexTrenutnog++;  return true; }
  76. T0
  77. void Niz<T>:: pocetak() { indexTrenutnog = 0; }
  78. T0
  79. void Niz<T>:: kraj() { indexTrenutnog = brojElemenata()-1; }
  80. T0
  81. void Niz<T>:: dodajIza(const T& el) {
  82.     if (brojElemenata() == 0) {
  83.         elementi = new T*[INITIAL_BUFFER]; pristupiElementu = new int*[INITIAL_BUFFER];
  84.         elementi[0] = new T(el); pristupiElementu[0] = new int(1); // TODO: ostale pokazivac inicijalizirati nulom
  85.         indexTrenutnog = 0; brElemenata++; maksProstora = INITIAL_BUFFER; }
  86.     else if(maksProstora == brojElemenata()) {
  87.         T** noviEl = new T*[(maksProstora*1,5) + 1]; int** noviPristup = new int*[int((maksProstora * 1.5) + 1)];
  88.         if(indexTrenutnog > 0) {
  89.             copy(&elementi[0], &elementi[indexTrenutnog], noviEl);
  90.             copy(&pristupiElementu[0], &pristupiElementu[indexTrenutnog], noviPristup);}
  91.         noviEl[indexTrenutnog] = new T(el); noviPristup[indexTrenutnog] = new int (1);
  92.         if(indexTrenutnog < brojElemenata()-1) {
  93.             copy(&elementi[indexTrenutnog+1], &elementi[brojElemenata()], noviEl[indexTrenutnog+1]);
  94.             copy(&pristupiElementu[indexTrenutnog+1], &pristupiElementu[brojElemenata()], noviPristup[indexTrenutnog+1]); }
  95.         brElemenata++; indexTrenutnog++; maksProstora = (maksProstora * 1.5) + 1;
  96.         delete[] elementi; delete[] pristupiElementu; elementi = noviEl; pristupiElementu = noviPristup;
  97.         }
  98.     else {
  99.         copy(&elementi[indexTrenutnog], &elementi[brojElemenata()], &elementi[indexTrenutnog+1]);
  100.         copy(&pristupiElementu[indexTrenutnog], &pristupiElementu[brojElemenata()], &pristupiElementu[indexTrenutnog+1]);
  101.         elementi[indexTrenutnog] = new T(el);  
  102.         pristupiElementu[indexTrenutnog] = new int (1);
  103.         indexTrenutnog++; brElemenata++; }
  104. }
  105. T0
  106. void Niz<T>:: dodajIspred(const T& el) {
  107.     indexTrenutnog++; dodajIza(el); indexTrenutnog -= 2; }
  108. T0
  109. void Niz<T>:: obrisi() {
  110.     if(brojElemenata() == 0) throw("Ne moze se nista obrisati iz praznog niza");
  111.     pristupiElementu[indexTrenutnog]--;
  112.     if(*(pristupiElementu[indexTrenutnog]) == 0) {
  113.         delete elementi[indexTrenutnog]; delete pristupiElementu[indexTrenutnog]; }
  114.     copy(&elementi[indexTrenutnog+1], &elementi[brojElemenata()], &elementi[indexTrenutnog]);
  115.     copy(&pristupiElementu[indexTrenutnog+1], &pristupiElementu[brojElemenata()], &pristupiElementu[indexTrenutnog]);
  116.     if(indexTrenutnog == brojElemenata()-1) indexTrenutnog--;
  117.     brElemenata--; }
  118. T0
  119. T& Niz<T>::operator[](int i) {
  120.     if(i<1 && i > brojElemenata()+1) throw("Neispravan index");
  121.     pristupiElementu[i-1]--;
  122.     T* dubokaKopija = new T(elementi[i-1]);
  123.     if(pristupiElementu[i-1] == 0) { delete elementi[i-1]; delete pristupiElementu[i-1]; }
  124.     elementi[i-1] = dubokaKopija; pristupiElementu[i-1] = new int (1);
  125.     return *elementi[i-1]; }
  126. T0
  127. T Niz<T>:: operator[](int i) const {
  128.     if(i<1 && i > brojElemenata()+1) throw ("Neispravan index");
  129.     return *elementi[i-1]; }   
  130.  
  131.  
  132. int main() {
  133.     Niz<int> test;
  134.     test.dodajIspred(1);
  135.    
  136.    
  137.     cout << "Sve OK, aBd." << endl; system("pause"); return 0; }
Advertisement
Add Comment
Please, Sign In to add comment