 |
Cerata
A library to generate structural hardware designs
|
27 #include <unordered_map>
29 #include "cerata/utils.h"
30 #include "cerata/logging.h"
43 explicit NamePart(std::string part,
bool append_sep =
true) :
str(std::move(part)),
sep(append_sep) {}
54 FlatType(
Type *t, std::vector<NamePart> prefix,
const std::string &
name,
bool invert);
64 [[nodiscard]] std::string
name(
const NamePart &root =
NamePart(),
const std::string &sep =
"_")
const;
75 const std::optional<FlatType> &parent,
81 const std::optional<FlatType> &parent,
85 void Flatten(std::vector<FlatType> *list,
87 const std::optional<FlatType> &parent,
88 const std::string &name,
102 std::string
ToString(std::vector<FlatType> flat_type_list);
117 elements_ = std::vector<T>(height_ * width_,
static_cast<T
>(0));
123 for (int64_t i = 0; i < dim; i++) {
134 T &
get(int64_t y, int64_t x) {
135 if (y >= height_ || x >= width_) {
136 CERATA_LOG(FATAL,
"Indices exceed matrix dimensions.");
138 return elements_[width_ * y + x];
142 [[nodiscard]]
const T &
get(int64_t y, int64_t x)
const {
143 if (y >= height_ || x >= width_) {
144 CERATA_LOG(FATAL,
"Indices exceed matrix dimensions.");
146 return elements_[width_ * y + x];
161 if (width_ == height_) {
162 for (
size_t d = 0; d < width_; d++) {
163 if (
get(d, d) != 1) {
174 for (int64_t y = 0; y < height_; y++) {
175 if (
get(y, x) > max) {
185 for (int64_t x = 0; x < width_; x++) {
186 if (
get(y, x) > max) {
195 std::vector<std::pair<int64_t, T>> ret;
196 for (int64_t y = 0; y < height_; y++) {
197 auto val =
get(y, x);
199 ret.emplace_back(y, val);
202 std::sort(ret.begin(), ret.end(), [](
const auto &x,
const auto &y) ->
bool {
203 return x.second < y.second;
210 using pair = std::pair<int64_t, T>;
211 std::vector<pair> ret;
212 for (int64_t x = 0; x < width_; x++) {
213 auto val =
get(y, x);
215 ret.emplace_back(x, val);
218 std::sort(ret.begin(), ret.end(), [](
const auto &x,
const auto &y) ->
bool {
219 return x.second < y.second;
233 for (int64_t y = 0; y < height_; y++) {
234 for (int64_t x = 0; x < width_; x++) {
235 ret(x, y) =
get(y, x);
249 std::stringstream ret;
250 for (int64_t y = 0; y < height_; y++) {
251 for (int64_t x = 0; x < width_; x++) {
252 ret << std::setw(3) << std::right << std::to_string(
get(y, x)) <<
" ";
261 std::vector<T> elements_;
280 using IOF = std::tuple<index, offset, FlatType>;
287 [[nodiscard]] int64_t
num_a()
const {
return a.size(); }
289 [[nodiscard]] int64_t
num_b()
const {
return b.size(); }
308 [[nodiscard]] std::shared_ptr<Node>
width_a(
const OptionalNode &no_width_increment = {})
const;
315 [[nodiscard]] std::shared_ptr<Node>
width_b(
const OptionalNode &no_width_increment = {})
const;
318 [[nodiscard]] std::string
ToString()
const;
331 static std::shared_ptr<TypeMapper>
Make(
Type *
a);
337 static std::shared_ptr<TypeMapper>
Make(
const std::shared_ptr<Type> &
a,
const std::shared_ptr<Type> &
b);
347 std::vector<FlatType>
flat_a()
const;
349 std::vector<FlatType>
flat_b()
const;
359 std::shared_ptr<TypeMapper>
Inverse()
const;
368 std::unordered_map<std::string, std::string>
meta;
372 std::vector<FlatType>
fa_;
374 std::vector<FlatType>
fb_;
bool sep
Whether we should insert a separator after this part.
static std::shared_ptr< TypeMapper > Make(Type *a)
Construct a new TypeMapper from some type to itself, and return a smart pointer to it.
std::shared_ptr< Node > width_b(const OptionalNode &no_width_increment={}) const
Return the total width of the types on side B.
std::vector< std::pair< int64_t, T > > mapping_row(int64_t y)
Obtain non-zero element indices and value from row y, sorted by value.
std::vector< FlatType > fa_
The list of flattened types on the "a"-side.
Type * b_
Type of the "b"-side.
MappingMatrix(int64_t height, int64_t width)
Construct a mapping matrix.
FlatType flat_type_a(int64_t i) const
Return the i-th FlatType on the "a"-side in the mapping matrix.
NamePart(std::string part, bool append_sep=true)
Constuct a new NamePart.
static MappingMatrix Identity(int64_t dim)
Return a square identity matrix.
std::string ToString() const
Return a human-readable string of this TypeMapper.
const T & get(int64_t y, int64_t x) const
Return a const references to a value in the matrix at some index.
Type * type_
A pointer to the original type.
A Record type containing zero or more fields.
A structure to dynamically define type mappings between flattened types.
std::shared_ptr< TypeMapper > Inverse() const
Return a new TypeMapper that is the inverse of this TypeMapper.
MappingMatrix & SetNext(int64_t y, int64_t x)
Set the next (existing maximum + 1) value in a matrix at some position.
std::vector< FlatType > flat_b() const
Return the list of flattened types on the "b"-side.
Contains every Cerata class, function, etc...
std::tuple< index, offset, FlatType > IOF
Tuple that stores all information required by a mapping pair on one side.
std::shared_ptr< Node > width_a(const OptionalNode &no_width_increment={}) const
Return the total width of the types on side A.
T & operator()(int64_t y, int64_t x)
Return a reference to a value in the matrix at some index.
MappingMatrix Transpose() const
Transpose the matrix.
Convenience struct to generate names in parts.
const T & operator()(int64_t y, int64_t x) const
Return a const references to a value in the matrix at some index.
bool CanConvert(const Type *a, const Type *b) const
Return true if this TypeMapper can map type a to type b.
std::optional< std::shared_ptr< Node > > OptionalNode
Optional shared pointer to a node.
T MaxOfRow(int64_t y) const
Return the maximum of some row.
std::vector< MappingPair > GetUniqueMappingPairs()
Get a list of unique mapping pairs.
std::string ToString(Expression::Op operation)
Human-readable expression operator.
T MaxOfColumn(int64_t x) const
Return the maximum of some column.
Type * a() const
Return the type on the "a"-side.
std::vector< NamePart > name_parts_
Name parts of this flattened type.
std::vector< std::pair< int64_t, T > > mapping_column(int64_t x)
Obtain non-zero element indices and value from column x, sorted by value.
int64_t IndexOfFlatType(const std::vector< FlatType > &flat_types_list, const Type *type)
Return the index of some Type in a list of FlatTypes.
bool operator<(const FlatType &a, const FlatType &b)
Compares two FlatTypes first by name, then by nesting level. Useful for sorting.
bool ContainsFlatType(const std::vector< FlatType > &flat_types_list, const Type *type)
Return true if some Type is contained in a list of FlatTypes, false otherwise.
static std::shared_ptr< TypeMapper > MakeImplicit(Type *a, Type *b)
Construct a new TypeMapper from some type to another type, and automatically determine the type mappi...
bool reverse_
Whether to invert this flattened type if it would be on a terminator node.
int64_t num_b() const
Return the number of FlatTypes on the "b"-side.
void FlattenRecord(std::vector< FlatType > *list, const Record *record, const std::optional< FlatType > &parent, bool invert)
Flatten a Record.
index index_b(int64_t i) const
Return the index of the i-th FlatType on the "b"-side in the mapping matrix.
TypeMapper(Type *a, Type *b)
TypeMapper constructor. Constructs an empty type mapping.
FlatType flat_type_b(int64_t i) const
Return the i-th FlatType on the "b"-side in the mapping matrix.
Convenience structure for anything that is named. Names are case-sensitive.
int64_t width()
Return the width of the matrix.
std::string name(const NamePart &root=NamePart(), const std::string &sep="_") const
Return the name of this flattened type, constructed from the name parts.
std::shared_ptr< Stream > stream(const std::string &name, const std::string &element_name, const std::shared_ptr< Type > &element_type, const std::vector< std::shared_ptr< Field >> &control)
Construct a new Stream type and return a shared pointer to it.
index index_a(int64_t i) const
Return the index of the i-th FlatType on the "a"-side in the mapping matrix.
Type * b() const
Return the type on the "b"-side.
offset offset_b(int64_t i) const
Return the offset of the i-th FlatType on the "b"-side in the mapping matrix.
std::shared_ptr< Record > record(const std::string &name, const std::vector< std::shared_ptr< Field >> &fields)
Create a new Record type, and return a shared pointer to it.
std::unordered_map< std::string, std::string > meta
KV storage for metadata of tools or specific backend implementations.
std::string str
The string of this name part.
A structure representing a mapping pair for a type mapping.
bool IsIdentity() const
Return true if this is an identity matrix.
void Flatten(std::vector< FlatType > *list, Type *type, const std::optional< FlatType > &parent, const std::string &name, bool invert, bool sep)
Flatten any Type.
MappingMatrix< int64_t > map_matrix()
Return the mapping matrix of this TypeMapper.
int64_t height()
Return the height of the matrix.
TypeMapper & Add(int64_t a, int64_t b)
Add a mapping between two FlatTypes to the mapper.
A matrix used for TypeMapper.
std::vector< IOF > a
Flattype and its index in a mapping matrix on the "a"-side.
void FlattenStream(std::vector< FlatType > *list, const Stream *stream, const std::optional< FlatType > &parent, bool invert)
Flatten a Stream.
void SetMappingMatrix(MappingMatrix< int64_t > map_matrix)
Set the mapping matrix of this TypeMapper.
MappingMatrix Empty() const
Create an empty matrix of the same size.
std::vector< FlatType > flat_a() const
Return the list of flattened types on the "a"-side.
Type * a_
Type of the "a"-side.
std::vector< FlatType > fb_
The list of flattened types on the "b"-side.
MappingMatrix< int64_t > matrix_
The mapping matrix.
int nesting_level_
Nesting level in a type hierarchy.
T & get(int64_t y, int64_t x)
Return a reference to a value in the matrix at some index.
std::vector< IOF > b
Flattype and its index in a mapping matrix on the "b"-side.
offset offset_a(int64_t i) const
Return the offset of the i-th FlatType on the "a"-side in the mapping matrix.
int64_t num_a() const
Return the number of FlatTypes on the "a"-side.
std::string ToString() const
Generate a human-readable version of this MappingPair.