Skip to content

Instantly share code, notes, and snippets.

@simmplecoder
Created February 6, 2017 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simmplecoder/9d6a67a4573dc5c8e0888466a948d34b to your computer and use it in GitHub Desktop.
Save simmplecoder/9d6a67a4573dc5c8e0888466a948d34b to your computer and use it in GitHub Desktop.
#include <cstddef>
#include <type_traits>
#include <cstring>
namespace playground
{
template<typename T>
struct count_indirections
{
static constexpr std::size_t value = 0;
};
template<typename T>
struct count_indirections<T *>
{
static constexpr std::size_t value = count_indirections<T>::value + 1;
};
template<typename Pointer>
class recursive_iterator
{
public:
static constexpr std::size_t current_indirection_level = count_indirections<Pointer>::value;
private:
Pointer data;
std::size_t dimensions[current_indirection_level];
// ^^ this will give the dimensions of the next level of indirections
std::size_t index;
public:
recursive_iterator(Pointer data_, const std::size_t (&dimensions_)[current_indirection_level],
std::size_t index) :
data(data_),
index()
{
std::memcpy(dimensions, dimensions_, sizeof(dimensions));
}
using element_type = std::remove_pointer_t<Pointer>;
std::enable_if_t<current_indirection_level, recursive_iterator<element_type>> begin()
{
std::size_t next_dimensions[current_indirection_level - 1];
std::memcpy(next_dimensions, dimensions, sizeof(next_dimensions));
return {data[index], next_dimensions, 0};
};
std::enable_if_t<current_indirection_level, recursive_iterator<element_type>> end()
{
std::size_t next_dimensions[current_indirection_level - 1];
std::memcpy(next_dimensions, dimensions, sizeof(next_dimensions));
return {data[index], next_dimensions, dimensions[current_indirection_level - 1] - 1};
};
recursive_iterator& operator++()
{
++index;
return *this;
}
recursive_iterator operator++(int)
{
auto copy= *this;
++index;
return copy;
}
element_type operator*()
{
return data[index];
}
template <typename T>
friend bool operator==(const recursive_iterator<T>& lhs, const recursive_iterator<T>& rhs);
template <typename T>
friend bool operator!=(const recursive_iterator<T>& lhs, const recursive_iterator<T>& rhs);
private:
recursive_iterator<element_type> dereference(std::true_type)
{
std::size_t next_dimensions[current_indirection_level - 1];
std::memcpy(next_dimensions, dimensions, sizeof(next_dimensions));
return {data[index], next_dimensions, 0};
}
element_type dereference(std::false_type)
{
return data[index];
}
};
template <typename Pointer>
bool operator==(const recursive_iterator<Pointer>& lhs, const recursive_iterator<Pointer>& rhs)
{
return (lhs.data == rhs.data) && (lhs.index == rhs.index);
}
template <typename Pointer>
bool operator!=(const recursive_iterator<Pointer>& lhs, const recursive_iterator<Pointer>& rhs)
{
return !(lhs == rhs);
}
}
@simmplecoder
Copy link
Author

The code is under unlicense license. Currently the only problem should be enabling dereferencing, because currenltly it cannot disambiguate between one indirection and multiple indirection case. If anyone can push it to the end, feel free to modify.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment