On 11/01/2015 at 13:57, xxxxxxxx wrote:
In C++ dictionaries are called maps.
These are the R14 examples I have in my notes.
I haven't used these in R16 yet. So the code might need to written slightly different. But hopefully it will give you a quick boost in using them.
//The HashMap array in R14++ is similar to a dictionary
//Each array element has a key, and a value
//The key can be used to look up the elements in the array. Instead of using element index numbers
//The key and value types must bet set up when creating the HM: maxon::HashMap<type, type> myHmap;
//NOTE: You can use HM->put to add new items to the array if you don't want to check if an entry already exists
Example #1
maxon::HashMap<Int, String> myHmap;
auto entry1 = myHmap.Put(1, "first"); // 1 is the key "first" is the value
auto entry2 = myHmap.Put(2, "second"); // 2 is the key "second" is the value
GePrint(entry1->GetValue());
//Or
GePrint(myHmap.FindEntry(2)->GetValue()); //Find using the key (which is 2 in this example)
Example #2
maxon::HashMap<Int, Float> myHmap;
auto entry1 = myHmap.Put(1, 5.5); // 1 is the key 5.5 is the value
auto entry2 = myHmap.Put(2, 22.4); // 2 is the key 22.4 is the value
GePrint(String::FloatToString(entry1->GetValue()));
//Or
GePrint(String::FloatToString( myHmap.FindEntry(2)->GetValue())); //Find using the key (which is 2 in this example)
//This is an example of filling a HashMap with Atom objects as the keys
//Then assigning String values to them
BaseObject *obj1 = doc->GetFirstObject();
if(!obj1) return false;
BaseObject *obj2 = obj1->GetNext();
if(!obj2) return false;
//Create a HashMap array and insert the two objects into it. And set their values as text
//The key values lets us look up the items in the HashMap array
maxon::HashMap<C4DAtom*, String> myHmap;
maxon::Bool created = false;
auto entry1 = myHmap.FindOrCreateEntry(obj1, created);
if(created) entry1->SetValue("first");
auto entry2 = myHmap.FindOrCreateEntry(obj2, created);
if(created) entry2->SetValue("second");
//Iterate through the HM array using an iterator
//The iterator 'it' is the value of each array element
for(auto it = myHmap.Begin(); it != myHmap.End(); ++it)
{
auto value = it;
Int32 t = it->GetKey()->GetType(); //Get the type of object in the HM array
String keyVal = it->GetValue(); //Get the key value in the HM for this object
//We can change the keys in the HashMap if we want
if (keyVal == "first") entry1->SetValue("newKeyValue");
GePrint(String::IntToString(t) + " " + keyVal);
}
//Print the new key value in the HashMap
GePrint(entry1->GetValue());
-ScottA