Skip to content

Commit

Permalink
tmp: clean up debug printings and improve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yan-Tong Lin committed Jul 22, 2024
1 parent 99b7567 commit 413620e
Showing 1 changed file with 24 additions and 41 deletions.
65 changes: 24 additions & 41 deletions include/fixed_containers/struct_view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <iostream>
#include <iterator>
#include <numeric>
#include <optional>
Expand Down Expand Up @@ -51,9 +50,18 @@
* we only update the indices when we need to. Users will specify a `ContiguousRangeSubStructView`
* instead of an array to denote the need of lazy evaluation.
*
* TODO: `ContiguousRangeSubStructView` currently only supports flat structs.
* To support partial lazy evaluation, use `PathPropertiesTree` in `StructView` instead of
* `PathPropertiesMap`
* Note:
* `ContiguousRangeSubStructView` currently only supports flat structs.
*
* TODO: (feature)
* - refine `FixedVectorView`
* - support for `std::bitset`
* - support for magic enums as `std::string_view`s
*
* TODO: (optimization)
* - switch to tree data structure
* - support partial lazy evaluation
* - dynamic shape of iterable and optional
*/

namespace fixed_containers::struct_view_detail
Expand Down Expand Up @@ -101,6 +109,7 @@ struct FixedTensorView
};

// Adaptors for common private types used by the `struct_view`
// Now we have supports for `std::optional` and `FixedVector`
template <typename T>
class OptionalView
{
Expand All @@ -110,11 +119,7 @@ class OptionalView

public:
using value_type = T;
void set_has_value(bool new_has_value)
{
has_value_ = new_has_value;
std::cout << "set as value to " << has_value_ << std::endl;
}
void set_has_value(bool new_has_value) { has_value_ = new_has_value; }
bool has_value() const { return has_value_; }
const T& value() const { return value_; }
T& value() { return value_; }
Expand Down Expand Up @@ -153,6 +158,7 @@ class OptionalView
}
};

// TODO: implement iterator completely
template <typename T, std::size_t MAXIMUM_SIZE>
class FixedVectorView
{
Expand All @@ -164,10 +170,6 @@ class FixedVectorView
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
// using const_iterator = IteratorImpl<IteratorConstness::CONSTANT_ITERATOR>;
// using iterator = IteratorImpl<IteratorConstness::MUTABLE_ITERATOR>;
// using reverse_iterator = std::reverse_iterator<iterator>;
// using const_reverse_iterator = std::reverse_iterator<const_iterator>;

T& operator[](std::size_t index) { return array_[index]; }

Expand All @@ -179,7 +181,7 @@ class FixedVectorView
{
if (preconditions::test(new_size <= MAXIMUM_SIZE))
{
// add Checking
// TODO: add Checking
return;
}
if (new_size <= MAXIMUM_SIZE)
Expand Down Expand Up @@ -301,6 +303,9 @@ constexpr void for_each_path_dfs_helper(S&& instance,
{
std::forward<PreFunction>(pre_fn)(std::as_const(*chain), std::forward<S>(instance));
// recurse into `value()` if exists, construct a dummy instance at `value()` if not exists
// unsafe cast to non-const
// However, we only construct when the optional has no value and destruct at the end of
// recursion
chain->push_back(OPTIONAL_PATH_NAME);
using InstanceType = decltype(instance);
bool constructed{false};
Expand All @@ -309,7 +314,6 @@ constexpr void for_each_path_dfs_helper(S&& instance,
const_cast<std::decay_t<InstanceType>*>(std::addressof(instance))->emplace();
constructed = true;
}
std::cout << "debug: asserting\n";
assert_or_abort(instance.has_value());
for_each_path_dfs_helper(instance.value(),
std::forward<PreFunction>(pre_fn),
Expand Down Expand Up @@ -394,18 +398,12 @@ constexpr void for_each_path_dfs_helper(S&& /*instance*/,
static_assert(std::is_same_v<S, void>, "Unreachable Fallback");
}

/*
* We collapse the type information into what is needed in `PathProperties`
* Unsafe, this assumes the following getter can get the desired field from the instance despite we
* are not passing the right template arguments
*/

// Type information of a tree node
enum class StructTreeNodeType
{
UNREACHABLE,
OPTIONAL,
// BITSET,
// BITSET, // TODO
// suppported iterators
FIXED_VECTOR,
STD_ARRAY,
Expand Down Expand Up @@ -481,16 +479,13 @@ struct FixedVectorCallInterface
using StructTreeNodeCallInterface =
std::variant<std::monostate, OptionalCallInterface, FixedVectorCallInterface>;

// MetadataType

// MetadataType combining type and call interface
struct StructTreeNodeMetadata
{
StructTreeNodeType type{};
StructTreeNodeCallInterface call_interface{};
};

// Metadata constructor to conditional assisn call interface with T, e.g. is T is OptiionalLike, use
// OptionalCallInterface
template <typename T>
StructTreeNodeMetadata make_struct_tree_node_metadata()
{
Expand Down Expand Up @@ -617,12 +612,12 @@ using Indices = struct_view_detail::Indices<struct_view_detail::MAX_DIM>;

using PathNameChain = struct_view_detail::PathNameChain;
using PathProperties = struct_view_detail::PathProperties<struct_view_detail::MAX_DIM>;
using StructTreeNodeType = struct_view_detail::StructTreeNodeType;

using StructTreeNodeMetadata = struct_view_detail::StructTreeNodeMetadata;
using StructTreeNodeType = struct_view_detail::StructTreeNodeType;
using StructTreeNodeCallInterface = struct_view_detail::StructTreeNodeCallInterface;
using FixedVectorCallInterface = struct_view_detail::FixedVectorCallInterface;
using OptionalCallInterface = struct_view_detail::OptionalCallInterface;
using StructTreeNodeCallInterface = struct_view_detail::StructTreeNodeCallInterface;
using StructTreeNodeMetadata = struct_view_detail::StructTreeNodeMetadata;

template <typename T>
inline constexpr StructTreeNodeType struct_tree_node_type_of_v =
Expand Down Expand Up @@ -930,13 +925,6 @@ void sub_struct_view_of(void* super_struct_base_pointer,
[super_struct_base_pointer, &super_struct_view](
const auto& path, const auto& path_properties, const auto& indices, void* field_ptr)
{
std::cout << "indices: ";
for (const auto& index : indices)
{
std::cout << index << " ";
}
std::cout << "\n";

const auto& super_struct_path_properties = super_struct_view.at(path);
const auto super_struct_field_ptr =
struct_view_detail::get_field<const void*, const void*>(
Expand All @@ -949,7 +937,6 @@ void sub_struct_view_of(void* super_struct_base_pointer,
auto super_api = get<FixedVectorCallInterface>(
super_struct_path_properties.metadata.call_interface);
sub_api.resize(field_ptr, super_api.size(super_struct_field_ptr));
std::cout << "resized to " << sub_api.size(field_ptr) << "\n";
}

if (path_properties.metadata.type == StructTreeNodeType::OPTIONAL)
Expand Down Expand Up @@ -987,10 +974,6 @@ void sub_struct_view_of(SuperStruct& super_struct,
super_struct_base_pointer, super_struct_view, sub_struct_base_pointer, sub_struct_view);
}

// TODO: use `FixedVector`for resizable iterable view directly
// template <typename T, std::size_t MAXIMUM_SIZE>
// using IterableView = fixed_containers::FixedVector<T, MAXIMUM_SIZE>;

template <typename SubStruct, std::size_t MAXIMUM_SIZE = MAX_NUM_PATHS>
class ContiguousRangeSubStructView
{
Expand Down

0 comments on commit 413620e

Please sign in to comment.