make ptr_iterator fully conceptually a random acccess iterator

v1
Brett 2024-06-30 12:44:35 -04:00
parent f3451b57ab
commit bccd2f4ba3
3 changed files with 81 additions and 6 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.20) cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake) include(cmake/color.cmake)
set(BLT_VERSION 0.17.24) set(BLT_VERSION 0.17.25)
set(BLT_TEST_VERSION 0.0.1) set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT) set(BLT_TARGET BLT)

View File

@ -19,6 +19,7 @@
#ifndef BLT_MEMORY_UTIL_H #ifndef BLT_MEMORY_UTIL_H
#define BLT_MEMORY_UTIL_H #define BLT_MEMORY_UTIL_H
#include <blt/std/types.h>
#include <type_traits> #include <type_traits>
#include <array> #include <array>
#include <cstring> #include <cstring>
@ -140,19 +141,24 @@ namespace blt
{ {
public: public:
using iterator_category = std::random_access_iterator_tag; using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t; using difference_type = blt::ptrdiff_t;
using value_type = V; using value_type = V;
using pointer = value_type*; using pointer = value_type*;
using reference = value_type&; using reference = value_type&;
using iter_reference = ptr_iterator&;
explicit ptr_iterator(V* v): _v(v) explicit ptr_iterator(V* v): _v(v)
{} {}
reference operator*() const reference operator*() const
{ return *_v; } {
return *_v;
}
pointer operator->() pointer operator->()
{ return _v; } {
return _v;
}
ptr_iterator& operator++() ptr_iterator& operator++()
{ {
@ -180,6 +186,63 @@ namespace blt
return tmp; return tmp;
} }
iter_reference operator+=(difference_type amount)
{
_v += amount;
return *this;
}
iter_reference operator-=(difference_type amount)
{
_v -= amount;
return *this;
}
reference operator[](difference_type index)
{
return *(_v + index);
}
reference operator[](blt::size_t index)
{
return *(_v + index);
}
friend bool operator<(const ptr_iterator& a, const ptr_iterator& b)
{
return b._v - a._v > 0;
}
friend bool operator>(const ptr_iterator& a, const ptr_iterator& b)
{
return a._v - b._v > 0;
}
friend bool operator<=(const ptr_iterator& a, const ptr_iterator& b)
{
return b._v - a._v >= 0;
}
friend bool operator>=(const ptr_iterator& a, const ptr_iterator& b)
{
return a._v - b._v >= 0;
}
friend difference_type operator-(const ptr_iterator& a, const ptr_iterator& b)
{
return a._v - b._v;
}
friend ptr_iterator operator+(const ptr_iterator& a, difference_type n)
{
return ptr_iterator(a._v + n);
}
friend ptr_iterator operator+(difference_type n, const ptr_iterator& a)
{
return ptr_iterator(a._v + n);
}
friend bool operator==(const ptr_iterator& a, const ptr_iterator& b) friend bool operator==(const ptr_iterator& a, const ptr_iterator& b)
{ {
return a._v == b._v; return a._v == b._v;

View File

@ -258,6 +258,18 @@ namespace blt
size_ -= remove_amount; size_ -= remove_amount;
return buffer_ + first_pos + 1; return buffer_ + first_pos + 1;
} }
template<typename T1, blt::size_t size1, typename T2, blt::size_t size2, std::enable_if_t<std::is_convertible_v<T1, T2>, bool> = true>
constexpr friend bool operator==(const static_vector<T1, size1>& v1, const static_vector<T2, size2>& v2)
{
if (v1.size() != v2.size())
return false;
for (blt::size_t i = 0; i < v1.size(); i++) {
if (v1[i] != v2[i])
return false;
}
return true;
}
}; };
template<typename T, typename ALLOC = std::allocator<T>> template<typename T, typename ALLOC = std::allocator<T>>
@ -277,8 +289,8 @@ namespace blt
using const_reference = const value_type&; using const_reference = const value_type&;
using pointer = value_type*; using pointer = value_type*;
using const_pointer = const pointer; using const_pointer = const pointer;
using iterator = T*; using iterator = blt::ptr_iterator<T>;
using const_iterator = const T*; using const_iterator = blt::ptr_iterator<const T>;
using reverse_iterator = std::reverse_iterator<iterator>; using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>; using const_reverse_iterator = std::reverse_iterator<const_iterator>;