Changeset 25365 in webkit for trunk/JavaScriptCore/wtf/HashMap.h


Ignore:
Timestamp:
Sep 4, 2007, 10:01:03 PM (18 years ago)
Author:
mjs
Message:

Reviewed by Darin.


  • Added Vector::appendRange(), which appends to a vector based on a given start and end iterator
  • Added keys() and values() functions to HashMap iterators, which give keys-only and values-only iterators


Together, these allow easy copying of a set, or the keys or values of a map, into a Vector. Examples:


HashMap<int, int> map;
HashSet<int> set;
Vector<int> vec;
...
vec.appendRange(set.begin(), set.end());
vec.appendRange(map.begin().keys(), map.end().keys());
vec.appendRange(map.begin().values(), map.end().values());

This also allows for a slightly nicer syntax when iterating a map. Instead of saying
(*it)->first, you can say *it.values(). Similarly for keys. Example:


HashMap<int, int>::const_iterator end = map.end();
for (HashMap<int, int>::const_iterator it = map.begin(); it != end; ++it)
printf(" [%d => %d]", *it.keys(), *it.values());

  • JavaScriptCore.xcodeproj/project.pbxproj:
  • wtf/HashIterators.h: Added. (WTF::): (WTF::HashTableConstKeysIterator::HashTableConstKeysIterator): (WTF::HashTableConstKeysIterator::get): (WTF::HashTableConstKeysIterator::operator*): (WTF::HashTableConstKeysIterator::operator->): (WTF::HashTableConstKeysIterator::operator++): (WTF::HashTableConstValuesIterator::HashTableConstValuesIterator): (WTF::HashTableConstValuesIterator::get): (WTF::HashTableConstValuesIterator::operator*): (WTF::HashTableConstValuesIterator::operator->): (WTF::HashTableConstValuesIterator::operator++): (WTF::HashTableKeysIterator::HashTableKeysIterator): (WTF::HashTableKeysIterator::get): (WTF::HashTableKeysIterator::operator*): (WTF::HashTableKeysIterator::operator->): (WTF::HashTableKeysIterator::operator++): (WTF::HashTableKeysIterator::operator HashTableConstKeysIterator<HashTableType, KeyType, MappedType>): (WTF::HashTableValuesIterator::HashTableValuesIterator): (WTF::HashTableValuesIterator::get): (WTF::HashTableValuesIterator::operator*): (WTF::HashTableValuesIterator::operator->): (WTF::HashTableValuesIterator::operator++): (WTF::HashTableValuesIterator::operator HashTableConstValuesIterator<HashTableType, KeyType, MappedType>): (WTF::operator==): (WTF::operator!=):
  • wtf/HashTable.h:
  • wtf/Vector.h: (WTF::::appendRange):
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/HashMap.h

    r24059 r25365  
    2626
    2727#include "HashTable.h"
     28#include "Vector.h"
    2829
    2930namespace WTF {
     
    376377        deleteAllPairFirsts<typename HashMap<T, U, V, W, X>::KeyType>(collection);
    377378    }
     379   
     380    template<typename T, typename U, typename V, typename W, typename X>
     381    inline void copyValuesToVector(const HashMap<T, U, V, W, X>& collection, Vector<U>& vector)
     382    {
     383        typedef typename HashMap<T, U, V, W, X>::const_iterator iterator;
     384       
     385        vector.resize(collection.size());
     386       
     387        iterator it = collection.begin();
     388        iterator end = collection.end();
     389        for (unsigned i = 0; it != end; ++it, ++i)
     390            vector[i] = (*it).second;
     391    }   
    378392
    379393} // namespace WTF
Note: See TracChangeset for help on using the changeset viewer.