MTK elegant code appreciation 1: Binary Search

Source: Internet
Author: User

 

Developers who are new to MTK always think that their work is boring and promising. They change bugs all day long. In fact, this idea is narrow. MTK learning can only be regarded as the tip of the iceberg in the embedded field, we should thank him for reducing the embedded threshold.

All others say that the software development language is the same. Similarly, the development of embedded systems is the same, and embedded systems are also the same, other systems can be quickly mastered and developed, and the entry-level MTK surface is indeed easy to grasp, however, the in-depth content still needs to be studied with great effort. This part of content is something common in the embedded system. It is the most simple and general idea.

According to a report on csdn, "only 10% programmers can correctly implement the binary search algorithm", MTK Code also has such a piece of code, which is extremely elegant and makes people feel that programmers are not just creating code, it is also creating an art. Only programmers close to artists can have the potential of software designers.

Author Zhang sufeng, reproduced please indicate the source: http://www.cnblogs.com/zhangsufeng/archive/2010/09/03/1816612.html

To put it bluntly, let's use MTK's beautiful code to perform binary search.

For MTK developers, the sethilitehandler function for setting menu highlight may already be familiar with. In this algorithm, the program first checks whether the menu has been highlighted and coexist in the dynamic highlighted table mmi_frm_int_hilite_hdlr_table. If the menu exists, it replaces the highlighted function. If the menu does not exist, it inserts the menu and sets the highlighted function. This search uses an extremely simple binary search. See the Code:

 

 

 

1 typedef struct <br/> 2 {<br/> 3 u32 key; <br/> 4 u32 data; <br/> 5} mmi_frm_pair_data_struct; <br/> 6 <br/> 7 typedef struct <br/> 8 {<br/> 9 u32 menu_id; <br/> 10 funcptr hilite_hdlr; <br/> 11} mmi_frm_hilite_hdlr_struct; <br/> 12 <br/> 13 <br/> 14 static mmi_frm_hilite_hdlr_struct limit [mmi_frm_max_hilite_hdlr]; <br/> 15 /********************************** ************* * *************************** <Br/> 16 * function <br/> 17 * sethilitehandler <br/> 18 * description <br/> 19 * this function is used for dynamcally register the handler for the menu item. if the <br/> 20 * menu items are pre-definable, the applications use this function to dynamcally set <br/> 21 * The highlight handlers. <br/> 22 * <br/> 23 * Note: (1) Please distinguish the function from Regis Terhighlighthandler () <br/> 24 * which is provided by UI Layer. sethilitehandler () is for single <br/> 25 * menu item; registerhighlighthandler () is global for all menu items. <br/> 26 * (2) when the screen switchs, The registred handler will invalid. <br/> 27 * parameters <br/> 28 * Itemid [in] ID of the item for which highlight handler needs <br/> 29 * to be set. <br/> 30 * hilitefuncptr [in] fu Nction to be executed whenever item with above <br/> 31 * ID is highlighted. <br/> 32 * returns <br/> 33 * void <br/> 34 ********************* **************************************** * **************/<br/> 35 void sethilitehandler (2010itemid, funcptr hilitefuncptr) <br/> 36 {<br/> 37/* variable */<br/> 38/* local variables */<br/> 39 /*--- ----------------------------------------------------------- */<Br/> 40 u32 index; <br/> 41 mmi_bool result; <br/> 42 <br/> 43/* -------------------------------------------------------------- */<br/> 44/* Code body */<br/> 45/* Others */<br/> 46 mmi_trace (mmi_fw_trc_g1_frm, trc_mmi_frm_event_setcurhilihte_hdlr, Itemid, hilitefuncptr); <Br/> 47 <br/> 48 mmi_assert (Progress <strong); <br/> 49 <br/> 50 result = mmi_frm_binary_search (u32) Itemid, (mmi_frm_pair_data_struct *) mmi_frm_int_hilite_hdlr_table, <br/> 51 (u32) mmi_frm_int_hilite_hdlr_count, & Index); <br/> 52 <br/> 53 If (result = mmi_false) <br/> 54 {<br/> 55 if (mmi_frm_int_hilite_hdlr_count! = 0) <br/> 56 {<br/> 57 memmove (& region [index + 1], & mmi_frm_int_hilite_hdlr_table [Index], <br/> 58 (mmi_frm_int_hilite_hdlr_count-index) * sizeof (mmi_frm_hilite_hdlr_struct); <br/> 59} <br/> 60 mmi_frm_int_hilite_hdlr_count ++; <br/> 61 }< br/> 62 <br/> 63 mmi_frm_int_hilite_hdlr_table [Index]. menu_id = Itemid; <br/> 64 mmi_frm_int_hilite_hdlr_table [Index]. hilite_hdlr = hilitefuncptr; <br/> 65} <br/> 66 <br/> 67 <br/> 68 <br/> 69 /************* **************************************** * ********************* <br/> 70 * function <br/> 71 * mmi_frm_binary_search <br /> 72 * description <br/> 73 * performs a binary search of a sorted array. <br/> 74 * parameters <br/> 75 * Key [in] object to search. <br/> 76 * search_table [in] pointer to base of search data. <br/> 77 * num [in] Number of elements. <br/> 78 * index [out] index to an occurrence of key in the search_table <br/> 79 * array. <br/> 80 * returns <br/> 81 * If key is found, the function returns mmi_true, else returns mmi_false. <br/> 82 *********************************** **************************************** **/<br/> 83 mmi_bool mmi_frm_binary_search (u32 key, mmi_frm_pair_data_struct * search_table, u32 num, u32 * index) <br/> 84 {<br/> 85/* variables */<br/> 86/* local variables */<br/> 87/* -------------------------------------------------------- */<br/> 88 s32 m = 0, low = 0, high = num-1; <br/> 89 <br/> 90/* -------------------------------------------------------------- */<br/> 91/* Code body */<br/> 92/* Others */<br/> 93 while (low <= high) <br/> 94 {<br/> 95 m = (low + high)/2; <br/> 96 <br/> 97 If (Key = search_table [M]. key) <br/> 98 {<br/> 99 * Index = m; <br/> 100 return mmi_true; <br/> 101} <br/> 102 else if (Key> search_table [M]. key) <br/> 103 {<br/> 104 low = m + 1; <br/> 105} <br/> 106 else <br/> 107 {<br/> 108 high = m-1; <br/> 109} <br/> 110} <br/> 111 <br/> 112 * Index = low; <br/> 113 return mmi_false; <br/> 114} <br/> 115 

 

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.