多核查找-顺序查找也疯狂
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
在去年的软件开发2.0技术大会上,我讲了一个支持动态负载均衡的多核查找设计方法。基本思想是采用数据结构分拆的方法,使用了多级的数据结构设计。下面先简要介绍一下这种多级数据结构的设计思路,然后给出一个采用数组顺序查找作为查找表实现的多级数据结构类CDHashArray。
在CDHashArray中,对数组的插入和删除都是顺序化的操作,查找也是近似于顺序化的操作,看起来似乎会很慢。实际上对于小数组,比如只有几个或十来个数组,其效率并不慢,这使得以前在单核时代无法用于大型查找的数组顺序查找,在多核时代却可以得到很好应用前景。
二级查找结构基本思想
要了解多级数据结构设计,首先得知道基本的二级查找数据结构设计思想。
二级查找结构就是在第1级查找时找到二级子表的位置,然后在找到的二级子表中进行第二次查找来找到对应的目标数据。
典型的二级查找结构示意图如下:
<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" /><group id="_x0000_s1060" style="WIDTH: 416.75pt; HEIGHT: 89.3pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" coordsize="8335,1786" editas="canvas" coordorigin="1497,1296"><lock aspectratio="t" v:ext="edit"></lock><shapetype id="_x0000_t75" stroked="f" filled="f" path="m@4@5l@4@11@9@11@9@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_s1061" style="LEFT: 1497px; WIDTH: 8335px; POSITION: absolute; TOP: 1296px; HEIGHT: 1786px" o:preferrelative="f" type="#_x0000_t75"><font face="宋体, MS Song"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></font></shape><rect id="_x0000_s1062" style="LEFT: 3541px; WIDTH: 1222px; POSITION: absolute; TOP: 1544px; HEIGHT: 428px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><font face="宋体, MS Song">1</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1063" style="LEFT: 4763px; WIDTH: 1225px; POSITION: absolute; TOP: 1544px; HEIGHT: 428px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><font face="宋体, MS Song">2</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1064" style="LEFT: 5988px; WIDTH: 1223px; POSITION: absolute; TOP: 1544px; HEIGHT: 428px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><font face="宋体, MS Song">3</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1065" style="LEFT: 7211px; WIDTH: 1189px; POSITION: absolute; TOP: 1544px; HEIGHT: 428px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><font face="宋体, MS Song">4</font></span></p> </div> </td></tr></tbody></table></textbox></rect><shapetype id="_x0000_t32" filled="f" path="m,l21600,21600e" o:spt="32" coordsize="21600,21600" o:oned="t"><path o:connecttype="none" arrowok="t" fillok="f"></path><lock v:ext="edit" shapetype="t"></lock></shapetype><shape id="_x0000_s1066" style="LEFT: 2615px; WIDTH: 1537px; POSITION: absolute; TOP: 1972px; HEIGHT: 624px; flip: x" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><rect id="_x0000_s1067" style="LEFT: 1934px; WIDTH: 454px; POSITION: absolute; TOP: 2596px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">锁</font></span></p> </div> </td></tr></tbody></table></textbox></rect><shape id="_x0000_s1068" style="LEFT: 4767px; WIDTH: 609px; POSITION: absolute; TOP: 1972px; HEIGHT: 624px; flip: x" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1069" style="LEFT: 6600px; WIDTH: 314px; POSITION: absolute; TOP: 1972px; HEIGHT: 600px" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><rect id="_x0000_s1070" style="LEFT: 2221px; WIDTH: 1259px; POSITION: absolute; TOP: 1556px; HEIGHT: 340px" stroked="f"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">一级查找表</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1071" style="LEFT: 1583px; WIDTH: 984px; POSITION: absolute; TOP: 2232px; HEIGHT: 340px" stroked="f"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">二级子表</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1072" style="LEFT: 3676px; WIDTH: 1019px; POSITION: absolute; TOP: 2232px; HEIGHT: 340px" stroked="f"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">二级子表</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1073" style="LEFT: 5676px; WIDTH: 984px; POSITION: absolute; TOP: 2232px; HEIGHT: 340px" stroked="f"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">二级子表</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1074" style="LEFT: 2388px; WIDTH: 454px; POSITION: absolute; TOP: 2596px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1075" style="LEFT: 2842px; WIDTH: 454px; POSITION: absolute; TOP: 2596px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1076" style="LEFT: 4086px; WIDTH: 454px; POSITION: absolute; TOP: 2596px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">锁</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1077" style="LEFT: 4540px; WIDTH: 454px; POSITION: absolute; TOP: 2596px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1078" style="LEFT: 4994px; WIDTH: 454px; POSITION: absolute; TOP: 2596px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1079" style="LEFT: 6233px; WIDTH: 454px; POSITION: absolute; TOP: 2572px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">锁</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1080" style="LEFT: 6687px; WIDTH: 454px; POSITION: absolute; TOP: 2572px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1081" style="LEFT: 7141px; WIDTH: 454px; POSITION: absolute; TOP: 2572px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1082" style="LEFT: 8162px; WIDTH: 454px; POSITION: absolute; TOP: 2572px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">锁</font></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1083" style="LEFT: 8616px; WIDTH: 454px; POSITION: absolute; TOP: 2572px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><rect id="_x0000_s1084" style="LEFT: 9070px; WIDTH: 454px; POSITION: absolute; TOP: 2572px; HEIGHT: 339px"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></rect><shape id="_x0000_s1085" style="LEFT: 7806px; WIDTH: 1037px; POSITION: absolute; TOP: 1972px; HEIGHT: 600px" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><rect id="_x0000_s1086" style="LEFT: 8843px; WIDTH: 984px; POSITION: absolute; TOP: 2208px; HEIGHT: 340px" stroked="f"><textbox inset=".5mm,.3mm,.5mm,.3mm"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">二级子表</font></span></p> </div> </td></tr></tbody></table></textbox></rect><?xml:namespace prefix = w ns = "urn:schemas-microsoft-com:office:word" /><wrap type="none"></wrap><anchorlock></anchorlock></group>
图16.2.1: 二级查找结构示意图
二级查找结构由一级查找表和二级子表构成,一个查找表中的每个节点指向一个二级查找子表。查找时,先将关键词映射成一级查找表的位置,然后将对应位置的二级子表取出,在子表中找到对应的查找目标数据。
Intel Threading Building Blocks(TBB)开源项目中,其中的concurrent_hash_map使用的就是一种最简单的二级查找结构。它使用了哈希表式的数据结构,并给哈希表的每个桶设一把锁。
对于普通的查找,这种简单的二级查找结构也许够用了,但是对于一些大型的查找,这种简单的二级查找结构并不能满足。首先的问题是如果子表数量过多,则锁的数量也非常多,锁本身需要占用大量的内存开销。
如果子表数量过少,那么又会引起另外一个重要的问题,那就是负载平衡问题。因为这种情况中有可能各个二级子表中的数据数量相差非常大,这将导致某些子表的访问量很少,而某些子表的访问量很大。这些访问量大的表很容易发生多个线程同时访问的情况,从而导致集中式锁竞争情况的发生。
为了解决二级查找结构中的不足,下面来看看多级查找结构的设计思想。
多级查找结构设计思想
多级查找结构是在二级查找结构的基础上设计的,当某个子表中数据个数过多时,可以将其拆分成两个或更多个子表,同时新建一个索引表来指向这几个拆分候的子表,指向原来子表的指针指向新建的索引表。
如果拆分后的子表内插入的数据过多时,可以继续将其分拆,这样一直分拆下去,将形成一个多级的查找数据结构,下图就是一个多级查找结构示意图。
<group id="_x0000_s1026" style="WIDTH: 389.45pt; HEIGHT: 212.6pt; mso-position-horizontal-relative: char; mso-position-vertical-relative: line" coordsize="7789,4252" editas="canvas" coordorigin="1077,7643"><lock aspectratio="t" v:ext="edit"></lock><shape id="_x0000_s1027" style="LEFT: 1077px; WIDTH: 7789px; POSITION: absolute; TOP: 7643px; HEIGHT: 4252px" o:preferrelative="f" type="#_x0000_t75"><font face="宋体, MS Song"><fill o:detectmouseclick="t"></fill><path o:connecttype="none" o:extrusionok="t"></path><lock v:ext="edit" text="t"></lock></font></shape><shapetype id="_x0000_t202" path="m,l,21600r21600,l21600,xe" o:spt="202" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><path o:connecttype="rect" gradientshapeok="t"></path></shapetype><shape id="_x0000_s1028" style="LEFT: 3366px; WIDTH: 582px; POSITION: absolute; TOP: 7644px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1028"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1029" style="LEFT: 3948px; WIDTH: 588px; POSITION: absolute; TOP: 7643px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1029"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1030" style="LEFT: 5124px; WIDTH: 1007px; POSITION: absolute; TOP: 7643px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1030"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><font face="宋体, MS Song"><span style="mso-spacerun: yes"></span><span lang="EN-US">…</span></font></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1031" style="LEFT: 4033px; WIDTH: 965px; POSITION: absolute; TOP: 10050px; HEIGHT: 523px" type="#_x0000_t202"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">查找表</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1032" style="LEFT: 3009px; WIDTH: 648px; POSITION: absolute; TOP: 8101px; HEIGHT: 775px; flip: x" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1033" style="LEFT: 4830px; WIDTH: 3554px; POSITION: absolute; TOP: 8100px; HEIGHT: 776px" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1034" style="LEFT: 2108px; WIDTH: 1123px; POSITION: absolute; TOP: 7711px; HEIGHT: 325px" stroked="f" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1034" inset="0,0,0,0"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">一级索引表</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1035" style="LEFT: 1692px; WIDTH: 977px; POSITION: absolute; TOP: 8913px; HEIGHT: 325px" stroked="f" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1035" inset="0,0,0,0"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">子索引表</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1036" style="LEFT: 1874px; WIDTH: 402px; POSITION: absolute; TOP: 10507px; HEIGHT: 865px; flip: x" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1037" style="LEFT: 3577px; WIDTH: 939px; POSITION: absolute; TOP: 9333px; HEIGHT: 717px" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1038" style="LEFT: 2844px; WIDTH: 331px; POSITION: absolute; TOP: 10507px; HEIGHT: 865px" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1039" style="LEFT: 2276px; WIDTH: 733px; POSITION: absolute; TOP: 9333px; HEIGHT: 717px; flip: x" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1040" style="LEFT: 4536px; WIDTH: 588px; POSITION: absolute; TOP: 7643px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1040"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1041" style="LEFT: 2718px; WIDTH: 582px; POSITION: absolute; TOP: 8876px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1041"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1042" style="LEFT: 3283px; WIDTH: 588px; POSITION: absolute; TOP: 8876px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1042"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1043" style="LEFT: 3871px; WIDTH: 588px; POSITION: absolute; TOP: 8876px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1043"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><font face="宋体, MS Song">…</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1044" style="LEFT: 5806px; WIDTH: 582px; POSITION: absolute; TOP: 8876px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1044"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1045" style="LEFT: 6371px; WIDTH: 588px; POSITION: absolute; TOP: 8876px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1045"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1046" style="LEFT: 6959px; WIDTH: 588px; POSITION: absolute; TOP: 8876px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1046"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><font face="宋体, MS Song">…</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1047" style="LEFT: 4242px; WIDTH: 1855px; POSITION: absolute; TOP: 8100px; HEIGHT: 776px" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1048" style="LEFT: 1985px; WIDTH: 582px; POSITION: absolute; TOP: 10050px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1048"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1049" style="LEFT: 2550px; WIDTH: 588px; POSITION: absolute; TOP: 10050px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1049"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">桶</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1050" style="LEFT: 3138px; WIDTH: 588px; POSITION: absolute; TOP: 10050px; HEIGHT: 457px" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1050"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><font face="宋体, MS Song">…</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1051" style="LEFT: 2692px; WIDTH: 965px; POSITION: absolute; TOP: 11372px; HEIGHT: 523px" type="#_x0000_t202"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">查找表</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1052" style="LEFT: 1391px; WIDTH: 965px; POSITION: absolute; TOP: 11372px; HEIGHT: 523px" type="#_x0000_t202"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">查找表</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1053" style="LEFT: 7901px; WIDTH: 965px; POSITION: absolute; TOP: 8876px; HEIGHT: 523px" type="#_x0000_t202"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">查找表</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1054" style="LEFT: 6704px; WIDTH: 965px; POSITION: absolute; TOP: 10032px; HEIGHT: 523px" type="#_x0000_t202"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">查找表</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1055" style="LEFT: 5434px; WIDTH: 965px; POSITION: absolute; TOP: 10032px; HEIGHT: 523px" type="#_x0000_t202"><textbox><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">查找表</font></span></p> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span lang="EN-US"><p><font face="宋体, MS Song"></font></p></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1056" style="LEFT: 5917px; WIDTH: 180px; POSITION: absolute; TOP: 9333px; HEIGHT: 699px; flip: x" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1057" style="LEFT: 6665px; WIDTH: 522px; POSITION: absolute; TOP: 9333px; HEIGHT: 699px" type="#_x0000_t32" o:connectortype="straight"><stroke endarrow="block"><font face="宋体, MS Song"></font></stroke></shape><shape id="_x0000_s1058" style="LEFT: 1077px; WIDTH: 857px; POSITION: absolute; TOP: 10111px; HEIGHT: 325px" stroked="f" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1058" inset="0,0,0,0"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">子索引表</font></span></p> </div> </td></tr></tbody></table></textbox></shape><shape id="_x0000_s1059" style="LEFT: 4830px; WIDTH: 906px; POSITION: absolute; TOP: 8913px; HEIGHT: 325px" stroked="f" type="#_x0000_t202"><textbox style="mso-next-textbox: #_x0000_s1059" inset="0,0,0,0"><table cellspacing="0" cellpadding="0" width="100%"><tbody><tr><td style="BORDER-RIGHT: #d4d0c8; BORDER-TOP: #d4d0c8; BORDER-LEFT: #d4d0c8; BORDER-BOTTOM: #d4d0c8; BACKGROUND-COLOR: transparent"> <div> <p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center" align="center"><span style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><font face="宋体, MS Song">子索引表</font></span></p> </div> </td></tr></tbody></table></textbox></shape><wrap type="none"></wrap><anchorlock></anchorlock></group>
在多级查找结构中进行操作时,所有索引表的操作都是不用锁的,只有访问查找表时才用锁,这使得它的查找效率并不会比二级查找结构降低。并且由于它是负载均衡的,每个查找子表的数量都是小于设定值的,这样每个查找表中查找的时间都是有保证的。而二级查找结构中并不能给出这种保证,比如链式的哈希表,如果哈希函数设计不好,某个桶中的元素个数过多,在这个桶中的查找将变慢。
在这种多级查找结构的设计中,查找表必须是可以分拆的,一般来说,比较易于分拆的查找数据结构有AVL树、数组等。
下面以数组为例来实现一个多级查找结构。由于基本算法思想在去年的软件开发大会上讲过,这里就不再详述了。如果没有听过讲座的也可以从下面的代码中去体会算法的基本思想。
使用数组作为查找表,可能很多人会认为速度很慢,因为数组的插入和删除速度看起来似乎很慢。但是实际上,如果数组设置得很小,比如只有几个或十几个数据,那么其插入和删除的速度并不慢,另外使用数组还有一个好处是容易进行CACHE行对齐,以避免伪共享问题(即多个线程操作的不同数据却位于同一Cache行的问题)。
对于这种小数组的查找,并不需要用到二分查找,可以采用近似于顺序查找的方法来进行查找,其查找效率也非常高效。
CDHashArray源代码
下面是使用数组作查找表的多级查找结构CDHashArray类的源代码。为方便起见,代码中对数据的比较等没有使用回调函数或仿函数,直接使用大于、等于或小于号进行比较。
template <class T>
struct INDEXNODE {
FASTLOCK lock;
T Key;
LONG volatile lType; //FIND_TABLE or INDEX_TABLE;
void *pSubTable; //sub table pointer,if lType is
// DARRAYSEARCH_FIND_TABLE, pSubTable is a Search Array,
// if lType is DARRAYSEARCH_INDEX_TABLE,
// then pSubTable pointer to a INDEXNODE array .
};
template <class T, class SearchArray>
class CDHashArray {
PRIVATE:
INDEXNODE<T> * m_pIndexNode;
int m_nBucketCount;
int m_nSplitCount;
HASHFUNC m_HashFunc;
void SubTableInsert(INDEXNODE<T> *pNode, T &Data);
void SubTableDelete(INDEXNODE<T> *pNode, T &Data);
void SubTableFind(INDEXNODE<T> *pNode, T &Data, T &OutData);
public:
CDHashArray(int nBucketCount, HASHFUNC HashFunc);
virtual ~CDHashArray();
void Insert(T &Data);
void Delete(T &Data);
void Find(T &Data, T &OutData);
};
/** 子表插入函数
@param INDEXNODE<T> *pNode - 要查找的索引节点
@param T &Data - 要查找的数据
@return void - 无
*/
template <class T, class SearchArray>
void CDHashArray<T, SearchArray>::SubTableInsert(
INDEXNODE<T> *pNode, T &Data)
{
FastLockAcquire(&(pNode->lock));
LONG lType = pNode->lType;
if ( lType == CDHASHARRAY_FIND_TABLE )
{
//将子表地址转换成一个索引表指针;
SearchArray *pArray = (SearchArray *)pNode->pSubTable;
pArray->Insert(Data);
if ( pArray->m_nCount == pArray->m_nSize )
{
CIndexTable<T, SearchArray> *pTable = new CIndexTable<T, SearchArray>;
pTable-><?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" /><place w:st="on"><city w:st="on"><span style="COLOR: black">Split</span></city></place>(pArray);
pNode->pSubTable = pTable;
//查找表的子表类型= 索引表(原子操作);
AtomicIncrement(&(pNode->lType));
}
}
else
{
//将查找表指针转换为索引表指针;
CIndexTable<T, SearchArray> *pTable
= (CIndexTable<