Professional C__ - Marc Gregoire [308]
}
protected:
Compare comp;
value_compare(Compare c) : comp(c) {}
};
// The iterator class needs access to protected members of hashmap
friend class HashIterator // Constructors explicit hashmap(const Compare& comp = Compare(), const Hash& hash = Hash()) throw(invalid_argument); template hashmap(InputIterator first, InputIterator last, const Compare& comp = Compare(), const Hash& hash = Hash()) throw(invalid_argument); // destructor, copy constructor, move constructor, // copy assignment operator and move assignment operator ~hashmap(); hashmap(const hashmap hashmap(hashmap hashmap const hashmap hashmap hashmap // C++11 initializer list constructor hashmap(initializer_list const Compare& comp = Compare(), const Hash& hash = Hash()) throw(invalid_argument); // C++11 initializer list assignment operator hashmap initializer_list // Iterator methods iterator begin(); iterator end(); const_iterator begin() const; const_iterator end() const; const_iterator cbegin() const; // For C++11 const_iterator cend() const; // For C++11 // Size methods bool empty() const; size_type size() const; size_type max_size() const; // Element insert methods T& operator[] (const key_type& x); pair iterator insert(iterator position, const value_type& x); template void insert(InputIterator first, InputIterator last); void insert(initializer_list // C++11 emplace methods pair iterator emplace_hint(iterator hint, value_type&& x); // Element delete methods void erase(iterator position); size_type erase(const key_type& x); void erase(iterator first, iterator last); // Other modifying utilities void swap(hashmap void clear(); // Access methods for STL conformity key_compare key_comp() const; value_compare value_comp() const; // Lookup methods iterator find(const key_type& x); const_iterator find(const key_type& x) const; size_type count(const key_type& x) const; protected: typedef list typename ListType::iterator findElement( const key_type& x, size_t& bucket) const; vector size_type mSize; Compare mComp; Hash mHash; }; Code snippet from Hashmap\FinalHashmap\hashmap.h hashmap Constructors The implementation of the default constructor is shown earlier. The second constructor is a method template so that it can take an iterator range from any container, not just other hashmaps. If it were not a method template, it would need to specify the InputIterator type explicitly as HashIterator, limiting it to iterators from hashmaps. Despite the syntax, the implementation is uncomplicated: It initializes all the data members, then calls insert() to actually insert all the elements in the specified range: // Make a call to insert() to actually insert the elements. template template hashmap InputIterator first, InputIterator last, const Compare& comp, const Hash& hash) throw(invalid_argument) : mSize(0), mComp(comp), mHash(hash) { if (mHash.numBuckets() <= 0) { throw invalid_argument("Number of buckets must be positive"); } mElems = new vector insert(first, last); } Code snippet from Hashmap\FinalHashmap\hashmap.cpp hashmap Initializer List Constructor C++11 supports initializer lists, which are discussed in Chapter 9. Following is the implementation of the hashmap constructor that takes an initializer list, which is very similar to the implementation of the constructor accepting an iterator range: template