1 \section{\class{wxHashMap
}}\label{wxhashmap
}
3 This is a simple, type-safe, and reasonably efficient hash map class,
4 whose interface is a subset of the interface of STL containers.
9 class MyClass
{ /* ... */
};
11 // declare a hash map with string keys and int values
12 WX_DECLARE_STRING_HASH_MAP( int, MyHash5 );
13 // same, with int keys and MyClass* values
14 WX_DECLARE_HASH_MAP( int, MyClass*, wxIntegerHash, wxIntegerEqual, MyHash1 );
15 // same, with wxString keys and int values
16 WX_DECLARE_STRING_HASH_MAP( int, MyHash3 );
17 // same, with wxString keys and values
18 WX_DECLARE_STRING_HASH_MAP( wxString, MyHash2 );
23 // store and retrieve values
24 h1
[1] = new MyClass(
1 );
26 h1
[50000] = new MyClass(
2 );
28 wxString tmp = h2
["Bill"
];
29 // since element with key "Joe" is not present, this will return
30 // the default value, that is an empty string in the case of wxString
31 MyClass tmp2 = h2
["Joe"
];
33 // iterate over all the elements in the class
35 for( it = h2.begin(); it != h2.end(); ++it )
37 wxString key = it->first, value = it->second;
38 // do something useful with key and value
42 \wxheading{Declaring new hash table types
}
45 WX_DECLARE_STRING_HASH_MAP( VALUE_T, // type of the values
46 CLASSNAME ); // name of the class
49 Declares an hash map class named CLASSNAME, with
{\tt wxString
} keys
53 WX_DECLARE_VOIDPTR_HASH_MAP( VALUE_T, // type of the values
54 CLASSNAME ); // name of the class
57 Declares an hash map class named CLASSNAME, with
{\tt void*
} keys
61 WX_DECLARE_HASH_MAP( KEY_T, // type of the keys
62 VALUE_T, // type of the values
64 KEY_EQ_T, // key equality predicate
65 CLASSNAME); // name of the class
68 The HASH
\_T and KEY
\_EQ\_T are the types
69 used for the hashing function and key comparison. wxWindows provides
70 three predefined hashing functions:
{\tt wxIntegerHash
}
71 for integer types (
{\tt int
},
{\tt long
},
{\tt short
},
72 and their unsigned counterparts ),
{\tt wxStringHash
} for strings
73 (
{\tt wxString
},
{\tt wxChar*
},
{\tt char*
} ), and
74 {\tt wxPointerHash
} for any kind of pointer.
75 Similarly three equality predicates:
76 {\tt wxIntegerEqual
},
{\tt wxStringEqual
},
{\tt wxPointerEqual
} are provided.
78 Using this you could declare an hash map mapping
{\tt int
} values
79 to
{\tt wxString
} like this:
82 WX_DECLARE_HASH_MAP( int,
89 \latexignore{\rtfignore{\wxheading{Types
}}}
91 In the documentation below you should replace wxHashMap with the name
92 you used in the class declaration.
95 \twocolitem{wxHashMap::key
\_type}{Type of the hash keys
}
96 \twocolitem{wxHashMap::mapped
\_type}{Type of the values stored in the hash map
}
97 \twocolitem{wxHashMap::value
\_type}{Equivalent to
98 {\tt struct \
{ key
\_type first; mapped
\_type second \
};
} }
99 \twocolitem{wxHashMap::iterator
}{Used to enumerate all the elements in an hash
100 map; it is similar to a
{\tt value
\_type*
}}
101 \twocolitem{wxHashMap::const
\_iterator}{Used to enumerate all the elements
102 in a constant hash map; it is similar to a
{\tt const value
\_type*
}}
103 \twocolitem{wxHashMap::size
\_type}{Used for sizes
}
106 \wxheading{Iterators
}
108 An iterator is similar to a pointer, and so you can use the usual pointer
109 operations:
{\tt ++it
} ( and
{\tt it++
} ) to move to the next element,
110 {\tt *it
} to access the element pointed to,
{\tt it->first
}
111 (
{\tt it->second
} ) to access the key ( value )
112 of the element pointed to. Hash maps provide forward only iterators, this
113 means that you can't use
{\tt --it
},
{\tt it +
3},
{\tt it1 - it2
}.
115 \wxheading{Include files
}
119 \latexignore{\rtfignore{\wxheading{Members
}}}
121 \membersection{wxHashMap::wxHashMap
}
123 \func{}{wxHashMap
}{\param{size
\_type}{ size =
10}}
125 The size parameter is just an hint, the table will resize automatically
126 to preserve performance.
128 \func{}{wxHashMap
}{\param{const wxHashMap\&
}{ map
}}
132 \membersection{wxHashMap::begin
}
134 \constfunc{const
\_iterator}{begin
}{}
136 \func{iterator
}{begin
}{}
138 Returns an iterator pointing at the first element of the hash map.
139 Please remember that hash maps do not guarantee ordering.
141 \membersection{wxHashMap::clear
}
145 Removes all elements from the hash map.
147 \membersection{wxHashMap::count
}
149 \constfunc{size
\_type}{count
}{\param{const key
\_type\&
}{ key
}}
151 Counts the number of elements with the given key present in the map.
152 This function can actually return
0 or
1.
154 \membersection{wxHashMap::empty
}
156 \constfunc{bool
}{empty
}{}
158 Returns true if the hash map does not contain any element, false otherwise.
160 \membersection{wxHashMap::end
}
162 \constfunc{const
\_iterator}{end
}{}
164 \func{iterator
}{end
}{}
166 Returns an iterator pointing at the one-after-the-last element of the hash map.
167 Please remember that hash maps do not guarantee ordering.
169 \membersection{wxHashMap::erase
}
171 \func{size
\_type}{erase
}{\param{const key
\_type\&
}{ key
}}
173 Erases the element with the given key, and returns the number of element
174 erased (either
0 or
1).
176 \func{void
}{erase
}{\param{iterator
}{ it
}}
178 \func{void
}{erase
}{\param{const
\_iterator}{ it
}}
180 Erases the element pointed to by the iterator. After the deletion
181 the iterator is no longer valid and must not be used.
183 \membersection{wxHashMap::find
}
185 \func{iterator
}{find
}{\param{const key
\_type\&
}{ key
}}
187 \constfunc{const
\_iterator}{find
}{\param{const key
\_type\&
}{ key
}}
189 If an element with the given key is present, the functions returns
190 an iterator pointing at that element, otherwise an invalid iterator
191 is returned (i.e. hashmap.find( non
\_existent\_key ) == hashmap.end()).
193 \membersection{wxHashMap::insert
}
195 \func{void
}{insert
}{\param{const value
\_type\&
}{ v
}}
197 Inserts the given value in the hash map.
199 \membersection{wxHashMap::operator
[]}
201 \func{mapped
\_type\&
}{operator
[]}{\param{const key
\_type\&
}{ key
}}
203 Use it as an array subscript. The only difference is that if the
204 given key is not present in the hash map, an element with the
205 default
{\tt value
\_type()
} is inserted in the table.
207 \membersection{wxHashMap::size
}
209 \constfunc{size
\_type}{size
}{}
211 Returns the numbers of elements in the map.