|
The wxHashMap class is a simple, type-safe, and reasonably efficient hash map class, with an interface that is a subset of the interface of STL containers. In particular, the interface is modeled after std::map and the non-standard std::hash_map. By using macros to create hash maps, you can choose from several combinations of keys and values, including int, wxString, or void* (arbitrary class). There are three macros for declaring a hash map. To declare a hash map class named CLASSNAME with wxString keys and VALUE_T values: WX_DECLARE_STRING_HASH_MAP(VALUE_T, CLASSNAME); To declare a hash map class named CLASSNAME with void* keys and VALUE_T values: WX_DECLARE_VOIDPTR_HASH_MAP(VALUE_T, CLASSNAME); To declare a hash map class named CLASSNAME with arbitrary keys or values: WX_DECLARE_HASH_MAP(KEY_T, VALUE_T, HASH_T, KEY_EQ_T, CLASSNAME); HASH_T and KEY_EQ_T are the types used for the hashing function and key comparison. wxWidgets provides three predefined hashing functions: wxInteger Hash for integer types (int, long, short, and their unsigned counterparts), wxStringHash for strings (wxString, wxChar*, char*), and wxPointerHash for any kind of pointer. Similarly, three equality predicates are provided: wxInteger Equal, wxStringEqual, and wxPointerEqual. The following code example shows the wxHashMap methods as well as how to create and use your custom hash type. // Our data class to store in the hash class Customer { public: int CustID; wxString CustName; }; // Declare our hash map class // This macro declares and implements CustomerList as a hash map WX_DECLARE_HASH_MAP(int, Customer*, wxIntegerHash, wxIntegerEqual, CustomerHash); void HashTest() { // Declare an instance of our list CustomerHash MyHash; bool IsEmpty = MyHash.empty(); // will be true // Create some customers Customer* CustA = new Customer; CustA->CustID = 10; CustA->CustName = wxT("Bob"); Customer* CustB = new Customer; CustB->CustID = 20; CustB->CustName = wxT("Sally"); Customer* CustC = new Customer; CustC->CustID = 5; CustC->CustName = wxT("Dmitri"); // Add the customers to the hash MyHash[CustA->CustID] = CustA; MyHash[CustB->CustID] = CustB; MyHash[CustC->CustID] = CustC; int Size = MyHash.size(); // will be 3 // count returns 0 or 1, i.e. is 20 in the hash? int Present = MyHash.count(20); // will be 1 // Customized iterator for our hash map CustomerHash::iterator i = MyHash.begin(); // End represents one past the last element while (i != MyHash.end()) { // first is the key, second is the value int CustID = i->first; Customer* Cust = i->second; // Process Customer // Advance to the next element in the hash i++; } // Remove Customer A from the hash MyHash.erase(10); // CustA object is NOT deleted by removing from hash delete CustA; // Return an iterator to the node with the specified key CustomerHash::iterator i2 = MyHash.find(21); // Find returns hash::end if the key was not found bool NotFound = (i2 == MyHash.end()); // will be true // This time the find will be successful i2 = MyHash.find(20); // Removing an element using the iterator MyHash.erase(i2); delete CustB; // Side-effect: A NULL value is inserted in the hash for key 30 Customer* Cust = MyHash[30]; // Cust will be NULL // Empties the hash map of all elements MyHash.clear(); delete CustC; } |
|