move ranges around (utility over memory)

TOOD: add reverse
v1
Brett 2023-12-10 13:21:26 -05:00
parent ce6ade1a90
commit e6d096a486
5 changed files with 87 additions and 82 deletions

View File

@ -308,6 +308,10 @@ namespace blt::string
std::vector<std::string> createTable(bool top = false, bool bottom = false); std::vector<std::string> createTable(bool top = false, bool bottom = false);
}; };
struct tree_node {
std::vector<tree_node*> children;
};
class BinaryTreeFormatter class BinaryTreeFormatter
{ {
public: public:

View File

@ -102,85 +102,6 @@ namespace blt
} }
} }
template<typename T>
struct range_itr
{
private:
T current;
public:
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = T;
using value_type = T;
using pointer = T*;
using reference = T&;
explicit range_itr(T current): current(current)
{}
value_type operator*() const
{ return current; }
value_type operator->()
{ return current; }
range_itr& operator++()
{
current++;
return *this;
}
range_itr& operator--()
{
current--;
return *this;
}
range_itr operator++(int)
{
auto tmp = *this;
++(*this);
return tmp;
}
range_itr operator--(int)
{
auto tmp = *this;
--(*this);
return tmp;
}
friend bool operator==(const range_itr& a, const range_itr& b)
{
return a.current == b.current;
}
friend bool operator!=(const range_itr& a, const range_itr& b)
{
return a.current != b.current;
}
};
template<typename T>
struct range
{
private:
T _begin;
T _end;
public:
range(T begin, T end): _begin(begin), _end(end)
{}
range_itr<T> begin()
{
return range_itr{_begin};
}
range_itr<T> end()
{
return range_itr{_end};
}
};
template<typename V> template<typename V>
struct ptr_iterator struct ptr_iterator
{ {

View File

@ -135,6 +135,80 @@ namespace blt
return enumerator{container.begin(), container.end()}; return enumerator{container.begin(), container.end()};
} }
template<typename T>
struct range
{
public:
struct range_itr
{
public:
using iterator_category = std::bidirectional_iterator_tag;
using difference_type = T;
using value_type = T;
using pointer = T*;
using reference = T&;
private:
T current;
bool forward;
public:
explicit range_itr(T current, bool forward): current(current), forward(forward)
{}
value_type operator*() const
{ return current; }
value_type operator->()
{ return current; }
range_itr& operator++()
{
if (forward)
current++;
else
current--;
return *this;
}
range_itr& operator--()
{
if (forward)
current--;
else
current++;
return *this;
}
friend bool operator==(const range_itr& a, const range_itr& b)
{
return a.current == b.current;
}
friend bool operator!=(const range_itr& a, const range_itr& b)
{
return a.current != b.current;
}
};
private:
T _begin;
T _end;
T offset = 0;
public:
range(T begin, T end): _begin(begin), _end(end), offset(end < begin ? 1 : 0)
{}
range_itr begin()
{
return range_itr(_begin - offset, offset == 0);
}
range_itr end()
{
// not sure if i like this
return range_itr(_end - offset, offset == 0);
}
};
#if defined(__GNUC__) || defined(__llvm__) #if defined(__GNUC__) || defined(__llvm__)
#define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline)) #define BLT_ATTRIB_NO_INLINE __attribute__ ((noinline))

View File

@ -101,8 +101,5 @@ void blt::test::memory::access()
void blt::test::memory::ranges() void blt::test::memory::ranges()
{ {
blt::range range(0, 10);
for (auto r : range)
BLT_TRACE_STREAM << r;
BLT_TRACE_STREAM << '\n'; BLT_TRACE_STREAM << '\n';
} }

View File

@ -148,5 +148,14 @@ void blt::test::utility::run()
testEnumerate(in); testEnumerate(in);
} }
for (auto r : blt::range(0, 10))
BLT_TRACE_STREAM << r << " ";
BLT_TRACE_STREAM << "\n";
for (auto r : blt::range(10, 0))
BLT_TRACE_STREAM << r << " ";
BLT_TRACE_STREAM << "\n";
fuckered(); fuckered();
} }