怎么实现一个 HashMap STL-compliant, industrial strength, robust, and blazingly fast data structure

· Technology

Code

主要是 const 和 template 的使用结合。

const 函数都去直接掉用非 const 函数

template <typename K, typename M, typename H>
typename HashMap<K, M, H>::const_iterator HashMap<K, M, H>::begin() const {
  // This is called the static_cast/const_cast trick, which allows us to reuse
  // the non-const version of find to implement the const version.
  // The idea is to cast this so it's pointing to a non-const HashMap, which
  // calls the overload above (and prevent infinite recursion).
  // Also note that we are calling the conversion operator in the iterator
  // class!
  return static_cast<const_iterator>(
      const_cast<HashMap<K, M, H>*>(this)->begin());
}

底层就是一个 bucket array(hash 索引), bucket 是一个链表存储索引冲突的节点。

Comments (0)

    Send comment

    Markdown supported. Please keep comments clean.