STL source code analysis-stl_algobase.h

Source: Internet
Author: User
// Filename: stl_algobase.h // comment by: Gel // E-mail: mdl2009@vip.qq.com // blog: http://blog.csdn.net/mdl13412// this file defines some of the most commonly used algorithms, I only provide one idea, // I will not explain it in detail. For more information about specific algorithms, see the algorithm book. I recommend the introduction to algorithms. // In addition, for those with weak foundations, I recommend "big talk Data Structure". I have read this book // a trial. It is suitable for beginners to learn/*** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and perform this software * and its documentation for any purpose is hereby Granted without partition, * 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. * ** 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 appear * in supporting documentation. silicon Graphics makes no * representations about the suitability o F 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 headers. * You shoshould not attempt to use it directly. */# ifndef _ DEFINE # DEFINE _ DEFINE # ifndef _ stl_config_h # include <stl_config.h> # endif # ifndef _ sgi_stl_internal_relops # include <stl_relops.h> # E Ndif # ifndef _ sgi_stl_internal_pair_h # include <stl_pair.h> # endif # ifndef _ type_traits_h _ # include <type_traits.h> # endif # include <string. h> # include <limits. h> # include <stdlib. h> # include <stddef. h> # include <New. h> # include <iostream. h> # ifndef _ pointer # include <stl_iterator.h> # endif _ stl_begin_namespace // Why is the third parameter a pointer? See <stl_iterator.h> template <class forwarditerator1, class forwardi Terator2, class T> inline void _ iter_swap (forwarditerator1 A, forwarditerator2 B, T *) {// The internal object t TMP = * A is actually exchanged here; * A = * B; * B = TMP;} template <class forwarditerator1, class forwarditerator2> inline void iter_swap (forwarditerator1 A, forwarditerator2 B) {// take the first type as the standard _ iter_swap (a, B, value_type (a);} // exchange operation, operator = () template <class T> inline void swap (T & A, T & B) {t tmp = A; A = B; B = TMP ;} # Ifndef _ BorlandC __# UNDEF min # UNDEF Max // Max and min are very simple. Because the returned result is a reference, therefore, you can use the template <class T> inline const T & min (const T & A, const T & B) {return B <? B: A;} template <class T> inline const T & MAX (const T & A, const T & B) {return a <B? B: A ;}# endif/* _ BorlandC _ */template <class T, class compare> inline const T & min (const T & A, const T & B, compare comp) {return comp (B, )? B: A;} template <class T, class compare> inline const T & MAX (const T & A, const T & B, compare comp) {return comp (, B )? B: A;} // This is the case where random access is not supported. template <class inputiterator, class outputiterator> inline outputiterator _ copy (inputiterator first, inputiterator last, outputiterator result, iterator) {// first! = Last leads to comparison of the iterator to be entered, with low efficiency for (; first! = Last; ++ result, ++ first) * result = * First; return result;} template <class randomaccessiterator, class outputiterator, class distance> inline outputiterator _ copy_d (randomaccessiterator first, randomaccessiterator last, outputiterator result, distance *) {// compare between non-row iterators, directly specify the number of cycles, efficient for (distance n = last-first; n> 0; -- N, ++ result, ++ first) * result = * First; return result ;} // This is a template that supports random access. <Class Identifier, class outputiterator> inline outputiterator _ copy (randomaccessiterator first, randomaccessiterator last, outputiterator result, iterator) {return _ copy_d (first, last, result, distance_type (first);} template <class inputiterator, class outputiterator> struct _ copy_dispatch {// here is an imitation function. distribute outputiterator operator () (inputiterator first, inputiterator Last, outputiterator result) {return _ copy (first, last, result, iterator_category (first ));}}; // provide compatibility # ifdef _ stl_class_partial_specialization // it can be moved directly without any additional template <class T> inline T * _ copy_t (const T * first, const T * Last, T * result, _ true_type) {memmove (result, first, sizeof (t) * (last-first )); return result + (last-first);} // some processing is required to ensure the correctness of object replication. template <class T> inline T * _ copy_t (Const T * First, const T * Last, T * result, _ false_type) {return _ copy_d (first, last, result, (ptrdiff_t *) 0 );} // provides a special template <class T> struct _ copy_dispatch <t *, T *> {T * operator () (T * First, T * last, T * result) {// determine whether it has trivial_assignment_operator internally to distribute typedef typename _ type_traits <t >:: has_trivial_assignment_operator t; return _ copy_t (first, last, result, T () ;}}; template <class T> St Ruct _ copy_dispatch <const T *, T *> {T * operator () (const T * First, const T * Last, T * result) {typedef typename _ type_traits <t>: has_trivial_assignment_operator t; return _ copy_t (first, last, result, T ());}}; # endif/* _ iterator * // copy [first, last) to the result. template <class inputiterator, class outputiterator> inline outputiterator copy (inputiterator first, inputiterator la St, outputiterator result) {// return _ copy_dispatch <inputiterator, outputiterator> () (first, last, result );} // special targeting of char strings, efficiency first, design concept of C ++ inline char * Copy (const char * First, const char * Last, char * result) {memmove (result, first, last-first); return result + (last-first) ;}// special targeting wchar_t string, with the highest efficiency, c ++ Design Concept inline wchar_t * Copy (const wchar_t * First, const wchar_t * Last, wchar_t * res Ult) {memmove (result, first, sizeof (wchar_t) * (last-first); return result + (last-first);} template <class bidirectionaliterator1, class bidirectionaliterator2> inline bidirectionaliterator2 _ copy_backward (bidirectionaliterator1 first, bidirealialiterator1 last, bidirealialiterator2 result) {While (first! = Last) * -- result = * -- last; return result;} template <class member, class bidirectionaliterator2> struct _ copy_backward_dispatch {Operator () (bidirectionaliterator1 first, bidirectionaliterator1 last, bidirectionaliterator2 result) {return _ copy_backward (first, last, result) ;};# ifdef _ stl_class_partial_specializationtemplate <class T> inline T * _ copy_backw Ard_t (const T * First, const T * Last, T * result, _ true_type) {const ptrdiff_t n = last-first; memmove (result-N, first, sizeof (t) * n); return result-N;} template <class T> inline T * _ copy_backward_t (const T * First, const T * Last, T * result, _ false_type) {return _ copy_backward (first, last, result);} template <class T> struct _ copy_backward_dispatch <t *, T *> {T * operator () (T * First, T * Last, T * Result) {typedef typename _ type_traits <t >:: has_trivial_assignment_operator t; return _ copy_backward_t (first, last, result, T ());}}; template <class T> struct _ copy_backward_dispatch <const T *, T *> {T * operator () (const T * First, const T * Last, T * result) {typedef typename _ type_traits <t>: has_trivial_assignment_operator t; return _ copy_backward_t (first, last, result, T ());}}; # endif/* _ STL _ Class_partial_specialization * // reverse copies the elements of [first, last) (..., last). The mechanism is very close to that of copy. The template <class bidirectionaliterator1, class bidirectionaliterator2> inline bidirectionaliterator2 copy_backward) {return _ copy_backward_dispatch <bidirectionaliterator1, bidirectionaliterator2> () (first, last, result);} temp Late <class inputiterator, class size, class outputiterator> pair <inputiterator, outputiterator> _ copy_n (inputiterator first, size count, outputiterator result, iterator) {for (; count> 0; -- count, ++ first, ++ result) * result = * First; return pair <inputiterator, outputiterator> (first, result);} template <class randomaccessiterator, class size, class outputiterator> inline pair <randomacces Siterator, outputiterator >__ copy_n (randomaccessiterator first, size count, outputiterator result, iterator) {// use copy () to select the most efficient copy algorithm randomaccessiterator last = first + count; return pair <randomaccessiterator, outputiterator> (last, copy (first, last, result);} // copy N values from first to template <class inputiterator, class size, class outputiterator> inline pair <inputiterator, outputiter Ator> copy_n (inputiterator first, size count, outputiterator result) {// For function distribution, select the efficient version return _ copy_n (first, Count, result, iterator_category (first);} // use value to fill in the [first, last) interval template <class forwarditerator, class T> void fill (forwarditerator first, forwarditerator last, const T & value) {for (; first! = Last; ++ first) * First = value; // operator = () is called. note this.} // fill [first, first + n) with value) to prevent cross-border attacks, use the following example: // vector <int> VEC (); // For (INT I = 0; I <10; ++ I) // Vec. push_back (I); // fill_n (inserter (IV, IV. begin (), 100, 10); // This allows the container to dynamically expand the template <class outputiterator, class size, class T> outputiterator fill_n (outputiterator first, size N, const T & Value) {for (; n> 0; -- N, ++ first) * fi RST = value; return first;} // locate the first mismatch between the two sequences and return the template <class inputiterator1, class inputiterator2> pair <inputiterator1, inputiterator2> mismatch (inputiterator1 first1, inputiterator1 last1, inputiterator2 first2) {// traverse the interval to find the mismatch point while (first1! = Last1 & * first1 = * first2) {++ first1; ++ first2;} return pair <inputiterator1, inputiterator2> (first1, first2 );} // User-Defined binary discriminant is provided. The rest are the same as template <class inputiterator1, Class Identifier, class binarypredicate> pair <average, inputiterator2> mismatch (specify first1, interval last1, interval first2, binarypredicate binary_pred) {While (first1! = Last1 & binary_pred (* first1, * first2) {++ first1; ++ first2;} return pair <inputiterator1, inputiterator2> (first1, first2 );} // If the sequence is equal in [first, last), true is returned. If the second sequence has redundant elements, // No comparison is performed and ignored directly. if the second sequence element is insufficient, undefined behavior template <class sequence, class sequence> inline bool equal (inputiterator1 first1, inputiterator1 last1, inputiterator2 first2) {for (; first1! = Last1; ++ first1, ++ first2) if (* first1! = * First2) // if there is an inequality, the result is false return false; return true;} // The user-specified binary discriminant is used for comparison, other templates <class inputiterator1, class inputiterator2, class binarypredicate> inline bool equal (initial first1, initial last1, initial first2, binarypredicate binary_pred) {for (; first1! = Last1; ++ first1, ++ first2) if (! Binary_pred (* first1, * first2) return false; return true;} // compare the lexicographically similar strings. // For more information, see the STL documentation. In addition, strcmp () you can also refer to template <class inputiterator1, class inputiterator2> bool partition (inputiterator1 first1, inputiterator1 last1, interval first2, interval last2) {for (; first1! = Last1 & first2! = Last2; ++ first1, ++ first2) {If (* first1 <* first2) return true; If (* first2 <* first1) return false ;} return first1 = last1 & first2! = Last2;} // specify the binary discriminant. The remaining values are the same as those of the template <class inputiterator1, class inputiterator2, class compare> bool values (inclufirst1, inclulast1, inclufirst2, inclulast2, compare comp) {for (; first1! = Last1 & first2! = Last2; ++ first1, ++ first2) {If (COMP (* first1, * first2) return true; If (COMP (* first2, * first1 )) return false;} return first1 = last1 & first2! = Last2;} // special for strings. The efficiency is above the inline limit (const unsigned char * first1, const unsigned char * last1, const unsigned char * first2, const unsigned char * last2) {const size_t len1 = last1-first1; const size_t len2 = last2-first2; const int result = memcmp (first1, first2, min (len1, len2); return result! = 0? Result <0: len1 <len2;} // special for strings. The efficiency is superior to inline bool lexicographical_compare (const char * first1, const char * last1, const char * first2, const char * last2) {# If char_max = schar_max return lexicographical_compare (const signed Char *) first1, (const signed Char *) last1, (const signed Char *) first2, (const signed Char *) last2); # else return lexicographical_compare (const unsigned char *) first1, (con St unsigned char *) last1, (const unsigned char *) first2, (const unsigned char *) last2); # endif} // in one sentence, this is strcmp () the general version of template <class inputiterator1, Class Partition> int partition (inputiterator1 first1, extends last1, inputiterator2 first2, inputiterator2 last2) {While (first1! = Last1 & first2! = Last2) {If (* first1 <* first2) Return-1; if (* first2 <* first1) return 1; ++ first1; ++ first2 ;} if (first2 = last2) {return! (First1 = last1);} else {return-1;} // for specific versions, efficiency determines all inline intlexicographical_compare_3way (const unsigned char * first1, const unsigned char * last1, const unsigned char * first2, const unsigned char * last2) {const inclulen1 = last1-first1; const inclulen2 = last2-first2; const int result = memcmp (first1, first2, min (len1, len2); return result! = 0? Result: (len1 = len2? 0: (len1 <len2? -1: 1);} inline int lexicographical_compare_3way (const char * first1, const char * last1, const char * first2, const char * last2) {# If char_max = schar_max return lexicographical_compare_3way (const signed Char *) first1, (const signed Char *) last1, (const signed Char *) first2, (const signed Char *) last2); # else return lexicographical_compare_3way (const unsigned char *) first1, (const unsigned char *) last1, (const unsigned char *) first2, (const unsigned char *) last2); # endif }__ stl_end_namespace # endif/* _ sgi_stl_internal_algobase_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.