Webkit HashIterator

来源:互联网 发布:unity3d 选中物体 编辑:程序博客网 时间:2024/05/29 02:13

HashIterator is a template class (it use the hashtable's parameters to initialize it) for visit hash table.


HashTableIteratorAdapter is a template class, for visit hash map (it will use Hasgiterator)

Contructor: HashTableIteratorAdapter<HashTableType, std::pair<KeyType, MappedType> >;

Member:HashTableType::const_iterator m_impl, which is the type of  hashmap.


iteration methods

there are three way traversaling the hash map:

a. traversal the whole map , which get a value like pair<KeyType, MappedType> at each time;

b. traversal the <KeyType>key field  of the map, which just get the "key" at each time;

c. traversal the <MappedType>value field of the map, which get the "value" at each time.


So we need three type of iterators, which are HashTableIteratorAdapter , HashTableKeysIterator  and HashTableValuesIterator.

Each iterator has four common methods, which are:

1.const ValueType* get(), return the current item value of the iterator (in point form).

HashTableIteratorAdapter , HashTableKeysIterator  and HashTableValuesIterator corresponding a/b/c above;

2.const ValueType& operator*()

get the value of the current value.

3.const ValueType* operator->()

same as get

4.HashTableConstIteratorAdapter& operator++()

move selft to next item.


for HashTableIteratorAdapter, there need another funcion for it.

Since the get method of it just return the type of pair<KeyType, MappedType> .

so we need ways to get the "KeyType key"and "MappedType value" separately which are

Keys keys() , HashTableConstKeysIterator<HashTableType, KeyType, MappedType>

Values values(),  HashTableConstValuesIterator<HashTableType, KeyType, MappedType> 

However I havn't tell the difference now. (I think i can understand now, but I can't understand what i didn't understand the first time i wrote it. strange and intrestring....)

Because the Key iterator , Value iterator and the pair iterator all has the same template, which are:

HashTableValuesIterator<HashTableType, KeyType, MappedType>



HashMap::iterator

a. type definition

        typedef HashTableIteratorAdapter<HashTableType, ValueType> iterator;

it was defined in "HashIterators.h"

b. HashTable's definition

        typedef HashTable<KeyType, ValueType, PairFirstExtractor<ValueType>,            HashFunctions, ValueTraits, KeyTraits> HashTableType;
c.members

    private:        typedef std::pair<KeyType, MappedType> ValueType;    public:        typedef HashTableKeysIterator<HashTableType, KeyType, MappedType> Keys;        typedef HashTableValuesIterator<HashTableType, KeyType, MappedType> Values;
d.operator

        ValueType* get() const { return (ValueType*)m_impl.get(); }        ValueType& operator*() const { return *get(); }        ValueType* operator->() const { return get(); }        HashTableIteratorAdapter& operator++() { ++m_impl; return *this; }

e.useage

    HashMap::const_iterator end = map.end();    for (HashMap::const_iterator i = map.begin(); i != end; ++i) {        jstring key = wtfStringToJstring(env, i->first);        jstring val = wtfStringToJstring(env, i->second);        ....    }

one thing used to bother me for quite a long time, how is "i->first" come out? because it doesn't has the member first  or second.

Now i understand

"i->first" is "const_iterator->first", that is "(HashTableIteratorAdapter<HashTableType, ValueType>)->first",

and in ("HashTableIteratorAdapter<HashTableType, ValueType>")'s operator "->", return get().

and get() return the "(ValueType*)m_impl" ,

that is "typename HashTableType::const_iterator m_impl;"

in HashMap, it is "pair<KeyType, MappedType> ValueType"

so, i->first, will return "pair<KeyType, MappedType> ValueType"'s first,

and first , second is the member of pair

template <class _T1, class _T2>struct pair {  typedef _T1 first_type;  typedef _T2 second_type;  _T1 first;  _T2 second;.....}

so "first" return the Key and "second" return the value.




also something about .HashMap

it was defined in “webkit\JavaScriptCore\wtf\HashMap.h”

    template<typename KeyArg, typename MappedArg,
 typename HashArg = typename DefaultHash<KeyArg>::Hash,        typename KeyTraitsArg = HashTraits<KeyArg>,
 typename MappedTraitsArg = HashTraits<MappedArg> >    class HashMap : public FastAllocBase {.....}

	
				
		
原创粉丝点击