Utility Libraries for BREW - A Hash Table Class
This article is the last one in a series that presents possible implementations of utility libraries in BREW. A hash table is introduced together with a XML parser.
A (Hash) Table...
Hash tables are unfortunately not part of the STL, even if unofficially several implementations are available . They are very popular constructs in Java 1.1 (as well as J2ME) so that having a C++ equivalent implementation allows smooth code porting from one language to the other. Our implementation is very simple and offers no surprises:
template<class K, class V > class BrewHashtable ; BrewHashtable() ; BrewHashtable(int hashSize); BrewHashtable(const BrewHashtable<K, V>&); void put(const K& k, const V& d) ; int get(const K& k, V& d); //returns 0 for success, 1 otherwise template<class T > void keys(T& bk); template<class T> void elements(T& bk); int remove(const K& key); void remove_all(); int size() const; bool isEmpty() const;
The only thing that might rise an eyebrow is how the return value is passed by refrence in get(), keys(), and elements() — a marginally better approach in terms of efficiency. Using BrewHashtable is straightforward:
BrewHashtable<int, int> hint; hint.put(10, 20); int res; hint.get(10, res); BrewHashtable< String, int> hstr; hstr.put("1", 220); hstr.put("2", 333); hstr.put("3", 22440); hstr.put("4", 888); hstr.get("2", res);