Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
mapping_example.C File Reference

Comprehensive example demonstrating Aleph-w's AHMapping class. More...

#include <iostream>
#include <iomanip>
#include <string>
#include <ah-mapping.H>
#include <tpl_dynArray.H>
Include dependency graph for mapping_example.C:

Go to the source code of this file.

Functions

void print_header (const string &title)
 
void print_subheader (const string &subtitle)
 
void demo_department_codes ()
 
void demo_variadic_constructor ()
 
void demo_numeric_mapping ()
 
void demo_encoding_decoding ()
 
void demo_modifiable_mapping ()
 
void demo_city_coordinates ()
 
int main ()
 

Detailed Description

Comprehensive example demonstrating Aleph-w's AHMapping class.

This example demonstrates AHMapping, a bidirectional key-value mapping container that provides efficient lookups in both directions. Unlike standard maps that only support forward lookup (key → value), AHMapping enables efficient reverse lookup (value → key) as well.

What is AHMapping?

AHMapping is a specialized container for bidirectional mappings:

  • Forward lookup: O(log n) - find value given key
  • Reverse lookup: O(log n) - find key given value (via inverse mapping)
  • Dual structure: Maintains both key→value and value→key mappings
  • Functional operations: Supports for_each and other functional ops

Key Features

Bidirectional Lookup

  • Forward: mapping[key] → returns value
  • Reverse: mapping.inverse()[value] → returns key
  • Both operations are O(log n) efficient

Data Structure

  • Uses balanced BST internally (typically Red-Black tree)
  • Maintains two mappings: forward and inverse
  • Automatic synchronization between mappings

Operations

  • Insert: Add key-value pair (updates both mappings)
  • Lookup: Find value by key or key by value
  • Iteration: Iterate over all pairs
  • Functional: Apply functions to all pairs

Use Cases

Translation Tables

  • Language codes ↔ names: "en" ↔ "English", "es" ↔ "Spanish"
  • Currency codes ↔ symbols: "USD" ↔ "$", "EUR" ↔ "€"
  • Country codes ↔ names: "CO" ↔ "Colombia", "US" ↔ "United States"

ID Mappings

  • User ID ↔ username: 12345 ↔ "john_doe"
  • Product ID ↔ name: 789 ↔ "Widget Pro"
  • Session ID ↔ user: "abc123" ↔ user_object

Configuration Parameters

  • Setting name ↔ value: "theme" ↔ "dark", "language" ↔ "es"
  • Environment variables: "PATH" ↔ "/usr/bin:/usr/local/bin"
  • Feature flags: "new_ui" ↔ true, "beta_features" ↔ false

Encoding/Decoding Schemes

  • Character encoding: ASCII codes ↔ characters
  • Base conversion: Decimal ↔ hexadecimal
  • Protocol mapping: Internal codes ↔ external codes

Colombian Theme

Examples use Colombian data for cultural relevance:

  • Departments: "ANT" ↔ "Antioquia", "CUN" ↔ "Cundinamarca"
  • Cities: "BOG" ↔ "Bogotá", "MED" ↔ "Medellín"
  • Cultural elements: Colombian regions, landmarks, traditions

Comparison with Standard Maps

Feature std::map AHMapping
Forward lookup O(log n) O(log n)
Reverse lookup O(n) O(log n)
Memory O(n) O(n)
Bidirectional No Yes

Advantage: AHMapping provides efficient reverse lookup without maintaining a separate reverse map manually.

Complexity

Operation Complexity Notes
Insert O(log n) Updates both mappings
Forward lookup O(log n) Key → value
Reverse lookup O(log n) Value → key
Iteration O(n) All pairs
Size O(1) Number of pairs

Usage Example

AHMapping<string, string> translations;
// Add translations
translations.insert("en", "English");
translations.insert("es", "Spanish");
translations.insert("fr", "French");
// Forward lookup
string lang = translations["en"]; // "English"
// Reverse lookup
string code = translations.inverse()["Spanish"]; // "es"
std::string code(Node *root)
Compute a string with the Lukasiewicz`s word of a tree.
See also
ah-mapping.H AHMapping implementation
Author
Leandro Rabindranath León

Definition in file mapping_example.C.

Function Documentation

◆ demo_city_coordinates()

◆ demo_department_codes()

◆ demo_encoding_decoding()

void demo_encoding_decoding ( )

◆ demo_modifiable_mapping()

◆ demo_numeric_mapping()

◆ demo_variadic_constructor()

◆ main()

◆ print_header()

void print_header ( const string &  title)

Definition at line 155 of file mapping_example.C.

References Aleph::maps().

◆ print_subheader()