Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdexcept>
- template <class T>
- class CustomVector
- {
- public:
- CustomVector();
- CustomVector(int elementsCount);
- ~CustomVector();
- const T& operator[] (const int index) const;
- T& operator[] (const int index);
- int Count() const;
- int Capacity() const;
- void Clear();
- void PushBack(const T& element);
- void PopBack();
- void Erase(const int index);
- private:
- const int defaultCapacity = 10;
- T* items;
- int count;
- int capacity;
- };
- template <class T>
- CustomVector<T>::CustomVector()
- : items(nullptr),
- count(0),
- capacity(defaultCapacity)
- {
- items = static_cast<T*>(malloc(sizeof(T) * capacity));
- if (items == nullptr) throw new std::bad_alloc();
- new (items) T[capacity]();
- }
- template <class T>
- CustomVector<T>::CustomVector(int elementsCount)
- : items(nullptr),
- count(elementsCount),
- capacity(elementsCount)
- {
- if (elementsCount <= 0) throw std::invalid_argument("Received invalid elements count.");
- items = static_cast<T*>(malloc(sizeof(T) * capacity));
- if (items == nullptr) throw new std::bad_alloc();
- new (items) T[capacity]();
- }
- template <class T>
- CustomVector<T>::~CustomVector()
- {
- delete[] items;
- }
- template <class T>
- const T& CustomVector<T>::operator[] (const int index) const
- {
- if ((count == 0) || (index > count - 1) || (index < 0)) throw new std::invalid_argument("Received invalid index.");
- return items[index];
- }
- template <class T>
- T& CustomVector<T>::operator[] (const int index)
- {
- if ((count == 0) || (index > count - 1) || (index < 0)) throw new std::invalid_argument("Received invalid index.");
- return items[index];
- }
- template <class T>
- int CustomVector<T>::Count() const
- {
- return count;
- }
- template <class T>
- int CustomVector<T>::Capacity() const
- {
- return capacity;
- }
- template <class T>
- void CustomVector<T>::Clear()
- {
- T* newItems = static_cast<T*>(realloc(items, sizeof(T) * defaultCapacity));
- if (newItems == nullptr) {
- free(items);
- items = nullptr;
- throw new std::bad_alloc();
- }
- items = newItems;
- count = 0;
- capacity = defaultCapacity;
- }
- template <class T>
- void CustomVector<T>::PushBack(const T& element)
- {
- if (count == capacity) {
- T* newItems = static_cast<T*>(realloc(items, sizeof(T) * (capacity << 1)));
- if (newItems == nullptr) {
- free(items);
- items = nullptr;
- throw new std::bad_alloc();
- }
- items = newItems;
- capacity <<= 1;
- }
- items[count++] = element;
- }
- template <class T>
- void CustomVector<T>::PopBack()
- {
- Erase(count - 1);
- }
- template <class T>
- void CustomVector<T>::Erase(const int index)
- {
- if ((count == 0) || (index > count - 1) || (index < 0)) throw new std::invalid_argument("Received invalid index.");
- memmove(items + index, items + index + 1, (count - (index + 1)) * sizeof(T));
- count--;
- if ((count < capacity / 2) && (capacity > defaultCapacity))
- {
- T* newItems = static_cast<T*>(realloc(items, sizeof(T) * (capacity >> 1)));
- if (newItems == nullptr) {
- free(items);
- items = nullptr;
- throw new std::bad_alloc();
- }
- items = newItems;
- capacity >>= 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement