Online Book Reader

Home Category

Professional C__ - Marc Gregoire [311]

By Root 1540 0
it.

(*mElems)[bucket].erase(it);

mSize--;

return 1;

} else {

return 0;

}

}

Code snippet from Hashmap\FinalHashmap\hashmap.cpp

The second form of erase() must remove the element at a specific iterator position. The iterator given is, of course, a HashIterator. Thus, the hashmap must have some ability to obtain the underlying bucket and list iterator from the HashIterator. The approach we take is to make the hashmap class a friend of the HashIterator (not shown in the preceding class definition).

template

void hashmap::erase(

typename hashmap::iterator position)

{

// Erase the element from its bucket.

(*mElems)[position.mBucket].erase(position.mIt);

mSize--;

}

Code snippet from Hashmap\FinalHashmap\hashmap.cpp

The final version of erase() removes a range of elements. It iterates from first to last, calling erase() on each element, thus letting the previous version of erase() do all the work:

template

void hashmap::erase(

typename hashmap::iterator first,

typename hashmap::iterator last)

{

typename hashmap::iterator cur, next;

// Erase all the elements in the range.

for (next = first; next != last; ) {

cur = next++;

erase(cur);

}

}

Code snippet from Hashmap\FinalHashmap\hashmap.cpp

hashmap Clear Operation

The clear() method uses the for_each() algorithm to call clear() on the list representing each bucket:

template

void hashmap::clear()

{

// Call clear on each list.

for_each(mElems->begin(), mElems->end(),

[](ListType& e){e.clear();});

mSize = 0;

}

Code snippet from Hashmap\FinalHashmap\hashmap.cpp

hashmap Accessor Operations

The standard requires access methods for the key comparison and value comparison objects. These methods must be called key_comp() and value_comp():

template

typename hashmap::key_compare

hashmap::key_comp() const

{

return mComp;

}

template

typename hashmap::value_compare

hashmap::value_comp() const

{

return value_compare(mComp);

}

Code snippet from Hashmap\FinalHashmap\hashmap.cpp

The find() method is identical to the version shown earlier for the basic hashmap, except for the return code. Instead of returning a pointer to the element, it constructs a HashIterator referring to it:

template

typename hashmap::iterator

hashmap::find(const key_type& x)

{

size_t bucket;

// Use the findElement() helper.

auto it = findElement(x, bucket);

if (it == (*mElems)[bucket].end()) {

// We didn't find the element--return the end iterator.

return end();

}

// We found the element--convert the bucket/iterator to a HashIterator.

return HashIterator(bucket, it, this);

}

Code snippet from Hashmap\FinalHashmap\hashmap.cpp

The const version of find() is identical, so its implementation is not shown here.

The implementation of count() is a wrapper for find(), returning 1 if it finds the element, and 0 if it doesn’t. The find() method returns the end iterator if it can’t find the element. count() retrieves an end iterator by calling end() in order to compare it. Since this hashmap does not allow duplicate keys, count() always returns either 0 or 1:

template

typename hashmap::size_type

hashmap::count(const key_type& x) const

{

// There are either 1 or 0 elements matching key x.

// If we can find a match, return 1, otherwise return 0.

if (find(x) == end()) {

return

Return Main Page Previous Page Next Page

®Online Book Reader