Tuesday, April 5, 2016

Swift's Dictionary and C++'s Unordered Map

I am not going to discuss and debate about which one is best. For me, both are tools to accomplish goals. So I am merely going to list out the similarities and differences between them.


Swift has dictionary and C++ has Unordered Map, similar to python's dictionary, in the sense of technical term and the key value pairs. Or similar to perls' hash as per semantics of key value pair. I am not going to discuss how they are all implemented internally. That is a whole new discussion for another day. For today, lets just look at the logical or syntactical differences and similarities.


What is?
Dictionary has key, value mapping. All keys belong to the same type. All values belong to the same type. Keys are unique, if a new key value pair is being added to this associative container and the key already exists as per the comparison logic, then the new values replace the old values. Old values will be lost forever.

Set:
Set the values is normally via the keys. Access the key directly, and set the value.

Swift:
var Dict = [Int: String]();
  1. Dict[16] = "sixteen" // to store the string value for a key value.Will add or replace to maintain uniqueness.

  or

  if let old = Dict.updateValue("sixteen", forKey: 16){ //updateValue returns old value as optional value if present. Otherwise returns nil. 
print("oldvalue \(old)");
  }

  //Remove an entry by doing this:

  Dict[16] = nil

  //Remove entry
  if let old = Dict.removeValueForKey(16) { //returns old value as optional value if exists
print("old value \(old)");
  }
  else {
print("old value did not exist");
  }


C++:
  1. unorderedmap[16] = "sixteen" // to store the string value for a key value.


Get:
Getting the values is normally via the keys. Provide the key directly, and Get the value. 

Swift:
  1. print(test[16]);
C++:
  1. cout << unorderedmap[16];

Sorted?
No. Both of them, dictionary, and unordered_map are not sorted. Hence the name unordered. 


Unique?
Yes. Both are. The keys are unique.

Speed:
Unordered associative containers such as dictionaries and unordered_maps are faster in accessing the values of the key because hash value of the key is calculated and values stored in buckets for faster access internally.


Initialize:
To initialize dictionary, you can either do 

  var Dict = [Int: String](); //empty dictionary

or

  var Dict = [16: "Sixteen", 14: "Fourteen"]//When you initialize directly, you do not have to specify type as long as the key and value types are consistent. Thanks to type inference

to initialize array of dictionaries:

var DictArray = [[String, String]](); //empty array of dictionaries

or

var DictArray: [[String, String]] = [ ["Name" : "Mark Timothy", "Address" : "address 1"],

                                      ["Name" : "Nathan Deneth", "Address" : "address 2"] ]
To initialize in C++11

std::unordered_map unorderedmap { {3, "three"}, {4, "four"} }; // Initializer list since C++11. Code gives error in Visual Studio 2012. Compiles fine in VS 2015


Looping through:
Swift:

for(key, value) in Dict{ //a for-in loop
print("\(key) : \(value)");

}

for key in Dict.keys { //Get only the keys
print("\(key)");
}
for value in Dict.values { //Get only the values. 
print("\(value)");
}

C++:

for(auto pair : unorderedmap){ // auto keyword from C++11
cout  << pair.first  <<  endl; // int
cout << pair.second; //string

}


Whatever covered here are mostly similarities. To find any difference I would have to dig deeper. That post would follow soon.








No comments: