Euphoria
ctree.h
Go to the documentation of this file.
1 #pragma once
2 
3 
4 
5 namespace eu::core
6 {
7  struct TreeStyle
8  {
9  std::string_view t_cross;
10  std::string_view l;
11  std::string_view down;
12  std::string_view space;
13 
14  bool include_space = false;
15  bool root_special = true;
16  };
17 
18 
19  // todo(Gustav): make constexpr
20 
21  constexpr TreeStyle slash_style =
22  {
23  .t_cross = "|-",
24  .l = "\\-",
25  .down = "| ",
26  .space = " ",
27  .include_space = true,
28  .root_special = true
29  };
30 
32  {
33  .t_cross = "├╴",
34  .l = "└╴",
35  .down = "│ ",
36  .space = " ",
37  .include_space = true,
38  .root_special = true
39  };
40 
42  {
43  .t_cross = "+- ",
44  .l = "+- ",
45  .down = "| ",
46  .space = " ",
47  .include_space = false,
48  .root_special = false
49  };
50 
51 
52  TreeStyle
54 
55 
56  template
57  <
58  typename T,
59  typename TLabelFunc,
60  typename TFindChildrenFunc,
61  typename TPrintFunc
62  >
63  void
65  (
66  T item,
67  TLabelFunc label_func,
68  TFindChildrenFunc find_children_func,
69  TPrintFunc print_func,
70  const TreeStyle& style = determine_style(),
71  const std::string& a_indent = "",
72  int index = 0,
73  bool last = true
74  )
75  {
76  const auto children = find_children_func(item);
77  const auto label = label_func(item);
78  const auto is_root = style.root_special && index == 0;
79 
80  std::string indent = a_indent;
81 
82  if (is_root)
83  {
84  print_func(label);
85  }
86  else
87  {
88  print_func(indent + std::string{ last ? style.l : style.t_cross } +label);
89 
90  if ( style.include_space && last && children.empty())
91  {
92  print_func(indent);
93  }
94 
95  indent += (last ? style.space : style.down);
96  }
97 
98  for (auto it = children.begin(); it != children.end(); ++it)
99  {
100  print_hierarchy<T, TLabelFunc, TFindChildrenFunc, TPrintFunc>
101  (
102  *it,
103  label_func,
104  find_children_func,
105  print_func,
106  style,
107  indent,
108  index + 1,
109  it + 1 == children.end()
110  );
111  }
112  }
113 }
TreeStyle determine_style()
Definition: ctree.cc:29
void print_hierarchy(T item, TLabelFunc label_func, TFindChildrenFunc find_children_func, TPrintFunc print_func, const TreeStyle &style=determine_style(), const std::string &a_indent="", int index=0, bool last=true)
Definition: ctree.h:65
constexpr TreeStyle slash_style
Definition: ctree.h:21
constexpr TreeStyle utf8_tree_style
Definition: ctree.h:31
constexpr TreeStyle cross_style
Definition: ctree.h:42
std::string_view space
Definition: ctree.h:12
bool include_space
Definition: ctree.h:14
std::string_view down
Definition: ctree.h:11
std::string_view t_cross
Definition: ctree.h:9
std::string_view l
Definition: ctree.h:10
bool root_special
Definition: ctree.h:15