Advertisement
Guest User

The Vector Class

a guest
May 4th, 2022
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.15 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. #ifndef _LIB_VECTOR_H
  4. #define _LIB_VECTOR_H
  5.  
  6. namespace abj{
  7.   template<typename T>
  8.   class Vector{
  9.   private:
  10.     int current_size;
  11.     int capacity;
  12.     T* storage;
  13.     const int initialization_size = 2;
  14.     void resize(int size);
  15.   public:
  16.     Vector();
  17.     Vector(int size);
  18.     Vector(abj::Vector<T>& initialize_data);
  19.  
  20.     ~Vector();
  21.  
  22.     void push(T& data);
  23.     void push(T&& data);
  24.     void push(abj::Vector<T>& new_data);
  25.  
  26.     void reverse();
  27.     void print();
  28.  
  29.     abj::Vector<T>& operator= (const abj::Vector<T>& data);
  30.     T operator[](int index);
  31.     void swap(abj::Vector<T>& data);
  32.  
  33.     int size();
  34.     T get(int index);
  35.     bool set(int index, T data);
  36.  
  37.     T* getStorage();
  38.     void setStorage(T* newStorage);
  39.     void setSize(int size);
  40.  
  41.     friend void test_function();
  42.   };
  43. }
  44.  
  45. template<typename T>
  46. abj::Vector<T>::Vector(){
  47.     this->storage = (T*)calloc(this->initialization_size,sizeof(T));   
  48.     this->capacity=this->initialization_size;
  49.     this->current_size=0;
  50. }
  51. template<typename T>
  52. abj::Vector<T>::Vector(int size){
  53.     this->storage = (T*)calloc(size,sizeof(T));
  54.     this->capacity=size;
  55.     this->current_size=0;
  56. }
  57. template<typename T>
  58. abj::Vector<T>::Vector(abj::Vector<T>& initializer){ // copy constructor, deep copy
  59.     this->storage = (T*)calloc(initializer.size(), sizeof(T));
  60.     this->capacity=initializer.size();
  61.  
  62.     for(int i=0; i<initializer.size(); i++){
  63.         this->storage[i]=initializer.get(i);
  64.     }
  65.     this->current_size=initializer.size();
  66. }
  67. template<typename T>
  68. abj::Vector<T>::~Vector(){
  69.   printf("Calling vector destructor. Did you destroy its elements before? :)\n");
  70.   if(this->storage!=NULL && this->current_size!=0)
  71.     {
  72.       // for(int i=0; i<this->current_size; i++) free(storage[i]);
  73.       free(this->storage);
  74.       this->storage = NULL;
  75.     }
  76.   this->capacity=0;
  77.   this->current_size=0;
  78. }
  79.  
  80. template<typename T>
  81. void abj::Vector<T>::push(T& data){
  82.     if(this->current_size+1>=this->capacity){
  83.         T* newStorage = (T*)calloc(this->capacity*2, sizeof(T));
  84.         this->capacity*=2;
  85.  
  86.         for(int i=0; i<this->current_size; i++) newStorage[i]=this->storage[i];
  87.         free(this->storage);
  88.         this->storage=newStorage;
  89.     }
  90.     this->storage[this->current_size] = data;
  91.     this->current_size++;
  92. }
  93. template<typename T>
  94. void abj::Vector<T>::push(T&& data){
  95.     if(this->current_size+1>=this->capacity){
  96.         T* temp = (T*)calloc(this->capacity*2, sizeof(T));
  97.         this->capacity*=2;
  98.  
  99.         for(int i=0; i<this->current_size; i++) temp[i]=this->storage[i];
  100.         free(this->storage);
  101.         this->storage=temp;
  102.     }
  103.     this->storage[this->current_size] = data;
  104.     this->current_size++;
  105. }
  106.  
  107.  
  108. // template<typename T>
  109. // void abj::Vector<T>::push(T data){
  110. //  if(this->current_size+1>this->capacity){
  111. //      this->resize(this->capacity*2);
  112. //  }
  113. //  this->storage[this->current_size++]=data; // usual
  114. // }
  115.  
  116.  
  117. template<typename T>
  118. void abj::Vector<T>::push(abj::Vector<T>& data){
  119.     /*
  120.     printf("---data size=%d\n",data.size());
  121.     data.print();
  122.     printf("Our data is fine!\n");
  123.     */
  124.     if(data.size()+this->current_size>=this->capacity)
  125.         this->resize(data.size()+this->current_size);  
  126.     int j=0;
  127.     for(int i=this->current_size; i<this->capacity && j<data.size(); i++, j++){
  128.         this->storage[i]=data.get(j);  
  129.         //printf("%d) %s\n",i+1,this->storage[i]);
  130.     }
  131.  
  132.     this->current_size = data.size()+this->current_size;
  133. }
  134.  
  135. template<typename T>
  136. T abj::Vector<T>::get(int index){
  137.   if(this->current_size==0) {
  138.     printf("Vector does not exist so can not access index %d!Exiting...\n",index);
  139.     exit(1);//return NULL;
  140.   }
  141.   if(index<0 || index>=this->current_size) {
  142.     printf("Vector index %d out of bound as vector size is %d!Exiting....\n",index, this->current_size);
  143.     exit(1);
  144.   }
  145.   return this->storage[index];
  146. }
  147.  
  148. template<typename T>
  149. void abj::Vector<T>::resize(int size){
  150.     T* temp = (T*)calloc(size,sizeof(T));
  151.     this->capacity = size;
  152.  
  153.     for(int i=0; i<this->current_size; i++){
  154.         temp[i]=this->storage[i];
  155.     }
  156.    
  157.     free(this->storage);
  158.     this->storage = temp;
  159.     this->current_size = this->current_size;
  160.    
  161. }
  162.  
  163. template<typename T>
  164. void abj::Vector<T>::print(){
  165.   printf("Printing vector:\n");
  166.   for(int i=0; i<this->current_size; i++){
  167.     std::cout<<this->storage[i]<<" ";
  168.   }
  169.   std::cout<<std::endl;
  170. }
  171.  
  172. template<typename T>
  173. int abj::Vector<T>::size(){
  174.   return this->current_size;
  175. }
  176.  
  177.  
  178. template<typename T>
  179. bool abj::Vector<T>::set(int index, T data){
  180.   if(index>=this->current_size){
  181.     printf("Vector Error! Can not set data to index %d as Current size is %d.\n",index,this->current_size);
  182.     return false;
  183.   }
  184.   this->storage[index]=data;
  185.   return true;
  186. }
  187. template<typename T>
  188. // A = B, Create new A and B
  189. abj::Vector<T>& abj::Vector<T>::operator=(const abj::Vector<T>& data){
  190.   // printf("The operator function is being used!\n");
  191.   if(this==&data) return *this;
  192.  
  193.   abj::Vector<T> temp(data);
  194.   this->swap(temp);
  195.   return *this;
  196. //   this->~Vector();
  197.  
  198. //   this->storage = (T*)calloc(data.size(), sizeof(T));
  199. //   for(int i=0; i<data.size(); i++) {
  200. //     T ref = data.get(i);
  201. //     this->storage[i]=ref;
  202. //   }
  203. //   this->current_size = data.size();
  204. }
  205.  
  206. template<typename T>
  207. void abj::Vector<T>::swap(abj::Vector<T>& data){
  208.   T* temp_storage = data.getStorage();
  209.   data.setStorage(this->storage);
  210.   this->storage = temp_storage;
  211.  
  212.   int temp_size = data.size();
  213.   data.setSize(this->size());
  214.   this->curr_size = temp_size;
  215. }
  216. // template<typename T>
  217. // void abj::Vector<T>::operator = (T& data){
  218. //   if(current_size+1>=this->capacity) resize(this->capacity*2);
  219. //   this->storage[this->current_size]=data;
  220. //   this->current_size++;
  221. // }
  222.  
  223. // template<typename T>
  224. // void abj::Vector<T>::operator=(abj::Vector<T>&& data){
  225. //   // printf("The operator function is being used!\n");
  226. //   if(this==&data) return ;
  227. //   this->~Vector();
  228. //   //this->resize(data.size());
  229. //   this->storage = (T*)calloc(data.size(), sizeof(T));
  230. //   for(int i=0; i<data.size(); i++) {
  231. //     T ref = data.get(i);
  232. //     this->storage[i]=ref;
  233. //   }
  234. //   this->current_size = data.size();
  235. // }
  236.  
  237. // template<typename T>
  238. // void abj::Vector<T>::operator = (T&& data){
  239. //   if(current_size+1>=this->capacity) resize(this->capacity*2);
  240. //   this->storage[this->current_size]=data;
  241. //   this->current_size++;
  242. // }
  243.  
  244.  
  245. template<typename T>
  246. T abj::Vector<T>::operator[](int index){
  247.   if(index<0 || index>=this->current_size) {
  248.     printf("\n[] : Vector Out of index!\n");
  249.     // return 0;
  250.     exit(1);
  251.   }
  252.   return this->storage[index];
  253. }
  254.  
  255. template<typename T>
  256. T* abj::Vector<T>::getStorage(){
  257.   return this->storage;
  258. }
  259. template<typename T>
  260. void abj::Vector<T>::setStorage(T* newStorage){
  261.   free(this->storage);
  262.   this->storage = newStorage;
  263. }
  264.  
  265. template<typename T>
  266. void abj::Vector<T>::setSize(int size){
  267.   this->current_size = size;
  268. }
  269.  
  270.  
  271. template<typename T>
  272. void abj::Vector<T>::reverse(){
  273.   T* new_storage = (T*)calloc(this->capacity, sizeof(T));
  274.  
  275.   int j=0;
  276.   for(int i=this->current_size-1; i>=0; i--, j++){
  277.     new_storage[j]=this->storage[i];
  278.   }
  279.   free(this->storage);
  280.   this->storage = new_storage;
  281. }
  282.  
  283.  
  284. #endif
  285.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement