STL source code analysis-stl_hash_set.h

Source: Internet
Author: User
// Filename: stl_hash_set.h // comment by: Cream // E-mail: mdl2009@vip.qq.com // blog: http://blog.csdn.net/mdl13412// hash_set and hash_multiset are simple packages for hashtable, easy to understand/** copyright (c) 1996 * Silicon Graphics Computer Systems, Inc. ** permission to use, copy, modify, distribute and publish this software * and its documentation for any purpose is hereby granted without handle, * provided that the above copyrigh T notice appear in all copies and * that both that copyright notice and this permission notice appear * 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 SE Ll 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 expre SS or implied warranty. ** // * Note: This is an internal header file, encoded ded by other STL headers. * You shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_hash_set_h # DEFINE _ sgi_stl_internal_hash_set_h1_stl_begin_namespace # If defined (_ SGI )&&! Defined (_ gnuc _) & (_ mips_sim! = _ Mips_sim_abi32) # pragma set woff 1174 # endif // If the compiler cannot deduce the default parameter type used later based on the preceding template parameters, you must manually specify it, for basic data types, the hash function # ifndef _ stl_limited_default_templatestemplate <class value, class hashfcn = hash <value> is provided in <stl_hash_fun.h>, class primary key = pai_to <value>, class alloc = alloc> # elsetemplate <class value, class hashfcn, class primary key, class alloc = alloc> # endifclass hash_set {PRIVATE: // identity <Value> is used to analyze the value typedef hashtable <value, value, hashfcn, identity <value>, primary key, alloc> HT; HT rep; // In fact, hash_set is a simple encapsulation of hashtable public: typedef typename ht: key_type; typedef typename ht: value_type; typedef typename ht: hasher; typedef typename ht: key_equal; // Note: reference, pointer, iterator is const, because hashtable // internal elements cannot be modified; otherwise, hashtable will become invalid typedef typ Ename ht: size_type; typedef typename ht; typedef typename ht: const_reference; typedef typename ht: const_iterator iterator; typedef typename ht: const_iterator; // return the hasher Function Hash_funct () const {return rep. hash_funct ();} key_equal key_eq () const {return rep. key_eq ();} public: hash_set (): rep (100, hasher (), key_equal () {} explicit hash_set (size_type N): rep (n, hasher (), key_equal () {} hash_set (size_type N, const hasher & HF): rep (n, Hf, key_equal () {} hash_set (size_type N, const hasher & HF, const key_equal & eql): rep (n, Hf, eql) {}# ifdef _ stl_member_templates Template <class inputiterator> hash_set (inputiterator F, inputiterator L): rep (100, hasher (), key_equal () {rep. insert_unique (F, L);} template <class inputiterator> hash_set (inputiterator F, inputiterator L, size_type N): rep (n, hasher (), key_equal () {rep. insert_unique (F, L);} template <class inputiterator> hash_set (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_set (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_set (const value_type * F, const value_type * l): rep (100, hasher (), key_equal () {rep. insert_unique (F, L);} hash_set (const value_type * F, const value_type * l, size_type n ): Rep (n, hasher (), key_equal () {rep. insert_unique (F, L);} hash_set (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_set (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_set (const_iterator F, const_iterator L): Re P (100, hasher (), key_equal () {rep. insert_unique (F, L);} hash_set (const_iterator F, const_iterator L, size_type N): rep (n, hasher (), key_equal () {rep. insert_unique (F, L);} hash_set (const_iterator F, const_iterator L, size_type N, const hasher & HF): rep (n, Hf, key_equal () {rep. insert_unique (F, L);} hash_set (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: // The following is a simple encapsulation of hashtable. See <stl_hashtable.h> 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_set & HS) {rep. swap (HS. rep);} friend bool operator ==_ _ stl_null_tmpl_args (const hash_set &, const hash _ Set &); iterator begin () const {return rep. begin ();} iterator end () const {return rep. end () ;}public: pair <iterator, bool> insert (const value_type & OBJ) {pair <typename ht: iterator, bool> P = rep. insert_unique (OBJ); R eturn pair <iterator, bool> (P. first, P. second) ;}# 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_iterator L) {rep. insert_unique (F, L) ;}# endif/* _ stl_member_templates * // Like hash_set, keys are not allowed to repeat pair <iterator, bool> insert_noresize (const value_type & OBJ) {pair <typename ht: iterator, bool> P = rep. insert_unique_noresize (OBJ); Return pair <iterator, bool> (P. first, P. second);} iterator find (const key_type & 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) 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 value, class hashfcn, class primary key, class alloc> inline bool operator = (Co NST hash_set <value, hashfcn, primary key, alloc> & HS1, const hash_set <value, hashfcn, primary key, alloc> & hs2) {return hs1.rep = hs2.rep ;} // If the compiler supports the special priority of template functions, implement global swap as a private swap using hash_set to improve efficiency # ifdef _ stl_function_tmpl_partial_ordertemplate <class Val, class hashfcn, class primary key, class alloc> inline void swap (hash_set <Val, hashfcn, primary key, alloc> & HS1, hash_set <Val, hashfcn, primary key, alloc> & H S2) {hs1.swap (hs2) ;}# endif/* _ stl_function_tmpl_partial_order * // hash_multiset and hash_set allow duplicate keys, other properties are consistent # ifndef _ stl_limited_default_templatestemplate <class value, class hashfcn = hash <value>, class primary key = required _to <value>, class alloc = alloc> # elsetemplate <class value, class hashfcn, class primary key, class alloc = alloc> # endifclass hash_multiset {PRIVATE: typedef hashtable <value, value, Hashfcn, identity <value>, primary key, alloc> HT; HT rep; public: typedef typename ht: key_type; typedef typename ht: value_type; typedef typename ht :: hasher; typedef typename ht: key_equal; typedef typename ht: size_type; typedef typename ht: difference_type; typedef typename ht: const_pointer pointer; typedef typename :: const_pointer C Onst_pointer; typedef typename ht: const_reference reference; typedef typename ht: const_reference; typedef typename ht: const_iterator 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_multiset (): rep (100, hasher (), key_equal () {} explicit hash_multise T (size_type N): rep (n, hasher (), key_equal () {} hash_multiset (size_type N, const hasher & HF): rep (n, Hf, key_equal () {} hash_multiset (size_type N, const hasher & HF, const key_equal & eql): rep (n, Hf, eql) {}# ifdef _ stl_member_templates template <class inputiterator> hash_multiset (inputiterator F, inputiterator L): rep (100, hasher (), key_equal () {rep. insert_equal (F, L);} template <class in Putiterator> hash_multiset (inputiterator F, inputiterator L, size_type N): rep (n, hasher (), key_equal () {rep. insert_equal (F, L);} template <class inputiterator> hash_multiset (inputiterator F, inputiterator L, size_type N, const hasher & HF): rep (n, Hf, key_equal ()) {rep. insert_equal (F, L);} template <class inputiterator> hash_multiset (inputiterator F, inputiterator L, size_type N, const hash Er & HF, const key_equal & eql): rep (n, Hf, eql) {rep. insert_equal (F, L) ;}# else hash_multiset (const value_type * F, const value_type * l): rep (100, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multiset (const value_type * F, const value_type * l, size_type N): rep (n, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multiset (const value_type * F, const value_type * l, size_type N, con St hasher & HF): rep (n, Hf, key_equal () {rep. insert_equal (F, L);} hash_multiset (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_multiset (const_iterator F, const_iterator L): rep (100, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multiset (const_iterator F, const_iterator L, size_type N ): Rep (n, hasher (), key_equal () {rep. insert_equal (F, L);} hash_multiset (const_iterator F, const_iterator L, size_type N, const hasher & HF): rep (n, Hf, key_equal () {rep. insert_equal (F, L);} hash_multiset (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_multiset & HS) {rep. swap (HS. rep);} friend bool operator ==_ _ stl_null_tmpl_args (const hash_multiset &, const hash_multiset &); iterator begin () const {return rep. begin ();} iterator end () const {return rep. end ();} public: iterator insert (const value_type & O BJ) {return rep. insert_equal (OBJ) ;}# ifdef _ stl_member_templates template <class inputiterator> void insert (inputiterator F, inputiterator L) {rep. insert_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 va Lue_type & OBJ) {return rep. insert_1__noresize (OBJ);} iterator find (const key_type & 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) const {return rep. inclu_range (key);} size_type erase (const key_type & Key) {return rep. erase (key);} void erase (iterator it) {rep. erase (it);} Vo Id 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 Val, class hashfcn, class equa Lkey, class alloc> inline bool operator = (const hash_multiset <Val, hashfcn, primary key, alloc> & HS1, const hash_multiset <Val, hashfcn, primary key, alloc> & hs2) {return hs1.rep = hs2.rep ;} // If the compiler supports the special priority of template functions // implement global swap as a private swap using hash_multiset to improve efficiency # ifdef _ stl_function_tmpl_partial_ordertemplate <class Val, class hashfcn, class primary key, class alloc> inline void swap (hash_multiset <Val, hashfcn, equalk Ey, alloc> & HS1, hash_multiset <Val, hashfcn, primary key, alloc> & hs2) {hs1.swap (hs2 );} # 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_set_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.