Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mapping_example.C
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
141#include <iostream>
142#include <iomanip>
143#include <string>
144
145#include <ah-mapping.H>
146#include <tpl_dynArray.H>
147
148using namespace std;
149using namespace Aleph;
150
151// ============================================================================
152// Helper functions
153// ============================================================================
154
155void print_header(const string& title)
156{
157 cout << "\n";
158 cout << "+" << string(70, '-') << "+" << endl;
159 cout << "| " << left << setw(68) << title << " |" << endl;
160 cout << "+" << string(70, '-') << "+" << endl;
161}
162
163void print_subheader(const string& subtitle)
164{
165 cout << "\n " << subtitle << endl;
166 cout << " " << string(subtitle.length(), '-') << endl;
167}
168
169// ============================================================================
170// Example 1: Basic Mapping - Colombian Department Codes
171// ============================================================================
172
174{
175 print_header("Example 1: Colombian Department Codes");
176
177 // Create mapping from DANE codes to department names
179
180 // Insert Colombian departments (DANE codes)
181 dept_codes.insert("05", "Antioquia");
182 dept_codes.insert("08", "Atlantico");
183 dept_codes.insert("11", "Bogota D.C.");
184 dept_codes.insert("13", "Bolivar");
185 dept_codes.insert("15", "Boyaca");
186 dept_codes.insert("17", "Caldas");
187 dept_codes.insert("19", "Cauca");
188 dept_codes.insert("20", "Cesar");
189 dept_codes.insert("23", "Cordoba");
190 dept_codes.insert("25", "Cundinamarca");
191 dept_codes.insert("27", "Choco");
192 dept_codes.insert("41", "Huila");
193 dept_codes.insert("44", "La Guajira");
194 dept_codes.insert("47", "Magdalena");
195 dept_codes.insert("50", "Meta");
196 dept_codes.insert("52", "Narino");
197 dept_codes.insert("54", "Norte de Santander");
198 dept_codes.insert("63", "Quindio");
199 dept_codes.insert("66", "Risaralda");
200 dept_codes.insert("68", "Santander");
201 dept_codes.insert("70", "Sucre");
202 dept_codes.insert("73", "Tolima");
203 dept_codes.insert("76", "Valle del Cauca");
204 dept_codes.insert("91", "Amazonas");
205
206 cout << "\n Total departments registered: " << dept_codes.size() << endl;
207
208 print_subheader("Forward lookup (code -> name)");
209
215
216 for (size_t i = 0; i < codes_to_lookup.size(); ++i)
217 {
218 const string& code = codes_to_lookup(i);
219 cout << " Code " << code << " -> " << dept_codes[code] << endl;
220 }
221
222 print_subheader("Inverse mapping (name -> code)");
223
224 // Create inverse mapping
226
228 names_to_lookup.append("Bogota D.C.");
229 names_to_lookup.append("Antioquia");
230 names_to_lookup.append("Valle del Cauca");
231 names_to_lookup.append("Amazonas");
232
233 for (size_t i = 0; i < names_to_lookup.size(); ++i)
234 {
235 const string& name = names_to_lookup(i);
236 cout << " " << left << setw(20) << name << " -> Code "
237 << name_to_code[name] << endl;
238 }
239
240 print_subheader("Check if key exists");
241
242 cout << " Has code '11'? " << (dept_codes.valid_key("11") ? "Yes" : "No") << endl;
243 cout << " Has code '99'? " << (dept_codes.valid_key("99") ? "Yes" : "No") << endl;
244}
245
246// ============================================================================
247// Example 2: Variadic Constructor - Coffee Regions
248// ============================================================================
249
251{
252 print_header("Example 2: Variadic Constructor - Coffee Regions");
253
254 // Create mapping using variadic constructor
256 "Huila", "Sur del pais, cafe con notas citricos",
257 "Narino", "Alta montania, cafe suave y frutal",
258 "Cauca", "Region volcanica, cafe de acidez brillante",
259 "Tolima", "Clima templado, cafe balanceado",
260 "Antioquia", "Tradicion cafetera, cafe con cuerpo",
261 "Caldas", "Eje Cafetero, cafe clasico colombiano",
262 "Quindio", "Paisaje Cultural Cafetero, cafe aromatico",
263 "Risaralda", "Eje Cafetero, cafe de montania",
264 "Santander", "Oriente, cafe organico y especial"
265 );
266
267 cout << "\n Coffee regions: " << coffee_regions.size() << endl;
268
269 print_subheader("Region profiles");
270
271 // Use for_each to iterate
272 coffee_regions.for_each([](const string& region, const string& profile) {
273 cout << " " << left << setw(12) << region << ": " << profile << endl;
274 });
275
276 print_subheader("Keys and values separately");
277
278 cout << "\n All regions: ";
279 auto keys = coffee_regions.keys();
280 for (auto it = keys.get_it(); it.has_curr(); it.next_ne())
281 cout << it.get_curr() << " ";
282 cout << endl;
283}
284
285// ============================================================================
286// Example 3: Numeric Mappings - Population Data
287// ============================================================================
288
290{
291 print_header("Example 3: Numeric Mappings - City Population");
292
293 // Mapping from city name to population (thousands)
294 AHMapping<string, int> population;
295
296 population.insert("Bogota", 8281);
297 population.insert("Medellin", 2569);
298 population.insert("Cali", 2228);
299 population.insert("Barranquilla", 1274);
300 population.insert("Cartagena", 1047);
301 population.insert("Cucuta", 711);
302 population.insert("Bucaramanga", 609);
303 population.insert("Pereira", 488);
304 population.insert("Santa Marta", 538);
305 population.insert("Ibague", 580);
306
307 print_subheader("Cities by population (thousands)");
308
309 population.for_each([](const string& city, int pop) {
310 cout << " " << left << setw(15) << city << ": "
311 << right << setw(6) << pop << " mil habitantes" << endl;
312 });
313
314 print_subheader("Total population");
315
316 int total = 0;
317 auto values = population.values();
318 for (auto it = values.get_it(); it.has_curr(); it.next_ne())
319 total += it.get_curr();
320
321 cout << " Sum of registered cities: " << total << " mil habitantes" << endl;
322 cout << " (Approx. " << total / 1000.0 << " millones)" << endl;
323
324 print_subheader("Contains value check");
325
326 cout << " Contains city with 2569k? "
327 << (population.contains_value(2569) ? "Yes (Medellin)" : "No") << endl;
328 cout << " Contains city with 5000k? "
329 << (population.contains_value(5000) ? "Yes" : "No") << endl;
330}
331
332// ============================================================================
333// Example 4: Encoding/Decoding - Indigenous Languages
334// ============================================================================
335
337{
338 print_header("Example 4: Encoding/Decoding - Language Codes");
339
340 // ISO 639-3 codes for Colombian indigenous languages
342
343 lang_codes.insert("way", "Wayuunaiki"); // Wayuu
344 lang_codes.insert("cag", "Embera"); // Chami
345 lang_codes.insert("iku", "Arhuaco"); // Ika
346 lang_codes.insert("snn", "Inga"); // Highland Inga
347 lang_codes.insert("kwi", "Awa Pit"); // Awa-Cuaiquer
348 lang_codes.insert("guc", "Guajiro"); // Wayuu variant
349 lang_codes.insert("pbb", "Nasa Yuwe"); // Paez
350 lang_codes.insert("mvt", "Motilon"); // Bari
351 lang_codes.insert("cub", "Cubeo"); // Cubeo
352 lang_codes.insert("tic", "Tikuna"); // Ticuna
353
354 cout << "\n Indigenous languages registered: " << lang_codes.size() << endl;
355
356 print_subheader("Encode: Name -> Code");
357
358 // Create decoder (inverse of encoder)
360
362 languages.append("Wayuunaiki");
363 languages.append("Nasa Yuwe");
364 languages.append("Tikuna");
365 languages.append("Embera");
366
367 for (size_t i = 0; i < languages.size(); ++i)
368 {
369 const string& lang = languages(i);
370 cout << " " << left << setw(15) << lang << " -> code: "
371 << decoder[lang] << endl;
372 }
373
374 print_subheader("Decode: Code -> Name");
375
377 codes.append("way");
378 codes.append("pbb");
379 codes.append("tic");
380 codes.append("cub");
381
382 for (size_t i = 0; i < codes.size(); ++i)
383 {
384 const string& code = codes(i);
385 cout << " Code " << code << " -> " << lang_codes[code] << endl;
386 }
387}
388
389// ============================================================================
390// Example 5: Modifiable Mapping - Currency Exchange
391// ============================================================================
392
394{
395 print_header("Example 5: Modifiable Mapping - Exchange Rates");
396
397 // Exchange rates (COP per unit of foreign currency)
399
400 exchange["USD"] = 4150.0;
401 exchange["EUR"] = 4520.0;
402 exchange["GBP"] = 5280.0;
403 exchange["MXN"] = 245.0;
404 exchange["BRL"] = 830.0;
405 exchange["ARS"] = 4.7;
406 exchange["PEN"] = 1120.0;
407 exchange["CLP"] = 4.5;
408
409 print_subheader("Current exchange rates (COP per unit)");
410
411 exchange.for_each([](const string& currency, double rate) {
412 cout << " 1 " << currency << " = " << fixed << setprecision(2)
413 << rate << " COP" << endl;
414 });
415
416 print_subheader("Update rates");
417
418 // Update some rates
419 exchange["USD"] = 4200.0; // Dollar strengthened
420 exchange["EUR"] = 4480.0; // Euro weakened
421
422 cout << " Updated USD: 1 USD = " << exchange["USD"] << " COP" << endl;
423 cout << " Updated EUR: 1 EUR = " << exchange["EUR"] << " COP" << endl;
424
425 print_subheader("Remove currency");
426
427 cout << " Removing ARS..." << endl;
428 exchange.remove("ARS");
429 cout << " Has ARS? " << (exchange.valid_key("ARS") ? "Yes" : "No") << endl;
430 cout << " Total currencies: " << exchange.size() << endl;
431
432 print_subheader("Conversion example");
433
434 double usd_amount = 100.0;
435 double eur_amount = 50.0;
436
437 cout << " $" << usd_amount << " USD = $"
438 << fixed << setprecision(0) << (usd_amount * exchange["USD"]) << " COP" << endl;
439 cout << " " << eur_amount << " EUR = $"
440 << (eur_amount * exchange["EUR"]) << " COP" << endl;
441}
442
443// ============================================================================
444// Example 6: Practical Application - City Coordinates
445// ============================================================================
446
448{
449 print_header("Example 6: City Coordinates Lookup");
450
451 // Simple coordinate structure
452 struct Coord {
453 double lat;
454 double lon;
455 };
456
457 // Mapping from city to coordinates
459
460 cities.insert("Bogota", {4.711, -74.072});
461 cities.insert("Medellin", {6.244, -75.574});
462 cities.insert("Cali", {3.451, -76.532});
463 cities.insert("Barranquilla", {10.964, -74.796});
464 cities.insert("Cartagena", {10.391, -75.479});
465 cities.insert("Cucuta", {7.893, -72.508});
466 cities.insert("Santa Marta", {11.241, -74.199});
467 cities.insert("Leticia", {-4.215, -69.940});
468 cities.insert("San Andres", {12.584, -81.701});
469
470 print_subheader("City coordinates (latitude, longitude)");
471
472 cities.for_each([](const string& city, const Coord& c) {
473 cout << " " << left << setw(15) << city
474 << fixed << setprecision(3)
475 << "(" << setw(8) << c.lat << ", " << setw(8) << c.lon << ")" << endl;
476 });
477
478 print_subheader("Distance approximation (Bogota to other cities)");
479
480 Coord bogota = cities["Bogota"];
481
482 // Simple approximation (not geodesic)
483 auto approx_distance = [&](const string& city) {
485 double dlat = (other.lat - bogota.lat) * 111.0; // ~111 km per degree
486 double dlon = (other.lon - bogota.lon) * 111.0 * cos(bogota.lat * M_PI / 180);
487 return sqrt(dlat * dlat + dlon * dlon);
488 };
489
491 target_cities.append("Medellin");
492 target_cities.append("Cali");
493 target_cities.append("Barranquilla");
494 target_cities.append("Leticia");
495 target_cities.append("San Andres");
496
497 for (size_t i = 0; i < target_cities.size(); ++i)
498 {
499 const string& city = target_cities(i);
500 cout << " Bogota -> " << left << setw(15) << city
501 << "~" << fixed << setprecision(0) << approx_distance(city) << " km" << endl;
502 }
503}
504
505// ============================================================================
506// Main
507// ============================================================================
508
509int main()
510{
511 cout << "\n";
512 cout << "========================================================================" << endl;
513 cout << " ALEPH-W MAPPING EXAMPLE" << endl;
514 cout << " Bidirectional Key-Value Mappings" << endl;
515 cout << "========================================================================" << endl;
516
523
524 cout << "\n";
525 cout << "========================================================================" << endl;
526 cout << " Example completed successfully!" << endl;
527 cout << "========================================================================" << endl;
528 cout << endl;
529
530 return 0;
531}
532
Bidirectional mapping between two types.
A generic key-value mapping container with inverse operation support.
Definition ah-mapping.H:70
DynList< ValueType > values() const
Gets all values in the mapping.
Definition ah-mapping.H:218
bool contains_value(const ValueType &value) const
Checks if the mapping contains a specific value.
Definition ah-mapping.H:257
void insert(const Key &key, const ValueType &value)
Inserts or updates a key-value pair in the mapping.
Definition ah-mapping.H:131
void for_each(F f) const
Applies a function to each key-value pair.
Definition ah-mapping.H:271
T & insert(const T &item)
Insert a new item by copy.
Definition htlist.H:1502
T & append(const T &item)
Append a new item by copy.
Definition htlist.H:1562
T remove()
Remove the first item of the list.
Definition htlist.H:1611
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
size_t length() const noexcept
Count the number of elements of a container.
Definition ah-dry.H:1385
void for_each(Operation &operation)
Traverse all the container and performs an operation on each element.
Definition ah-dry.H:685
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_cos_function > > cos(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4069
__gmp_expr< T, __gmp_unary_expr< __gmp_expr< T, U >, __gmp_sqrt_function > > sqrt(const __gmp_expr< T, U > &expr)
Definition gmpfrxx.h:4058
double Coord
Definition graphpic.C:316
void demo_city_coordinates()
void demo_variadic_constructor()
void demo_department_codes()
void print_subheader(const string &subtitle)
void demo_encoding_decoding()
void demo_modifiable_mapping()
int main()
void demo_numeric_mapping()
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
void print_header()
Aleph::DynList< T > keys() const
Definition ah-dry.H:1516
Lazy and scalable dynamic array implementation.