Skip to content

Commit

Permalink
Amidst a major refactor. Not sure what even builds.
Browse files Browse the repository at this point in the history
  • Loading branch information
asoffer committed Apr 27, 2024
1 parent aac5a3f commit df98e5e
Show file tree
Hide file tree
Showing 19 changed files with 247 additions and 433 deletions.
19 changes: 19 additions & 0 deletions common/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ cc_library(
],
)

cc_library(
name = "constant",
hdrs = ["constant.h"],
deps = [
"@nth_cc//nth/meta:type",
"@com_google_absl//absl/container:flat_hash_set",
],
)

cc_test(
name = "constant_test",
srcs = ["constant_test.cc"],
deps = [
":constant",
"@nth_cc//nth/debug",
"@nth_cc//nth/test:main",
],
)

cc_library(
name = "debug",
hdrs = ["debug.h"],
Expand Down
92 changes: 92 additions & 0 deletions common/constant.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#ifndef ICARUS_COMMON_CONSTANT_H
#define ICARUS_COMMON_CONSTANT_H

#include <concepts>
#include <cstddef>
#include <cstdint>
#include <span>
#include <utility>
#include <vector>

#include "absl/container/flat_hash_set.h"
#include "nth/base/attributes.h"
#include "nth/meta/type.h"

namespace ic {

struct ConstantComponentAppender {
explicit constexpr ConstantComponentAppender(std::vector<uint64_t>& v)
: v_(v) {}
void append(uint64_t n) { v_.push_back(n); }

private:
std::vector<uint64_t>& v_;
};

template <typename C>
concept ConstantType = requires(C const& c) {
{ IcConstantIndex(c) } -> std::convertible_to<size_t>;
typename C::constant_insertion_type;
{
IcConstantAppend(std::declval<ConstantComponentAppender&>,
std::declval<typename C::constant_insertion_type const&>())
} -> std::same_as<void>;
};

namespace internal_constant {

struct Index {
uint32_t kind;
uint32_t index;
};

struct HashEqBase {
explicit constexpr HashEqBase(
std::vector<uint64_t> const& components NTH_ATTRIBUTE(lifetimebound))
: components_(components) {}

std::span<uint64_t const> SpanFor(uint32_t index) const {
uint64_t size = components_[index];
return std::span<uint64_t const>(&components_[index + 1], size);
}

std::vector<uint64_t> const& components_;
};

template <ConstantType... Cs>
struct Hash : HashEqBase {
explicit constexpr Hash(
std::vector<uint64_t> const& components NTH_ATTRIBUTE(lifetimebound))
: HashEqBase(components) {}
size_t operator()(Index i) const { return Hashers[i.kind](SpanFor(i.index)); }

private:

using hasher_type = size_t (*)(std::span<uint64_t const>);
static constexpr hasher_type Hashers[sizeof...(Cs)] = {
+[](std::span<uint64_t const> components) -> size_t {
return static_cast<size_t>(IcHashConstant(nth::type<Cs>, components));
}...};
};

template <ConstantType... Cs>
struct Eq : HashEqBase {
explicit constexpr Eq(
std::vector<uint64_t> const& components NTH_ATTRIBUTE(lifetimebound))
: HashEqBase(components) {}
bool operator()(Index lhs, Index rhs) const { std::abort(); }
};

} // namespace internal_constant

template <ConstantType... Cs>
struct ConstantTable {
std::vector<uint64_t> components_;
absl::flat_hash_set<internal_constant::Index, internal_constant::Hash<Cs...>,
internal_constant::Eq<Cs...>>
index_;
};

} // namespace ic

#endif // ICARUS_COMMON_CONSTANT_H
48 changes: 0 additions & 48 deletions common/constant/BUILD

This file was deleted.

18 changes: 0 additions & 18 deletions common/constant/category.h

This file was deleted.

40 changes: 0 additions & 40 deletions common/constant/component.h

This file was deleted.

20 changes: 0 additions & 20 deletions common/constant/entry.h

This file was deleted.

42 changes: 0 additions & 42 deletions common/constant/manifest.cc

This file was deleted.

Loading

0 comments on commit df98e5e

Please sign in to comment.