C ++ from the perspective of assembly (generic programming)

Source: Internet
Author: User

 

[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]

 

 

Generic programming is not difficult. Essentially, generic programming is to apply general algorithms to all data types. Specifically, int is a familiar Integer type. In general, how should we write an int integer sorting algorithm? Can you try it by yourself? The following code is an example of mine;

Void bubble_sort (int array [], int length)

{

Int temp = 0;

Int outer = 0;

Int inner = 0;

Assert (NULL! = Array & 0! = Length );

 

For (outer = length-1; outer> = 1; outer --)

{

For (inner = 0; inner <= outer; inner ++)

{

If (array [inner]> array [inner + 1])

{

Temp = array [inner];

Array [inner] = array [inner + 1];

Array [inner + 1] = temp;

}

}

}

 

Return;

}

Void bubble_sort (int array [], int length)

{

Int temp = 0;

Int outer = 0;

Int inner = 0;

Assert (NULL! = Array & 0! = Length );

 

For (outer = length-1; outer> = 1; outer --)

{

For (inner = 0; inner <= outer; inner ++)

{

If (array [inner]> array [inner + 1])

{

Temp = array [inner];

Array [inner] = array [inner + 1];

Array [inner + 1] = temp;

}

}

}

 

Return;

} If you change the data type to a common data type, what do you need? Two: (1) arithmetic operator "=" overload; (2) Comparison function. The following is a designed class type.

Class type

{

Int data;

Public:

Type (int value = 0): data (value ){}

Type (type & t) {data = t. get_data ();}

~ Type (){}

Type & operator = (type & t) {data = t. get_data (); return * this ;}

Int get_data () {return data ;}

};

Class type

{

Int data;

Public:

Type (int value = 0): data (value ){}

Type (type & t) {data = t. get_data ();}

~ Type (){}

Type & operator = (type & t) {data = t. get_data (); return * this ;}

Int get_data () {return data ;}

}; So what about the comparison function? We can use a global function instead.

Int type_compare (type & t1, type & t2)

{

Return t1.get _ data ()> t2.get _ data ()? 1: 0;

}

Int type_compare (type & t1, type & t2)

{

Return t1.get _ data ()> t2.get _ data ()? 1: 0;

} Now all the functions have been modified, so the bubble_sort function should also be modified. Let's see how to do it?

Template <typename data>

Void bubble_sort (data array [], int length, int (* compare) (data &, data &))

{

Data temp;

Int outer = 0;

Int inner = 0;

Assert (NULL! = Array & 0! = Length );

 

For (outer = length-1; outer> = 1; outer --)

{

For (inner = 0; inner <= outer; inner ++)

{

If (compare (array [inner], array [inner + 1])

{

Temp = array [inner];

Array [inner] = array [inner + 1];

Array [inner + 1] = temp;

}

}

}

 

Return;

}

Template <typename data>

Void bubble_sort (data array [], int length, int (* compare) (data &, data &))

{

Data temp;

Int outer = 0;

Int inner = 0;

Assert (NULL! = Array & 0! = Length );

 

For (outer = length-1; outer> = 1; outer --)

{

For (inner = 0; inner <= outer; inner ++)

{

If (compare (array [inner], array [inner + 1])

{

Temp = array [inner];

Array [inner] = array [inner + 1];

Array [inner + 1] = temp;

}

}

}

 

Return;

} The code is ready for use. Let's see how to use it. Let's look at the following code:

272: type t [2] = {type (2), type (1 )};

0040148D push 2

0040148F lea ecx, [ebp-14h]

00401492 call @ ILT + 25 (type: type) (0040101e)

00401497 mov dword ptr [ebp-4], 0

0040149E push 1

004014A0 lea ecx, [ebp-10h]

004014A3 call @ ILT + 25 (type: type) (0040101e)

004014A8 mov byte ptr [ebp-4], 1

273: bubble_sort <type> (t, 2, type_compare );

004014AC push offset @ ILT + 20 (type_compare) (00401019)

004014B1 push 2

004014B3 lea eax, [ebp-14h]

004014B6 push eax

004014B7 call @ ILT + 50 (bubble_sort) (00401037)

004014BC add esp, 0Ch

274: return;

004014BF mov byte ptr [ebp-4], 0

004014C3 lea ecx, [ebp-10h]

004014C6 call @ ILT + 5 (type ::~ Type) (0040100a)

004014CB mov dword ptr [ebp-4], 0 FFFFFFFFh

004014D2 lea ecx, [ebp-14h]

004014D5 call @ ILT + 5 (type ::~ Type) (0040100a)

275 :}

272: type t [2] = {type (2), type (1 )};

0040148D push 2

0040148F lea ecx, [ebp-14h]

00401492 call @ ILT + 25 (type: type) (0040101e)

00401497 mov dword ptr [ebp-4], 0

0040149E push 1

004014A0 lea ecx, [ebp-10h]

004014A3 call @ ILT + 25 (type: type) (0040101e)

004014A8 mov byte ptr [ebp-4], 1

273: bubble_sort <type> (t, 2, type_compare );

004014AC push offset @ ILT + 20 (type_compare) (00401019)

004014B1 push 2

004014B3 lea eax, [ebp-14h]

004014B6 push eax

004014B7 call @ ILT + 50 (bubble_sort) (00401037)

004014BC add esp, 0Ch

274: return;

004014BF mov byte ptr [ebp-4], 0

004014C3 lea ecx, [ebp-10h]

004014C6 call @ ILT + 5 (type ::~ Type) (0040100a)

004014CB mov dword ptr [ebp-4], 0 FFFFFFFFh

004014D2 lea ecx, [ebp-14h]

004014D5 call @ ILT + 5 (type ::~ Type) (0040100a)

275:} we can see that the simple sorting has been completed, and the function will eventually call the bubble_sort function. Although generics are complex and involve knowledge about function pointers, Arithmetic Operators, and template functions, they become more convenient and easier to use as long as they are brave enough to try.

 

 

Problem:

(1) Can you try to compile an insert_sort generic function?

(2) try to write a binary generic processing function?

(3) How many factors may be considered when I try to compile a quick_sort generic function? But you can try it.

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.