Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma once
- #include <iostream>
- template <typename T>
- class TemplateArray
- {
- private:
- T *arr;
- int capacity;
- void CopyElements(T *, int);
- void CopyElements(T *, int, int);
- void CopyElements(T * a, T * source, int len, int start = 0);
- void Shift(int);
- //void Shift(int *, int, int);
- void Swap(T *, T *);
- bool IsTemplateArraysEq(T *, int);
- public:
- //void Scan(int);
- void Print();
- int Find(T);
- int & operator [](int);
- TemplateArray & operator = (TemplateArray &x);
- TemplateArray operator += (const T);
- TemplateArray operator + (T);
- TemplateArray operator += (TemplateArray);
- TemplateArray operator + (TemplateArray);
- TemplateArray operator -= (int);
- TemplateArray operator -(T);
- bool operator ==(TemplateArray);
- bool operator !=(TemplateArray);
- //friend std::ostream& operator << (std::ostream &r, const TemplateArray &x)
- //{
- // for (int i = 0; i < x.capacity; i++)
- // {
- // r << x.arr[i] << ' ';
- // }
- // return r;
- //}
- //friend std::istream & operator >> (std::istream &r, TemplateArray &x)
- //{
- // int tempNum;
- // r >> tempNum;
- // x += tempNum;
- // return r;
- //}
- TemplateArray(int Capacity = 0);
- TemplateArray(T *, int);
- TemplateArray(const TemplateArray &x);
- ~TemplateArray();
- };
- #include <bitset>
- #include "TemplateArray.h"
- #define INT16_MAX 2147483647
- #pragma region PrivateFunctions
- template <typename T>
- void TemplateArray<T>::CopyElements(T *source, int len)
- {
- for (int i = 0; i < len; i++)
- {
- arr[i] = source[i];
- }
- }
- template <typename T>
- void TemplateArray<T>::CopyElements(T *source, int len, int start)
- {
- for (int i = 0; i < len; i++)
- {
- arr[i + start] = source[i];
- }
- }
- template <typename T>
- void TemplateArray<T>::CopyElements(T *a, T *source, int len, int start)
- {
- for (int i = 0; i < len; i++)
- {
- a[i + start] = source[i];
- }
- }
- //template <typename T>
- //void TemplateArray<T>::Shift(T *a, int start, int end)
- //{
- // for (int i = end - 1; i >= start; i--)
- // {
- // a[i + 1] = a[i];
- // }
- //}
- template <typename T>
- void TemplateArray<T>::Shift(int index)
- {
- for (int i = index; i < capacity; i++)
- {
- arr[i] = arr[i + 1];
- }
- capacity--;
- }
- template <typename T>
- bool TemplateArray<T>::IsTemplateArraysEq(T *a, int len)
- {
- for (int i = 0; i < len; i++)
- {
- if (arr[i] != a[i])
- return false;
- }
- return true;
- }
- template <typename T>
- void TemplateArray<T>::Swap(T *xp, T *yp)
- {
- int temp = *xp;
- *xp = *yp;
- *yp = temp;
- }
- //
- //void TemplateArray::BubbleSort(int *a, int n)
- //{
- // int i, j;
- // bool isSwapped;
- // for (i = 0; i < n - 1; i++)
- // {
- // isSwapped = false;
- // for (j = 0; j < n - i - 1; j++)
- // {
- // if (a[j] > a[j + 1])
- // {
- // Swap(&a[j], &a[j + 1]);
- // isSwapped = true;
- // }
- // }
- // if (isSwapped == false)
- // break;
- // }
- //}
- //
- //void TemplateArray::InsertionSort(int *a, int n)
- //{
- // int curentValue;
- // for (int i = 1; i < n; i++)
- // {
- // for (int j = 0; j < i; j++)
- // {
- // curentValue = a[i];
- // if (a[i] < a[j])
- // {
- // Shift(a, j, i);
- // a[j] = curentValue;
- // }
- // }
- // }
- //}
- //
- //void TemplateArray::ShellSort(int *a, int n)
- //{
- // int i;
- // int value;
- // for (int p = n / 2; p >= 0; p--)
- // {
- // for (int q = 0; q < p; q++)
- // {
- // for (int j = q + p; j < n; j += p)
- // {
- // for (i = j - p, value = a[j]; i >= 0 && a[i] > value; i -= p)
- // {
- // a[i + p] = a[i];
- // }
- // a[i + p] = value;
- // }
- // }
- // }
- //}
- //
- //void TemplateArray::MoveElement(int *a, int n, int index)
- //{
- // int j = 2 * index + 1;
- // if (j > n)
- // return;
- // int value = a[index];
- // int i = index;
- // if (a[j + 1] > a[j] && j + 1 < n)
- // {
- // j++;
- // }
- // while (a[j] > value)
- // {
- // a[i] = a[j];
- // i = j;
- // j = 2 * i + 1;
- // if (j >= n)
- // {
- // break;
- // }
- // if (j + 1 < n && a[j + 1] > a[j])
- // {
- // j++;
- // }
- // }
- // a[i] = value;
- //}
- //
- //void TemplateArray::BuildPyramid(int *a, int n)
- //{
- // for (int curentIndex = n / 2 - 1; curentIndex >= 0; curentIndex--)
- // {
- // MoveElement(a, n, curentIndex);
- // }
- //}
- //
- //void TemplateArray::PyramidSort(int *a, int n)
- //{
- // BuildPyramid(a, n);
- // for (int N = n; N > 0; )
- // {
- // Swap(&a[0], &a[N - 1]);
- // N--;
- // MoveElement(a, N, 0);
- // //BuildPyramid(a, N);
- // }
- // Swap(&a[0], &a[1]);
- //}
- //
- //void TemplateArray::BubbleSort(int *a, int n, int start, int end)
- //{
- // int i, j;
- // bool isSwapped;
- // for (i = start; i < end - 1; i++)
- // {
- // isSwapped = false;
- // for (j = 0; j < n - i - 1; j++)
- // {
- // if (a[j] > a[j + 1])
- // {
- // Swap(&a[j], &a[j + 1]);
- // isSwapped = true;
- // }
- // }
- // if (isSwapped == false)
- // break;
- // }
- //}
- //
- //void TemplateArray::QuickSort(int *a, int n, int start, int end)
- //{
- // int i = start;
- // int j = end;
- // int pillar = a[(end + start) / 2];
- //
- // while (i < j)
- // {
- // for (; a[i] < pillar && i <= end; i++);
- // for (; a[j] > pillar && j >= start; j--);
- // if (i <= j)
- // {
- // Swap(&a[i], &a[j]);
- // i++;
- // j--;
- // }
- // }
- // if (start < j)
- // {
- // QuickSort(a, n, start, j);
- // }
- // if (end > i)
- // {
- // QuickSort(a, n, i, end);
- // }
- //}
- //
- //void PrintTemplateArray(int *a, int n, int mask)
- //{
- // std::cout << "Mask: \n" << std::bitset<4>(mask) << "\n\n";
- // for (int i = 0; i < n; i++)
- // {
- // std::cout << std::bitset<4>(a[i]);
- // std::cout << '\n';
- // }
- // std::cout << '\n';
- //}
- //
- //void TemplateArray::ByteSort(int *a, int n, int start, int end, int cmpByte)
- //{
- // int i = start;
- // int j = end;
- // if (i <= j)
- // {
- // int mask = 1 << cmpByte;
- // if (mask <= 16)
- // {
- // //PrintTemplateArray(a, n, mask);
- // }
- // while (i <= j)
- // {
- // for (; i < j && (a[i] & mask); i++);
- // for (; i < j && !(a[j] & mask); j--);
- // if (i <= j)
- // {
- // //std::cout << "Swap " << a[i] << " and " << a[j] << '\n';
- // Swap(&a[i], &a[j]);
- // i++;
- // j--;
- // }
- // }
- // if (start < j)
- // {
- // ByteSort(a, n, start, j, cmpByte - 1);
- // }
- // if (i < end)
- // {
- // ByteSort(a, n, i, end, cmpByte - 1);
- // }
- // }
- //}
- #pragma endregion
- #pragma region PublicFunctions
- //template <typename T>
- //void TemplateArray<T>::Scan(int m)
- //{
- // int *buffer = new T[m + capacity];
- // CopyElements(buffer, arr, capacity)
- // for (int i = 0; i < m; i++)
- // {
- // std::cin >> buffer[i + capacity];
- // }
- // capacity += m;
- // delete arr;
- // arr = buffer;
- //}
- template <typename T>
- void TemplateArray<T>::Print()
- {
- for (int i = 0; i < capacity; i++)
- {
- std::cout << arr[i] << ' ';
- }
- std::cout << '\n';
- }
- //int TemplateArray::Max()
- //{
- // int maxIndex = 0;
- // for (int i = 1; i < capacity; i++)
- // {
- // if (arr[i] > arr[maxIndex])
- // {
- // maxIndex = i;
- // }
- // }
- // return maxIndex;
- //}
- //
- //int TemplateArray::Min()
- //{
- // int minIndex = 0;
- // for (int i = 1; i < capacity; i++)
- // {
- // if (arr[i] < arr[minIndex])
- // {
- // minIndex = i;
- // }
- // }
- // return minIndex;
- //}
- template <typename T>
- int TemplateArray<T>::Find(T key)
- {
- for (int i = 0; i < capacity; i++)
- {
- if (arr[i] == key)
- {
- return i;
- }
- }
- return -1;
- }
- //void TemplateArray::Sort(sortType a = sortType::bubbleSort)
- //{
- // switch (a)
- // {
- // case bubbleSort:
- // {
- // BubbleSort(arr, capacity);
- // break;
- // }
- // case insertionSort:
- // {
- // InsertionSort(arr, capacity);
- // break;
- // }
- // case shellSort:
- // {
- // ShellSort(arr, capacity);
- // break;
- // }
- // case pyramidSort:
- // {
- // PyramidSort(arr, capacity);
- // break;
- // }
- // case quickSort:
- // {
- // QuickSort(arr, capacity, 0, capacity - 1);
- // break;
- // }
- // case byteSort:
- // {
- // ByteSort(arr, capacity, 0, capacity - 1, 4);
- // //ByteSort(arr, size, 0, size - 1, 31);
- // break;
- // }
- // default:
- // {
- // std::cout << "Unexpected argument" << std::endl;
- // throw std::exception("Couldnt find function for this argument");
- // break;
- // }
- // }
- //}
- #pragma endregion
- #pragma region Operators
- template <typename T>
- int &TemplateArray<T>::operator [] (int i)
- {
- return arr[i];
- //throw std::exception("Index out of TemplateArray!");
- }
- template <typename T>
- TemplateArray<T> TemplateArray<T>::operator += (const T key)
- {
- int *buffer = new int[capacity + 1];
- CopyElements(buffer, arr, capacity);
- buffer[capacity] = key;
- delete arr;
- arr = buffer;
- capacity++;
- return *this;
- }
- template <typename T>
- TemplateArray<T> TemplateArray<T>::operator + (T key)
- {
- TemplateArray result(*this);
- result += key;
- return result;
- }
- template <typename T>
- TemplateArray<T> TemplateArray<T>::operator += (TemplateArray a)
- {
- int *buffer = new int[capacity + a.capacity];
- CopyElements(buffer, arr, capacity);
- delete arr;
- arr = buffer;
- CopyElements(arr, a.arr, a.capacity, capacity);
- capacity = capacity + a.capacity;
- return *this;
- }
- template <typename T>
- TemplateArray<T> TemplateArray<T>::operator + (TemplateArray a)
- {
- TemplateArray result(capacity + a.capacity);
- CopyElements(result.arr, arr, capacity);
- CopyElements(result.arr, a.arr, a.capacity, capacity);
- return result;
- }
- template <typename T>
- TemplateArray<T> & TemplateArray<T>::operator = (TemplateArray &x)
- {
- delete arr;
- arr = new int[x.capacity];
- CopyElements(x.arr, x.capacity);
- capacity = x.capacity;
- return *this;
- }
- template <typename T>
- TemplateArray<T> TemplateArray<T>::operator -= (int index)
- {
- Shift(index);
- return *this;
- }
- template <typename T>
- TemplateArray<T> TemplateArray<T>::operator -(T key)
- {
- for (int i = 0; i < capacity; i++)
- {
- if (key == arr[i])
- {
- Shift(i);
- break;
- }
- }
- return *this;
- }
- template <typename T>
- bool TemplateArray<T>::operator ==(TemplateArray a)
- {
- if (a.capacity != capacity)
- return false;
- return IsTemplateArraysEq(a.arr, a.capacity);
- }
- template <typename T>
- bool TemplateArray<T>::operator !=(TemplateArray a)
- {
- if (a.capacity != capacity)
- return true;
- return !IsTemplateArraysEq(a.arr, a.capacity);
- }
- #pragma endregion
- #pragma region Contructors
- template <typename T>
- TemplateArray<T>::TemplateArray(int Capacity)
- {
- arr = new T[Capacity];
- capacity = Capacity;
- }
- template <typename T>
- TemplateArray<T>::TemplateArray(T *a, int Capacity)
- {
- arr = a;
- capacity = Capacity;
- }
- template <typename T>
- TemplateArray<T>::TemplateArray(const TemplateArray & x)
- {
- if (!arr)
- delete []arr;
- arr = new T[x.capacity];
- CopyElements(x.arr, x.capacity);
- capacity = x.capacity;
- }
- template <typename T>
- TemplateArray<T>::~TemplateArray()
- {
- delete []arr;
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement