Cortenn
load.hpp
Go to the documentation of this file.
1 
9 #include "pbm/data.hpp"
10 
11 #ifndef PBM_LOAD_HPP
12 #define PBM_LOAD_HPP
13 
14 namespace pbm
15 {
16 
18 struct PathedTens final
19 {
20  ~PathedTens (void)
21  {
22  for (auto& child : children_)
23  {
24  delete child.second;
25  }
26  }
27 
30  void join (PathedTens* other)
31  {
32  {
33  std::vector<std::string> labels;
34  for (auto opair : other->tens_)
35  {
36  if (tens_.end() != tens_.find(opair.first))
37  {
38  labels.push_back(opair.first);
39  }
40  }
41  if (tens_.size() > 0)
42  {
43  logs::warnf("duplicate base labels %s",
44  fmts::to_string(labels.begin(), labels.end()).c_str());
45  }
46  }
47 
48  for (auto cpair : other->children_)
49  {
50  std::string label = cpair.first;
51  auto it = children_.find(label);
52  if (children_.end() == it)
53  {
54  children_.emplace(label, cpair.second);
55  }
56  else
57  {
58  it->second->join(cpair.second);
59  }
60  }
61  }
62 
64  ade::TensptrT get_labelled (StringsT path) const
65  {
66  return get_labelled(path.begin(), path.end());
67  }
68 
70  void set_labelled (StringsT path, ade::TensptrT tens)
71  {
72  set_labelled(path.begin(), path.end(), tens);
73  }
74 
77  ade::TensptrT get_labelled (
78  StringsT::iterator path_begin,
79  StringsT::iterator path_end) const
80  {
81  if (path_begin == path_end)
82  {
83  return nullptr;
84  }
85  auto path_it = path_begin++;
86  if (path_begin == path_end)
87  {
88  auto it = tens_.find(*path_it);
89  if (tens_.end() != it)
90  {
91  return it->second;
92  }
93  }
94  else
95  {
96  auto it = children_.find(*path_it);
97  if (nullptr != it->second)
98  {
99  return it->second->get_labelled(path_begin, path_end);
100  }
101  }
102  return nullptr;
103  }
104 
106  void set_labelled (StringsT::iterator path_begin,
107  StringsT::iterator path_end, ade::TensptrT tens)
108  {
109  if (path_begin == path_end)
110  {
111  return;
112  }
113  std::string label = *(path_begin++);
114  if (path_begin == path_end)
115  {
116  tens_.emplace(label, tens);
117  return;
118  }
119  PathedTens* child;
120  auto it = children_.find(label);
121  if (it == children_.end())
122  {
123  child = new PathedTens();
124  children_.emplace(label, child);
125  }
126  else
127  {
128  assert(nullptr != it->second);
129  child = it->second;
130  }
131  child->set_labelled(path_begin, path_end, tens);
132  }
133 
135  std::unordered_map<std::string,PathedTens*> children_;
136 
138  std::unordered_map<std::string,ade::TensptrT> tens_;
139 };
140 
142 struct GraphInfo final
143 {
145  std::unordered_set<ade::TensptrT> roots_;
146 
149 };
150 
152 void load_graph (GraphInfo& out, const cortenn::Graph& in,
153  DataLoaderT dataloader);
154 
155 }
156 
157 #endif // PBM_GRAPH_HPP
Tree node for labeling Tensptrs.
Definition: load.hpp:18
std::list< std::string > StringsT
String list type used for paths.
Definition: data.hpp:32
ade::TensptrT get_labelled(StringsT::iterator path_begin, StringsT::iterator path_end) const
Definition: load.hpp:77
ade::TensptrT get_labelled(StringsT path) const
Return tensor associated with input path if found otherwise nullptr.
Definition: load.hpp:64
~PathedTens(void)
Definition: load.hpp:20
Contains all information necessary to recreate labelled ADE graph.
Definition: load.hpp:142
void set_labelled(StringsT path, ade::TensptrT tens)
Set input path to reference tensor.
Definition: load.hpp:70
PathedTens tens_
Labelled tensors.
Definition: load.hpp:148
std::unordered_map< std::string, PathedTens * > children_
Map of labels to branching nodes.
Definition: load.hpp:135
void join(PathedTens *other)
Definition: load.hpp:30
std::function< ade::TensptrT(const char *, ade::Shape, size_t, std::string)> DataLoaderT
Data deserialization functor.
Definition: data.hpp:29
std::unordered_set< ade::TensptrT > roots_
Set of all roots (Tensptrs without any parent)
Definition: load.hpp:145
void load_graph(GraphInfo &out, const cortenn::Graph &in, DataLoaderT dataloader)
Return graph info through out available from in graph.
Definition: load.cpp:33
Definition: data.hpp:18
std::unordered_map< std::string, ade::TensptrT > tens_
Map of labels to tensor leaves.
Definition: load.hpp:138
void set_labelled(StringsT::iterator path_begin, StringsT::iterator path_end, ade::TensptrT tens)
Set path between iterators begin and end to reference tensor.
Definition: load.hpp:106