Advertisement
Guest User

vector implementation

a guest
Aug 10th, 2016
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.52 KB | None | 0 0
  1. //
  2. //  Array.h
  3. //  Array
  4. //
  5. //  Created by Kelvin Zhang on 8/9/16.
  6. //  Copyright © 2016 Kelvin Zhang. All rights reserved.
  7. //
  8.  
  9. #ifndef Array_h
  10. #define Array_h
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. template <class T>
  14. class Array
  15. {
  16. private:
  17.     T *buffer;
  18.     unsigned int length;
  19.     unsigned int capacity;
  20.     bool ensureCapacity()
  21.     {
  22.         if (length >= capacity)
  23.         {
  24.             unsigned int newCapacity = capacity * 2;
  25.            
  26.             void *tmp = realloc(buffer, newCapacity * sizeof(T));
  27.             if (!tmp)
  28.             {
  29.                 return false;
  30.             }
  31.             buffer = (T*)tmp;
  32.             capacity = newCapacity;
  33.         }
  34.         return true;
  35.     }
  36. public:
  37.     Array()
  38.     {
  39.         capacity = 4;
  40.         length = 0;
  41.         buffer = (T*)malloc(capacity * sizeof(T));
  42.     }
  43.    
  44.     Array(const Array &arr)
  45.     {
  46.         length = arr.length;
  47.         capacity = arr.capacity;
  48.         buffer = malloc(capacity);
  49.         memcpy(buffer, arr.buffer, length);
  50.         printf("Copy %p created from %p by const &arr()\n", buffer, arr.buffer);
  51.     }
  52.    
  53.     Array(Array &&arr)
  54.     {
  55.         length = arr.length;
  56.         capacity = arr.capacity;
  57.         buffer = (T*)malloc(capacity);
  58.         memcpy(buffer, arr.buffer, length);
  59.         printf("Copy %p created from %p by Arr&&()\n", buffer, arr.buffer);
  60.     }
  61.    
  62.     Array &operator=(const Array &arr)
  63.     {
  64.         length = arr.length;
  65.         capacity = arr.capacity;
  66.         buffer = (T*)malloc(arr.capacity);
  67.         memcpy(buffer, arr.buffer, length);
  68.         printf("Copy %p created from %p by operator=()\n", buffer, arr.buffer);
  69.         return *this;
  70.     }
  71.    
  72.     Array(unsigned int initialCapacity)
  73.     {
  74.         if(initialCapacity == 0)
  75.         {
  76.             initialCapacity = 4;
  77.         }
  78.         capacity = initialCapacity;
  79.         length = 0;
  80.         buffer = (T*)malloc(capacity * sizeof(T));
  81.         printf("buffer: %p capacity: %d\n", buffer, capacity);
  82.     }
  83.    
  84.     ~Array()
  85.     {
  86.         printf("deallocing %p\n", buffer);
  87.         for(int i = 0; i < length; i ++)
  88.         {
  89.             buffer[i].~T();
  90.         }
  91.         free(buffer);
  92.     }
  93.    
  94.     T *begin()
  95.     {
  96.         return buffer;
  97.     }
  98.    
  99.     T* data()
  100.     {
  101.         return buffer;
  102.     }
  103.    
  104.     T* end()
  105.     {
  106.         return buffer + length;
  107.     }
  108.    
  109.     unsigned int size()
  110.     {
  111.         return length;
  112.     }
  113.    
  114.     bool push_back(T &&t)
  115.     {
  116.         printf("Pass by r ref...\n");
  117.         if(!ensureCapacity())
  118.         {
  119.             return false;
  120.         }
  121.         buffer[length] = t;
  122.         length++;
  123.         return true;
  124.     }
  125.    
  126.    
  127.     bool push_back(const T &t)
  128.     {
  129.         printf("Pass by l ref...\n");
  130.         if(!ensureCapacity())
  131.         {
  132.             return false;
  133.         }
  134.         buffer[length] = t;
  135.         length++;
  136.         return true;
  137.     }
  138.    
  139.    
  140.    
  141.     bool pop_back()
  142.     {
  143.         if (length)
  144.         {
  145.             length--;
  146.             return buffer[length];
  147.         }
  148.     }
  149.    
  150.     T& operator[](int index)
  151.     {
  152.         return buffer[index];
  153.     }
  154.    
  155.     void clear()
  156.     {
  157.         length = 0;
  158.     }
  159.    
  160.     bool erase(int index)
  161.     {
  162.         if (index >= length || index < 0)
  163.         {
  164.             return false;
  165.         }
  166.         buffer[index].~T();
  167.         for (int i = index + 1; i < length; i++)
  168.         {
  169.             buffer[i - 1] = buffer[i];
  170.         }
  171.         length--;
  172.     }
  173. };
  174.  
  175. #endif /* Array_h */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement