STL source code analysis --- stl_hash_map.h Reading Notes

Source: Internet
Author: User
The bottom layer of map in sgi stl is implemented by the red and black trees, and hash_map is implemented by the hash table.

Hash_map does not allow insertion of duplicate key values. hash_multimap allows insertion of duplicate key values. The relationship between the two is like the relationship between map and multimap. The underlying hash table provides most of the operations. Most hash_map (hash_multimap) directly calls the hash table function.

G ++ 2.91.57, Cygnus \ cygwin-b20 \ include \ G ++ \ stl_hash_map.h complete list/** copyright (c) 1996 * Silicon Graphics Computer Systems, Inc. ** permission to use, copy, modify, distribute and merge this software * and its documentation for any purpose is hereby granted without tables, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice app Ear * in supporting documentation. silicon Graphics makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. * ** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and submit this software * and its documentation for any purpose is hereby granted without handle, * provided That the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. hewlett-Packard Company makes no * representations about the suitability of this software for any * purpose. it is provided "as is" without express or implied warranty. ** // * Note: This is an internal header file, encoded ded by other STL Header S. * You shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_hash_map_h # DEFINE _ EXIST # If defined (_ SGI )&&! Defined (_ gnuc _) & (_ mips_sim! = _ Mips_sim_abi32) # pragma set woff 1174 # endif # ifndef _ stl_limited_default_templates // hash is a function object, which is defined in <stl_hash_fun.h>. // example: hash <int >:: operator () (int x) const {return X;} template <class key, class T, class hashfcn = hash <key>, class primary key = pai_to <key>, class alloc = alloc> # elsetemplate <class key, class T, class hashfcn, class primary key, class alloc = alloc> # endifclass hash_m AP {PRIVATE: // The select1st used below is defined in <stl_function.h>. Typedef hashtable <pair <const key, T>, key, hashfcn, select1st <pair <const key, T>, primary key, alloc> HT; HT rep; // complete public: typedef typename ht: key_type; typedef t data_type; typedef t mapped_type; typedef typename ht: value_type; typedef typename ht :: hasher; typedef typename ht: key_equal; typedef typename ht: size_type; typedef type Name ht: pointer; typedef typename ht: const_pointer; typedef typename ht: Reference reference; typedef typename ht: const_reference; typedef typename ht: iterator; typedef typename ht: const_iterator; hasher hash_funct () const {return rep. hash_funct ();} key_equal key_eq () const {Return rep. key_eq ();} public: // by default, 100 vector buckets is used to adjust the hash table to a prime number greater than 100 hash_map (): rep (100, hasher (), key_equal () {} explicit hash_map (size_type N): rep (n, hasher (), key_equal () {} hash_map (size_type N, const hasher & HF ): rep (n, Hf, key_equal () {} hash_map (size_type N, const hasher & HF, const key_equal & eql): rep (n, Hf, eql) {}# ifdef _ stl_member_templates // below, insert all the data using insert_uniq UE (). duplicate key values are not allowed. Template <class inputiterator> hash_map (inputiterator F, inputiterator L): rep (100, hasher (), key_equal () {rep. insert_unique (F, L);} template <class inputiterator> hash_map (inputiterator F, inputiterator L, size_type N): rep (n, hasher (), key_equal () {rep. insert_unique (F, L);} template <class inputiterator> hash_map (inputiterator F, inputiterator L, size_type N, const hasher & HF): rep (n, h F, key_equal () {rep. insert_unique (F, L);} template <class inputiterator> hash_map (inputiterator F, inputiterator L, size_type N, const hasher & HF, const key_equal & eql): rep (n, HF, eql) {rep. insert_unique (F, L) ;}# else hash_map (const value_type * F, const value_type * l): rep (100, hasher (), key_equal () {rep. insert_unique (F, L);} hash_map (const value_type * F, const value_type * l, size_type n ): Rep (n, hasher (), key_equal () {rep. insert_unique (F, L);} hash_map (const value_type * F, const value_type * l, size_type N, const hasher & HF): rep (n, Hf, key_equal ()) {rep. insert_unique (F, L);} hash_map (const value_type * F, const value_type * l, size_type N, const hasher & HF, const key_equal & eql): rep (n, Hf, eql) {rep. insert_unique (F, L);} hash_map (const_iterator F, const_iterator L): Re P (100, hasher (), key_equal () {rep. insert_unique (F, L);} hash_map (const_iterator F, const_iterator L, size_type N): rep (n, hasher (), key_equal () {rep. insert_unique (F, L);} hash_map (const_iterator F, const_iterator L, size_type N, const hasher & HF): rep (n, Hf, key_equal () {rep. insert_unique (F, L);} hash_map (const_iterator F, const_iterator L, size_type N, const hasher & HF, const key_equal & Eql): rep (n, Hf, eql) {rep. insert_unique (F, L) ;}# endif/* _ stl_member_templates */public: // almost all operations have versions corresponding to the hash table, which can be called directly. Size_type size () const {return rep. size ();} size_type max_size () const {return rep. max_size ();} bool empty () const {return rep. empty ();} void swap (hash_map & HS) {rep. swap (HS. rep);} friend bool operator ==_ _ stl_null_tmpl_args (const hash_map &, const hash_map &); iterator begin () {return rep. begin ();} iterator end () {return rep. end ();} const_iterator begin () const {return rep. begin ();} Const_iterator end () const {return rep. end ();} public: pair <iterator, bool> insert (const value_type & OBJ) {return rep. insert_unique (OBJ);} # ifdef _ stl_member_templates template <class inputiterator> void insert (inputiterator F, inputiterator L) {rep. insert_unique (F, L);} # else void insert (const value_type * F, const value_type * l) {rep. insert_unique (F, L);} void insert (const_iterator F, const_ I Terator L) {rep. insert_unique (F, L);} # endif/* _ stl_member_templates */pair <iterator, bool> insert_noresize (const value_type & OBJ) {return rep. insert_unique_noresize (OBJ);} iterator find (const key_type & Key) {return rep. find (key);} const_iterator find (const key_type & Key) const {return rep. find (key) ;}t & operator [] (const key_type & Key) {return rep. find_or_insert (value_type (Key, T ())). sec OND;} size_type count (const key_type & Key) const {return rep. count (key) ;}pair <iterator, iterator> pai_range (const key_type & Key) {return rep. pai_range (key);} pair <const_iterator, const_iterator> pai_range (const key_type & Key) const {return rep. inclu_range (key);} size_type erase (const key_type & Key) {return rep. erase (key);} void erase (iterator it) {rep. erase (it);} void erase (iterat Or F, iterator L) {rep. erase (F, L);} void clear () {rep. clear ();} public: void resize (size_type hint) {rep. resize (hint);} size_type bucket_count () const {return rep. bucket_count ();} size_type max_bucket_count () const {return rep. max_bucket_count ();} size_type elems_in_bucket (size_type N) const {return rep. elems_in_bucket (n) ;}}; template <class key, class T, class hashfcn, class primary key, Class alloc> inline bool operator = (const hash_map <key, T, hashfcn, primary key, alloc> & hm1, const hash_map <key, T, hashfcn, primary key, alloc> & hm2) {return hm1.rep = hm2.rep;} # ifdef _ stl_function_tmpl_partial_ordertemplate <class key, class T, class hashfcn, class primary key, class alloc> inline void swap (hash_map <key, T, hashfcn, primary key, alloc> & hm1, hash_map <key, T, hashfcn, primary key, alloc> & hm2) {Hm1.swap (hm2) ;}# endif/* _ stl_function_tmpl_partial_order */# ifndef _ stl_limited_default_templatestemplate <class key, class T, class hashfcn = hash <key>, class primary key = pai_to <key>, class alloc = alloc> # elsetemplate <class key, class T, class hashfcn, class primary key, class alloc = alloc> # endif // It is almost the same as hash_map. It only allows the insertion of duplicate key values class hash_multimap {PRIVATE: typedef hashtable <pair <const key, T>, Ke Y, hashfcn, select1st <pair <const key, T>, primary key, alloc> HT; HT rep; public: typedef typename ht: key_type; typedef t data_type; typedef t mapped_type; typedef typename ht: value_type; typedef typename ht: hasher; typedef typename ht: key_equal; typedef typename ht:: difference_type; typedef typename H T: pointer; typedef typename ht: const_pointer; typedef typename ht: Reference reference; typedef typename ht: const_reference; typedef typename ht: iterator; typedef typename ht: const_iterator; hasher hash_funct () const {return rep. hash_funct ();} key_equal key_eq () const {return rep. key_eq ();} public: hash_multimap (): rep (100, has Her (), key_equal () {} explicit hash_multimap (size_type N): rep (n, hasher (), key_equal () {} hash_multimap (size_type N, const hasher & HF): rep (n, Hf, key_equal () {} hash_multimap (size_type N, const hasher & HF, const key_equal & eql): rep (n, Hf, eql) {}# ifdef _ stl_member_templates // and insert all the following values using insert_equal (). duplicate key values are allowed. Template <class inputiterator> hash_multimap (inputiterator F, inputiterator L): rep (100, hasher (), key_equal () {rep. insert_equal (F, L);} template <class inputiterator> hash_multimap (inputiterator F, inputiterator L, size_type N): rep (n, hasher (), key_equal () {rep. insert_equal (F, L);} template <class inputiterator> hash_multimap (inputiterator F, inputiterator L, size_type N, const hasher & H F): rep (n, Hf, key_equal () {rep. insert_equal (F, L);} template <class inputiterator> hash_multimap (inputiterator F, inputiterator L, size_type N, const hasher & HF, const key_equal & eql): rep (n, HF, eql) {rep. insert_equal (F, L) ;}# else hash_multimap (const value_type * F, const value_type * l): rep (100, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multimap (const value_type * F, const v Alue_type * l, size_type N): rep (n, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multimap (const value_type * F, const value_type * l, size_type N, const hasher & HF): rep (n, Hf, key_equal ()) {rep. insert_equal (F, L);} hash_multimap (const value_type * F, const value_type * l, size_type N, const hasher & HF, const key_equal & eql): rep (n, Hf, eql) {rep. insert_equal (F, L);} hash_multimap (con St_iterator F, const_iterator L): rep (100, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multimap (const_iterator F, const_iterator L, size_type N): rep (n, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multimap (const_iterator F, const_iterator L, size_type N, const hasher & HF): rep (n, Hf, key_equal () {rep. insert_equal (F, L);} hash_multimap (const_iterator F, const_iterator L, size_type N, const hasher & HF, const key_equal & eql): rep (n, Hf, eql) {rep. insert_equal (F, L) ;}# endif/* _ stl_member_templates */public: size_type size () const {return rep. size ();} size_type max_size () const {return rep. max_size ();} bool empty () const {return rep. empty ();} void swap (hash_multimap & HS) {rep. swap (HS. rep);} friend bool operator = _ stl_null_tmpl_args (const hash_multima P &, const hash_multimap &); iterator begin () {return rep. begin ();} iterator end () {return rep. end ();} const_iterator begin () const {return rep. begin ();} const_iterator end () const {return rep. end ();} public: iterator insert (const value_type & OBJ) {return rep. insert_equal (OBJ) ;}# ifdef _ stl_member_templates template <class inputiterator> void insert (inputiterator F, inputiterator L) {rep. I Nsert_equal (F, L) ;}# else void insert (const value_type * F, const value_type * l) {rep. insert_equal (F, L);} void insert (const_iterator F, const_iterator L) {rep. insert_equal (F, L);} # endif/* _ stl_member_templates */iterator insert_noresize (const value_type & OBJ) {return rep. insert_1__noresize (OBJ);} iterator find (const key_type & Key) {return rep. find (key);} const_iterator find (const key_ty PE & Key) const {return rep. find (key);} size_type count (const key_type & Key) const {return rep. count (key) ;}pair <iterator, iterator> pai_range (const key_type & Key) {return rep. pai_range (key);} pair <const_iterator, const_iterator> pai_range (const key_type & Key) const {return rep. inclu_range (key);} size_type erase (const key_type & Key) {return rep. erase (key);} void erase (iterator it ){ Rep. erase (it);} void erase (iterator F, iterator L) {rep. erase (F, L);} void clear () {rep. clear ();} public: void resize (size_type hint) {rep. resize (hint);} size_type bucket_count () const {return rep. bucket_count ();} size_type max_bucket_count () const {return rep. max_bucket_count ();} size_type elems_in_bucket (size_type N) const {return rep. elems_in_bucket (n) ;}}; template <class key, Class T, class HF, class eqkey, class alloc> inline bool operator = (const hash_multimap <key, T, Hf, eqkey, alloc> & hm1, const hash_multimap <key, T, HF, eqkey, alloc> & hm2) {return hm1.rep = hm2.rep;} # ifdef _ stl_function_tmpl_partial_ordertemplate <class key, class T, class hashfcn, class primary key, class alloc> inline void swap (hash_multimap <key, T, hashfcn, primary key, alloc> & hm1, hash_multimap <key, T, h Ashfcn, primary key, alloc> & hm2) {hm1.swap (hm2) ;}# endif/* _ stl_function_tmpl_partial_order */# If defined (_ SGI )&&! Defined (_ gnuc _) & (_ mips_sim! = _ Mips_sim_abi32) # pragma reset woff 1174 # endif _ stl_end_namespace # endif/* _ sgi_stl_internal_hash_map_h * // local variables: // mode: C ++/end:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.