ranges broken a bit

v1
Brett 2024-09-23 19:40:19 -04:00
parent c5b8830ff0
commit b944b936f4
4 changed files with 117 additions and 147 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.20)
include(cmake/color.cmake)
set(BLT_VERSION 0.20.19)
set(BLT_VERSION 0.20.20)
set(BLT_TEST_VERSION 0.0.1)
set(BLT_TARGET BLT)

View File

@ -19,6 +19,13 @@
namespace blt
{
template<typename T>
struct enumerate_item
{
blt::size_t index;
T value;
};
template<typename Iter>
class enumerate_base
{
@ -34,13 +41,15 @@ namespace blt
class enumerate_base_fwd : public enumerate_base<Iter>
{
public:
explicit enumerate_base_fwd(Iter iter): enumerate_base<Iter>(std::move(iter)), index(0)
explicit enumerate_base_fwd(Iter iter, blt::size_t place = 0): enumerate_base<Iter>(std::move(iter)), index(place)
{}
explicit enumerate_base_fwd(Iter iter, blt::size_t place): enumerate_base<Iter>(std::move(iter)), index(place)
{}
enumerate_item<blt::meta::deref_return_t<Iter>>* operator->() const
{
return &std::pair{index, *this->iter};
}
std::pair<blt::size_t, blt::meta::deref_return_t<Iter>> operator*() const
enumerate_item<blt::meta::deref_return_t<Iter>> operator*() const
{
return {index, *this->iter};
}
@ -59,15 +68,15 @@ namespace blt
return tmp;
}
bool operator==(const enumerate_base_fwd& other) const
{
return other.iter == this->iter;
}
friend bool operator==(const enumerate_base_fwd& a, const enumerate_base_fwd& b)
{
return a.iter == b.iter;
}
friend bool operator!=(const enumerate_base_fwd& a, const enumerate_base_fwd& b)
{
return a.iter != b.iter;
}
protected:
blt::size_t index;
@ -76,6 +85,7 @@ namespace blt
template<typename Iter>
class enumerate_base_bidirectional : public enumerate_base_fwd<Iter>
{
public:
using enumerate_base_fwd<Iter>::enumerate_base_fwd;
enumerate_base_bidirectional& operator--()
@ -93,11 +103,21 @@ namespace blt
}
};
template<typename Iter, typename Category = typename std::iterator_traits<Iter>::iterator_cateogry>
constexpr bool is_forward_only = std::is_same_v<Category, std::forward_iterator_tag>;
template<typename Iter, typename Category = typename std::iterator_traits<Iter>::iterator_category>
constexpr bool is_bidirectional_or_better =
std::is_same_v<Category, std::bidirectional_iterator_tag> || std::is_same_v<Category, std::random_access_iterator_tag>;
template<typename Iter, typename = std::void_t<>>
class enumerate_wrapper;
class enumerate_wrapper
{
static_assert("Unsupported iterator type!");
};
template<typename Iter>
class enumerate_wrapper<Iter, std::void_t<std::enable_if_t<std::is_same_v<typename Iter::iterator_category, std::forward_iterator_tag>, bool>>>
class enumerate_wrapper<Iter, std::enable_if_t<is_forward_only<Iter>, std::void_t<std::forward_iterator_tag>>> : public enumerate_base_fwd<Iter>
{
public:
using iterator_category = std::forward_iterator_tag;
@ -105,17 +125,22 @@ namespace blt
using difference_type = typename std::iterator_traits<Iter>::difference_type;
using pointer = typename std::iterator_traits<Iter>::pointer;
using reference = typename std::iterator_traits<Iter>::reference;
public:
explicit enumerate_wrapper(Iter iter): iter(std::move(iter)), index(0)
{}
explicit enumerate_wrapper(Iter iter, blt::size_t place): iter(std::move(iter)), index(place)
{}
private:
Iter iter;
blt::size_t index;
using enumerate_base_fwd<Iter>::enumerate_base_fwd;
};
template<typename Iter>
class enumerate_wrapper<Iter, std::enable_if_t<is_bidirectional_or_better<Iter>, std::void_t<std::bidirectional_iterator_tag>>>
: public enumerate_base_bidirectional<Iter>
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = typename std::iterator_traits<Iter>::value_type;
using difference_type = typename std::iterator_traits<Iter>::difference_type;
using pointer = typename std::iterator_traits<Iter>::pointer;
using reference = typename std::iterator_traits<Iter>::reference;
using enumerate_base_bidirectional<Iter>::enumerate_base_bidirectional;
};
namespace itr
@ -143,56 +168,6 @@ namespace blt
End end_;
};
template<typename TYPE_ITR>
class iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = typename std::iterator_traits<TYPE_ITR>::value_type;
using difference_type = typename std::iterator_traits<TYPE_ITR>::difference_type;
using pointer = typename std::iterator_traits<TYPE_ITR>::pointer;
using reference = typename std::iterator_traits<TYPE_ITR>::reference;
using const_reference = const reference;
private:
blt::size_t index;
TYPE_ITR current;
public:
explicit iterator(TYPE_ITR current, blt::size_t index): index(index), current(std::move(current))
{}
auto iter()
{
return current;
}
iterator& operator++()
{
++index;
++current;
return *this;
}
bool operator==(iterator other) const
{
return current == other.current;
}
bool operator!=(iterator other) const
{
return current != other.current;
}
std::pair<blt::size_t, blt::meta::deref_return_t<TYPE_ITR>> operator*() const
{
return {index, *current};
};
std::pair<blt::size_t, blt::meta::deref_return_t<TYPE_ITR>> operator*()
{
return {index, *current};
};
};
// TODO: cleanup! all of this! add support for reversing
template<typename C1_TYPE, typename C2_TYPE>
class pair_iterator
@ -242,56 +217,6 @@ namespace blt
C1_TYPE current_c1_iter;
C2_TYPE current_c2_iter;
};
template<typename TYPE_ITR>
class reverse_iterator
{
public:
using iterator_category = std::forward_iterator_tag;
using value_type = typename std::iterator_traits<TYPE_ITR>::value_type;
using difference_type = typename std::iterator_traits<TYPE_ITR>::difference_type;
using pointer = typename std::iterator_traits<TYPE_ITR>::pointer;
using reference = typename std::iterator_traits<TYPE_ITR>::reference;
using const_reference = const reference;
private:
blt::size_t index;
TYPE_ITR current;
public:
explicit reverse_iterator(TYPE_ITR current, blt::size_t index): index(index), current(std::move(current))
{}
auto iter()
{
return current;
}
reverse_iterator& operator++()
{
--index;
--current;
return *this;
}
bool operator==(reverse_iterator other) const
{
return current == other.current;
}
bool operator!=(reverse_iterator other) const
{
return current != other.current;
}
std::pair<blt::size_t, const_reference> operator*() const
{
return {index, *current};
};
std::pair<blt::size_t, reference> operator*()
{
return {index, *current};
};
};
}
template<typename Begin, typename End>
@ -306,60 +231,106 @@ namespace blt
return itr::itr_container{std::reverse_iterator(std::forward<Begin>(end)), std::reverse_iterator(std::forward<End>(begin))};
}
template<typename TYPE_ITR, template<typename> typename iterator = itr::iterator>
class enumerator
template<typename Iter>
class enumerator_base
{
public:
explicit enumerator(TYPE_ITR begin, TYPE_ITR end): begin_(std::move(begin), 0), end_(std::move(end), 0)
explicit enumerator_base(Iter begin, Iter end): begin_(std::move(begin)), end_(std::move(end))
{}
explicit enumerator(TYPE_ITR begin, TYPE_ITR end, blt::size_t i_begin, blt::size_t i_end):
begin_(std::move(begin), i_begin), end_(std::move(end), i_end)
explicit enumerator_base(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
begin_(std::move(begin), begin_index), end_(std::move(end), end_index)
{}
itr::iterator<TYPE_ITR> begin()
auto begin()
{
return begin_;
}
itr::iterator<TYPE_ITR> end()
auto end()
{
return end_;
}
private:
iterator<TYPE_ITR> begin_;
iterator<TYPE_ITR> end_;
protected:
enumerate_wrapper<Iter> begin_;
enumerate_wrapper<Iter> end_;
};
template<typename Iter, typename = std::void_t<>>
class enumerator
{
static_assert("Unsupported iterator type!");
};
template<typename Iter>
class enumerator<Iter, std::enable_if_t<is_forward_only<Iter>, std::void_t<std::forward_iterator_tag>>> : public enumerator_base<Iter>
{
public:
using enumerator_base<Iter>::enumerator_base;
};
template<typename Iter>
class enumerator<Iter, std::enable_if_t<is_bidirectional_or_better<Iter>, std::void_t<std::bidirectional_iterator_tag>>>
: public enumerator_base<Iter>
{
public:
//using enumerator_base<Iter>::enumerator_base;
explicit enumerator(Iter begin, Iter end, blt::size_t container_size):
enumerator_base<Iter>(std::move(begin), std::move(end)), container_size(container_size)
{}
explicit enumerator(Iter begin, Iter end, blt::size_t begin_index, blt::size_t end_index):
enumerator_base<Iter>(std::move(begin), std::move(end), begin_index, end_index), container_size(end_index - begin_index)
{}
enumerator rev()
{
return enumerator<Iter>{std::reverse_iterator{this->end_}, std::reverse_iterator{this->begin_}, container_size - 1, 0ul};
}
protected:
blt::size_t container_size;
};
template<typename Iter>
enumerator(Iter, Iter) -> enumerator<Iter>;
template<typename Iter>
enumerator(Iter, Iter, blt::size_t) -> enumerator<Iter>;
template<typename Iter>
enumerator(Iter, Iter, blt::size_t, blt::size_t) -> enumerator<Iter>;
template<typename T>
static inline auto enumerate(const T& container)
{
return enumerator{container.begin(), container.end()};
return enumerator{container.begin(), container.end(), container.size()};
}
template<typename T, blt::size_t size>
static inline auto enumerate(const T(& container)[size])
{
return enumerator{&container[0], &container[size]};
return enumerator{&container[0], &container[size], size};
}
template<typename T, blt::size_t size>
static inline auto enumerate(T(& container)[size])
{
return enumerator{&container[0], &container[size]};
return enumerator{&container[0], &container[size], size};
}
template<typename T>
static inline auto enumerate(T& container)
{
return enumerator{container.begin(), container.end()};
return enumerator{container.begin(), container.end(), container.size()};
}
template<typename T>
static inline auto enumerate(T&& container)
{
return enumerator{container.begin(), container.end()};
return enumerator{container.begin(), container.end(), container.size()};
}
template<typename C1_ITER, typename C2_ITER, template<typename, typename> typename iterator = itr::pair_iterator>

View File

@ -122,10 +122,10 @@ namespace blt
{
std::string result = "[";
for (const auto& value : blt::enumerate(vec))
for (const auto& [index, value] : blt::enumerate(vec))
{
result += to_string(value.second);
if (value.first != vec.size() - 1)
result += to_string(value);
if (index != vec.size() - 1)
result += ", ";
}
result += "]";

View File

@ -141,10 +141,9 @@ namespace blt::parse
obj_model_t obj_loader::parseFile(std::string_view file)
{
auto lines = blt::fs::getLinesFromFile(std::string(file));
for (auto line_e : blt::enumerate(lines))
for (const auto& [index, line] : blt::enumerate(lines))
{
auto& line = line_e.second;
current_line = line_e.first;
current_line = index;
char_tokenizer token(line);
if (!token.has_next() || token.read_fully().empty())
continue;
@ -220,9 +219,9 @@ namespace blt::parse
void obj_loader::handle_face_vertex(const std::vector<std::string>& face_list, int32_t* arr)
{
for (const auto& pair : blt::enumerate(face_list))
for (const auto& [e_index, value] : blt::enumerate(face_list))
{
auto indices = blt::string::split(pair.second, '/');
auto indices = blt::string::split(value, '/');
BLT_ASSERT(indices.size() == 3 && "Must have vertex, uv, and normal indices!!");
auto vi = get<std::int32_t>(indices[0]) - 1;
@ -242,14 +241,14 @@ namespace blt::parse
BLT_DEBUG("Vertex: (%f, %f, %f), UV: (%f, %f), Normal: (%f, %f, %f)", vertices[vi].x(), vertices[vi].y(), vertices[vi].z(),
uvs[ui].x(), uvs[ui].y(), normals[ni].x(), normals[ni].y(), normals[ni].z());
vertex_map.insert({face, index});
arr[pair.first] = index;
arr[e_index] = index;
} else
{
BLT_TRACE("Using cached data; %d; map size: %d", loc->second, vertex_data.size());
//const auto& d = vertex_data[loc->second];
BLT_TRACE("Vertex: (%f, %f, %f), UV: (%f, %f), Normal: (%f, %f, %f)", d.vertex.x(), d.vertex.y(), d.vertex.z(),
d.uv.x(), d.uv.y(), d.normal.x(), d.normal.y(), d.normal.z());
arr[pair.first] = loc->second;
arr[e_index] = loc->second;
}
}
}