| 1 |
#include <algorithm> |
| 2 |
/* |
| 3 |
template <typename Iterator> |
| 4 |
struct iterator_types |
| 5 |
{ |
| 6 |
using pointer = Iterator::pointer; |
| 7 |
using reference = Iterator::reference; |
| 8 |
}; |
| 9 |
*/ |
| 10 |
template <typename T> |
| 11 |
struct iterator_types //<T *> |
| 12 |
{ |
| 13 |
using pointer = T *; |
| 14 |
using reference = T &; |
| 15 |
}; |
| 16 |
|
| 17 |
template <typename Iterator> |
| 18 |
class reverse_iter |
| 19 |
{ |
| 20 |
public: |
| 21 |
using pointer = iterator_types<Iterator>::pointer; |
| 22 |
using reference = iterator_types<Iterator>::reference; |
| 23 |
|
| 24 |
explicit reverse_iter(Iterator x) |
| 25 |
: |
| 26 |
current(x) |
| 27 |
{} |
| 28 |
reference operator*() const |
| 29 |
{ |
| 30 |
Iterator tmp = current; |
| 31 |
return (*--tmp); |
| 32 |
} |
| 33 |
pointer operator->() const |
| 34 |
{ |
| 35 |
return &(operator*()); |
| 36 |
} |
| 37 |
reverse_iter<Iterator>& operator++() |
| 38 |
{ |
| 39 |
--current; |
| 40 |
return (*this); |
| 41 |
} |
| 42 |
reverse_iter<Iterator> operator++(int) |
| 43 |
{ |
| 44 |
reverse_iter<Iterator> tmp(current--); |
| 45 |
return (tmp); |
| 46 |
} |
| 47 |
bool operator!=(reverse_iter<Iterator> const &other) |
| 48 |
{ |
| 49 |
return (current != other.current); |
| 50 |
} |
| 51 |
private: |
| 52 |
Iterator current; |
| 53 |
}; |
| 54 |
|
| 55 |
template <typename Type> |
| 56 |
class Vector |
| 57 |
{ |
| 58 |
using iterator = Type *; |
| 59 |
using reverse_iterator = reverse_iter<iterator>; |
| 60 |
|
| 61 |
public: |
| 62 |
Vector() |
| 63 |
{ |
| 64 |
init(0); |
| 65 |
}; |
| 66 |
Vector(size_t n) |
| 67 |
{ |
| 68 |
init(n); |
| 69 |
} |
| 70 |
Vector(Vector<Type> const &other) |
| 71 |
{ |
| 72 |
construct(other); |
| 73 |
} |
| 74 |
~Vector() |
| 75 |
{ |
| 76 |
delete[] start; |
| 77 |
} |
| 78 |
Vector<Type> const &operator=(Vector<Type> const &other) |
| 79 |
{ |
| 80 |
if (this != &other) |
| 81 |
{ |
| 82 |
delete[] start; |
| 83 |
construct(other); |
| 84 |
} |
| 85 |
return (*this); |
| 86 |
} |
| 87 |
Type &operator[](size_t index) |
| 88 |
{ |
| 89 |
if (index > (finish - start)) |
| 90 |
throw "Vector array index out of bounds"; |
| 91 |
return (start[index]); |
| 92 |
} |
| 93 |
Vector<Type> &sort() |
| 94 |
{ |
| 95 |
::sort(start, finish); |
| 96 |
return (*this); |
| 97 |
} |
| 98 |
void push_back(Type const &value) |
| 99 |
{ |
| 100 |
if (!finish) |
| 101 |
init(1); |
| 102 |
else if (finish == end_of_storage) |
| 103 |
{ |
| 104 |
Vector<Type> |
| 105 |
enlarged((end_of_storage - start) << 1); |
| 106 |
copy(start, finish, enlarged.start); |
| 107 |
delete[] start; |
| 108 |
finish = enlarged.start + (finish - start); |
| 109 |
start = enlarged.start; |
| 110 |
end_of_storage = enlarged.end_of_storage; |
| 111 |
enlarged.start = 0; |
| 112 |
} |
| 113 |
*finish++ = value; |
| 114 |
} |
| 115 |
iterator begin() |
| 116 |
{ |
| 117 |
return (start); |
| 118 |
} |
| 119 |
iterator end() |
| 120 |
{ |
| 121 |
return (finish); |
| 122 |
} |
| 123 |
reverse_iterator rbegin() |
| 124 |
{ |
| 125 |
return (reverse_iterator(finish)); |
| 126 |
} |
| 127 |
reverse_iterator rend() |
| 128 |
{ |
| 129 |
return (reverse_iterator(start)); |
| 130 |
} |
| 131 |
size_t size() |
| 132 |
{ |
| 133 |
return (finish - start); |
| 134 |
} |
| 135 |
private: |
| 136 |
void init(size_t n) |
| 137 |
{ |
| 138 |
if (n) |
| 139 |
{ |
| 140 |
start = new Type[n]; |
| 141 |
finish = start + n; |
| 142 |
end_of_storage = start + n; |
| 143 |
} |
| 144 |
else |
| 145 |
{ |
| 146 |
start = 0; |
| 147 |
finish = 0; |
| 148 |
end_of_storage = 0; |
| 149 |
} |
| 150 |
} |
| 151 |
void construct(Vector<Type> const &other) |
| 152 |
{ |
| 153 |
init(other.finish - other.start); |
| 154 |
copy(other.start, other.finish, start); |
| 155 |
} |
| 156 |
|
| 157 |
iterator |
| 158 |
start, |
| 159 |
finish, |
| 160 |
end_of_storage; |
| 161 |
}; |