2022-10-09 05:13:17 +00:00
|
|
|
#ifndef _RSTL_POINTER_ITERATOR
|
|
|
|
#define _RSTL_POINTER_ITERATOR
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-10-04 00:00:46 +00:00
|
|
|
#include "stddef.h"
|
2022-10-09 05:13:17 +00:00
|
|
|
#include "types.h"
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-08-09 23:03:51 +00:00
|
|
|
#include "rstl/construct.hpp"
|
2022-11-17 15:46:10 +00:00
|
|
|
#include "rstl/iterator.hpp"
|
2022-04-10 00:17:06 +00:00
|
|
|
|
|
|
|
namespace rstl {
|
2022-09-18 05:55:13 +00:00
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
template < typename T, typename Vec, typename Alloc >
|
|
|
|
class const_pointer_iterator {
|
|
|
|
public:
|
2022-10-04 00:00:46 +00:00
|
|
|
typedef ptrdiff_t difference_type;
|
2022-09-18 05:55:13 +00:00
|
|
|
typedef random_access_iterator_tag iterator_category;
|
2022-11-27 03:04:19 +00:00
|
|
|
typedef T value_type;
|
2022-09-18 05:55:13 +00:00
|
|
|
|
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++() {
|
2022-09-19 04:19:46 +00:00
|
|
|
++this->current;
|
2022-09-18 06:05:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2022-10-04 00:00:46 +00:00
|
|
|
const_pointer_iterator operator++(int) { return const_pointer_iterator(this->current++); }
|
2022-09-18 06:05:46 +00:00
|
|
|
const_pointer_iterator& operator--() {
|
2022-09-19 04:19:46 +00:00
|
|
|
--this->current;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-10-04 00:00:46 +00:00
|
|
|
const_pointer_iterator operator--(int) { return const_pointer_iterator(this->current--); }
|
2022-09-19 04:19:46 +00:00
|
|
|
const_pointer_iterator& operator+=(int v) {
|
|
|
|
this->current += v;
|
2022-09-18 06:05:46 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2022-09-19 04:19:46 +00:00
|
|
|
const_pointer_iterator& operator-=(int v) {
|
|
|
|
this->current -= v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
const_pointer_iterator operator+(int v) const {
|
2022-10-02 21:45:14 +00:00
|
|
|
const_pointer_iterator it = *this;
|
|
|
|
return it += v;
|
2022-09-19 04:19:46 +00:00
|
|
|
}
|
|
|
|
const_pointer_iterator operator-(int v) const {
|
2022-10-02 21:45:14 +00:00
|
|
|
const_pointer_iterator it = *this;
|
|
|
|
return it -= v;
|
2022-09-19 04:19:46 +00:00
|
|
|
}
|
2022-10-04 00:00:46 +00:00
|
|
|
difference_type operator-(const const_pointer_iterator& other) const {
|
|
|
|
return this->current - other.current;
|
|
|
|
}
|
|
|
|
const T* get_pointer() const { return current; }
|
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-10-02 21:45:14 +00:00
|
|
|
bool operator<(const const_pointer_iterator& other) { return current < other.current; }
|
|
|
|
bool operator>(const const_pointer_iterator& other) { return current > other.current; }
|
|
|
|
bool operator<=(const const_pointer_iterator& other) { return current <= other.current; }
|
|
|
|
bool operator>=(const const_pointer_iterator& other) { return current >= other.current; }
|
2022-04-10 00:17:06 +00:00
|
|
|
|
2022-09-19 04:19: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 > {
|
2022-10-04 00:00:46 +00:00
|
|
|
typedef const_pointer_iterator< T, Vec, Alloc > base;
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
public:
|
2022-10-19 22:29:03 +00:00
|
|
|
typedef typename base::difference_type difference_type;
|
|
|
|
typedef typename base::iterator_category iterator_category;
|
|
|
|
typedef typename base::value_type value_type;
|
2022-10-04 00:00:46 +00:00
|
|
|
|
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); }
|
2022-10-04 00:00:46 +00:00
|
|
|
T& operator*() const { return *this->current; }
|
|
|
|
T* operator->() const { return this->current; }
|
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;
|
|
|
|
}
|
2022-11-10 10:55:04 +00:00
|
|
|
pointer_iterator operator++(int) {
|
|
|
|
return *this += 1;
|
|
|
|
}
|
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;
|
|
|
|
}
|
2022-09-21 05:18:07 +00:00
|
|
|
pointer_iterator operator--(int) { return pointer_iterator(this->current--); }
|
2022-09-19 04:19:46 +00:00
|
|
|
pointer_iterator& operator+=(int v) {
|
|
|
|
this->current += v;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
pointer_iterator& operator-=(int v) {
|
|
|
|
this->current -= v;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-10-02 21:45:14 +00:00
|
|
|
pointer_iterator operator+(int v) const {
|
|
|
|
pointer_iterator it = *this;
|
|
|
|
return it += v;
|
|
|
|
}
|
|
|
|
pointer_iterator operator-(int v) const {
|
|
|
|
pointer_iterator it = *this;
|
|
|
|
return it -= v;
|
|
|
|
}
|
2022-10-04 00:00:46 +00:00
|
|
|
// HACK: non-const operator- is required to match vector::insert
|
2022-10-09 05:13:17 +00:00
|
|
|
difference_type operator-(const pointer_iterator& other) { return this->current - other.current; }
|
2022-04-10 00:17:06 +00:00
|
|
|
};
|
2022-10-04 00:00:46 +00:00
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
struct const_counting_iterator {
|
|
|
|
const T* ptr;
|
|
|
|
int count;
|
|
|
|
|
|
|
|
const_counting_iterator(const T* ptr, int count) : ptr(ptr), count(count) {}
|
2022-11-21 22:54:14 +00:00
|
|
|
|
|
|
|
const_counting_iterator& operator++() {
|
|
|
|
++this->count;
|
|
|
|
return *this;
|
|
|
|
}
|
2022-10-04 00:00:46 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template < typename It >
|
2022-12-07 17:18:48 +00:00
|
|
|
typename It::difference_type __distance(It first, It last, random_access_iterator_tag) {
|
2022-10-04 00:00:46 +00:00
|
|
|
return last - first;
|
|
|
|
}
|
|
|
|
|
2022-12-05 21:35:31 +00:00
|
|
|
template < typename It, typename S >
|
2022-12-07 17:18:48 +00:00
|
|
|
void __advance(It& it, S count, random_access_iterator_tag) {
|
2022-12-05 21:35:31 +00:00
|
|
|
it += count;
|
|
|
|
}
|
2022-11-17 15:46:10 +00:00
|
|
|
|
2022-11-27 03:04:19 +00:00
|
|
|
template < typename T >
|
|
|
|
struct iterator_traits {};
|
|
|
|
|
|
|
|
template < typename T >
|
|
|
|
struct iterator_traits<T*> {
|
|
|
|
typedef T value_type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template < typename T, typename Vec, typename Alloc >
|
|
|
|
struct iterator_traits< pointer_iterator<T, Vec, Alloc> > {
|
|
|
|
typedef typename pointer_iterator<T, Vec, Alloc>::value_type value_type;
|
|
|
|
};
|
|
|
|
|
2022-04-10 00:17:06 +00:00
|
|
|
} // namespace rstl
|
|
|
|
|
2022-10-09 05:13:17 +00:00
|
|
|
#endif // _RSTL_POINTER_ITERATOR
|