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

Source: Internet
Author: User
Tags power of power

Stl_numeric.h contains numerical algorithms, which are related to numerical calculation.

G ++ 2.91.57, Cygnus \ cygwin-b20 \ include \ G ++ \ stl_numeric.h complete list/*** copyright (c) 1994 * Hewlett-Packard Company ** permission to use, copy, modify, distribute and modify 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 suppor Ting 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,1997 * 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 Fe E, * 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 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 he Aders. * You shoshould not attempt to use it directly. */# ifndef _ sgi_stl_internal_numeric_h # DEFINE _ counts // calculate the sum of the init and [first last) interval elements and // version 1 template <class inputiterator, class T> T accumulate (inputiterator first, inputiterator last, t init) {for (; first! = Last; ++ first) Init = init + * First; // Add the initial values of each element to the init return Init;} // version 2 template <class inputiterator, class t, class binaryoperation> T accumulate (inputiterator first, inputiterator last, t init, binaryoperation binary_op) {for (; first! = Last; ++ first) Init = binary_op (init, * First); // perform the binary operation return init for each element;} // calculate the inner product of the two elements, you need to provide the initial value init. The two fields have the same length, so last2 is not provided in the second section. // version 1 template <class alias, class inputiterator2, class T> T inner_product, t init) {for (; first1! = Last1; ++ first1, ++ first2) Init = init + (* first1 ** first2); // execute the general internal return init of the two sequences ;} // version 2 template <Class Identifier, Class Identifier, class T, class binaryoperation1, class binaryoperation2> T inner_product (struct first1, struct last1, inputiterator2 first2, t init, binaryoperation1 binary_op1, binaryoperation2 binary_op2) {for (; first1! = Last1; ++ first1, ++ first2) // replace operator * and operator + in the first version with external functions. // OP2 ranges between two elements. OP1 is used between OP2 results and init. Init = binary_op1 (init, binary_op2 (* first1, * first2); Return Init;} // _ partial_sum is defined before partial_sum, and the latter calls the former template <class inputiterator, class outputiterator, class T> outputiterator _ partial_sum (inputiterator first, inputiterator last, outputiterator result, T *) {T value = * First; while (++ first! = Last) {value = value + * First; // sum of the First n elements * + + Result = value; // specify to the destination} return ++ result ;} // version 1 template <class inputiterator, class outputiterator> outputiterator partial_sum (inputiterator first, inputiterator last, outputiterator result) {If (first = last) return result; * result = * First; return _ partial_sum (first, last, result, value_type (first); // Hou Jie considers (and confirms the certificate) that the call does not need to be transferred like the uplink call, you can use the following statement (entire function ):// If (first = last) return result; // * result = * First; // iterator_traits <inputiterator >:: value_type value = * First; // while (++ first! = Last) {// value = value + * First; // * ++ result = value; //} // return ++ result; // such concepts and practices, applies to all functions} template <class inputiterator, class outputiterator, class T, class binaryoperation> outputiterator _ partition (inputiterator first, last, outputiterator result, T *, binaryoperation binary_op) {T value = * First; while (++ first! = Last) {value = binary_op (value, * First); * ++ result = value;} return ++ result;} // version 2 template <class inputiterator, class outputiterator, class binaryoperation> outputiterator partial_sum (inputiterator first, inputiterator last, outputiterator result, binaryoperation binary_op) {If (first = last) return result; * result = * first; return _ partial_sum (first, last, result, value_type (first), binar Y_op); // Hou Jie believes that (and confirms the certificate), you do not need to transfer the call like the uplink, you can use the following method (the entire function): // If (first = last) return result; // * result = * First; // iterator_trait <inputiterator>: value_type value = * First; // while (++ first! = Last) {// value = binary_op (value, * First); // * ++ result = value; //} // return ++ result; /// this concept and practice applies to all functions} template <class inputiterator, class outputiterator, class T> outputiterator _ partition (inputiterator first, inputiterator last, outputiterator result, T *) {T value = * First; while (++ first! = Last) {// walk through the entire range t TMP = * First; * ++ result = TMP-value; // divide the difference between the two adjacent elements (before-after ), assigned to destination value = TMP;} return ++ result;} // version 1 template <class inputiterator, class outputiterator> outputiterator adjacent_difference (inputiterator first, inputiterator last, outputiterator result) {If (first = last) return result; * result = * First; // records the first element return _ adjacent_difference (first, last, result, value_type (First);} template <class inputiterator, class outputiterator, class T, class binaryoperation> outputiterator _ operator (partition first, inputiterator last, outputiterator result, T *, binaryoperation binary_op) {T value = * First; while (++ first! = Last) {t tmp = * First; * ++ result = binary_op (TMP, value); // calculates the result of the adjacent two elements, assigned to destination value = TMP;} return ++ result;} // version 2 template <class inputiterator, class outputiterator, class binaryoperation> outputiterator adjacent_difference (inputiterator first, inputiterator last, outputiterator result, binaryoperation binary_op) {If (first = last) return result; * result = * First; return _ adjacent_differe NCE (first, last, result, value_type (first), binary_op);} // power is SGI exclusive and is not in the STL standard column, it is used to calculate the N power of a certain number. // Here, the power of N refers to a certain number of operations performed on itself, and the operation type can be performed by the outside world. Multiplication is a power. // Version 2, to the power of power. If this parameter is set to multiplication, X ** N is returned when n> = 0. // Note that "multiplication" must meet the Associative Law, // but does not need to meet the commutative law ). Template <class T, class integer, class monoidoperation> T power (t x, integer N, monoidoperation OP) {If (n = 0) return identity_element (OP ); // retrieve the "same element" identity element. the certification element introduces else {While (N & 1) = 0) {n> = 1; X = OP (x, x) in the following chapters ); // both parameters are X. Because each time n is doubled (N is an even number, because N & 1 = 0)} t result = x; n> = 1; while (n! = 0) {x = OP (x, x); If (N & 1 )! = 0) Result = OP (result, x); N >>= 1;} return result ;}// version 1, multiplied by limit. Template <class T, class integer> inline t power (t x, integer N) {return power (x, N, multiplies <t> ();} // Hou Jie: what is the abbreviation of iota? // Function meaning: Enter value, value + 1, value + 2 in the range of [first, last .... Template <class forwarditerator, class T> void iota (forwarditerator first, forwarditerator last, T value) {While (first! = Last) * First ++ = value ++; }__ stl_end_namespace # endif/* _ sgi_stl_internal_numeric_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.