|
@ -1,22 +1,37 @@ |
|
|
#pragma once
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
#include <cstddef>
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
|
|
|
|
|
|
// BUG: none of this is in a namespace
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief An enumeration that may be used to determine iterator categories |
|
|
|
|
|
* |
|
|
|
|
|
*/ |
|
|
enum class iterator_type_t{ |
|
|
enum class iterator_type_t{ |
|
|
contiguous_iterator, |
|
|
|
|
|
non_contiguous_iterator, |
|
|
|
|
|
lazy_iterator |
|
|
|
|
|
|
|
|
contiguous_iterator, /**< Defines an iterator for something that is continuous and random access */ |
|
|
|
|
|
non_contiguous_iterator, /**< Defines an iterator for a non contiguous datastructure, for example an iterator over a hashmap or a tree*/ |
|
|
|
|
|
lazy_iterator /**< Defines an iterator for which the actual data layout and availability are still unknown*/ |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief An abstraction of a pointer to iterate against, in both normal and reverse order |
|
|
|
|
|
* |
|
|
|
|
|
* @tparam T The type of data pointed by the iterator |
|
|
|
|
|
* @tparam sign the direction in which data is scrutinized, should be either 1 or -1, behaviour for other value is left undefined |
|
|
|
|
|
*/ |
|
|
template<typename T, int sign = 1> |
|
|
template<typename T, int sign = 1> |
|
|
struct pointer_iterator final |
|
|
struct pointer_iterator final |
|
|
{ |
|
|
{ |
|
|
T* data; |
|
|
|
|
|
typedef T value_type; |
|
|
|
|
|
typedef std::size_t difference_type; |
|
|
|
|
|
static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
T* data; /**< the only data field of the class */ |
|
|
|
|
|
typedef T value_type; /**< The type of which a reference will be returned on dereferencing */ |
|
|
|
|
|
typedef std::size_t difference_type; /**< The type of the substraction of two pointers */ |
|
|
|
|
|
static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator; /**< @see iterator_type_t */ |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief Generates an empty iterator |
|
|
|
|
|
*/ |
|
|
constexpr pointer_iterator() |
|
|
constexpr pointer_iterator() |
|
|
: data{nullptr} |
|
|
: data{nullptr} |
|
|
{} |
|
|
{} |
|
@ -25,11 +40,19 @@ struct pointer_iterator final |
|
|
: data{oth.data} |
|
|
: data{oth.data} |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief Generates an iterator from any pointer |
|
|
|
|
|
*/ |
|
|
constexpr pointer_iterator(T* ptr) |
|
|
constexpr pointer_iterator(T* ptr) |
|
|
: data{ptr} |
|
|
: data{ptr} |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
constexpr T& operator*() |
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief Dereference unary operator |
|
|
|
|
|
* |
|
|
|
|
|
* @return constexpr T& returns a reference to the pointed value |
|
|
|
|
|
*/ |
|
|
|
|
|
constexpr T& operator*() const |
|
|
{ |
|
|
{ |
|
|
return *data; |
|
|
return *data; |
|
|
} |
|
|
} |
|
@ -60,22 +83,22 @@ struct pointer_iterator final |
|
|
return p; |
|
|
return p; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator+(const std::size_t offset) |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator+(const std::size_t offset) const |
|
|
{ |
|
|
{ |
|
|
return pointer_iterator{data+sign*offset}; |
|
|
return pointer_iterator{data+sign*offset}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator+(const int offset) |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator+(const int offset) const |
|
|
{ |
|
|
{ |
|
|
return pointer_iterator{data+sign*offset}; |
|
|
return pointer_iterator{data+sign*offset}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator-(const std::size_t offset) |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator-(const std::size_t offset) const |
|
|
{ |
|
|
{ |
|
|
return pointer_iterator{data-sign*offset}; |
|
|
return pointer_iterator{data-sign*offset}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator-(const int offset) |
|
|
|
|
|
|
|
|
constexpr pointer_iterator operator-(const int offset) const |
|
|
{ |
|
|
{ |
|
|
return pointer_iterator{data-sign*offset}; |
|
|
return pointer_iterator{data-sign*offset}; |
|
|
} |
|
|
} |
|
@ -85,44 +108,57 @@ struct pointer_iterator final |
|
|
return ((T*)data-(T*)oth.data)*sign; |
|
|
return ((T*)data-(T*)oth.data)*sign; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool operator==(const pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool operator==(const pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return data==oth.data; |
|
|
return data==oth.data; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool operator!=(pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool operator!=(pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return data!=oth.data; |
|
|
return data!=oth.data; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool before_or_equal(const pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool before_or_equal(const pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data); |
|
|
return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool operator<=(const pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool operator<=(const pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return before_or_equal(oth); |
|
|
return before_or_equal(oth); |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief An identical twin to the pointer_iterator, but which dereference to a const reference |
|
|
|
|
|
* |
|
|
|
|
|
* @see pointer_iterator |
|
|
|
|
|
* @tparam T @see pointer_iterator |
|
|
|
|
|
* @tparam sign @see pointer_iterator |
|
|
|
|
|
*/ |
|
|
template<typename T, int sign = 1> |
|
|
template<typename T, int sign = 1> |
|
|
struct const_pointer_iterator final |
|
|
struct const_pointer_iterator final |
|
|
{ |
|
|
{ |
|
|
const T* data; |
|
|
|
|
|
typedef T value_type; |
|
|
|
|
|
typedef std::size_t difference_type; |
|
|
|
|
|
static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator; |
|
|
|
|
|
|
|
|
const T* data; /**< @see pointer_iterator */ |
|
|
|
|
|
typedef T value_type; /**< @see pointer_iterator */ |
|
|
|
|
|
typedef std::size_t difference_type; /**< @see pointer_iterator */ |
|
|
|
|
|
static constexpr iterator_type_t iterator_type = iterator_type_t::contiguous_iterator; /**< @see pointer_iterator */ |
|
|
|
|
|
|
|
|
constexpr const_pointer_iterator(const const_pointer_iterator& oth) |
|
|
constexpr const_pointer_iterator(const const_pointer_iterator& oth) |
|
|
: data{oth.data} |
|
|
: data{oth.data} |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief @see pointer_iterator |
|
|
|
|
|
*/ |
|
|
constexpr const_pointer_iterator(const T* ptr) |
|
|
constexpr const_pointer_iterator(const T* ptr) |
|
|
: data{ptr} |
|
|
: data{ptr} |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
constexpr const T& operator*() |
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @brief Dereferencing returns a const version of what a pointer_iterator would return |
|
|
|
|
|
*/ |
|
|
|
|
|
constexpr const T& operator*() const |
|
|
{ |
|
|
{ |
|
|
return *data; |
|
|
return *data; |
|
|
} |
|
|
} |
|
@ -153,17 +189,17 @@ struct const_pointer_iterator final |
|
|
return const_pointer_iterator{p}; |
|
|
return const_pointer_iterator{p}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr const_pointer_iterator operator+(const std::size_t offset) |
|
|
|
|
|
|
|
|
constexpr const_pointer_iterator operator+(const std::size_t offset) const |
|
|
{ |
|
|
{ |
|
|
return const_pointer_iterator{data+sign*offset}; |
|
|
return const_pointer_iterator{data+sign*offset}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr const_pointer_iterator operator+(const int offset) |
|
|
|
|
|
|
|
|
constexpr const_pointer_iterator operator+(const int offset) const |
|
|
{ |
|
|
{ |
|
|
return const_pointer_iterator{data+sign*offset}; |
|
|
return const_pointer_iterator{data+sign*offset}; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr const_pointer_iterator operator-(const std::size_t offset) |
|
|
|
|
|
|
|
|
constexpr const_pointer_iterator operator-(const std::size_t offset) const |
|
|
{ |
|
|
{ |
|
|
return const_pointer_iterator{data-sign*offset}; |
|
|
return const_pointer_iterator{data-sign*offset}; |
|
|
} |
|
|
} |
|
@ -178,22 +214,22 @@ struct const_pointer_iterator final |
|
|
return ((T*)data-(T*)oth.data)*sign; |
|
|
return ((T*)data-(T*)oth.data)*sign; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool operator==(const const_pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool operator==(const const_pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return data==oth.data; |
|
|
return data==oth.data; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool operator!=(const_pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool operator!=(const_pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return data!=oth.data; |
|
|
return data!=oth.data; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool before_or_equal(const const_pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool before_or_equal(const const_pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data); |
|
|
return reinterpret_cast<std::intptr_t>(data) <= reinterpret_cast<std::intptr_t>(oth.data); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
constexpr bool operator<=(const const_pointer_iterator oth) |
|
|
|
|
|
|
|
|
constexpr bool operator<=(const const_pointer_iterator oth) const |
|
|
{ |
|
|
{ |
|
|
return before_or_equal(oth); |
|
|
return before_or_equal(oth); |
|
|
} |
|
|
} |