Logo

Programming-Idioms

  • Lua
  • Pascal
  • Rust
  • C
  • C++
#include <unordered_map>
std::unordered_map<std::string, double> mymap = {
    {"mom", 5.4},
    {"dad", 6.1},
    {"bro", 5.9}
};
#include <map>
std::map<const char*, int> x;
x["one"] = 1;
x["two"] = 2;
x = {one = 1, two = 2}
uses fgl;
type TMap = specialize TFPGMap<String, Integer>;
var x: TMap;

begin
  x := TMap.Create;
  x['one'] := 1;
  x['two'] := 2;  
end.
use std::collections::BTreeMap;
let mut x = BTreeMap::new();
x.insert("one", 1);
x.insert("two", 2);

Something different than a BTreeMap might be used, depending on the usage.

The function new of the type BTreeMap returns a usable map.
The map is stored mutable in the variable x.

Maps in Rust are generic and type safe at compile time.
use std::collections::HashMap;
let x: HashMap<&str, i32> = [
    ("one", 1),
    ("two", 2),
].into_iter().collect();
#include <search.h>
ENTRY a = {"foo", "twenty"};
ENTRY b = {"bar", "three"};
if (hcreate (23)) {
    hsearch(a, ENTER);
    hsearch(b, ENTER);
}

This POSIX functions maintain a single global hashmap. The GNU C library provides hcreate_r
with Ada.Containers.Indefinite_Hashed_Maps;
with Ada.Strings.Hash;

use Ada.Containers;
declare
   package Maps is new Indefinite_Hashed_Maps (Key_Type => String,
                                               Element_Type => Integer,
                                               Hash => Ada.Strings.Hash,
                                               Equivalent_Keys => "=");
      
   use Maps;
      
   X : Map := Empty_Map;
begin
   X.Insert ("One", 1);
   X.Insert ("Two", 2);
   X.Insert ("Three", 3);
end;

New implementation...