Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. //
  2. // Created by Peter Stanko on 8/15/16.
  3. //
  4. #ifndef CUSTOMARRAY_CUSTOMARRAY_H
  5. #define CUSTOMARRAY_CUSTOMARRAY_H
  6. #include <iostream>
  7. #include <array>
  8. namespace pb161 {
  9. template <typename T, size_t S>
  10. class CustomArray {
  11. private:
  12. std::array<T, S> array{};
  13. public:
  14. using size_type = size_t;
  15. using value_type = T;
  16. using reference = T&;
  17. using const_reference = const T&;
  18. using iterator = typename std::array<T, S>::iterator;
  19. using const_iterator = typename std::array<T, S>::const_iterator;
  20. using reverse_iterator = typename std::array<T, S>::reverse_iterator;
  21. using const_reverse_iterator = typename std::array<T, S>::const_reverse_iterator;
  22. iterator begin() {
  23. return array.begin();
  24. }
  25. const_iterator begin() const {
  26. return array.begin();
  27. }
  28. iterator end(){
  29. return array.end();
  30. }
  31. const_iterator end() const {
  32. return array.end();
  33. }
  34. reverse_iterator rbegin() {
  35. return array.rbegin();
  36. }
  37. const_reverse_iterator rbegin() const {
  38. return array.rbegin();
  39. }
  40. reverse_iterator rend() {
  41. return array.rend();
  42. }
  43. const_reverse_iterator rend() const {
  44. return array.rend();
  45. }
  46. constexpr bool empty() {
  47. return array.empty();
  48. }
  49. constexpr size_type size() {
  50. return array.size();
  51. }
  52. constexpr size_type size() const {
  53. return array.size();
  54. }
  55. reference front() {
  56. return array[0];
  57. }
  58. const_reference front() const {
  59. return array[0];
  60. }
  61. reference back() {
  62. return array[S-1];
  63. }
  64. const_reference back() const {
  65. return array[S-1];
  66. }
  67. void swap(CustomArray& array) {
  68. std::swap_ranges(begin(), end(), array.begin());
  69. }
  70. reference at(size_type i) {
  71. return array.at(i);
  72. }
  73. const_reference at(size_type i) const {
  74. return array.at(i);
  75. }
  76. reference operator[](size_type i){
  77. return array[i];
  78. }
  79. const_reference operator[](size_type i) const {
  80. return array[i];
  81. }
  82. void fill(const value_type& val) {
  83. array.fill(val);
  84. }
  85. template<class Generator>
  86. void generate(Generator generator) {
  87. iterator beginIterator = begin();
  88. iterator lastIterator = end();
  89. while (beginIterator != lastIterator) {
  90. *beginIterator = generator();
  91. beginIterator = beginIterator+1;
  92. }
  93. }
  94. template<typename X, std::size_t Y>
  95. friend bool operator==(const CustomArray<X, Y> &first, const CustomArray<X, Y> &second);
  96. template<typename X, std::size_t Y>
  97. friend bool operator!=(const CustomArray<X, Y> &first, const CustomArray<X, Y> &second);
  98. template<typename X, std::size_t Y>
  99. friend bool operator<(const CustomArray<X, Y> &first, const CustomArray<X, Y> &second);
  100. template<typename X, std::size_t Y>
  101. friend bool operator>(const CustomArray<X, Y> &first, const CustomArray<X, Y> &second);
  102. template<typename X, std::size_t Y>
  103. friend bool operator<=(const CustomArray<X, Y> &first, const CustomArray<X, Y> &second);
  104. template<typename X, std::size_t Y>
  105. friend bool operator>=(const CustomArray<X, Y> &first, const CustomArray<X, Y> &second);
  106. };
  107. template<class T, std::size_t S>
  108. void swap(CustomArray<T, S>& first, CustomArray<T, S>& second) {
  109. std::swap(first, second);
  110. }
  111. template<typename T, std::size_t S>
  112. bool operator==(const CustomArray<T, S> &first, const CustomArray<T, S> &second) {
  113. return first.array == second.array;
  114. }
  115. template<typename T, std::size_t S>
  116. bool operator<(const CustomArray<T, S> &first, const CustomArray<T, S> &second) {
  117. return first.array < second.array;
  118. }
  119. template<typename T, std::size_t S>
  120. bool operator>(const CustomArray<T, S> &first, const CustomArray<T, S> &second) {
  121. return first > second;
  122. }
  123. template<typename T, std::size_t S>
  124. bool operator<=(const CustomArray<T, S> &first, const CustomArray<T, S> &second) {
  125. return !(first > second);
  126. }
  127. template<typename T, std::size_t S>
  128. bool operator>=(const CustomArray<T, S> &first, const CustomArray<T, S> &second) {
  129. return !(first < second);
  130. }
  131. template<typename T, std::size_t S>
  132. bool operator!=(const CustomArray<T, S> &first, const CustomArray<T, S> &second) {
  133. return !(first == second);
  134. }
  135. }
  136. #endif //CUSTOMARRAY_CUSTOMARRAY_H
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement