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 {
|
|
|
|
template < typename T, typename Vec, typename Alloc >
|
|
|
|
class const_pointer_iterator {
|
|
|
|
protected:
|
|
|
|
const T* current;
|
|
|
|
|
|
|
|
public:
|
|
|
|
const_pointer_iterator() : current(nullptr) {}
|
|
|
|
const_pointer_iterator(const T* begin) : current(begin) {}
|
|
|
|
void operator++() { ++current; }
|
|
|
|
void operator--() { --current; }
|
|
|
|
T* get_pointer() const { return const_cast< T* >(current); }
|
|
|
|
const T& operator*() const { return *get_pointer(); }
|
|
|
|
const T* operator->() const { return get_pointer(); }
|
|
|
|
bool CheckValid() const { return current != nullptr; }
|
|
|
|
bool operator==(const const_pointer_iterator& other) { return current == other.current; }
|
|
|
|
bool operator!=(const const_pointer_iterator& other) { return current != other.current; }
|
|
|
|
|
|
|
|
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); }
|
|
|
|
};
|
|
|
|
|
|
|
|
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-04-10 00:17:06 +00:00
|
|
|
void operator=(const T& other) {
|
2022-08-26 03:46:24 +00:00
|
|
|
if (this->CheckValid()) {
|
2022-04-10 00:17:06 +00:00
|
|
|
*get_pointer() = other;
|
|
|
|
}
|
|
|
|
}
|
2022-08-26 03:46:24 +00:00
|
|
|
T* get_pointer() const { return const_cast< T* >(this->current); }
|
2022-04-10 00:17:06 +00:00
|
|
|
// T* operator*() const { if (CheckValid()) return get_pointer(); else return
|
|
|
|
// nullptr; }
|
|
|
|
T* operator->() const { return get_pointer(); }
|
|
|
|
void destroy() const {
|
2022-08-26 03:46:24 +00:00
|
|
|
if (this->CheckValid()) {
|
2022-04-10 00:17:06 +00:00
|
|
|
rstl::destroy(get_pointer());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
friend pointer_iterator operator+(const pointer_iterator& x, int v) { return pointer_iterator(x.get_pointer() + v); }
|
|
|
|
friend pointer_iterator operator-(const pointer_iterator& x, int v) { return pointer_iterator(x.get_pointer() - v); }
|
|
|
|
};
|
|
|
|
} // namespace rstl
|
|
|
|
|
|
|
|
#endif
|