Lots of stuff

Former-commit-id: 39b5f3c01e
This commit is contained in:
2022-10-03 20:00:46 -04:00
parent 469c499a6b
commit bc202ba493
47 changed files with 701 additions and 312 deletions

View File

@@ -2,6 +2,7 @@
#define _RSTL_POINTER_ITERATOR_HPP
#include "types.h"
#include "stddef.h"
#include "rstl/construct.hpp"
@@ -15,6 +16,7 @@ struct random_access_iterator_tag : public bidirectional_iterator_tag {};
template < typename T, typename Vec, typename Alloc >
class const_pointer_iterator {
public:
typedef ptrdiff_t difference_type;
typedef random_access_iterator_tag iterator_category;
typedef T* value_type;
@@ -24,12 +26,12 @@ public:
++this->current;
return *this;
}
const_pointer_iterator& operator++(int) { return const_pointer_iterator(this->current++); }
const_pointer_iterator operator++(int) { return const_pointer_iterator(this->current++); }
const_pointer_iterator& operator--() {
--this->current;
return *this;
}
const_pointer_iterator& operator--(int) { return const_pointer_iterator(this->current--); }
const_pointer_iterator operator--(int) { return const_pointer_iterator(this->current--); }
const_pointer_iterator& operator+=(int v) {
this->current += v;
return *this;
@@ -46,7 +48,10 @@ public:
const_pointer_iterator it = *this;
return it -= v;
}
int operator-(const const_pointer_iterator& other) const { return this->current - other.current; }
difference_type operator-(const const_pointer_iterator& other) const {
return this->current - other.current;
}
const T* get_pointer() const { return current; }
const T& operator*() const { return *current; }
const T* operator->() const { return current; }
bool operator==(const const_pointer_iterator& other) { return current == other.current; }
@@ -69,13 +74,18 @@ protected:
template < typename T, typename Vec, typename Alloc >
class pointer_iterator : public const_pointer_iterator< T, Vec, Alloc > {
typedef const_pointer_iterator< T, Vec, Alloc > base;
public:
typedef base::difference_type difference_type;
typedef base::iterator_category iterator_category;
typedef base::value_type value_type;
pointer_iterator() : const_pointer_iterator< T, Vec, Alloc >(nullptr) {}
pointer_iterator(T* begin) : const_pointer_iterator< T, Vec, Alloc >(begin) {}
void operator=(const T& other) { rstl::construct(this->current, other); }
T& operator*() { return *this->current; }
T* operator->() { return this->current; }
void destroy() const { rstl::destroy(this->current); }
T& operator*() const { return *this->current; }
T* operator->() const { return this->current; }
pointer_iterator& operator++() {
++this->current;
return *this;
@@ -102,7 +112,29 @@ public:
pointer_iterator it = *this;
return it -= v;
}
// HACK: non-const operator- is required to match vector::insert
difference_type operator-(const pointer_iterator& other) {
return this->current - other.current;
}
};
template < typename T >
struct const_counting_iterator {
const T* ptr;
int count;
const_counting_iterator(const T* ptr, int count) : ptr(ptr), count(count) {}
};
template < typename It >
inline typename It::difference_type __distance(It first, It last, random_access_iterator_tag) {
return last - first;
}
template < typename It >
inline typename It::difference_type distance(It first, It last) {
return __distance(first, last, It::iterator_category());
}
} // namespace rstl
#endif