Using MAUtil Set, Map, HashMap
The MAUtil classes Set, Map and HashMap provide generic containers similar to std::set, map and unordered_map in the STL or Java's Set, Map and Hashtable. They provide many of the same familiar operations. Here we describe examples for Set, Map, and HashMap:
Set
/**
* @file Set.cpp
* @author Naveed Asif
*
* Description:
*
* In this tutorial we will learn the use of Set
* container. Initially a list of numbers is
* created and then the difference between erase()
* clear() is shown.
*/
#include <MAUtil/Moblet.h>
#include <conprint.h>
#include <MAUtil/Set.h>
using namespace MAUtil;
class MyMoblet : public Moblet
{
public:
// Set is declared.
Set<int> myNumbers;
MyMoblet()
{
// Add some numbers.
myNumbers.insert(786);
myNumbers.insert(111);
myNumbers.insert(222);
myNumbers.insert(333);
// Shows the original dictionary in the console.
// Note: The dictionary values will be displayed in
// ascending order.
showDictionary();
// Locates and Erases any specific Number.
myNumbers.erase(333);
// Look at the dictionary and notice that 333 is erased.
showDictionary();
// This will delete all the members of container.
myNumbers.clear();
// Will show that you got 0 members.
showDictionary();
}
/**
* Called when a key is pressed on the keypad.
*/
void keyPressEvent(int keyCode, int nativeCode)
{
if (MAK_BACK == keyCode || MAK_0 == keyCode)
{
close();
}
}
/**
* showDictionary function is used to display all the
* members in myNumbers container.
*/
void showDictionary()
{
printf("I've got %d Numbers", myNumbers.size());
if (myNumbers.size() > 0)
{
printf("Numbers are:");
}
for(Set<int>::Iterator iter = myNumbers.begin();
iter != myNumbers.end();
iter++)
{
printf("%d", *iter);
}
}
};
/*
* Main function where the program starts
*/
extern "C" int MAMain()
{
Moblet::run(new MyMoblet());
return 0;
}
Map
/**
* @file Map.cpp
* @author Naveed Asif
*
* Description:
*
* In this tutorial we will learn the use of Map
* data structure. Initially a list of friends is
* created and then the Iterator's methods begin(),
* erase(), and find() are used.
*/
#include <MAUtil/Moblet.h>
#include <conprint.h>
#include <MAUtil/Map.h>
#include <MAUtil/String.h>
using namespace MAUtil;
class MyFriend
{
private:
String myName;
String myAddress;
public:
MyFriend(String name, String address)
{
myName = name;
myAddress = address;
}
void show()
{
printf("%s, %s", myName.c_str(), myAddress.c_str());
}
};
class MyMoblet : public Moblet
{
public:
// Map is declared.
Map<String, MyFriend*> myFriends;
MyMoblet()
{
// Add some friends.
addFriend("Rupert Bear", "Norwood, England");
addFriend("Ghuman", "Stockholm, Sweden");
addFriend("Windy Miller", "Trumpton 2323");
// Shows the original dictionary in the console.
// Note: The dictionary values will be displayed in
// ascending order.
showDictionary();
// Finds a friend using the find method and displays it
// in the console.
printf("--------------");
printf("Found friend:");
MyFriend* myFriend = myFriends.find("Rupert Bear")->second;
myFriend->show();
// Locates and erases a friend.
myFriends.erase("Ghuman");
// Look at the dictionary and notice that "Ghuman" is erased.
showDictionary();
// Finds the first entered friend.
printf("--------------");
printf("My first friend is:");
Map<String, MyFriend*>::Iterator iter = myFriends.begin();
iter->second->show();
}
/**
* Add a new friend to the dictionary.
*/
void addFriend(String name, String address)
{
myFriends.insert(name, new MyFriend(name, address));
}
/**
* Called when a key is pressed on the keypad.
*/
void keyPressEvent(int keyCode, int nativeCode)
{
if (MAK_BACK == keyCode || MAK_0 == keyCode)
{
close();
}
}
/**
* showDictionary function is used to display all the
* pair values in myFriends Map function
*/
void showDictionary()
{
printf("--------------");
printf("I've got %d friends:", myFriends.size());
for(Map<String, MyFriend*>::Iterator iter = myFriends.begin();
iter != myFriends.end();
iter++)
{
iter->second->show();
}
}
};
/*
* Main function where the program starts
*/
extern "C" int MAMain()
{
Moblet::run(new MyMoblet());
return 0;
}
HashMap
The HashMap works same like Map does except that Map always displays results in ascending order. You can simply change Map to HashMap in above example and it will work.
Similarities
The containers have a number of things in common. They have default constructors, copy constructors and destructors. Elements are copied by value, so care should be taken when dealing with pointers. Large elements should be passed by pointer or reference-counted. There are Iterators and ConstIterators. The functions find(), begin() and end() can return either type. The erase() function works either with a Key or with an Iterator, but not with a ConstIterator. We can also use size() and clear() functions.
The constructor has an optional argument for specifying the comparison function. The default comparison function uses the Key's operator<, so if your Key doesn't have it and you can't readily add it, this argument is useful.
Differences
Set has only one datum per element, whereas Map and HashMap have two data per element: a Key and a Value. Both must be specified when inserting a new element. Both are also accessible from Iterators. Map and HashMap also have the operator[].
HashMap's constructor has an optional argument for specifying the hash function. The default value is THashFunction< Key >. This template function has no default implementation. MoSync ships with implementations for MAUtil::String and int. If you use another key type, you'll need to implement a hash function for it.
Set and Map are sorted. The comparison function decides the sorting order.
HashMap is unsorted. The comparison function is only used in case of hash collision. While HashMap Iterators present the elements in a certain order, that order may change whenever an element is added or removed and should not be relied upon.
Further information
All the classes have reference documentation. Also, the source code is available in $MOSYNCDIR/include/MAUtil/ in your MoSync package.
- Printer-friendly version
- Login or register to post comments
Share on Facebook