2022-04-10 00:17:06 +00:00
|
|
|
#ifndef _RSTL_POINTER_ITERATOR_HPP
|
|
|
|
#define _RSTL_POINTER_ITERATOR_HPP
|
|
|
|
|
|
|
|
#include "types.h"
|
|
|
|
|
2022-08-09 23:03:51 +00:00
|
|
|
#include "rstl/construct.hpp"
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
namespace rstl {
|
2022-09-18 05:55:13 +00:00
|
|
|
struct input_iterator_tag {};
|
|
|
|
struct output_iterator_tag {};
|
|
|
|
struct forward_iterator_tag : public input_iterator_tag {};
|
|
|
|
struct bidirectional_iterator_tag : public forward_iterator_tag {};
|
|
|
|
struct random_access_iterator_tag : public bidirectional_iterator_tag {};
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
template < typename T, typename Vec, typename Alloc >
|
|
|
|
class const_pointer_iterator {
|
|
|
|
public:
|
2022-09-18 05:55:13 +00:00
|
|
|
typedef random_access_iterator_tag iterator_category;
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
const_pointer_iterator() : current(nullptr) {}
|
2022-09-18 17:51:07 +00:00
|
|
|
const_pointer_iterator(const T* begin) : current(const_cast< T* >(begin)) {}
|
2022-09-18 06:05:46 +00:00
|
|
|
const_pointer_iterator& operator++() {
|
|
|
|
++current;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_pointer_iterator& operator--() {
|
|
|
|
--current;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-09-18 17:51:07 +00:00
|
|
|
const T& operator*() const { return *current; }
|
|
|
|
const T* operator->() const { return current; }
|
2022-04-10 00:17:06 +00:00
|
|
|
bool operator==(const const_pointer_iterator& other) { return current == other.current; }
|
|
|
|
bool operator!=(const const_pointer_iterator& other) { return current != other.current; }
|
|
|
|
|
2022-09-18 06:05:46 +00:00
|
|
|
friend const_pointer_iterator operator+(const const_pointer_iterator& x, int v) {
|
|
|
|
return const_pointer_iterator(x.current + v);
|
|
|
|
}
|
|
|
|
friend const_pointer_iterator operator-(const const_pointer_iterator& x, int v) {
|
|
|
|
return const_pointer_iterator(x.current - v);
|
|
|
|
}
|
2022-09-18 17:51:07 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
T* current;
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template < typename T, typename Vec, typename Alloc >
|
|
|
|
class pointer_iterator : public const_pointer_iterator< T, Vec, Alloc > {
|
|
|
|
public:
|
2022-08-26 03:46:24 +00:00
|
|
|
pointer_iterator() : const_pointer_iterator< T, Vec, Alloc >(nullptr) {}
|
|
|
|
pointer_iterator(T* begin) : const_pointer_iterator< T, Vec, Alloc >(begin) {}
|
2022-09-18 17:51:07 +00:00
|
|
|
void operator=(const T& other) { rstl::construct(this->current, other); }
|
|
|
|
T& operator*() { return *this->current; }
|
|
|
|
T* operator->() { return this->current; }
|
2022-04-10 00:17:06 +00:00
|
|
|
void destroy() const {
|
2022-09-18 17:51:07 +00:00
|
|
|
if (this->current != nullptr) {
|
|
|
|
rstl::destroy(this->current);
|
2022-04-10 00:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-18 06:05:46 +00:00
|
|
|
pointer_iterator& operator++() {
|
2022-09-18 17:51:07 +00:00
|
|
|
++this->current;
|
2022-09-18 06:05:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
pointer_iterator& operator--() {
|
2022-09-18 17:51:07 +00:00
|
|
|
--this->current;
|
2022-09-18 06:05:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-09-18 06:05:46 +00:00
|
|
|
friend pointer_iterator operator+(const pointer_iterator& x, int v) {
|
2022-09-18 17:51:07 +00:00
|
|
|
return pointer_iterator(x.current + v);
|
2022-09-18 06:05:46 +00:00
|
|
|
}
|
|
|
|
friend pointer_iterator operator-(const pointer_iterator& x, int v) {
|
2022-09-18 17:51:07 +00:00
|
|
|
return pointer_iterator(x.current - v);
|
2022-09-18 06:05:46 +00:00
|
|
|
}
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
|
|
|
} // namespace rstl
|
|
|
|
|
|
|
|
#endif
|