karol_dziachan

ArrayList c++ implementation

Nov 20th, 2020 (edited)
1,013
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.15 KB | None | 0 0
  1.  
  2. class ArrayList
  3. {
  4. public:
  5.     ArrayList();
  6.     ArrayList(int iSize, string sName);
  7.     ArrayList(const ArrayList& obj);
  8.     ~ArrayList();
  9.  
  10.     void add(int iElem);
  11.     void add(int iIndex, int iElem);
  12.     void remove(int iIndex);
  13.     void clear();
  14.     bool isEmpty();
  15.     bool contains(int elem);
  16.     int get(int iIndex);
  17.     int size() { return iCounter;  }
  18.     void toString();
  19.  
  20.     string getSName() { return sName; }
  21.     int getILength() { return iLength; }
  22.     int* getArray() { return pi_array; }
  23.     ArrayList operator+ (const ArrayList& pcOther);
  24.     void operator= (const ArrayList& pcOther);
  25.  
  26. private:
  27.     void amortizedArrayUp();
  28.     void amortizedArrayDown();
  29.     void vSetValueAt(int offSet, int newValue);
  30.     string sName;
  31.     int iLength;
  32.     int iCounter;
  33.     int* pi_array;
  34.  
  35. };
  36.  
  37.  
  38. ArrayList::ArrayList()
  39. {
  40.     pi_array = new int[DEFAULT_LENGTH];
  41.     sName = DEFAULT_NAME;
  42.     iLength = DEFAULT_LENGTH;
  43.     iCounter = 0;
  44. }
  45.  
  46. ArrayList::ArrayList(int iSize, string sName)
  47. {
  48.     pi_array = new int[iSize];
  49.     this->sName = sName;
  50.     iLength = iSize;
  51.     iCounter = 0;
  52. }
  53.  
  54. ArrayList::ArrayList(const ArrayList& pcOther)
  55. {
  56.     iLength = pcOther.iLength;
  57.     iCounter = pcOther.iCounter;
  58.     sName = pcOther.sName + "_copy";
  59.  
  60.     if (iLength > 1)
  61.     {
  62.         pi_array = new int[iLength];
  63.         for (int i = 0; i < iLength; i++)
  64.             pi_array[i] = pcOther.pi_array[i];
  65.     }
  66.     else
  67.         cout << UNCORRECT_DATA << endl;
  68.  
  69.     if (DEBUG)  cout << COPY << endl;
  70.     if (DEBUG)  cout << COPY << sName << endl;
  71. }
  72.  
  73.  
  74. ArrayList::~ArrayList()
  75. {
  76.     if (DEBUG) cout << DELETE << sName << endl;
  77.     if (DEBUG) cout << DELETE << iLength << endl;
  78.     delete[] pi_array;
  79. }
  80.  
  81.  
  82. void ArrayList::operator=(const ArrayList& obj)
  83. {
  84.     clear();
  85.  
  86.     for (int ii = 0; ii < obj.iCounter; ii++)
  87.         add(obj.pi_array[ii]);
  88.  
  89. }
  90.  
  91.  
  92. ArrayList ArrayList::operator+(const ArrayList& pcOther)
  93. {
  94.     ArrayList cont_list;
  95.  
  96.     for (int ii = 0; ii < iCounter+pcOther.iCounter ; ii++)
  97.     {
  98.         if (ii < iCounter)
  99.             cont_list.add(pi_array[ii]);
  100.         else
  101.             cont_list.add(pcOther.pi_array[ii - iCounter]);
  102.     }
  103.  
  104.     return cont_list;
  105. }
  106.  
  107. int ArrayList::get(int iElem)
  108. {
  109.     if (iElem < iCounter && iElem > 0)
  110.     return pi_array[iElem];
  111. }
  112.  
  113. void ArrayList::add(int iElem)
  114. {
  115.     if (iCounter < iLength)
  116.         pi_array[iCounter] = iElem;
  117.     else
  118.         amortizedArrayUp();
  119.     pi_array[iCounter] = iElem;
  120.  
  121.     iCounter++;
  122. }
  123.  
  124. void ArrayList::add(int iIndex, int iElem)
  125. {
  126.     if (iIndex < iCounter && iIndex > 0)
  127.     {
  128.         if (iCounter >= iLength)
  129.             amortizedArrayUp();
  130.  
  131.         int* help_array = new int[iCounter - iIndex];
  132.  
  133.         for (int ii = iIndex; ii < iCounter; ii++)
  134.             help_array[ii - iIndex] = pi_array[ii];
  135.  
  136.         pi_array[iIndex] = iElem;
  137.  
  138.         iCounter++;
  139.         iIndex++;
  140.  
  141.  
  142.         for (int ii = iIndex; ii < iCounter; ii++)
  143.         {
  144.             pi_array[ii] = help_array[ii - iIndex];
  145.         }
  146.         delete[] help_array;
  147.     }
  148.     else
  149.         cout << UNCORRECT_DATA << endl;
  150. }
  151.  
  152. void ArrayList::amortizedArrayUp()
  153. {
  154.     int* pi_new_tab = new int[iLength * INCREASE_FACTOR];
  155.  
  156.     for (int ii = 0; ii < iCounter; ii++)
  157.         pi_new_tab[ii] = pi_array[ii];
  158.  
  159.     delete[] pi_array;
  160.     iLength *= INCREASE_FACTOR;
  161.     pi_array = pi_new_tab;
  162. }
  163.  
  164. void ArrayList::amortizedArrayDown()
  165. {
  166.     int* pi_new_tab = new int[(int) iLength / DECREASE_FACTOR + 1];
  167.  
  168.     for (int ii = 0; ii < iCounter; ii++)
  169.         pi_new_tab[ii] = pi_array[ii];
  170.  
  171.     delete[] pi_array;
  172.     iLength = (int) iLength / DECREASE_FACTOR + 1;
  173.     pi_array = pi_new_tab;
  174. }
  175.  
  176. void ArrayList::toString()
  177. {
  178.     for (int ii = 0; ii < iCounter; ii++)
  179.     {
  180.         cout << (ii + 1) << ": " << (pi_array[ii]) << endl;
  181.     }
  182. }
  183.  
  184. bool ArrayList::contains(int iElem)
  185. {
  186.     for (int ii = 0; ii < iCounter; ii++)
  187.         if (pi_array[ii] == iElem)
  188.             return true;
  189.  
  190.     return false;
  191. }
  192.  
  193. void ArrayList::remove(int iIndex)
  194. {
  195.     if (iIndex < iCounter && iIndex > 0)
  196.     {
  197.         iCounter--;
  198.  
  199.         for (int ii = iIndex--; ii < iCounter; ii++)
  200.             pi_array[ii] = pi_array[ii + 1];
  201.     }
  202.     else
  203.         cout << UNCORRECT_DATA << endl;
  204.  
  205.     if (iCounter < iLength / DECREASE_FACTOR && iCounter > DEFAULT_LENGTH)
  206.         amortizedArrayDown();
  207. }
  208.  
  209. void ArrayList::clear()
  210. {
  211.     string name = sName;
  212.     delete[] pi_array;
  213.  
  214.     pi_array = new int[DEFAULT_LENGTH];
  215.     iLength = DEFAULT_LENGTH;
  216.     iCounter = 0;
  217.     sName = name;
  218. }
  219.  
  220. bool ArrayList::isEmpty()
  221. {
  222.     return iCounter == 0;
  223. }
  224.  
  225.    
  226.  
  227.  
Advertisement
Add Comment
Please, Sign In to add comment