102using namespace Aleph;
111 cout <<
"+" << string(70,
'-') <<
"+" << endl;
112 cout <<
"| " << left << setw(68) << title <<
" |" << endl;
113 cout <<
"+" << string(70,
'-') <<
"+" << endl;
118 cout <<
"\n " << subtitle << endl;
119 cout <<
" " << string(subtitle.length(),
'-') << endl;
131 double add(
double a,
double b) {
return a + b; }
132 double sub(
double a,
double b) {
return a - b; }
133 double mul(
double a,
double b) {
return a * b; }
134 double div(
double a,
double b)
136 if (b == 0)
throw runtime_error(
"Division by zero");
139 double mod(
double a,
double b)
141 return static_cast<int>(a) %
static_cast<int>(b);
143 double pow(
double a,
double b) {
return std::pow(a, b); }
148 print_header(
"Example 1: Calculator with Function Pointers");
151 using CalcOp = double(*)(double, double);
162 cout <<
"\n Registered operations: + - * / % ^\n" << endl;
165 struct TestCase {
double a;
char op;
double b; };
168 tests.append({10,
'-', 3});
169 tests.append({7,
'*', 8});
170 tests.append({100,
'/', 4});
171 tests.append({17,
'%', 5});
172 tests.append({2,
'^', 10});
174 cout <<
" Expression Result" << endl;
175 cout <<
" " << string(35,
'-') << endl;
177 for (
size_t i = 0; i <
tests.size(); ++i)
182 double result = calc.
run(t.op, t.a, t.b);
183 cout <<
" " << setw(6) << t.a <<
" " << t.op <<
" "
184 << setw(6) << t.b <<
" = " << setw(10) << result << endl;
189 cout <<
"\n Checking for unregistered operation '!':" << endl;
190 cout <<
" valid_key('!') = " << (calc.
valid_key(
'!') ?
"true" :
"false") << endl;
201 using TextOp = function<string(
const string&)>;
205 processor.
insert(
"upper", [](
const string& s) {
207 for (
char& c : result) c =
toupper(c);
211 processor.
insert(
"lower", [](
const string& s) {
213 for (
char& c : result) c =
tolower(c);
217 processor.
insert(
"reverse", [](
const string& s) {
218 return string(s.rbegin(), s.rend());
221 processor.
insert(
"length", [](
const string& s) {
222 return to_string(s.length()) +
" characters";
225 processor.
insert(
"words", [](
const string& s) {
227 bool in_word =
false;
229 if (isspace(c)) in_word =
false;
230 else if (!in_word) {
count++; in_word =
true; }
235 processor.
insert(
"vowels", [](
const string& s) {
239 if (lower ==
'a' || lower ==
'e' || lower ==
'i' ||
240 lower ==
'o' || lower ==
'u')
247 string text =
"Colombia es un pais de gente trabajadora";
249 cout <<
"\n Original text: \"" << text <<
"\"\n" << endl;
251 cout <<
" Transformation Result" << endl;
252 cout <<
" " << string(55,
'-') << endl;
255 operations.
append(
"upper");
256 operations.
append(
"lower");
257 operations.
append(
"reverse");
258 operations.
append(
"length");
259 operations.
append(
"words");
260 operations.
append(
"vowels");
262 for (
size_t i = 0; i < operations.
size(); ++i)
264 const string& op = operations(i);
265 string result = processor.
run(op, text);
266 cout <<
" " << left << setw(18) << op << result << endl;
270 cout <<
"\n Available operations: ";
272 for (
auto it =
keys.get_it(); it.has_curr(); it.next_ne())
273 cout << it.get_curr() <<
" ";
283 print_header(
"Example 3: Colombian Regions Information System");
285 using RegionInfo = function<void()>;
288 regions.
insert(
"andina", []() {
289 cout <<
"\n REGION ANDINA" << endl;
290 cout <<
" Capital: Bogota" << endl;
291 cout <<
" Departments: Cundinamarca, Boyaca, Santander, Antioquia..." << endl;
292 cout <<
" Climate: Temperate to cold (varies with altitude)" << endl;
293 cout <<
" Products: Coffee, flowers, potatoes, emeralds" << endl;
296 regions.
insert(
"caribe", []() {
297 cout <<
"\n REGION CARIBE" << endl;
298 cout <<
" Major cities: Barranquilla, Cartagena, Santa Marta" << endl;
299 cout <<
" Departments: Atlantico, Bolivar, Magdalena, La Guajira..." << endl;
300 cout <<
" Climate: Tropical hot" << endl;
301 cout <<
" Products: Bananas, coal, tourism, fishing" << endl;
304 regions.
insert(
"pacifica", []() {
305 cout <<
"\n REGION PACIFICA" << endl;
306 cout <<
" Major cities: Cali, Buenaventura, Quibdo" << endl;
307 cout <<
" Departments: Valle del Cauca, Choco, Narino, Cauca" << endl;
308 cout <<
" Climate: Very humid tropical" << endl;
309 cout <<
" Products: Sugar cane, timber, gold, platinum" << endl;
312 regions.
insert(
"orinoquia", []() {
313 cout <<
"\n REGION ORINOQUIA (Los Llanos)" << endl;
314 cout <<
" Major cities: Villavicencio, Yopal" << endl;
315 cout <<
" Departments: Meta, Casanare, Arauca, Vichada" << endl;
316 cout <<
" Climate: Tropical with dry season" << endl;
317 cout <<
" Products: Cattle, oil, rice, palm oil" << endl;
320 regions.
insert(
"amazonia", []() {
321 cout <<
"\n REGION AMAZONIA" << endl;
322 cout <<
" Major cities: Leticia, Florencia" << endl;
323 cout <<
" Departments: Amazonas, Caqueta, Putumayo, Guaviare" << endl;
324 cout <<
" Climate: Humid equatorial" << endl;
325 cout <<
" Products: Timber, rubber, ecotourism, biodiversity" << endl;
328 regions.
insert(
"insular", []() {
329 cout <<
"\n REGION INSULAR" << endl;
330 cout <<
" Islands: San Andres, Providencia, Santa Catalina" << endl;
331 cout <<
" Location: Caribbean Sea" << endl;
332 cout <<
" Climate: Tropical maritime" << endl;
333 cout <<
" Products: Tourism, coconut, fishing" << endl;
338 region_codes.
append(
"andina");
339 region_codes.
append(
"caribe");
340 region_codes.
append(
"pacifica");
341 region_codes.
append(
"orinoquia");
342 region_codes.
append(
"amazonia");
343 region_codes.
append(
"insular");
345 cout <<
"\n Colombia's Natural Regions:" << endl;
346 cout <<
" " << string(40,
'=') << endl;
348 for (
size_t i = 0; i < region_codes.
size(); ++i)
349 regions.
run(region_codes(i));
358 print_header(
"Example 4: Order Processing State Machine");
368 void log(
const string& action) {
369 history +=
" -> " + action +
" (from " + state +
")\n";
373 Order order = {1001,
"created",
"Juan Perez", 250000.0,
""};
375 using StateAction = function<void(Order&)>;
379 state_machine.
insert(
"confirm", [](Order& o) {
380 if (o.state !=
"created") {
381 cout <<
" [ERROR] Cannot confirm - order not in 'created' state" << endl;
385 o.state =
"confirmed";
386 cout <<
" [OK] Order " << o.id <<
" confirmed" << endl;
389 state_machine.
insert(
"pay", [](Order& o) {
390 if (o.state !=
"confirmed") {
391 cout <<
" [ERROR] Cannot pay - order not confirmed" << endl;
396 cout <<
" [OK] Payment of $" << fixed << setprecision(0) << o.amount
397 <<
" COP received" << endl;
400 state_machine.
insert(
"ship", [](Order& o) {
401 if (o.state !=
"paid") {
402 cout <<
" [ERROR] Cannot ship - order not paid" << endl;
407 cout <<
" [OK] Order shipped to " << o.customer << endl;
410 state_machine.
insert(
"deliver", [](Order& o) {
411 if (o.state !=
"shipped") {
412 cout <<
" [ERROR] Cannot deliver - order not shipped" << endl;
416 o.state =
"delivered";
417 cout <<
" [OK] Order delivered successfully!" << endl;
420 state_machine.
insert(
"cancel", [](Order& o) {
421 if (o.state ==
"delivered") {
422 cout <<
" [ERROR] Cannot cancel delivered order" << endl;
426 o.state =
"cancelled";
427 cout <<
" [OK] Order cancelled" << endl;
431 cout <<
"\n Order #" << order.id <<
" - Customer: " << order.customer << endl;
432 cout <<
" Amount: $" << fixed << setprecision(0) << order.amount <<
" COP" << endl;
433 cout <<
"\n Processing order:" << endl;
434 cout <<
" " << string(40,
'-') << endl;
437 state_machine.
run(
"confirm", order);
438 state_machine.
run(
"pay", order);
439 state_machine.
run(
"ship", order);
440 state_machine.
run(
"deliver", order);
443 cout <<
"\n Attempting invalid transition:" << endl;
444 state_machine.
run(
"cancel", order);
447 cout <<
"\n Order History:" << endl;
448 cout << order.history;
449 cout <<
" Final state: " << order.state << endl;
458 print_header(
"Example 5: Hash-based Dispatcher Performance");
461 using MetricHandler = function<double(
double)>;
465 metrics.
insert(
"usd_to_cop", [](
double usd) {
469 metrics.
insert(
"cop_to_usd", [](
double cop) {
473 metrics.
insert(
"inflation_adjust", [](
double value) {
477 metrics.
insert(
"vat", [](
double price) {
481 metrics.
insert(
"income_tax", [](
double income) {
483 if (income < 4500000)
return 0.0;
484 if (income < 10000000)
return (income - 4500000) * 0.19;
485 if (income < 25000000)
return (income - 10000000) * 0.28 + 1045000;
486 return (income - 25000000) * 0.33 + 5245000;
489 cout <<
"\n Colombian Economic Calculations:" << endl;
490 cout <<
" " << string(50,
'-') << endl;
493 double usd_amount = 100.0;
494 cout <<
"\n Currency Conversion:" << endl;
495 cout <<
" $" << usd_amount <<
" USD = $" << fixed << setprecision(0)
496 << metrics.
run(
"usd_to_cop", usd_amount) <<
" COP" << endl;
498 double cop_amount = 1000000.0;
499 cout <<
" $" << cop_amount <<
" COP = $" << setprecision(2)
500 << metrics.
run(
"cop_to_usd", cop_amount) <<
" USD" << endl;
503 cout <<
"\n Price Calculations:" << endl;
504 double base_price = 500000.0;
505 cout <<
" Base price: $" << setprecision(0) << base_price <<
" COP" << endl;
506 cout <<
" With VAT (19%): $" << metrics.
run(
"vat", base_price) <<
" COP" << endl;
507 cout <<
" Inflation adjusted: $" << metrics.
run(
"inflation_adjust", base_price)
511 cout <<
"\n Monthly Income Tax Examples:" << endl;
518 for (
size_t i = 0; i < incomes.
size(); ++i)
520 double income = incomes(i);
521 double tax = metrics.
run(
"income_tax", income);
522 cout <<
" Income $" << setw(12) << income <<
" COP -> Tax: $"
523 << setw(10) << tax <<
" COP" << endl;
533 print_header(
"Example 6: Variadic Arguments Dispatcher");
541 for (
size_t i = 0; i < args.
size(); ++i) {
542 if (i > 0) result +=
" ";
550 for (
size_t i = 0; i < args.
size(); ++i)
556 return args.
size() > 0 ? args(0) :
"";
560 return args.
size() > 0 ? args(args.
size() - 1) :
"";
570 cities.
append(
"Medellin");
572 cities.
append(
"Barranquilla");
573 cities.
append(
"Cartagena");
575 cout <<
"\n Arguments: Bogota, Medellin, Cali, Barranquilla, Cartagena\n" << endl;
577 cout <<
" Operation Result" << endl;
578 cout <<
" " << string(50,
'-') << endl;
580 cout <<
" join: " << flex.
run(
"join", cities) << endl;
581 cout <<
" concat: " << flex.
run(
"concat", cities) << endl;
582 cout <<
" first: " << flex.
run(
"first", cities) << endl;
583 cout <<
" last: " << flex.
run(
"last", cities) << endl;
584 cout <<
" count: " << flex.
run(
"count", cities) << endl;
594 cout <<
"========================================================================" << endl;
595 cout <<
" ALEPH-W DISPATCHER EXAMPLE" << endl;
596 cout <<
" Dynamic Command Dispatching" << endl;
597 cout <<
"========================================================================" << endl;
607 cout <<
"========================================================================" << endl;
608 cout <<
" Example completed successfully!" << endl;
609 cout <<
"========================================================================" << endl;
Command dispatcher pattern implementation.
size_t size_t int32_t value
Tree-based command dispatcher.
bool valid_key(const Key &key)
Check if a key is registered.
void insert(const Key &key, Operation op)
Register a key-operation pair.
auto run(const Key &key, Args &&... args) const
Execute the operation associated with a key.
DynList< Key > keys() const
Get all registered keys.
Hash-based command dispatcher.
auto run(const Key &key, Args ... args) const
Execute the operation associated with a key.
void insert(const Key &key, Operation op)
Register a key-operation pair.
size_t size() const noexcept
Return the current dimension of array.
T & append()
Allocate a new entry to the end of array.
void demo_state_machine()
void demo_hash_dispatcher()
void demo_variadic_dispatcher()
void demo_text_processor()
void print_subheader(const string &subtitle)
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_log_function > > log(const __gmp_expr< T, U > &expr)
Main namespace for Aleph-w library functions.
std::string tolower(const char *str)
Convert a C std::string to lower-case.
std::string to_string(const time_t t, const std::string &format)
Format a time_t value into a string using format.
std::string toupper(const char *str)
Convert a C std::string to upper-case.
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Count elements equal to a value.
Basic arithmetic operations for the calculator.
double sub(double a, double b)
double add(double a, double b)
double div(double a, double b)
double mul(double a, double b)
double pow(double a, double b)
double mod(double a, double b)
Lazy and scalable dynamic array implementation.