Advertisement
Guest User

std::initializer_list<int> container

a guest
Jan 18th, 2019
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.18 KB | None | 0 0
  1. #define INITIALIZER_TEST 0
  2. #define FUNCTION_TEST 1
  3. #include <iostream>
  4. #include <cassert>
  5. #include <initializer_list>
  6. using namespace std;
  7.  
  8. class IntArray
  9. {
  10. private:
  11.     unsigned int m_size     = 0U;
  12.     unsigned int m_capacity = 0U;
  13.              int * m_data   = nullptr;
  14.  
  15. public:
  16.     // 1-1. normal ctor
  17.     IntArray(const unsigned int & size = 1) : m_capacity(size)
  18.     {
  19.         assert(size > 0);
  20.         // 사이즈 짝수: 여분 용량 +2; 홀수: 여분 용량 +1; => 계산의 편의를 위해..
  21.         if (size % 2 == 0) m_capacity += 2;
  22.         else m_capacity += 1;
  23.  
  24.         m_data = new int[m_capacity];
  25.         m_size = 0;
  26.         if (m_data == nullptr)
  27.         {
  28.             cout << "memory allocation failed.\n";
  29.             exit(1);
  30.         }
  31.     }
  32.     // 1-2. initializer_list ctor
  33.     IntArray(const initializer_list<int> & list) : IntArray(list.size())
  34.     {
  35.         m_size = list.size();
  36.        
  37.         int cnt = 0;
  38.         for (const auto & e : list)
  39.         {
  40.             m_data[cnt++] = e;
  41.         }
  42.     }
  43.     // 1-3. copy ctor
  44.     IntArray(const IntArray & origin)
  45.     {
  46.         // delete obsolete data.
  47.         delete[] this->m_data;
  48.         // and reallocate with size of origin.
  49.         //m_data = new int[origin.m_size];
  50.         if (origin.m_data != nullptr)
  51.         {
  52.             m_size = origin.m_size;
  53.             m_capacity = origin.m_capacity;
  54.             m_data = new int[origin.m_size];
  55.         }
  56.         else
  57.         {
  58.             cout << "Assign Operatoration failed.\n";
  59.             this->m_data = nullptr;
  60.             exit(1);
  61.         }
  62.         // finally, copy the values.
  63.         for (unsigned int i = 0; i < origin.m_size; ++i)
  64.             m_data[i] = origin.m_data[i];
  65.     }
  66.     // 1-4. dtor
  67.     ~IntArray()
  68.     {
  69.         delete[] this->m_data;
  70.     }
  71.  
  72.     // 2-1. return size and capacity.
  73.     unsigned int size()     { return m_size; }
  74.     unsigned int capacity() { return m_capacity; }
  75.     // 2-2. check container state.
  76.     bool isEmpty()  { return m_size == 0 ? true : false; }
  77.     bool isFull()   { return m_capacity == m_size ? true : false; }
  78.    
  79.     // 3. return front or back element.
  80.     int& front()
  81.     {
  82.         if (isEmpty()) { cout << "list is empty!\n"; assert(!isEmpty()); }
  83.         return m_data[0];
  84.     }
  85.     int& back()
  86.     {
  87.         if (isEmpty()) { cout << "list is empty!\n"; assert(!isEmpty()); }
  88.         return m_data[m_size - 1];
  89.     }
  90.  
  91.     // 4-1. push back element and memory optimization.
  92.     void push_back(const int & element)
  93.     {
  94.         if (isFull() && !upAlloc())
  95.         {
  96.             cout << "Upscaling memory allocation failed.\n";
  97.             return;
  98.         }
  99.  
  100.         m_data[m_size++] = element;
  101.     }
  102.     // 4-2. pop back element and memory optimization.
  103.     void pop_back()
  104.     {
  105.         if (isEmpty())
  106.         {
  107.             cout << "list is empty.\n";
  108.             assert(!isEmpty());
  109.             return;
  110.         }
  111.         else if (m_size - 1 == m_capacity / 2)
  112.         {
  113.             if (!downAlloc())
  114.             {
  115.                 cout << "DownScaling memory allocation failed.\n";
  116.             }
  117.         }
  118.  
  119.         m_size--;
  120.     }
  121.  
  122.     // 5-1. double the capacity of the array.
  123.     bool upAlloc()
  124.     {
  125.         m_capacity *= 2;
  126.         int * temp_data = new int[m_capacity];
  127.         if (temp_data == nullptr) return false;
  128.  
  129.         for (unsigned int i = 0; i < m_size; ++i)
  130.             temp_data[i] = m_data[i];
  131.  
  132.         delete[] this->m_data;
  133.         m_data = temp_data;
  134.  
  135.         return true;
  136.     }
  137.     // 5-2. decrease the array capacity.
  138.     bool downAlloc()
  139.     {
  140.         // 용량이 짝수였으면 절반크기 + 2만큼, 홀수였으면 절반크기 + 1로 무조건 용량을 짝수로 만듦
  141.         if ((m_capacity / 2) % 2 == 0)
  142.             m_capacity = (m_capacity / 2) + 2;
  143.         else
  144.             m_capacity = (m_capacity / 2) + 1;
  145.  
  146.         int * temp_data = new int[m_capacity];
  147.         if (temp_data == nullptr) return false;
  148.  
  149.         for (unsigned int i = 0; i < m_size - 1; ++i)
  150.             temp_data[i] = m_data[i];
  151.  
  152.         delete[] this->m_data;
  153.         m_data = temp_data;
  154.  
  155.         return true;
  156.     }
  157.  
  158.     // 6-1. '=' operator overloading.
  159.     IntArray& operator = (IntArray & rhs)
  160.     {
  161.         if (this == &rhs) return *this;
  162.         delete[] this->m_data; 
  163.  
  164.         if (rhs.m_data != nullptr)
  165.         {
  166.             m_capacity = rhs.capacity();
  167.             m_size = rhs.size();
  168.  
  169.             m_data = new int[rhs.capacity()];
  170.  
  171.             for (unsigned int i = 0; i < rhs.size(); ++i)
  172.                 m_data[i] = rhs.m_data[i];
  173.         }
  174.         else
  175.         {
  176.             cout << "Assign Operatoration failed.\n";
  177.             this->m_data = nullptr;
  178.             exit(1);
  179.         }
  180.         return *this;
  181.     }
  182.     // 6-2. '<<' operator overloading.
  183.     friend ostream& operator << (ostream& out, const IntArray & rhs)
  184.     {
  185.         for (unsigned int i = 0; i < rhs.m_size; ++i)
  186.         {
  187.             out << rhs.m_data[i] << ' ';
  188.         }
  189.         return out;
  190.     }
  191.     // 6-3. '[]' operator overloading.
  192.     int & operator [] (const unsigned int & index)
  193.     {
  194.         assert(index >= 0 && index < m_size);
  195.         return m_data[index];
  196.     }
  197. };
  198.  
  199. int main()
  200. {
  201. #if INITIALIZER_TEST == 0
  202.     // initializer_list
  203.     IntArray my_array{ 1,2,3,4,5 };
  204.     cout << "Initializer list : " << my_array << endl;
  205.  
  206.     // copy constructor
  207.     IntArray my_array2(my_array);
  208.     cout << "Copy Constructor : " << my_array2 << endl;
  209.    
  210.     // Assign Operator
  211.     my_array2 = my_array;
  212.     cout << "Assign Operation : " << my_array2 << endl;
  213.  
  214. #elif FUNCTION_TEST == 1
  215.     IntArray arr(5);
  216.     for (int i = 1; i <= 5; i++)
  217.         arr.push_back(i);
  218.     cout << "Initial State: " << arr << "\tsize(): " << arr.size() << "\tcapacity(): " << arr.capacity() << endl << endl;
  219.  
  220.     uint32_t size = arr.size();
  221.     for (uint32_t i = 0; i < size; i++)
  222.     {
  223.         cout << i + 1 << "th pop\n";
  224.         arr.pop_back();
  225.         cout << "After State: " << arr << "\tsize(): " << arr.size() << "\tcapacity(): " << arr.capacity() << endl << endl;
  226.     }
  227. #endif
  228.     return 0;
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement