Tenncor
inode.hpp
Go to the documentation of this file.
1 //
8 
9 #include "estd/estd.hpp"
10 
11 #include "teq/itensor.hpp"
12 
13 #include "eteq/eigen.hpp"
14 
15 #ifndef ETEQ_INODE_HPP
16 #define ETEQ_INODE_HPP
17 
18 namespace eteq
19 {
20 
22 template <typename T>
23 struct iNode
24 {
25  static_assert(egen::TypeInfo<T>::type != egen::BAD_TYPE,
26  "Cannot create node of unknown type");
27 
28  virtual ~iNode (void) = default;
29 
31  iNode<T>* clone (void) const
32  {
33  return this->clone_impl();
34  }
35 
38  {
39  return get_tensor()->shape();
40  }
41 
43  std::string to_string (void) const
44  {
45  return get_tensor()->to_string();
46  }
47 
49  virtual T* data (void) = 0;
50 
52  virtual void update (void) = 0;
53 
55  virtual teq::TensptrT get_tensor (void) const = 0;
56 
57 protected:
58  virtual iNode<T>* clone_impl (void) const = 0;
59 };
60 
62 template <typename T>
63 using NodeptrT = std::shared_ptr<iNode<T>>;
64 
66 template <typename T>
67 using NodesT = std::vector<NodeptrT<T>>;
68 
70 template <typename T>
71 using NodeBuilderF = std::function<NodeptrT<T>(teq::TensptrT)>;
72 
74 template <typename T>
75 struct NodeConverters final
76 {
78  static std::unordered_map<size_t,NodeBuilderF<T>> builders_;
79 
82  {
83  const std::type_info& tp = typeid(*tens);
84  return estd::must_getf(builders_, tp.hash_code(),
85  "unknown tensor type `%s` with `%s` dtype",
86  tp.name(), egen::name_type(egen::get_type<T>()).c_str())(tens);
87  }
88 
89  NodeConverters (void) = delete;
90 };
91 
92 template <typename T>
93 std::unordered_map<size_t,NodeBuilderF<T>> NodeConverters<T>::builders_;
94 
97 template <typename TensType, typename T>
99 {
100  const std::type_info& tp = typeid(TensType);
102  emplace(tp.hash_code(), builder).second;
103 }
104 
106 #define TO_NODE(tens) NodeConverters<T>::to_node(tens)
107 
108 }
109 
110 #endif // ETEQ_INODE_HPP
virtual void update(void)=0
Trigger internal typed tensor update.
Definition: constant.hpp:17
std::string to_string(void) const
Return string representation of internal tensor.
Definition: inode.hpp:43
static NodeptrT< T > to_node(teq::TensptrT tens)
Return node associated with tensor type.
Definition: inode.hpp:81
Definition: shape.hpp:62
Interface node for wrapping typed tensor.
Definition: inode.hpp:23
NodeConverters(void)=delete
std::vector< NodeptrT< T > > NodesT
Vector of nodes.
Definition: inode.hpp:67
teq::Shape shape(void)
Return shape of internal tensor.
Definition: inode.hpp:37
static std::unordered_map< size_t, NodeBuilderF< T > > builders_
Map tensor type to node creation function.
Definition: inode.hpp:78
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
bool register_builder(NodeBuilderF< T > builder)
Definition: inode.hpp:98
virtual iNode< T > * clone_impl(void) const =0
std::function< NodeptrT< T >(teq::TensptrT)> NodeBuilderF
Function for building a node from tensor.
Definition: inode.hpp:71
std::shared_ptr< iNode< T > > NodeptrT
Smart pointer of node.
Definition: inode.hpp:63
virtual teq::TensptrT get_tensor(void) const =0
Return internal tensor.
iNode< T > * clone(void) const
Return deep copy of node where internal typed tensor is copied.
Definition: inode.hpp:31
virtual T * data(void)=0
Return raw data stored in internal typed tensor.
virtual ~iNode(void)=default
Node registry of tensor types and tensor to node function.
Definition: inode.hpp:75