Webkit HashIterator

本文详细介绍了哈希表迭代器在访问哈希映射时的使用方法,包括整表遍历、键遍历和值遍历,并解释了不同迭代器之间的关系与实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 {
.....
}





                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值