9 template<
typename algorithm_t>
12 auto meta = T::meta();
14 ast::Value s = std::move(meta).build_static_args_ast_value();
16 gather_types(
m_data->m_algorithms, std::move(meta));
19 = eval::pattern_eval(std::move(s), m_root_type,
m_data->m_algorithms);
21 CHECK(
m_data->m_registered.count(static_s) == 0) <<
"registered twice";
22 m_data->m_registered[std::move(static_s)] = [](Env&& env) {
23 return std::make_unique<T>(std::move(env));
27 template<
typename algorithm_t>
29 return m_data->m_algorithms;
32 template<
typename algorithm_t>
34 return m_data->m_algorithms;
37 template<
typename algorithm_t>
38 inline std::vector<pattern::Algorithm> Registry<algorithm_t>::all_algorithms_with_static_internal(
39 std::vector<AlreadySeenPair>& already_seen,
42 std::vector<pattern::Algorithm> r;
44 using AlgorithmArgs = std::vector<pattern::Arg>;
46 for (
auto& c :
m_data->m_algorithms.at(type)) {
47 std::vector<std::vector<AlgorithmArgs>> args_variations;
49 auto seen = AlreadySeenPair {
54 for (
auto& x: already_seen) {
56 if (x.pair[0] == type) {
65 already_seen.push_back(seen);
67 for (
auto& arg : c.arguments()) {
68 const std::string& arg_name = arg.name();
69 CHECK(arg_name.size() > 0);
71 auto arg_type = arg.type();
72 bool is_static = arg.is_static();
74 std::vector<AlgorithmArgs> arg_variations;
76 for(
auto arg : all_algorithms_with_static_internal(already_seen, arg_type)) {
77 arg_variations.push_back(AlgorithmArgs {
79 std::string(arg_name),
84 args_variations.push_back(arg_variations);
88 already_seen.pop_back();
91 std::vector<pattern::Arg> x_args;
94 std::vector<AlgorithmArgs> r_;
95 if (args_variations.size() == 0) {
96 pattern::Algorithm x {
103 r_ = cross<AlgorithmArgs>(
104 std::move(args_variations), [](AlgorithmArgs s,
113 for (
auto& elem : r_) {
114 r.push_back(pattern::Algorithm {
115 std::string(c.name()),
124 template<
typename algorithm_t>
125 inline std::vector<pattern::Algorithm> Registry<algorithm_t>::all_algorithms_with_static(
View type)
const {
126 std::vector<pattern::Algorithm> filtered_r;
128 std::vector<AlreadySeenPair> already_seen;
129 for (
auto x : all_algorithms_with_static_internal(already_seen, type)) {
130 if (
m_data->m_registered.count(x) > 0) {
131 filtered_r.push_back(std::move(x));
138 template<
typename algorithm_t>
139 inline std::vector<pattern::Algorithm> Registry<algorithm_t>::check_for_undefined_algorithms() {
140 std::vector<pattern::Algorithm> r;
141 for (
auto& s : all_algorithms_with_static(m_root_type)) {
142 if (
m_data->m_registered.count(s) == 0) {
149 template<
typename algorithm_t>
150 inline Registry<algorithm_t> Registry<algorithm_t>::with_all_from(std::function<
void(Registry&)> f,
const std::string& root_type) {
151 Registry r(root_type);
156 template<
typename algorithm_t>
157 inline std::string Registry<algorithm_t>::generate_doc_string(
const std::string& title)
const {
158 auto print = [](std::vector<decl::Algorithm>& x,
size_t iden) {
159 std::vector<std::string> cells;
162 auto spec = y.to_string(
true);
164 std::stringstream where;
166 for (
auto& z : y.arguments()) {
168 where <<
"\n where ";
173 where <<
"`" << z.name() <<
"` is one of [" << z.type() <<
"],";
175 auto s = spec + where.str();
176 if (y.arguments().size() > 0) {
177 s = s.substr(0, s.size() - 1);
180 cells.push_back(y.doc());
186 std::stringstream ss;
188 ss <<
" [" << title <<
"]\n";
189 ss << print(
m_data->m_algorithms[m_root_type], 2) <<
"\n\n";
191 ss <<
" [Argument types]\n";
192 for (
auto& x :
m_data->m_algorithms) {
193 if (x.first == m_root_type) {
196 ss <<
" [" << x.first <<
"]\n";
197 ss << print(x.second, 4) <<
"\n\n";
203 template<
typename algorithm_t>
204 inline std::unique_ptr<algorithm_t> Registry<algorithm_t>::select_algorithm(
const AlgorithmValue& algo)
const {
205 auto& static_only_evald_algo = algo.static_selection();
207 if (
m_data->m_registered.count(static_only_evald_algo) > 0) {
208 auto env = std::make_shared<EnvRoot>(AlgorithmValue(algo));
210 auto& constructor =
m_data->m_registered[static_only_evald_algo];
212 return constructor(Env(env, env->algo_value()));
214 throw std::runtime_error(
"No implementation found for " + m_root_type +
" " 215 + static_only_evald_algo.to_string()
220 template<
typename algorithm_t>
221 inline AlgorithmValue Registry<algorithm_t>::parse_algorithm_id(
224 ast::Parser p { text };
225 auto parsed_algo = p.parse_value();
226 auto options = eval::cl_eval(std::move(parsed_algo),
230 return std::move(options).to_algorithm();
233 template<
typename algorithm_t>
235 const std::string& options)
const {
237 return select_algorithm(parse_algorithm_id(options));
Contains the text compression and encoding framework.
std::string make_table(const std::vector< std::string > &data, size_t cols, bool draw_grid=true)
Renders the given dataset into an ASCII table.
void register_algorithm()
Registers an tdc::Algorithm.
std::string indent_lines(const std::string &s, size_t indent)
Indents each line of a string (separated by \n) by the specified amount of spaces.
eval::AlgorithmTypes & algorithm_map()
Automatically select compress mode, internal use in TextDS only.