Advertisement
Tarferi

Dynamic array with intuitive syntax

Jul 5th, 2019
529
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.91 KB | None | 0 0
  1.  
  2. template<typename type> class Array {
  3.  
  4.     class ArrayProxy {
  5.         Array* array;
  6.         int index;
  7.     public:
  8.         ArrayProxy(Array* array, int index) {
  9.             this->array = array;
  10.             this->index = index;
  11.         }
  12.         type operator= (type value) { array->set(index, value); return array->get(index); }
  13.         operator type() { return array->get(index); }
  14.     };
  15.  
  16. public:
  17.  
  18.     void set(unsigned int index, type value) {
  19.         if (index > this->____size) {
  20.             this->ensureAdditionalSize(this->____size - index);
  21.         }
  22.         this->____rawData[index] = value;
  23.         if (index > this->____dataSize) {
  24.             this->____dataSize = index + 1;
  25.         }
  26.     }
  27.  
  28.     type get(unsigned int index) {
  29.         return this->____rawData[index];
  30.     }
  31.  
  32.     unsigned int getSize() {
  33.         return this->____dataSize;
  34.     }
  35.  
  36.     void append(type value) {
  37.         this->ensureAdditionalSize(32);
  38.         this->____rawData[this->____dataSize] = value;
  39.         this->____dataSize++;
  40.     }
  41.  
  42.     ArrayProxy operator[] (unsigned int index) {
  43.         return ArrayProxy(this, index);
  44.     }
  45.  
  46.     ~Array() {
  47.         if (this->____rawData != nullptr) {
  48.             free(this->____rawData);
  49.             this->____rawData = nullptr;
  50.         }
  51.     }
  52.  
  53.  
  54. private:
  55.  
  56.     type* ____rawData = nullptr;
  57.  
  58.     unsigned int ____size = 0;
  59.  
  60.     unsigned  int ____dataSize = 0;
  61.  
  62.     void ensureAdditionalSize(unsigned int size) {
  63.         if (this->____dataSize + size > this->____size) {
  64.             if (this->____rawData != nullptr) {
  65.                 void* toFree = this->____rawData;
  66.                 unsigned int newSize = this->____size * ARRAY_INCREATE_FACTOR;
  67.                 MALLOC(this->____rawData, type, newSize);
  68.                 memset(this->____rawData, 0, newSize);
  69.                 memcpy(this->____rawData, toFree, this->____size * sizeof(type));
  70.                 this->____size = newSize;
  71.                 free(toFree);
  72.             }
  73.             else {
  74.                 unsigned int newSize = ARRAY_DEFAULT_SIZE;
  75.                 MALLOC(this->____rawData, type, newSize);
  76.                 memset(this->____rawData, 0, newSize);
  77.                 this->____size = newSize;
  78.                 this->____dataSize = 0;
  79.             }
  80.             this->ensureAdditionalSize(size);
  81.         }
  82.     }
  83.  
  84. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement