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 6be729b
Showing 1 changed file with 21 additions and 37 deletions.
58 changes: 21 additions & 37 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,17 @@
* 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`
*
* 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 +108,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 @@ -113,7 +121,6 @@ class OptionalView
void set_has_value(bool new_has_value)
{
has_value_ = new_has_value;
std::cout << "set as value to " << has_value_ << std::endl;
}
bool has_value() const { return has_value_; }
const T& value() const { return value_; }
Expand Down Expand Up @@ -153,6 +160,7 @@ class OptionalView
}
};

// TODO: implement iterator completely
template <typename T, std::size_t MAXIMUM_SIZE>
class FixedVectorView
{
Expand All @@ -164,10 +172,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 +183,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 +305,8 @@ 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 +315,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 +399,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 +480,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 +613,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 +926,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 +938,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 +975,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 6be729b

Please sign in to comment.