|
| 1 | +#include "sion-vector.h" |
| 2 | + |
| 3 | + |
| 4 | +template<class type> |
| 5 | +sion::vector<type>::vector() |
| 6 | +{ |
| 7 | + max_length = 10; |
| 8 | + length = 0; |
| 9 | + elements = (type*)(std::malloc(max_length * sizeof(type))); |
| 10 | +} |
| 11 | + |
| 12 | +template<class type> |
| 13 | +sion::vector<type>::vector(const sion::vector<type>& obj) |
| 14 | +{ |
| 15 | + max_length = obj.max_length; |
| 16 | + length = obj.length; |
| 17 | + elements = (type*)(std::malloc(max_length * sizeof(type))); |
| 18 | + size_t i = 0; |
| 19 | + for(; i < length; i += 1) |
| 20 | + { |
| 21 | + elements[i] = obj.elements[i]; |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +template<class type> |
| 26 | +sion::vector<type>::~vector() |
| 27 | +{ |
| 28 | + std::free(elements); |
| 29 | +} |
| 30 | + |
| 31 | +template<class type> |
| 32 | +int sion::vector<type>::push_back(type item) |
| 33 | +{ |
| 34 | + try |
| 35 | + { |
| 36 | + if(length == max_length) |
| 37 | + { |
| 38 | + max_length *= 2; |
| 39 | + elements = (type*)(std::realloc(elements, max_length * sizeof(type))); |
| 40 | + elements[length] = item; |
| 41 | + length += 1; |
| 42 | + return 0; |
| 43 | + } |
| 44 | + else |
| 45 | + { |
| 46 | + elements[length] = item; |
| 47 | + length += 1; |
| 48 | + return 0; |
| 49 | + } |
| 50 | + } |
| 51 | + catch(...) |
| 52 | + { |
| 53 | + return 1; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +template<class type> |
| 58 | +type sion::vector<type>::pop_back() |
| 59 | +{ |
| 60 | + if(length == 0) |
| 61 | + { |
| 62 | + type t; |
| 63 | + return t; |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + type t = elements[length - 1]; |
| 68 | + length -= 1; |
| 69 | + return t; |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +template<class type> |
| 74 | +size_t sion::vector<type>::size() |
| 75 | +{ |
| 76 | + return length; |
| 77 | +} |
| 78 | + |
| 79 | +template<class type> |
| 80 | +int sion::vector<type>::clear() |
| 81 | +{ |
| 82 | + try |
| 83 | + { |
| 84 | + std::free(elements); |
| 85 | + max_length = 10; |
| 86 | + length = 0; |
| 87 | + elements = (type*)(std::malloc(max_length * sizeof(type))); |
| 88 | + return 0; |
| 89 | + } |
| 90 | + catch(...) |
| 91 | + { |
| 92 | + return 1; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +template<class type> |
| 97 | +void sion::vector<type>::sort() |
| 98 | +{ |
| 99 | + std::sort(begin(), end()); |
| 100 | +} |
| 101 | + |
| 102 | +template<class type> |
| 103 | +void sion::vector<type>::sort(std::string mode) |
| 104 | +{ |
| 105 | + std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower); |
| 106 | + |
| 107 | + if(mode == "asc") |
| 108 | + { |
| 109 | + std::sort(begin(), end()); |
| 110 | + } |
| 111 | + else if(mode == "desc") |
| 112 | + { |
| 113 | + std::sort(begin(), end(), std::greater<type>()); |
| 114 | + } |
| 115 | + else |
| 116 | + { |
| 117 | + throw std::invalid_argument("Please use a valid argument: asc or desc"); |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +template<class type> |
| 122 | +sion::vector<type> sion::vector<type>::sorted() |
| 123 | +{ |
| 124 | + vector<type> v = *this; |
| 125 | + std::sort(v.begin(), v.end()); |
| 126 | + return v; |
| 127 | +} |
| 128 | + |
| 129 | +template<class type> |
| 130 | +sion::vector<type> sion::vector<type>::sorted(std::string mode) |
| 131 | +{ |
| 132 | + std::transform(mode.begin(), mode.end(), mode.begin(), ::tolower); |
| 133 | + |
| 134 | + if(mode == "asc") |
| 135 | + { |
| 136 | + vector<type> v = *this; |
| 137 | + std::sort(v.begin(), v.end()); |
| 138 | + return v; |
| 139 | + } |
| 140 | + else if(mode == "desc") |
| 141 | + { |
| 142 | + vector<type> v = *this; |
| 143 | + std::sort(v.begin(), v.end(), std::greater<type>()); |
| 144 | + return v; |
| 145 | + } |
| 146 | + else |
| 147 | + { |
| 148 | + throw std::invalid_argument("Please use a valid argument: asc or desc"); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +template<class type> |
| 153 | +void sion::vector<type>::reverse() |
| 154 | +{ |
| 155 | + std::reverse(begin(), end()); |
| 156 | +} |
| 157 | + |
| 158 | +template<class type> |
| 159 | +sion::vector<type> sion::vector<type>::reversed() |
| 160 | +{ |
| 161 | + vector<type> v = *this; |
| 162 | + std::reverse(v.begin(), v.end()); |
| 163 | + return v; |
| 164 | +} |
| 165 | + |
| 166 | +template<class type> |
| 167 | +type* sion::vector<type>::begin() |
| 168 | +{ |
| 169 | + return elements; |
| 170 | +} |
| 171 | + |
| 172 | +template<class type> |
| 173 | +type* sion::vector<type>::end() |
| 174 | +{ |
| 175 | + return elements + length; |
| 176 | +} |
| 177 | + |
| 178 | +/* |
| 179 | + template<class type> |
| 180 | + type* sion::vector<type>::rbegin() |
| 181 | + { |
| 182 | + return elements + length; |
| 183 | + } |
| 184 | + */ |
| 185 | + |
| 186 | +/* |
| 187 | + template<class type> |
| 188 | + type* sion::vector<type>::rend() |
| 189 | + { |
| 190 | + return elements; |
| 191 | + } |
| 192 | + */ |
| 193 | + |
| 194 | +template<class type> |
| 195 | +sion::vector<type>& sion::vector<type>::operator =(const sion::vector<type>& obj) |
| 196 | +{ |
| 197 | + if(this != &obj) |
| 198 | + { |
| 199 | + std::free(elements); |
| 200 | + max_length = obj.max_length; |
| 201 | + length = obj.length; |
| 202 | + elements = (type*)(std::malloc(max_length * sizeof(type))); |
| 203 | + size_t i = 0; |
| 204 | + for(; i < length; i += 1) |
| 205 | + { |
| 206 | + elements[i] = obj.elements[i]; |
| 207 | + } |
| 208 | + } |
| 209 | + return *this; |
| 210 | +} |
| 211 | + |
| 212 | +template<class type> |
| 213 | +type& sion::vector<type>::operator [](size_t index) |
| 214 | +{ |
| 215 | + if((index >= length) || (index < 0)) |
| 216 | + { |
| 217 | + throw std::out_of_range("Index out of bounds."); |
| 218 | + } |
| 219 | + else |
| 220 | + { |
| 221 | + return elements[index]; |
| 222 | + } |
| 223 | +} |
| 224 | + |
0 commit comments