C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Classes | Namespaces | Functions
exprtk_groups_examples.cpp File Reference
#include <algorithm>
#include <cassert>
#include <chrono>
#include <cstdio>
#include <iterator>
#include <map>
#include <set>
#include <string>
#include "exprtk.hpp"
Include dependency graph for exprtk_groups_examples.cpp:

Go to the source code of this file.

Classes

struct  groups_impl::groups_collection< T >
 
class  groups_impl::create< T >
 
class  groups_impl::add< T >
 
class  groups_impl::contains< T >
 
class  groups_impl::size< T >
 
class  groups_impl::get< T >
 
class  groups_impl::erase< T >
 

Namespaces

namespace  groups_impl
 

Functions

template<typename SetType , typename T >
SetType * groups_impl::make_set_handle (T v)
 
template<typename T >
void groups_example ()
 
int main ()
 

Function Documentation

◆ groups_example()

template<typename T >
void groups_example ( )

Definition at line 343 of file exprtk_groups_examples.cpp.

344{
345 using symbol_table_t = exprtk::symbol_table<T>;
346 using expression_t = exprtk::expression<T>;
347 using parser_t = exprtk::parser<T>;
348
349 using strset_t = std::set<std::string>;
350
351 strset_t girl_names;
352 strset_t boy_names;
353
354 girl_names.insert("jane");
355 girl_names.insert("jill");
356
357 boy_names.insert("jack");
358 boy_names.insert("jim" );
359 boy_names.insert("john");
360
362
363 names_groups.add_group("girls", girl_names);
364 names_groups.add_group("boys", boy_names );
365
366 assert(names_groups.contains("girls"));
367 assert(names_groups.contains("boys" ));
368
369 groups_impl::create <T> group_create (names_groups);
370 groups_impl::add <T> group_add (names_groups);
371 groups_impl::contains<T> group_contains(names_groups);
372 groups_impl::size <T> group_size (names_groups);
373 groups_impl::get <T> group_get (names_groups);
374 groups_impl::erase <T> group_erase (names_groups);
375
377
378 symbol_table_t symbol_table;
379 expression_t expression;
380 parser_t parser;
381
382 symbol_table.add_function("create" , group_create );
383 symbol_table.add_function("add" , group_add );
384 symbol_table.add_function("contains", group_contains);
385 symbol_table.add_function("size" , group_size );
386 symbol_table.add_function("get" , group_get );
387 symbol_table.add_function("erase" , group_erase );
388 symbol_table.add_function("println" , println );
389
390 expression.register_symbol_table(symbol_table);
391
392 const std::string program =
393 " /* Load the groups from symbol table */ "
394 " var girls := create('girls'); "
395 " var boys := create('boys' ); "
396 " "
397 " if (not(girls)) "
398 " { "
399 " println('failed to create girls set!'); "
400 " return []; "
401 " }; "
402 " "
403 " if (not(boys)) "
404 " { "
405 " println('failed to create boys set!'); "
406 " return []; "
407 " }; "
408 " "
409 " var name := 'john'; "
410 " "
411 " if (contains(girls, name)) "
412 " { "
413 " println(name, ' in girls set'); "
414 " } "
415 " else "
416 " { "
417 " println(name, ' NOT in girls set'); "
418 " }; "
419 " "
420 " if (contains(boys, name)) "
421 " { "
422 " println(name, ' in boys set'); "
423 " } "
424 " else "
425 " { "
426 " println(name, ' NOT in boys set'); "
427 " }; "
428 " "
429 " /* Create a group from scratch */ "
430 " var pets := create('pets'); "
431 " "
432 " add(pets, 'buddy'); "
433 " add(pets, 'fluffy', 'snowy'); "
434 " add(pets, 'ziggy', 'monty', 'teddy'); "
435 " "
436 " if (contains(pets, name)) "
437 " { "
438 " println(name, ' in pets set'); "
439 " } "
440 " else "
441 " { "
442 " println(name, ' NOT in pets set'); "
443 " }; "
444 " "
445 " if (contains(pets, name, 'fluffy')) "
446 " { "
447 " println(name, ' or fluffy in pets set'); "
448 " } "
449 " else "
450 " { "
451 " println(name, ' or fluffy NOT in pets set'); "
452 " }; "
453 " "
454 " println('List of \\'pets\\':'); "
455 " "
456 " for (var i := 0; i < size(pets); i += 1) "
457 " { "
458 " println('[', i ,']: ', get(pets,i)); "
459 " }; "
460 " "
461 " if (erase(pets, 'fluffy', 'teddy') != 2) "
462 " { "
463 " println('Failed to erase fluffy and teddy'); "
464 " return []; "
465 " }; "
466 " "
467 " println('List of \\'pets\\':'); "
468 " "
469 " for (var i := 0; i < size(pets); i += 1) "
470 " { "
471 " println('[', i ,']: ', get(pets,i)); "
472 " }; "
473 " ";
474
475 if (!parser.compile(program, expression))
476 {
477 printf("Error: %s\tExpression: %s\n",
478 parser.error().c_str(),
479 program.c_str());
480 return;
481 }
482
483 expression.value();
484
485 // Benchmark
486 //{
487 // constexpr auto rounds = 100000;
488 //
489 // const auto begin = std::chrono::system_clock::now();
490 // {
491 // for (std::size_t i = 0; i < rounds; ++i)
492 // {
493 // expression.value();
494 // }
495 // }
496 // const auto end = std::chrono::system_clock::now();
497 //
498 // const auto total_time_ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - begin).count();
499 //
500 // printf("[*] total time: %5.4fsec rate: %8.4feval/sec\n",
501 // total_time_ms / 1000.0,
502 // (1000.0 * rounds) / total_time_ms);
503 //}
504}
bool contains(const std::string &group_name) const
bool add_group(const std::string &group_name, const std::set< T > &set)

References groups_impl::groups_collection< T >::add_group(), and groups_impl::groups_collection< T >::contains().

Here is the call graph for this function:

◆ main()

int main ( )

Definition at line 506 of file exprtk_groups_examples.cpp.

507{
508 groups_example<double>();
509 return 0;
510}