10 #ifndef PEL_Vector_HPP_INCLUDED
11 #define PEL_Vector_HPP_INCLUDED
15 #include <initializer_list>
23 #include "./cereal/cereal.hpp"
33 void rangeCheck(std::size_t i) {
35 throw std::out_of_range{
"Out of range index"};
43 : v_{
new T[0]}, space_{v_}, last_{v_} { }
45 Vector(std::size_t size, T
const& val)
46 : v_{
new T[size]}, space_{v_ + size}, last_{space_}
49 for (std::size_t i = 0; i < size; ++i)
58 Vector(std::initializer_list<T>
const& list)
59 : v_{
new T[list.size()]}, space_{v_ + list.size()}, last_{space_}
63 for (
auto q = list.begin(); q != list.end(); ++p, ++q)
73 : v_{
new T[vec.size()]}, space_{v_ + vec.size()}, last_{space_}
76 for (std::size_t i = 0; i < vec.size(); ++i)
88 std::swap(v_, tmp.v_);
89 std::swap(space_, tmp.space_);
90 std::swap(last_, tmp.last_);
99 void push_back(T
const& val) {
100 if (space_ == last_) {
101 auto const sz = size(),
103 cp = (sz)? static_cast<std::size_t>(1.5*sz) : 2;
104 T* newBlock =
new T[cp];
106 for (std::size_t i = 0; i < sz; ++i)
128 T* erase(T* position) {
129 for (T* p = position + 1; p != space_; ++p)
135 T* insert(T* position, T
const& val) {
136 if (space_ != last_ && position == space_) {
139 }
else if (space_ != last_) {
140 *space_ = *(space_ - 1);
145 auto it1 = space_ - 1,
147 while (position != it2)
151 std::size_t
const n = position - v_;
153 auto const sz = size(),
154 cp = (sz)? static_cast<std::size_t>(1.5*size()) : 2;
155 T* newBlock =
new T[cp];
157 for (std::size_t i = 0; i < n; ++i)
160 for (std::size_t i = n; i < sz; ++i)
161 newBlock[i + 1] = v_[i];
176 void resize(std::size_t n) {
177 int const toDestroy = size() - n;
179 for (
int i = 0; i < toDestroy; ++i)
181 }
else if (toDestroy < 0) {
182 for (
int i = toDestroy; i < 0; ++i)
190 std::size_t size()
const {
return space_ - v_; }
191 std::size_t capacity()
const {
return last_ - v_; }
192 bool empty()
const {
return v_ == space_; }
197 T
const& operator[](std::size_t i)
const {
return v_[i]; }
198 T& operator[](std::size_t i) {
return v_[i]; }
200 T
const& at(std::size_t i)
const { rangeCheck(i);
return v_[i]; }
201 T& at(std::size_t i) { rangeCheck(i);
return v_[i]; }
203 T
const& front()
const {
return *v_; }
204 T& front() {
return *v_; }
206 T
const& back()
const {
return *(space_ - 1); }
207 T& back() {
return *(space_ - 1); }
212 T
const* begin()
const {
return v_; }
213 T* begin() {
return v_; }
215 T
const* end()
const {
return space_; }
216 T* end() {
return space_; }
220 template<
class Archive>
221 void save(Archive& archive)
const {
222 archive(cereal::make_size_tag(size()));
224 for (std::size_t i = 0; i < size(); i++)
228 template<
class Archive>
229 void load(Archive& ar) {
230 cereal::size_type sz;
231 ar(cereal::make_size_tag(sz));
235 for (std::size_t i = 0; i < sz; i++)
239 void writeToFile(std::string fileName) {
241 f.open(fileName.c_str(), std::ios::out);
243 f << std::to_string(size()) <<
"\n";
244 for (std::size_t i = 0; i < size(); i++) {
245 v_[i].writeToFile(&f);
249 void readFromFile(std::string fileName) {
251 f.open(fileName.c_str(), std::ios::in);
255 std::getline(f, buffer);
256 std::istringstream(buffer) >> cuantos;
260 for (std::size_t i = 0; i < size(); i++) {
261 v_[i].readFromFile(&f);
268 #endif // PEL_Vector_HPP_INCLUDED
Definition: pelVector.hpp:25
Definition: pelVector.hpp:28