<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7954696955252676474</id><updated>2024-10-07T06:14:45.965+02:00</updated><category term="C/C++"/><category term="DirectX"/><category term="BookReading"/><category term="ITA"/><category term="LSSaver"/><category term="Vim"/><category term="software"/><category term="Fedora"/><category term="STL"/><category term="tools"/><category term="DotNet"/><title type='text'>Bing&#39;s Programming Experiences and Tips on C++</title><subtitle type='html'>Here you find my experiences and tips on programming C++ for Windows and Linux that I got from software projects I&#39;ve been involved in.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>66</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-4136600408669943815</id><published>2016-05-05T11:23:00.004+02:00</published><updated>2016-05-05T11:23:43.543+02:00</updated><title type='text'>An Updated Reading List</title><content type='html'>&lt;h3 class=&quot;post-title entry-title&quot;&gt;
&lt;br /&gt;
&lt;/h3&gt;
&lt;div class=&quot;post-header&quot;&gt;
&lt;/div&gt;
&lt;b&gt;Mathematics, Data Structures &amp;amp; Algorithms&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Discrete-Mathematics-Applications-Kenneth-Rosen/dp/0073229725?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Discrete Mathematics and Its Applications&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0073229725&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Fifth Edition), Kenneth H.Rosen&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Concrete-Mathematics-Foundation-Computer-Science/dp/0201558025?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Concrete Mathematics : A Foundation for Computer Science&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201558025&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Second Edition), Ronald L. Graham / Donald E. Knuth / Oren Patashnik&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Introduction-Algorithms-Second-Thomas-Cormen/dp/0262032937?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;* Introduction to Algorithms (Second Edition)&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0262032937&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;, Thomas H. Cormen / Charles E. Leiserson / Ronald L. Rivest / Clifford Stein&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Data-Structures-Algorithms-Applications-C/dp/0071155953?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Data Structures, Algorithms, and Applications in C++&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0071155953&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;， Sartaj Sahni &lt;br /&gt;
Data Structures &amp;amp; Program Design In C (Second Edition), Robert Kruse / C.L. Tondo / Bruce Leung&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Art-Computer-Programming-Volumes-Boxed/dp/0201485419?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;The Art of Computer Programming&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201485419&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; &lt;br /&gt;
Donald E.Knuth &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Compiler&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Compilers-Principles-Techniques-Tools-2nd/dp/0321486811?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Compilers: Principles, Techniques, and Tools&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0321486811&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (2nd Edition)&lt;br /&gt;
[Alfred V. Aho / Ravi Sethi / Jeffrey D. Ullman]&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Advanced-Compiler-Design-Implementation-Muchnick/dp/8178672413?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Advanced Compiler Design and Implementation&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=8178672413&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
[Steven S. Muchnick]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Operating System &lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Operating-Concepts-Seventh-Abraham-Silberschatz/dp/0471694665?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Operating System Concepts&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0471694665&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Now we have the 7th Edition)&lt;br /&gt;
[Abraham Silberschatz / Peter Baer Galvin / Greg Gagne]&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Operating-Systems-Design-Implementation-3rd/dp/0131429388?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Operating Systems : Design and Implementation&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0131429388&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Now we have the 3rd Edition)&lt;br /&gt;
[Andrew S. Tanenbaum / Albert S. Woodhull]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;C++ &lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326965?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;C++ Primer &lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0672326965&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Lippman / Josee Lajoie]&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/C-Programming-Language-Special/dp/0201700735?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;*The C++ Programming Language (Special Edition)&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201700735&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
[Bjarne Stroustrup]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;*Effective C++&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0321334876&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Now we have the 3rd Edition)&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/More-Effective-Improve-Programs-Designs/dp/020163371X?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;*More Effective C++&lt;/a&gt;&lt;br /&gt;
&lt;a href=&quot;http://amzn.to/1TouALT&quot;&gt;*Effective Modern C++: 42 Specific Ways to Improve Your Use of C++11 and C++14&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=020163371X&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; &lt;b&gt;(New)&lt;/b&gt;&lt;br /&gt;
Scott Meyers&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Modern C++ Design: Generic Programming and Design Patterns Applied&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201704315&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Andrei Alexandrescu&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Go&lt;/b&gt;&lt;br /&gt;
&lt;a href=&quot;http://amzn.to/1ZgH9gK&quot;&gt;The Go Programming Language&lt;/a&gt;&lt;br /&gt;
Alan A. A. Donovan&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;ASM&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Art-Assembly-Language-Randall-Hyde/dp/1593272073?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;The Art of Assembly Language&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=1593272073&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Randall Hyde&amp;nbsp; (x86)&lt;br /&gt;
&lt;br /&gt;
Introduction to 80x86 Assembly Language and Computer Architecture&lt;br /&gt;
Richard C. Detmer&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;UNIX Programming&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Advanced Programming in the UNIX Environment&lt;br /&gt;
[W. Richard Stevens]&lt;br /&gt;
&lt;br /&gt;
The UNIX Programming Environment&lt;br /&gt;
[Brianw. Kernighan]&lt;br /&gt;
&lt;br /&gt;
UNIX Network Programming, Volume 1 : The Sockets Networking API (Third Edition)&lt;br /&gt;
[W. Richard Stevens / Bill Fenner / Andrew M. Rudoff]&lt;br /&gt;
&lt;br /&gt;
UNIX Network Programming Volume 2 : Interprocess Communications (Second Edition)&lt;br /&gt;
[W. Richard Stevens]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Computer Networks&amp;nbsp; &lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Computer Networks (Fourth Edition)&lt;br /&gt;
[Andrew S. Tanenbaum]&lt;br /&gt;
&lt;br /&gt;
TCP/IP Illustrated, Volume 1 : The Protocols&lt;br /&gt;
TCP/IP Illustrated, Volume 2 : The Implementation&lt;br /&gt;
TCP/IP Illstrated, Volume 3 : TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols&lt;br /&gt;
[W. Richard Stevens]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Internetworking with TCP/IP Vol I : Principles, Protocols, and Architecture (Third Edition)&lt;br /&gt;
Internetworking with TCP/IP Vol II : Design, Implementation, and Internals (Second Edition)&lt;br /&gt;
Internetworking with TCP/IP Vol III : Client-Server Programming and Applications, BSD Socket Version (Second Edition)&lt;br /&gt;
Internetworking with TCP/IP Vol III : Client-Server Programming and Applications, Windows Sockets Version&lt;br /&gt;
[Douglas E. Comer / David L. Stevens]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Database&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
An Introduction to Database Systems (Seventh Edition)&lt;br /&gt;
[C. J.Date]&lt;br /&gt;
&lt;br /&gt;
Database System Concepts (Fourth Edition)&lt;br /&gt;
[Abraham Silberschat / Henry F.Korth / S.Sudarshan]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Software Engineering / Craftsmanship&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://amzn.to/1VKXtIH&quot;&gt;Code Complete: A Practical Handbook of Software Construction, Second Edition&lt;/a&gt;， Steven C. McConnell&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://amzn.to/1VKWc4j&quot;&gt;**Pragmatic programmer: from journeyman to master&lt;/a&gt;， &lt;span class=&quot;ptBrand&quot;&gt;Andrew Hunt and David Thomas&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;Peopleware：Productive Projects and Teams, Tom DeMarco, Timothy Lister&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;a href=&quot;http://amzn.to/1rrISoL&quot;&gt;&lt;span class=&quot;ptBrand&quot;&gt;**The Software Craftsman: Professionalism, Pragmatism, Pride (Robert C. Martin Series)&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;a href=&quot;http://amzn.to/1SQvfGH&quot;&gt;&lt;span class=&quot;ptBrand&quot;&gt;Clean Code: A Handbook of Agile Software Craftsmanship&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;a href=&quot;http://amzn.to/1WaSy4e&quot;&gt;**Soft Skills: The software developer&#39;s life manual&lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;a href=&quot;http://amzn.to/23rg1gk&quot;&gt;**The Lean Startup: How Today&#39;s Entrepreneurs Use Continuous Innovation to Create Radically Successful Businesses &lt;/a&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;b&gt;Data Science&lt;/b&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;a href=&quot;http://amzn.to/1VKYgcM&quot;&gt;***Data Science from Scratch: First Principles with Python&lt;/a&gt;, Joel Grus&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span class=&quot;ptBrand&quot;&gt;&lt;br /&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/4136600408669943815/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/4136600408669943815' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4136600408669943815'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4136600408669943815'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2016/05/an-updated-reading-list.html' title='An Updated Reading List'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-8638339143312633860</id><published>2015-04-11T14:36:00.001+02:00</published><updated>2015-04-11T14:37:43.965+02:00</updated><title type='text'>Fedora 21 Installed!</title><content type='html'>Haven&#39;t written anything since three years and now I come back, with my beloved Fedora.&lt;br /&gt;
&lt;br /&gt;
Just installed the latest Fedora 21 and now I&#39;m enjoying typing with it.&lt;br /&gt;
&lt;br /&gt;
Fedora 21 provides a Scientific spin, very cool, C/C++, Python, LaTex and a lot of other new and old friends!

</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/8638339143312633860/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/8638339143312633860' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8638339143312633860'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8638339143312633860'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2015/04/fedora-21-installed.html' title='Fedora 21 Installed!'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-7597686418268258920</id><published>2012-09-05T23:04:00.000+02:00</published><updated>2012-09-05T23:07:34.718+02:00</updated><title type='text'>A Real-life Story about exception specification</title><content type='html'>Today, just before I thought I&#39;d finish my day and go home, I got a urgent problem. One of our component kept coring, making services unavailable to clients.&lt;br /&gt;
&lt;br /&gt;
Investigation started right away and we found the cause after a while. The cause is quite simple but unexpected.&lt;br /&gt;
&lt;br /&gt;
The reason is we have a function specified with a exception specification. For those who are not familiar with the exception specification, it&#39;s the famous throw(A) that you can put after a function declaration or definition. It&#39;s used to tell compiler what exceptions should be allow to be thrown in the function, particularly, if nothing is in the (), that means the function is not allowed to throw anything.&lt;br /&gt;
&lt;br /&gt;
Well we have one such function that with a throw() however, it DO throw an exception. So what happens? The exception is caught and treated as unexpected, then terminate() is called, then terminate() calls abort(), program aborts with a core (SIGABRT 6).&lt;br /&gt;
&lt;br /&gt;
It&#39;s NOT hard to understand all of this, but we had a painful time to find out that it&#39;s actually this exception specification had caused the problem, it&#39;s in the same line as the function definition, far away from when the line where actually throws something.&lt;br /&gt;
&lt;br /&gt;
It&#39;s a shame that after years of debate on this topic, we still fail on that. Here are some links:&lt;br /&gt;
http://www.gotw.ca/publications/mill22.htm&lt;br /&gt;
http://www.gotw.ca/gotw/082.htm&lt;br /&gt;
http://www.boost.org/development/requirements.html#Exception-specification&lt;br /&gt;
All those gurus suggest NOT USING EXCEPTION SPECIFICATION !&lt;br /&gt;
&lt;br /&gt;
After beaten senselessly, now I remember, never write a exception specification.&lt;br /&gt;
&lt;br /&gt;
Also I have to put another point on that, a function with exception specification is just a trap for other programmers who is working on it, it may not notice the very existence of the throw() and throw something in the function body, or even harder to prevent, he may just call a function that throws inside.&amp;nbsp; I&#39;m such a victim, modified a function with throw() but throw something. &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/7597686418268258920/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/7597686418268258920' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/7597686418268258920'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/7597686418268258920'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2012/09/a-real-life-story-about-exception.html' title='A Real-life Story about exception specification'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-1597523536355978719</id><published>2012-06-30T22:08:00.002+02:00</published><updated>2012-06-30T22:08:15.326+02:00</updated><title type='text'>Fedora 17 Installed!</title><content type='html'>Another step towards a better OS, Fedora 17 has been released quite some time ago but only I have a chance to upgrade to it for my PC. So now I&#39;m posting on my new Fedora 17!&lt;br /&gt;
&lt;br /&gt;
It has a new kernel version and most recent development tools. The only little thing is the reboot panic but solutions are out &lt;a href=&quot;http://kparal.wordpress.com/2012/05/31/fedora-17-warning-kernel-panic-after-upgrade-part-2/&quot;&gt;there&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/1597523536355978719/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/1597523536355978719' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/1597523536355978719'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/1597523536355978719'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2012/06/fedora-17-installed.html' title='Fedora 17 Installed!'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-4717605168238750850</id><published>2012-04-27T23:16:00.001+02:00</published><updated>2012-04-27T23:16:53.076+02:00</updated><title type='text'>A Real-life Story about Floating Point Value Comparison</title><content type='html'>I got a problem today in our software. In general, when we have two input double values like v1= 800 and v2= 750, it&#39;s working correctly while if v1=1000 and v2 = 900, it&#39;s not working as expected.&lt;br /&gt;
&lt;br /&gt;
The logic is based on the comparison result of the two values. While for both two cases above we have v1 &amp;gt; v2, the results are not the same, why?&lt;br /&gt;
&lt;br /&gt;
After tracking down to where the comparison is made, I found out that it&#39;s essentially written like this:&lt;br /&gt;
if (FormatToString(v1) &amp;gt; FormatToString(v2)) DoSomething();&lt;br /&gt;
&lt;br /&gt;
I&#39;m pretty sure, at the first look to this line, it&#39;s not doing what whoever wrote it wanted to do. The above line is actually doing a lexical comparison of the two strings converted from two double values. &lt;br /&gt;
&lt;br /&gt;
Take a look at how the operator &amp;gt; is overloaded for string type in the basic_string.h : &lt;br /&gt;
&lt;br /&gt;
&amp;nbsp; /**&lt;br /&gt;&amp;nbsp;&amp;nbsp; *&amp;nbsp; @brief&amp;nbsp; Test if string follows string.&lt;br /&gt;&amp;nbsp;&amp;nbsp; *&amp;nbsp; @param lhs&amp;nbsp; First string.&lt;br /&gt;&amp;nbsp;&amp;nbsp; *&amp;nbsp; @param rhs&amp;nbsp; Second string.&lt;br /&gt;&amp;nbsp;&amp;nbsp; *&amp;nbsp; @return&amp;nbsp; True if @a lhs follows @a rhs.&amp;nbsp; False otherwise.&lt;br /&gt;&amp;nbsp;&amp;nbsp; */&lt;br /&gt;&amp;nbsp; template&lt;typename _alloc=&quot;&quot; _chart,=&quot;&quot; _traits,=&quot;&quot; typename=&quot;&quot;&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; inline bool&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; operator&amp;gt;(const basic_string&amp;lt;_CharT, _Traits, _Alloc&amp;gt;&amp;amp; __lhs,&lt;br /&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; const basic_string&amp;lt;_CharT, _Traits, _Alloc&amp;gt;&amp;amp; __rhs)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; { return __lhs.compare(__rhs) &amp;gt; 0; }&lt;/typename&gt;&lt;br /&gt;
&lt;br /&gt;
Notice that in the comments, it&#39;s stated as &quot;Test if string follows string&quot;&amp;nbsp; and the implementation is using the basic_string::compare() method which by default will do a lexical comparison like this : it returns &lt;tt&gt;0&lt;/tt&gt; if all the characters in the 
compared contents compare equal, a negative value if the first character
 that does not match compares to less (the character&#39;s ASCII value) in the object than in the comparing string, and a positive value in the opposite case.&lt;br /&gt;
&lt;br /&gt;
So come to our example:&lt;br /&gt;
&quot;800&quot; &amp;gt; &quot;750&quot; : Ture, but&lt;br /&gt;
&quot;1000&quot; &amp;gt; &quot;900&quot; : False because &#39;1&#39; &amp;lt; &#39;9&#39;.&lt;br /&gt;
&lt;br /&gt;
Fix it is simple, but the question is why the guy wrote this ? A simple mistake (and has been there since several years without being detected and corrected) ? I don&#39;t know exactly but tracking up in the code I found some clue that might explain.&lt;br /&gt;
&lt;br /&gt;
Some lines before this buggy line, I found a similar comparison but it&#39;s only testing the equality of two double values and there, the double values are formatted into strings then compared for equality:&lt;br /&gt;
&lt;br /&gt;
if (FormatToString(v1) != FormatToString(v2)) DoSomething();&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;This works fine and hides all the double value maths complexity behind the FormatToString method who knows exactly how many digits should be considered.&lt;br /&gt;
&lt;br /&gt;
I guess later on, there comes another need to test not only for equality but also to know which value is larger then the guy took a simple copy-paste-modifiy way which results in this bug.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The the question comes back to how to compare two double values ? This is a hard question to answer, read &lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.22.6768&quot;&gt;this paper&lt;/a&gt; and you will know why. However, in our case, a really simple solution does exist. Guess what?&lt;br /&gt;
&lt;br /&gt;
Just patch the shorter one of the two strings with leading &#39;0&#39;s, then the lexical comparison will be an equivalent to the value comparison!&lt;br /&gt;
&lt;br /&gt;
&quot;1000&quot; &amp;gt; &quot;900&quot;&amp;nbsp; : False, but&lt;br /&gt;
&quot;1000&quot; &amp;gt; &quot;0900&quot;: True !&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/4717605168238750850/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/4717605168238750850' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4717605168238750850'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4717605168238750850'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2012/04/real-life-story-about-floating-point.html' title='A Real-life Story about Floating Point Value Comparison'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-529716414601591670</id><published>2011-11-25T22:27:00.001+01:00</published><updated>2011-11-25T22:35:31.153+01:00</updated><title type='text'>Introduction to Algorithms - The Reading Notes: Part 7, The Quick Sort</title><content type='html'>Now comes our famous QuickSort. It&#39;s famous because it&#39;s quick, so how quick it is? 
The answer from the theoretical analysis is &lt;a href=&quot;http://en.wikipedia.org/wiki/Big_O_notation#Family_of_Bachmann.E2.80.93Landau_notations&quot;&gt;Θ&lt;/a&gt;&lt;span class=&quot;MATH&quot;&gt;(&lt;i&gt;n&lt;/i&gt; ln&amp;nbsp;&lt;i&gt;n&lt;/i&gt;)&lt;/span&gt;
 in average. OK, you may
argue that the MergeSort and HeapSort we talked before also run in 
&lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt; ln&amp;nbsp;&lt;i&gt;n&lt;/i&gt;)&lt;/span&gt;
, why
this one is quicker? The answer is hidden in the big O notation, we usually don&#39;t write
the constant factor in the function.
In fact, QuickSort has a smaller hidden factor than others which distinguishes in practice
QuickSort from the others. 
However, QuickSort can run, in the worst case, in &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;O&lt;/i&gt;(&lt;i&gt;n&lt;/i&gt;&lt;sup&gt;2&lt;/sup&gt;)&lt;/span&gt;
 time. Now let&#39;s see how to write some code to do QuickSort.&lt;br /&gt;
&lt;br /&gt;
As a starting point, we write our unit
tests. As usual, we only need to add test cases in SortingTest.h and implement them in SortingTest.cpp. 

&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;    CPPUNIT_TEST( testQuickSort_0 );
    CPPUNIT_TEST( testQuickSort_1 );
    CPPUNIT_TEST( testQuickSort_Dec_0 );
    CPPUNIT_TEST( testQuickSort_Dec_1 );
...
    void testQuickSort_0();
    void testQuickSort_1();
    void testQuickSort_Dec_0();
    void testQuickSort_Dec_1();&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
The implementation of the tests which is quite straightforward, actually, same
as tests for other sorting algorithms except the method used to sort, here we 
use QuickSort::Sort(v): 

&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;    // Setup Array.
...
    QuickSort::Sort(v);
...
    // Test Assertions.&lt;/pre&gt;
&lt;pre&gt;&lt;/pre&gt;
Now if we try to compile, we will get some compiling errors, of course. We do
not have our QuickSort class created. So now it&#39;s the time to create it. 

&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;    class QuickSort 
    {
        public:
            static void Sort(std::vector&lt;int&gt; &amp;amp;v, bool bNonDecreasing = true);
        protected:
            static int  Partition(std::vector&lt;int&gt; &amp;amp;v, int p, int r, bool bNonDecreasing);
            static void DoSort(std::vector&lt;int&gt; &amp;amp;v, int p, int r, bool bNonDecreasing);
    };&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;int&gt;&lt;int&gt;&lt;int&gt;
&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/pre&gt;
The QuickSort provides exactly the same interface as other sorting algorithms which is the 
Sort method. As usual, we allow to sort in both non-decreasing and decreasing orders.&lt;br /&gt;
&lt;br /&gt;
The DoSort method will recursively call itself to sort two sub-arrays partitioned by the 
method Partition.&lt;br /&gt;
&lt;br /&gt;
Write the QuickSort class, compile it with scons, then run the unit tests. Failed!! 
Double check the code, then fix all the bugs until all unit tests pass. Below is the 
resulting code.

&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;void QuickSort::Sort(std::vector&lt;int&gt; &amp;amp;v, bool bNonDecreasing) 
{
    if (v.size() &amp;lt;= 1 )
    {
        return;
    }

    DoSort(v, 0, v.size()-1, bNonDecreasing);
}

int QuickSort::Partition(std::vector&lt;int&gt; &amp;amp;v, int p, int r, bool bNonDecreasing)
{
    int pivot = v[r];
    int i = p-1; // i marks the end of the left partition.

    // j markes the end of the right partition
    for (int j = p; j &amp;lt;= r-1;  j++)
    {
        if ((v[j] &amp;lt; pivot &amp;amp;&amp;amp;  bNonDecreasing) ||
            (v[j] &amp;gt; pivot &amp;amp;&amp;amp; !bNonDecreasing))
        {
            i++;
            int t = v[i];
            v[i]  = v[j];
            v[j]  = t;
        }
    }
    v[r] = v[i+1];
    v[i+1] = pivot;

    return i+1;
}

void QuickSort::DoSort(std::vector&lt;int&gt; &amp;amp;v, int p, int r, bool bNonDecreasing)
{
    std::cout &amp;lt;&amp;lt; &quot;Sorting sub array: &quot; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; &quot;:&quot; &amp;lt;&amp;lt; r &amp;lt;&amp;lt; std::endl;
    if (p &amp;lt; r)
    {
        int q = Partition(v, p, r, bNonDecreasing);
        DoSort(v, p, q-1, bNonDecreasing);
        DoSort(v, q+1, r, bNonDecreasing);
    }
}
&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;

&lt;a href=&quot;http://www.blogger.com/blogger.g?blogID=7954696955252676474&quot; name=&quot;SECTION00030000000000000000&quot;&gt;
About this document ...&lt;/a&gt;
&lt;/h1&gt;
&lt;b&gt;ITA Reading Notes&lt;/b&gt;
This document was generated using the
&lt;a href=&quot;http://www.latex2html.org/&quot;&gt;&lt;b&gt;LaTeX&lt;/b&gt;2&lt;tt&gt;HTML&lt;/tt&gt;&lt;/a&gt; translator Version 2008 (1.71)
&lt;br /&gt;
Copyright © 1993, 1994, 1995, 1996,
&lt;a href=&quot;http://cbl.leeds.ac.uk/nikos/personal.html&quot;&gt;Nikos Drakos&lt;/a&gt;, 
Computer Based Learning Unit, University of Leeds.
&lt;br /&gt;
Copyright © 1997, 1998, 1999,
&lt;a href=&quot;http://www.maths.mq.edu.au/%7Eross/&quot;&gt;Ross Moore&lt;/a&gt;, 
Mathematics Department, Macquarie University, Sydney.
&lt;br /&gt;
The command line arguments were: &lt;br /&gt;
&lt;b&gt;latex2html&lt;/b&gt; &lt;tt&gt;-html_version 4.0,math -no_navigation -scalable_fonts -split 0 -no_auto_link ITAReadingNotes-Main&lt;/tt&gt;
&lt;br /&gt;
The translation was initiated by Bing Han on 2011-11-25</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/529716414601591670/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/529716414601591670' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/529716414601591670'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/529716414601591670'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/11/introduction-to-algorithms-reading_25.html' title='Introduction to Algorithms - The Reading Notes: Part 7, The Quick Sort'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-8190818408742094064</id><published>2011-11-20T22:50:00.001+01:00</published><updated>2011-11-20T22:50:53.381+01:00</updated><title type='text'>Introduction to Algorithms - The Reading Notes: Part 6, The Heap Sort</title><content type='html'>&lt;h1&gt;&lt;a href=&quot;http://www.blogger.com/post-edit.g?blogID=7954696955252676474&amp;amp;postID=8190818408742094064&quot; name=&quot;SECTION00010000000000000000&quot;&gt; Heap Sort&lt;/a&gt; &lt;/h1&gt;The basic idea of Heap Sort is to sort with help of a special data structure called heap.  A heap is a binary tree where each note in the tree contains a value.  If the value of each node is no less than it&#39;s children, we call it a Max-Heap. If the  value of each node is no more than it&#39;s children, we call it a Min-Heap.&lt;br /&gt;
&lt;br /&gt;
Don&#39;t be confused by name heap, which is also used commonly as a notion of a region of memory space where program can dynamically allocate.  &lt;br /&gt;
&lt;br /&gt;
Heap is very interesting, on one hand, it&#39;s a binary tree, so it benefits binary tree properties such as it&#39;s height grows logarithmically with the number of nodes it has; on the other hand,  it has one important order relationship between parent and child nodes.  &lt;br /&gt;
&lt;br /&gt;
As usual, we won&#39;t discuss in detail the theoretical aspect of the algorithm, instead, we focus on the implementation of the algorithm.   &lt;br /&gt;
&lt;br /&gt;
Before implementing the algorithm, let&#39;s first setup the unit test. As we already has our test infrastructure for previous sorting algorithms we discussed, we simply need to add some test cases inside.   &lt;br /&gt;
&lt;br /&gt;
For Heap Sort, I&#39;m going to create a class with name HeapSort which provides a Sort method as other sort algorithms. So in the CppUnitTest/SortingTest.h, I add some test cases and their declarations:   &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;CPPUNIT_TEST( testHeapSort_0 );
    CPPUNIT_TEST( testHeapSort_1 );
...
    void testHeapSort_0();
    void testHeapSort_1();
&lt;/pre&gt;&lt;br /&gt;
Then I define those tests (methods) in the CppUnitTest/SortingTest.cpp:   &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;void SortingTest::testHeapSort_0()
{
    int iArray[] = {10, 5, 7, -2, 19, 50, 300, 320, 160, 1000};
    std::vector&lt;int&gt; v(iArray, iArray + sizeof(iArray) / sizeof(int));
    HeapSort::Sort(v);
    CPPUNIT_ASSERT_EQUAL(v[0], -2);
    CPPUNIT_ASSERT_EQUAL(v[1], 5);
    CPPUNIT_ASSERT_EQUAL(v[2], 7);
    CPPUNIT_ASSERT_EQUAL(v[3], 10);
    CPPUNIT_ASSERT_EQUAL(v[4], 19);
    CPPUNIT_ASSERT_EQUAL(v[5], 50);
    CPPUNIT_ASSERT_EQUAL(v[6], 160);
    CPPUNIT_ASSERT_EQUAL(v[7], 300);
    CPPUNIT_ASSERT_EQUAL(v[8], 320);
    CPPUNIT_ASSERT_EQUAL(v[9], 1000);
}


void SortingTest::testHeapSort_1()
{
...
}
&lt;/int&gt;&lt;/pre&gt;&lt;br /&gt;
Now I can start coding the HeapSort class. First, I declare the main interface method, which is Sort for the HeapSort class, then I declare some helper methods Sort will use.   &lt;br /&gt;
&lt;br /&gt;
Same as described in the ITA, we will construct a binary tree directly on the input  array, by using the index of an item in the array. So item 0 is always the root of the  heap tree. Then, item 1 and 2 are two children of item 0, item 3 and 4 are two children of item 1, item 5 and 6 are children of item 2, and so on. In short, given a node &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; , we know that it&#39;s left child is &lt;span class=&quot;MATH&quot;&gt;2*&lt;i&gt;i&lt;/i&gt; + 1&lt;/span&gt;  and it&#39;s right child is &lt;span class=&quot;MATH&quot;&gt;(&lt;i&gt;i&lt;/i&gt; + 1)*2&lt;/span&gt; , if they exist,  where &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt;  starts from 0. As for it&#39;s parent, it depends. If &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt; = 0&lt;/span&gt; , then it&#39;s the root  node, so it does not really have a parent, otherwise, the parent node&#39;s index can be computed as &lt;span class=&quot;MATH&quot;&gt;(&lt;i&gt;i&lt;/i&gt; - 1)/2&lt;/span&gt; .&lt;br /&gt;
&lt;br /&gt;
We declare Parent, Left and Right methods to do the computation described above.  &lt;br /&gt;
Also, the Heap Sort is based on first building a MaxHeap ( in case we  need to sort non-decreasingly, otherwise, we build a MinHeap), then each  time takes the root (which is the largest item in the heap), then  reconstruct the heap. So we will also declare two auxiliary methods  BuildMaxHeap and MaxHeapify to do the job. Methods for building and  reconstructing MinHeap are also provided. The whole HeapSort class  declaration looks like follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;class HeapSort 
{
    public:
    static void Sort(std::vector&lt;int&gt; &amp;amp;v, bool bNonDecreasing = true);

    protected:
    static int  Parent(const int i) { return ((i &amp;gt; 0) ? (i - 1) &amp;gt;&amp;gt; 1: 0); }
    static int  Left(const int i)   { return (i &amp;lt;&amp;lt; 1) + 1; }
    static int  Right(const int i)  { return (i + 1) &amp;lt;&amp;lt; 1; }

    static void BuildMaxHeap(std::vector&lt;int&gt; &amp;amp;v);
    static void BuildMinHeap(std::vector&lt;int&gt; &amp;amp;v);
    static void MaxHeapify(std::vector&lt;int&gt; &amp;amp;v, const int i, const int iHeapSize);
    static void MinHeapify(std::vector&lt;int&gt; &amp;amp;v, const int i, const int iHeapSize);
};&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/int&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;Now comes the time to implement those methods. We will implement them in  a bottom-up way. So, first look at the MaxHeapify method, it takes  three parameters, a vector representing the heap (or a binary tree to be  built to a heap) and a integer &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt;  which is an index in the vector, representing a sub-tree rooted at &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; . Also the heap size is provided as the third parameter. MaxHeapify assumes that two sub-trees rooted at both of &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; &#39;s children are already MaxHeaps, only &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt;  is possibly violating the MaxHeap property. So the goal is just to make a MaxHeap rooted at &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; . To this end, it simply recursively floats down the content of node &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt;  until a proper place is found. &quot;Float down&quot; is just an exchange of &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt;  with the maximum of &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; , Left(&lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; ) and Right(&lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt; ). Of cause, in case that the maximum is &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;i&lt;/i&gt;&lt;/span&gt;  itself, nothing needs to be done. The code reads as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;void HeapSort::MaxHeapify(std::vector&lt;int&gt; &amp;amp;v, const int i, const int iHeapSize)
{
    int l = Left(i);
    int r = Right(i);

    int largest = i;

    if ( l &amp;lt; iHeapSize &amp;amp;&amp;amp; v[l] &amp;gt; v[i])
    {
        largest = l;
    }

    if ( r &amp;lt; iHeapSize &amp;amp;&amp;amp; v[r] &amp;gt; v[largest])
    {
        largest = r;
    }

    if (largest != i)
    {
        int temp = v[largest];
        v[largest] = v[i];
        v[i] = temp;

        MaxHeapify(v, largest, iHeapSize);
    }
}&lt;/int&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;Next, we will implement BuildMaxHeap. With the help of MaxHeapify, this  is fairly straightforward.  We simply need to first MaxHeapify the sub-trees at the lowest level  (actually the next to lowest, as the lowest are leaf nodes that does not  have any child so trivially they are MaxHeap). As a result, we can  start MaxHeapify the sub-trees from the middle of the vector.  The trick is, given a vector of size &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;n&lt;/i&gt;&lt;/span&gt; , if we count from the end of the vector, the first item that may have a child always appears no early than the &lt;span class=&quot;MATH&quot;&gt;&lt;i&gt;n&lt;/i&gt;/2&lt;/span&gt; th item (think why?).  As a result, we have the following simply implementation of BuildMaxHeap:   &lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;void HeapSort::BuildMaxHeap(std::vector&lt;int&gt; &amp;amp;v)
{
    for (int i = (v.size() &amp;gt;&amp;gt; 1); i &amp;gt;= 0; i--)
    {
        MaxHeapify(v, i, v.size());
    }
}&lt;/int&gt;&lt;/pre&gt;&lt;pre&gt;&lt;/pre&gt;We can implement similar methods for MinHeap too.&lt;br /&gt;
&lt;br /&gt;
Now that we have everything in hand, let&#39;s implement the final method  Sort.  We start by building a MaxHeap (or MinHeap), then each time, we exchange  the root of the heap (item 0) with the last item in the heap. This  exchange does two things: put the current largest at the end of the  vector and put a smaller one at the heap root, breaking heap property.  Then we use MaxHeapify to reconstruct the heap.&lt;br /&gt;
Once the heap is reconstructed, we have again the largest item at root,  then we simply repeat the exchange-reconstruct. The code looks like the  following, sorting by non-decreasing or non-increasing order are  supported with parameter bNonDecreasing, as other sort algorithms:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;void HeapSort::Sort(std::vector&lt;int&gt; &amp;amp;v, bool bNonDecreasing) 
{
    if (v.size() &amp;lt;= 1 )
    {
        return;
    }
    
    if (bNonDecreasing)
    {
        BuildMaxHeap(v);
    }
    else
    {
        BuildMinHeap(v);
    }

    int iHeapSize = v.size();
    
    for (int i = v.size() - 1; i &amp;gt;=1; i--)
    {
        int temp = v[0];
        v[0] = v[i];
        v[i] = temp;

        iHeapSize--;
        if (bNonDecreasing)
        {
            MaxHeapify(v, 0, iHeapSize);
        }
        else
        {
            MinHeapify(v, 0, iHeapSize);
        }
    }
}
&lt;/int&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h1&gt;&lt;a href=&quot;http://www.blogger.com/post-edit.g?blogID=7954696955252676474&amp;amp;postID=8190818408742094064&quot; name=&quot;SECTION00020000000000000000&quot;&gt; About this document ...&lt;/a&gt; &lt;/h1&gt;&lt;b&gt;ITA Reading Notes&lt;/b&gt; This document was generated using the &lt;a href=&quot;http://www.latex2html.org/&quot;&gt;&lt;b&gt;LaTeX&lt;/b&gt;2&lt;tt&gt;HTML&lt;/tt&gt;&lt;/a&gt; translator Version 2008 (1.71) &lt;br /&gt;
Copyright © 1993, 1994, 1995, 1996, &lt;a href=&quot;http://cbl.leeds.ac.uk/nikos/personal.html&quot;&gt;Nikos Drakos&lt;/a&gt;,  Computer Based Learning Unit, University of Leeds. &lt;br /&gt;
Copyright © 1997, 1998, 1999, &lt;a href=&quot;http://www.maths.mq.edu.au/%7Eross/&quot;&gt;Ross Moore&lt;/a&gt;,  Mathematics Department, Macquarie University, Sydney. &lt;br /&gt;
The command line arguments were: &lt;br /&gt;
&lt;b&gt;latex2html&lt;/b&gt; &lt;tt&gt;-html_version 4.0,math -no_navigation -scalable_fonts -split 0 -no_auto_link ITAReadingNotes-Main&lt;/tt&gt; &lt;br /&gt;
The translation was initiated by Bing Han on 2011-11-20</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/8190818408742094064/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/8190818408742094064' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8190818408742094064'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8190818408742094064'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/11/introduction-to-algorithms-reading_20.html' title='Introduction to Algorithms - The Reading Notes: Part 6, The Heap Sort'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-531433266972031194</id><published>2011-11-20T22:50:00.000+01:00</published><updated>2011-11-20T22:50:36.751+01:00</updated><title type='text'>Introduction to Algorithms - The Reading Notes: Part 5, Maximun Sum Sub-Array (DAC)</title><content type='html'>To be completed.</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/531433266972031194/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/531433266972031194' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/531433266972031194'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/531433266972031194'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/11/introduction-to-algorithms-reading.html' title='Introduction to Algorithms - The Reading Notes: Part 5, Maximun Sum Sub-Array (DAC)'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-2197075213428064278</id><published>2011-11-12T22:05:00.000+01:00</published><updated>2011-11-12T22:05:13.318+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Fedora"/><title type='text'>Fedora 16 Release</title><content type='html'>Fedora 16 released on November 8, 2011!&lt;br /&gt;
&lt;br /&gt;
I tried to download the 3.5GB DVD image&amp;nbsp; with firefox under windows. I started the download quite late last night, as it takes about 2 hours to finish, I paused it and shut down my PC then went to bed. I resumed it this morning and it finished later, but surprisingly, the resulted .iso file has 6GB!!&lt;br /&gt;
&lt;br /&gt;
I don&#39;t know how the firefox downloader worked on it but I do know, I don&#39;t have a DVD on which I can write a 6GB image. However, instead delete the bad file directly, I&#39;m curious to see how bad it is. Interestingly, I can mount it with a WinISO virtual driver and then I can open it. Selecting all items in the image, the total size seems correct, i.e. about 3.5GB.&lt;br /&gt;
&lt;br /&gt;
Of cause, the SHA256 checksum failed. (See here on &lt;a href=&quot;https://fedoraproject.org/en/verify&quot;&gt;how to verify the image&lt;/a&gt;)&lt;br /&gt;
&lt;br /&gt;
Finally, I switched to Fedora and let wget get the things done and it did it correctly. &lt;br /&gt;
&lt;br /&gt;
So, now I have the DVD image to write and install ! :D</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/2197075213428064278/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/2197075213428064278' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2197075213428064278'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2197075213428064278'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/11/fedora-16-release.html' title='Fedora 16 Release'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-163149939340789342</id><published>2011-10-31T16:18:00.001+01:00</published><updated>2011-10-31T16:18:35.554+01:00</updated><title type='text'>Fedora 16 Soon!</title><content type='html'>&lt;script id=&quot;fedora-banner&quot; src=&quot;http://fedoraproject.org/static/js/release-counter-ext.js?lang=en&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/163149939340789342/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/163149939340789342' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/163149939340789342'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/163149939340789342'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/10/fedora-16-soon.html' title='Fedora 16 Soon!'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-2435419626411824243</id><published>2011-06-11T21:49:00.002+02:00</published><updated>2011-06-11T21:51:59.407+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Fedora"/><title type='text'>Fedora 15!</title><content type='html'>Released at 24 May, the Lovelock&amp;nbsp; is another great step to a better user experience.&amp;nbsp; I installed it today and was shocked by the revolutionary changes to the GNOME desktop, now it&#39;s the GNOME shell that I&#39;m interacting with.&lt;br /&gt;
&lt;iframe align=&quot;left&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=aprsnida-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=B004AO8G44&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;align: left; height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;&lt;br /&gt;
It may seem unfamiliar and even irritating when I found no minimize button and everything in my Desktop folder does not show up on the desktop and I have to find applications in another place. But, but, but after I read the release note which is well prepared for the situation that a user like me can have, it answered me all my questions, and in one hour, I started to love it! Fedora name this 15th release Lovelock, does that mean something?&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The Super key is really handy, it calls up immediately all windows opened for you to select one. Also it brings the launch bar to front so launching an application can be done instantly with the advantage that it does not take any space when you are working in the application.&lt;br /&gt;
&lt;br /&gt;
Also the Firefox 4, just great! I love it, especially the Sync.&lt;br /&gt;
&lt;br /&gt;
As a programmer, I have the gcc 4.6 in this new release, I have vim 7.3, I have sons 2.0.1 ...&lt;br /&gt;
&lt;br /&gt;
All in all, I love the Lovelock, better than ever.</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/2435419626411824243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/2435419626411824243' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2435419626411824243'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2435419626411824243'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/06/fedora-15.html' title='Fedora 15!'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-4549547635238289237</id><published>2011-06-11T14:24:00.000+02:00</published><updated>2011-06-11T14:24:40.016+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BookReading"/><category scheme="http://www.blogger.com/atom/ns#" term="C/C++"/><category scheme="http://www.blogger.com/atom/ns#" term="ITA"/><title type='text'>Introduction to Algorithms - The Reading Notes: Part 4, Maximun Sum Sub-Array (BF)</title><content type='html'>Maximum Sub Sub-Array is an interesting problem. It goes like this, I&#39;m given a series of values and I&#39;m asked to find a continuous sub-series that has the maximum sum.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
For example, if I&#39;m given a series of value -3, 2, 5, 6, -1, 2, -5, 5, then I find that the sub-series 2,5,6 gives 13 which quite large, but 2, 5, 6, -1, 2 sums up even more, it gives 14. If I look further, I find that the sub-series 2, 5, 6, -1, 2, -5, 5 gives the same maximum of 14. So we may have more than one maximum and our goal is to find any one of them.&lt;br /&gt;
&lt;br /&gt;
&lt;iframe align=&quot;left&quot; frameborder=&quot;0&quot; marginheight=&quot;0&quot; marginwidth=&quot;0&quot; scrolling=&quot;no&quot; src=&quot;http://rcm.amazon.com/e/cm?t=aprsnida-20&amp;amp;o=1&amp;amp;p=8&amp;amp;l=bpl&amp;amp;asins=0262033844&amp;amp;fc1=000000&amp;amp;IS2=1&amp;amp;lt1=_blank&amp;amp;m=amazon&amp;amp;lc1=0000FF&amp;amp;bc1=000000&amp;amp;bg1=FFFFFF&amp;amp;f=ifr&quot; style=&quot;height: 245px; padding-right: 10px; padding-top: 5px; width: 131px;&quot;&gt;&lt;/iframe&gt;ITA mentions the brutal force algorithm that has Ω(n^2) running time, it says the algorithm is to calculate sums of all sub-series possible (there is O(n^2) number of sub-series) and if we can evaluate each sub-series in constant time then we can have Ω(n^2) algorithm, but it does not say how. ITA introduces also a very fast (O(nlgn) algorithm based on DaC (Divide and Conquer).&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
I understand that this ITA chapter is for DaC so no need to talk more details on this brutal force algorithm and it&#39;s put in the exercise. So, let&#39;s finish the Ω(n^2) brutal force algorithm first then we will see how to implement the DaC one.&lt;br /&gt;
&lt;br /&gt;
So you already know the number of sub-series is O(n^2), suppose we have a n-length series, but why?&amp;nbsp; The answer is this number equals to the number of ways you have when you try to select any two numbers in the series (those two numbers defines the start and end of the sub-series), that is n(n-1)/2.&lt;br /&gt;
&lt;br /&gt;
Now comes to the question how we calculate the sum of each sub-series. If we just simply calculate sums for all sub-series, then we have to sum n sub-series of length 1, n-1 sub-series of length 2, n-2 sub-series of length 3, ..., 1 sub-series of length n. In total, we need to do add n+2(n-1)+3(n-2)...+ n times, which is equivalent to sum(for i = 1 to n) (i*(n-i+1)),&amp;nbsp; which renders our algorithm O(n^3) in terms of time consumption. &lt;br /&gt;
&lt;br /&gt;
Now we need to do more to get a faster algorithm. Just one more glance at our previous O(n^3) algorithm we find that the sub-series have many common parts and we add those parts repeatedly many times once for each sub-series. That&#39;s the key to reduce running time.&lt;br /&gt;
&lt;br /&gt;
I will now use a n*n matrix to save sums of each sub-series, say, [0-1], [0-2]... [1,2], [1,3]... Also each single element represents also a sub-series of length 1, they are also stored in the matrix [0,0], [1,1] ... So only the upper triangle of the matrix is used. The advantage to save the sums in the matrix is that it&#39;s very easy to find a sub-series that can be used to build the next sum, because we know that:&lt;br /&gt;
sum(sub[i, j]) = sum(sub[i, j-1]) + v[j] = sum(sub[i, j-1]) + sub[j, j] &lt;br /&gt;
&lt;br /&gt;
OK, that&#39;s enough for the theory, let&#39;s see the code. But wait a moment, before implementing the algorithm, let&#39;s put in place the unit test! &lt;br /&gt;
&lt;br /&gt;
I&#39;ll use another class for the MaxSubArray unit test, separated from that of the sort algorithms. With CppUnit, I just simply create a .h and .cpp for the new tests, no other configuration is needed. &lt;br /&gt;
&lt;br /&gt;
MiscTest.h&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;#ifndef MISCTEST_H &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;preproc&quot;&gt;#define&lt;/span&gt; MISCTEST_H &lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;#include &amp;lt;cppunit/extensions/HelperMacros.h&amp;gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;class&lt;/span&gt; MiscTest : &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt; CppUnit::TestFixture {&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    CPPUNIT_TEST_SUITE( MiscTest);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    CPPUNIT_TEST( testMaxSubArray_0);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    CPPUNIT_TEST_SUITE_END();&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;private&lt;/span&gt;:&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;public&lt;/span&gt;:&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; setUp();&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; tearDown();&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// Test cases.&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; testMaxSubArray_0();&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;};&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;&lt;span class=&quot;preproc&quot;&gt;#endif&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
MiscTest.cpp&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;#include &lt;span class=&quot;str&quot;&gt;&quot;MiscTest.h&quot;&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;#include &lt;span class=&quot;str&quot;&gt;&quot;MaxSubArray.h&quot;&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;using&lt;/span&gt; &lt;span class=&quot;kwrd&quot;&gt;namespace&lt;/span&gt; Common::Algorithm::MaxSubArray;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;// Registers the fixture into the &#39;registry&#39;&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;CPPUNIT_TEST_SUITE_REGISTRATION( MiscTest );&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; MiscTest::setUp()&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; MiscTest::tearDown()&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;// Test normal case.&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; MiscTest::testMaxSubArray_0()&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; iArray[] = {-10, 5, 7, -2, 19, 50, -30, 32, -24, 18};&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;    std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; v(iArray, iArray + &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(iArray) / &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;));&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; rStart = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; rEnd = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; rSum = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;    MaxSubArray::CalcBF(v, rStart, rEnd, rSum);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;    &lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL( 1, rStart);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL( 7, rEnd);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(81, rSum);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  30:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  31:  &lt;/span&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Finally, I can start implementing the algorithm. Then I run the unit test each time I think my algorithm will work, then fix bugs and retry. Here is the final version that passes the test above: &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; MaxSubArray::CalcBF(std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp;v, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; &amp;amp;oStart, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; &amp;amp;oEnd, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; &amp;amp;oSum)&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// Trivial cases, size = 0 or 1.&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (v.size() == 0)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    oStart = 0; &lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    oEnd = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    oSum = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (v.size() == 1)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    oStart = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    oEnd = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    oSum = v[0];&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;    oSum= 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;    oStart = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;    oEnd = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;    std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; vResult;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// Initialize vResult.&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (size_t i = 0; i &amp;lt; v.size(); i++)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (size_t j = 0; j &amp;lt; v.size(); j++)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  30:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (i == j)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  31:  &lt;/span&gt;        {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  32:  &lt;/span&gt;        vResult.push_back(v[i]);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  33:  &lt;/span&gt;        }&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  34:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  35:  &lt;/span&gt;        {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  36:  &lt;/span&gt;        vResult.push_back(0);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  37:  &lt;/span&gt;        }&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  38:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  39:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  40:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  41:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  42:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// Calculate the results.&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  43:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// We use a matrix to store sum of all intervals&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  44:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// 00   01   02   03   04&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  45:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// 10   11   12   13   14&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  46:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// 20   21   22   23   24&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  47:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// 30   31   32   33   34&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  48:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// 40   41   42   43   44&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  49:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// M[i,i] = v[i]&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  50:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// M[i,j] = sum(i,j) for 0 &amp;lt;= i &amp;lt; j &amp;lt;= v.size(). &lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  51:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// It takes O(n^2) time.&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  52:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (size_t i = 0; i &amp;lt; v.size(); i++)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  53:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  54:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (size_t j = 0; j &amp;lt; v.size(); j++)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  55:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  56:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (i &amp;lt; j)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  57:  &lt;/span&gt;        {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  58:  &lt;/span&gt;        vResult[ i * v.size() + j] = vResult[ i * v.size() + j - 1 ] +&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  59:  &lt;/span&gt;                                     vResult[ j * v.size() + j];&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  60:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (oSum &amp;lt; vResult[i * v.size() +j])&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  61:  &lt;/span&gt;        {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  62:  &lt;/span&gt;            oSum = vResult[i * v.size() + j];&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  63:  &lt;/span&gt;            oStart = i;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  64:  &lt;/span&gt;            oEnd = j;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  65:  &lt;/span&gt;        }&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  66:  &lt;/span&gt;        }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  67:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  68:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  69:  &lt;/span&gt;}&lt;/span&gt;&amp;nbsp;&lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Let discuss the faster version that takes only O(nlgn) time, in the next post. Thank you and like to know your feedback.</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/4549547635238289237/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/4549547635238289237' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4549547635238289237'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4549547635238289237'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/06/introduction-to-algorithms-reading_11.html' title='Introduction to Algorithms - The Reading Notes: Part 4, Maximun Sum Sub-Array (BF)'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-7507694424873417371</id><published>2011-06-02T15:17:00.001+02:00</published><updated>2011-06-10T20:51:25.438+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BookReading"/><category scheme="http://www.blogger.com/atom/ns#" term="C/C++"/><category scheme="http://www.blogger.com/atom/ns#" term="ITA"/><title type='text'>Introduction to Algorithms - The Reading Notes: Part 3, Merge Sort</title><content type='html'>Merge sort is a better sorting algorithm than insertion sort in terms of worst case running time. It&#39;s also the algorithm that I ran into when I have my interview for my current job.&lt;br /&gt;
&lt;br /&gt;
The ITA book uses it as an example of the Divide and Conquer philosophy which basically divides a big problem into several smaller ones then solve each of them first, when the smaller problems are solved, the big problem becomes easier to solve. This usually results in a recursive algorithm as the case of the merge sort. I&#39;m not sure it&#39;s a good place to introduce this concept in the beginning part of the book, the learning curve is quite steep. &lt;br /&gt;
&lt;br /&gt;
The merge sort contains two parts, the divide or recursive part and the merge part. The idea is to divide the whole list to sort into two lists each of half the original size and sort the two lists separately, then merge the two sorted smaller lists into one sorted list to get the problem solved. The magic is to repeat this procedure for the two smaller lists too, so they became four even smaller lists, and so on. &lt;br /&gt;
&lt;br /&gt;
The merge part is quite straightforward, compare the elements at the head position of two sorted list, and put the larger (or smaller) into the result list until one of the list contains no element, then we can safely put the rest of the other list into the result list. Only one thing to remark, the vector constructor who takes to iterators s and e means to construct a vector that contains elements from s to e-1 of the original vector. &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; MergeSort::Merge(std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp;v, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; p, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; q, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; r, &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; bNonDecreasing)&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    std::cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Merging sub array: &quot;&lt;/span&gt; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;:&quot;&lt;/span&gt;&amp;lt;&amp;lt; q-1 &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot; and &quot;&lt;/span&gt; &amp;lt;&amp;lt; q &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;:&quot;&lt;/span&gt; &amp;lt;&amp;lt; r-1 &amp;lt;&amp;lt; std::endl;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; vSub1(v.begin()+p, v.begin()+q);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; vSub2(v.begin()+q, v.begin()+r);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    std::cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;vSub1.size: &quot;&lt;/span&gt; &amp;lt;&amp;lt; vSub1.size() &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;, vSub2.size: &quot;&lt;/span&gt;&amp;lt;&amp;lt; vSub2.size() &amp;lt;&amp;lt; std::endl;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; iLastValue = (bNonDecreasing ? INT_MAX : INT_MIN);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    &lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    vSub1.push_back(iLastValue);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    vSub2.push_back(iLastValue);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; k = p; k &amp;lt; r; k++)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; ( bNonDecreasing &amp;amp;&amp;amp; (vSub1[i] &amp;lt; vSub2[j]) ||&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;        !bNonDecreasing &amp;amp;&amp;amp; (vSub1[i] &amp;gt; vSub2[j]))&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;       v[k] = vSub1[i];&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;       i++;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;else&lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  24:  &lt;/span&gt;        v[k] = vSub2[j];&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  25:  &lt;/span&gt;        j++;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  26:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  27:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  28:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  29:  &lt;/span&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
The recursive part is a bit tricky. Personally, I prefer to give a unique interface to the users of my sorting library, so I&#39;d like to call my interface MergeSort::Sort(input_vector_to_sort, order). Then I found that I need a helper method that can be called recursively and I named it as DoSort(). DoSort() takes two additional parameters, the start and the end position in the list that defines the sub-list to be sorted. It starts by dividing the input vector into two smaller ones then it calls itself recursively for each smaller vector, finally it merges the two smaller vector (sorted now). &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; MergeSort::DoSort(std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp;v, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; p, &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; r, &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; bNonDecreasing)&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    std::cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Sorting sub array: &quot;&lt;/span&gt; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;:&quot;&lt;/span&gt; &amp;lt;&amp;lt; r &amp;lt;&amp;lt; std::endl;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (p &amp;lt; r)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; q = (p+r)/2; &lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    std::cout &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;Dividing sub array: &quot;&lt;/span&gt; &amp;lt;&amp;lt; p &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;:&quot;&lt;/span&gt; &amp;lt;&amp;lt; q  &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;, &quot;&lt;/span&gt; &amp;lt;&amp;lt; q+1 &amp;lt;&amp;lt; &lt;span class=&quot;str&quot;&gt;&quot;:&quot;&lt;/span&gt; &amp;lt;&amp;lt; r &amp;lt;&amp;lt; std::endl;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    DoSort(v, p, q, bNonDecreasing);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    DoSort(v, q+1, r, bNonDecreasing);&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    Merge(v, p, q+1, r+1, bNonDecreasing);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Finally, the interface method is fairly simple:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; MergeSort::Sort(std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp;v, &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; bNonDecreasing) &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (v.size() &amp;lt; 1 )&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    {&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    }&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    DoSort(v, 0, v.size()-1, bNonDecreasing);&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/7507694424873417371/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/7507694424873417371' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/7507694424873417371'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/7507694424873417371'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/06/introduction-to-algorithms-reading.html' title='Introduction to Algorithms - The Reading Notes: Part 3, Merge Sort'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-5944152920551423418</id><published>2011-05-11T21:38:00.003+02:00</published><updated>2011-06-10T20:51:05.065+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BookReading"/><category scheme="http://www.blogger.com/atom/ns#" term="ITA"/><title type='text'>Introduction to Algorithms - The Reading Notes: Part 2, Insertion Sort</title><content type='html'>&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;Insertion Sort is probably the simplest algorithm so it is used in many books as the first example. ItA discusses this minion algorithm right in the Part I Foundations, Chapter 2 Getting Started.&lt;br /&gt;
&lt;br /&gt;
The aim of the algorithm is to sort a set of keys in a certain order: You have some keys as input then the algorithm will give you a ordered list of your keys. Here is how the insertion sort works: suppose all the keys are in an input list, then the algorithm starts working on the the keys one by one in a certain order, for example from left to right. Each time it works on a key, it save it&#39;s value, then it looks all keys already processed (they are to the left of the current key), from right to left, by comparing the current key with them and moving the processed keys one place to the right if it should be placed after the current key, according to the ordering rule. This process ends until when an already processed key that should be placed to the left of the current key or there is no more already processed keys. Then we put the current key in the place of the last already processed key we found. Now that we find a good place for the current key, we can start processing the next unprocessed key. &lt;br /&gt;
&lt;br /&gt;
The algorithm is simple, code the pseudo code in C++ is straightforward. What I&#39;d to say here is how I set up the whole project with CppUnit and SCons. &lt;br /&gt;
&lt;br /&gt;
CppUnit is very handy for unit test, especially when I&#39;m writing libraries. Just as in this ITA project, I&#39;d like to put all the algorithms in some libraries, I don&#39;t need to write a separate main cpp file just to test these algorithms, instead, I setup a CppUnit testing framework for this. It&#39;s easier to maintain. For how to install/use CppUnit, here is the &lt;a href=&quot;http://cppunit.sourceforge.net/doc/lastest/index.html&quot;&gt;CppUnit Documentation&lt;/a&gt;. &lt;br /&gt;
&lt;br /&gt;
First, I implement the Insertion Sort algorithm in InsertSort.cpp and .h files and I put them in a folder named Sorting. With TDD in mind, I write only the class declaration but leaving the method empty. &lt;br /&gt;
&lt;br /&gt;
Then I setup the CppUnit framework: I need three files: SortingTest.h and .cpp, where I&#39;ll implement test code for all the sorting algorithms I&#39;ll write; TestMain.cpp. where is the &quot;main&quot; function of the test code. The beauty of using CppUnit is this &quot;main&quot; function has only infrastructure interest, it&#39;s not related to the particular test cases. So once setup, I don&#39;t need to change it. I&#39;ll only work on the SortingTest class. &lt;br /&gt;
&lt;br /&gt;
Now I will write a test case:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;rem&quot;&gt;// Test normal case.&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; SortingTest::testInsertSort_0()&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;{&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; iArray[] = {10, 5, 7, -2, 19, 50, 300, 320, 160, 1000};&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; v(iArray, iArray + &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(iArray) / &lt;span class=&quot;kwrd&quot;&gt;sizeof&lt;/span&gt;(&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;));&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    Common::Algorithm::Sort::InsertSort(v);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[0], -2);&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[1], 5);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[2], 7);&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[3], 10);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[4], 19);&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[5], 50);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[6], 160);&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[7], 300);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[8], 320);&lt;/span&gt;&lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;    CPPUNIT_ASSERT_EQUAL(v[9], 1000);&lt;/span&gt;&lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;}&lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
The aim of the test case is to test the basic function of the algorithm, I initialize a vector with 10 integers, then I call the InsertSort(), finally, I test that the vector is sorted by examining that each element has the expected value. CppUnit provides a macro CPPUNIT_ASSERT_EQUAL for this kind of test.&lt;br /&gt;
&lt;br /&gt;
Now that I have the algorithm code (although not yet complete), I have the testing code, I can compile both and run my test. I use SCons for this. SCons is very convenient yet powerful.&amp;nbsp; Basically, you write a configuration file usually named as SConstruct, inside you tell scons how to build your project. &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;Library(target=&lt;span class=&quot;str&quot;&gt;&#39;Sorting/Sorting&#39;&lt;/span&gt;, \&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;        source=Glob(&lt;span class=&quot;str&quot;&gt;&#39;Sorting/*.cpp&#39;&lt;/span&gt;))&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;Program(target=&lt;span class=&quot;str&quot;&gt;&#39;CppUnitTest/TestMain&#39;&lt;/span&gt;, \&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;        source=Glob(&lt;span class=&quot;str&quot;&gt;&#39;CppUnitTest/*.cpp&#39;&lt;/span&gt;), \&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;        LIBS=[&lt;span class=&quot;str&quot;&gt;&#39;cppunit&#39;&lt;/span&gt;,&lt;span class=&quot;str&quot;&gt;&#39;Sorting&#39;&lt;/span&gt;], \&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;        LIBPATH=&lt;span class=&quot;str&quot;&gt;&#39;Sorting&#39;&lt;/span&gt;, \&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;        CPPPATH=[&lt;span class=&quot;str&quot;&gt;&#39;Sorting&#39;&lt;/span&gt;, &lt;span class=&quot;str&quot;&gt;&#39;CppUnitTest&#39;&lt;/span&gt;])&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
The Library keyword at line 1 specifies a library to build, the source code are all the .cpp files in the Sorting folder. &lt;br /&gt;
&lt;br /&gt;
The test code should be built with the Program keyword, as in line 3. It needs all source code in the CppUnitTest folder and two libraries: the CppUnit library and the Sorting library that I&#39;m testing. The Sorting library is in my local folder so I need to tell SCons where to find it with LIBPATH keyword as in line 6 above. Finally, the test code need the Sorting library header to compile, I&#39;ll tell SCons to find the Sorting headers in the Sorting folder with CPPPATH keyword as in line 7.&lt;br /&gt;
&lt;br /&gt;
OK, now I save the Sconstruct file then I type scons. It compiles my Sorting library and the testing code.&lt;br /&gt;
&lt;br /&gt;
I can run the test immediately and it fails evidently. Now you can start to write the InsertSort() method, but personally, I will write some more test cases before writing the InsertSort() method. For example, I will write one case when the input is empty, another when all inputs have the same value, etc.&lt;br /&gt;
&lt;br /&gt;
Finally, I write my InsertSort() method and correct all failing cases, once all test cases pass, I&#39;m quite confident that my InsertSort() works correctly.&lt;br /&gt;
&lt;br /&gt;
Here is the how the InsertSort() is implemented:&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; Sort::InsertSort(std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp;v) &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   2:  &lt;/span&gt;{&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   3:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; key = 0;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   4:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   5:  &lt;/span&gt;    &lt;span class=&quot;rem&quot;&gt;// Trivial case, nothing to do. &lt;/span&gt;&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   6:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;if&lt;/span&gt; (v.size() &amp;lt;= 1)&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   7:  &lt;/span&gt;    {   &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   8:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;return&lt;/span&gt;;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;   9:  &lt;/span&gt;    }   &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  10:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  11:  &lt;/span&gt;    &lt;span class=&quot;kwrd&quot;&gt;for&lt;/span&gt; (&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; j = 1; j &amp;lt; v.size(); j++)&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  12:  &lt;/span&gt;    {   &lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  13:  &lt;/span&gt;        key = v[j];&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  14:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt; i = j-1;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  15:  &lt;/span&gt;        &lt;span class=&quot;kwrd&quot;&gt;while&lt;/span&gt; (i &amp;gt;= 0 &amp;amp;&amp;amp; v[i] &amp;gt; key)                                                                                            &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  16:  &lt;/span&gt;        {   &lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  17:  &lt;/span&gt;            v[i+1] = v[i];&lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  18:  &lt;/span&gt;            i--;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  19:  &lt;/span&gt;        }   &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  20:  &lt;/span&gt;        v[i+1] = key;&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  21:  &lt;/span&gt;    }   &lt;/span&gt; &lt;/pre&gt;&lt;pre&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  22:  &lt;/span&gt;}&lt;/span&gt; &lt;/pre&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span class=&quot;lnum&quot;&gt;  23:  &lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/pre&gt;&lt;/div&gt;&lt;br /&gt;
Modification: When I started to write the merge sort, I found it could be better to rename the class and the methods, and finally, I ended up with a solution that gives each sorting algorithm a class and they all have the same Sort() method declaration.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;csharpcode&quot;&gt;&lt;pre class=&quot;alt&quot;&gt;&lt;span style=&quot;font-size: xx-small;&quot;&gt;&lt;span lnum=&quot;&quot; style=&quot;font-size: xx-small;&quot;&gt;   1:  &lt;/span&gt;&lt;span class=&quot;kwrd&quot;&gt;void&lt;/span&gt; InsertSort::Sort(std::vector&amp;lt;&lt;span class=&quot;kwrd&quot;&gt;int&lt;/span&gt;&amp;gt; &amp;amp;v, &lt;span class=&quot;kwrd&quot;&gt;bool&lt;/span&gt; bNonDecreasing) &lt;/span&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/5944152920551423418/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/5944152920551423418' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5944152920551423418'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5944152920551423418'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/05/introduction-to-algorithms-reading_11.html' title='Introduction to Algorithms - The Reading Notes: Part 2, Insertion Sort'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-2242562665861181453</id><published>2011-05-09T11:43:00.001+02:00</published><updated>2011-05-09T11:46:38.129+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BookReading"/><category scheme="http://www.blogger.com/atom/ns#" term="ITA"/><title type='text'>Introduction to Algorithms - The Reading Notes: Part 1, Introduction</title><content type='html'>&lt;div xmlns=&#39;http://www.w3.org/1999/xhtml&#39;&gt;I started reading this book recently and I will share my understandings and thoughts with you. &lt;br/&gt;&lt;br/&gt;I will discuss the problems in the book and I&#39;d like to inject in particular more on the software engineering aspects of those algorithms: how to implement them so they can be used in a real-life software project to solve practical problems. &lt;br/&gt;&lt;br/&gt;Finally, I will code those algorithms in C++ and build the code under Fedora Linux with Scons. I will also adopt the TDD methodology (TDD stands for Test Driven Development) and use CppUnit for unit testing.&lt;br/&gt;&lt;br/&gt;Here are some useful links for this project: &lt;br/&gt;&lt;a href=&#39;http://www.manoli.net/csharpformat/&#39;&gt;The code formatter&lt;/a&gt;, it formats the source code into HTML so they look better when put in the blog. Currently it formats C#, VB and some other languages but not C/C++. I&#39;ll continue looking for one but before I find something, I&#39;ll use this one for the moment. &lt;br/&gt;&lt;br/&gt;&lt;a href=&#39;http://www.scons.org/doc/HTML/scons-user/index.html&#39;&gt;The SCons user&#39;s guide&lt;/a&gt;.&lt;br/&gt;&lt;br/&gt;&lt;a href=&#39;http://cppunit.sourceforge.net/doc/lastest/index.html&#39;&gt;The CppUnit Documentation.&lt;/a&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/2242562665861181453/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/2242562665861181453' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2242562665861181453'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2242562665861181453'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/05/introduction-to-algorithms-reading_09.html' title='Introduction to Algorithms - The Reading Notes: Part 1, Introduction'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-8831926516164505439</id><published>2011-05-07T16:37:00.000+02:00</published><updated>2011-05-07T16:37:42.192+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C/C++"/><title type='text'>A Real-life Story about Return by Reference - Part II</title><content type='html'>Now we know that we should follow some &quot;best practices&quot; when dealing with return by reference:&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue; font-family: Helvetica,Arial,Verdana,&#39;Trebuchet MS&#39;,sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue; font-family: Helvetica,Arial,Verdana,&#39;Trebuchet MS&#39;,sans-serif; font-size: 13px;&quot;&gt;1. Use toto aToto = getToto() or toto aToto(getToto()), don&#39;t write&amp;nbsp;toto aToto; aToto = getToto();&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue; font-family: Helvetica,Arial,Verdana,&#39;Trebuchet MS&#39;,sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue; font-family: Helvetica,Arial,Verdana,&#39;Trebuchet MS&#39;,sans-serif; font-size: 13px;&quot;&gt;2. Don&#39;t return a reference to a local variable, except when you know what you are doing&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue; font-family: Helvetica,Arial,Verdana,&#39;Trebuchet MS&#39;,sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue; font-family: Helvetica,Arial,Verdana,&#39;Trebuchet MS&#39;,sans-serif; font-size: 13px;&quot;&gt;3. Let member function return a reference to a member variable when the performance is really an issue, and call it with toto &amp;amp;aTotoRef = aToto.getTotoRef() to really take advantage of return by reference. Otherwise, return by value. Always take care of the variable holding the reference result, use it only during the life time of the object itself.&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue; font-family: Helvetica,Arial,Verdana,&#39;Trebuchet MS&#39;,sans-serif; font-size: 13px;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
There is one last thing you may want to know, that is the RVO does not work all the time, as a result it cost you one more construction/destruction of your object when you return it by value. I won&#39;t say it&#39;s a penalty you have to pay, it&#39;s just the fair cost you have to pay as the compiler can not help you more because your code asks for more. So when RVO does not work? Well, when the compiler can not decide which is the object it needs to return at compile time. Here is an example:&lt;br /&gt;
&lt;br /&gt;
toto getTotoWithParam(bool first)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (first)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; toto aToto;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return aToto;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; else&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; toto bToto;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return bToto;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Then, when calling this function, it will show you that the RVO is not apllied.&lt;br /&gt;
&lt;br /&gt;
toto a = getTotoWithParam(false);&lt;br /&gt;
toto constructor.&lt;br /&gt;
toto copy constructor.&lt;br /&gt;
toto destructor.&lt;br /&gt;
toto destructor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The object is constructed inside the function then used to copy construct the object holding the result.&amp;nbsp; Compared with the getToto() function, one more object (temporary object) is created.&lt;br /&gt;
&lt;br /&gt;
toto aToto = getToto():&lt;br /&gt;
toto constructor.&lt;br /&gt;
toto destructor.&lt;br /&gt;
&lt;br /&gt;
So that all about the return by value/reference that I want to tell you, I hope you now know what to do given a certain situation.&lt;br /&gt;
&lt;br /&gt;
Good luck and I&#39;d like to know what you are thinking about this.</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/8831926516164505439/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/8831926516164505439' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8831926516164505439'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8831926516164505439'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/05/real-life-story-about-return-by.html' title='A Real-life Story about Return by Reference - Part II'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-5499010407820789363</id><published>2011-04-24T23:39:00.001+02:00</published><updated>2015-06-02T21:51:39.826+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BookReading"/><title type='text'>Books to read (once more)</title><content type='html'>&lt;b&gt;Mathematics, Data Structures &amp;amp; Algorithms&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Discrete-Mathematics-Applications-Kenneth-Rosen/dp/0073229725?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Discrete Mathematics and Its Applications&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0073229725&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Fifth Edition), Kenneth H.Rosen&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Concrete-Mathematics-Foundation-Computer-Science/dp/0201558025?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Concrete Mathematics : A Foundation for Computer Science&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201558025&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Second Edition), Ronald L. Graham / Donald E. Knuth / Oren Patashnik&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Introduction-Algorithms-Second-Thomas-Cormen/dp/0262032937?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Introduction to Algorithms (Second Edition)&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0262032937&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;, Thomas H. Cormen / Charles E. Leiserson / Ronald L. Rivest / Clifford Stein&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Data-Structures-Algorithms-Applications-C/dp/0071155953?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Data Structures, Algorithms, and Applications in C++&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0071155953&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;， Sartaj Sahni &lt;br /&gt;
Data Structures &amp;amp; Program Design In C (Second Edition), Robert Kruse / C.L. Tondo / Bruce Leung&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Art-Computer-Programming-Volumes-Boxed/dp/0201485419?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;The Art of Computer Programming&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201485419&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; &lt;br /&gt;
Donald E.Knuth &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Compiler&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Compilers-Principles-Techniques-Tools-2nd/dp/0321486811?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Compilers: Principles, Techniques, and Tools&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0321486811&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (2nd Edition)&lt;br /&gt;
[Alfred V. Aho / Ravi Sethi / Jeffrey D. Ullman]&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Advanced-Compiler-Design-Implementation-Muchnick/dp/8178672413?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Advanced Compiler Design and Implementation&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=8178672413&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
[Steven S. Muchnick]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Operating System &lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Operating-Concepts-Seventh-Abraham-Silberschatz/dp/0471694665?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Operating System Concepts&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0471694665&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Now we have the 7th Edition)&lt;br /&gt;
[Abraham Silberschatz / Peter Baer Galvin / Greg Gagne]&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Operating-Systems-Design-Implementation-3rd/dp/0131429388?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Operating Systems : Design and Implementation&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0131429388&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Now we have the 3rd Edition)&lt;br /&gt;
[Andrew S. Tanenbaum / Albert S. Woodhull]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;C++ &lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326965?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;C++ Primer &lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0672326965&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Lippman / Josee Lajoie]&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/C-Programming-Language-Special/dp/0201700735?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;The C++ Programming Language (Special Edition)&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201700735&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
[Bjarne Stroustrup]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Effective-Specific-Improve-Programs-Designs/dp/0321334876?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Effective C++&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0321334876&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt; (Now we have the 3rd Edition)&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/More-Effective-Improve-Programs-Designs/dp/020163371X?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;More Effective C++&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=020163371X&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Scott Meyers&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Gotchas-Avoiding-Common-Problems-Coding/dp/0321125185?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;C++ Gotchas: Avoiding Common Problems in Coding and Design&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0321125185&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Stephen C.Dewhurst&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Modern-Design-Generic-Programming-Patterns/dp/0201704315?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;Modern C++ Design: Generic Programming and Design Patterns Applied&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0201704315&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Andrei Alexandrescu&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;ASM&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/Art-Assembly-Language-Randall-Hyde/dp/1593272073?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;The Art of Assembly Language&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=1593272073&quot; height=&quot;1&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
Randall Hyde&amp;nbsp; (x86)&lt;br /&gt;
&lt;br /&gt;
Introduction to 80x86 Assembly Language and Computer Architecture&lt;br /&gt;
Richard C. Detmer&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;UNIX Programming&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Advanced Programming in the UNIX Environment&lt;br /&gt;
[W. Richard Stevens]&lt;br /&gt;
&lt;br /&gt;
The UNIX Programming Environment&lt;br /&gt;
[Brianw. Kernighan]&lt;br /&gt;
&lt;br /&gt;
UNIX Network Programming, Volume 1 : The Sockets Networking API (Third Edition)&lt;br /&gt;
[W. Richard Stevens / Bill Fenner / Andrew M. Rudoff]&lt;br /&gt;
&lt;br /&gt;
UNIX Network Programming Volume 2 : Interprocess Communications (Second Edition)&lt;br /&gt;
[W. Richard Stevens]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Computer Networks&amp;nbsp; &lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Computer Networks (Fourth Edition)&lt;br /&gt;
[Andrew S. Tanenbaum]&lt;br /&gt;
&lt;br /&gt;
TCP/IP Illustrated, Volume 1 : The Protocols&lt;br /&gt;
TCP/IP Illustrated, Volume 2 : The Implementation&lt;br /&gt;
TCP/IP Illstrated, Volume 3 : TCP for Transactions, HTTP, NNTP, and the UNIX Domain Protocols&lt;br /&gt;
[W. Richard Stevens]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Internetworking with TCP/IP Vol I : Principles, Protocols, and Architecture (Third Edition)&lt;br /&gt;
Internetworking with TCP/IP Vol II : Design, Implementation, and Internals (Second Edition)&lt;br /&gt;
Internetworking with TCP/IP Vol III : Client-Server Programming and Applications, BSD Socket Version (Second Edition)&lt;br /&gt;
Internetworking with TCP/IP Vol III : Client-Server Programming and Applications, Windows Sockets Version&lt;br /&gt;
[Douglas E. Comer / David L. Stevens]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Database&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
An Introduction to Database Systems (Seventh Edition)&lt;br /&gt;
[C. J.Date]&lt;br /&gt;
&lt;br /&gt;
Database System Concepts (Fourth Edition)&lt;br /&gt;
[Abraham Silberschat / Henry F.Korth / S.Sudarshan]&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Software Engineering&lt;/b&gt; &lt;br /&gt;
&lt;br /&gt;
Code Complete， Steven C. McConnell&lt;br /&gt;
&lt;br /&gt;
Pragmatic programmer: from journeyman to master， &lt;span class=&quot;ptBrand&quot;&gt;Andrew Hunt and David Thomas&lt;/span&gt;&lt;br /&gt;
&lt;span class=&quot;ptBrand&quot;&gt;Peopleware：Productive Projects and Teams, Tom DeMarco, Timothy Lister &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&amp;nbsp;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/5499010407820789363/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/5499010407820789363' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5499010407820789363'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5499010407820789363'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/04/books-to-read-once-more.html' title='Books to read (once more)'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-8426439520641253889</id><published>2011-03-09T08:26:00.000+01:00</published><updated>2011-03-09T08:26:14.793+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C/C++"/><title type='text'>A Real-life Story about Return by Reference - Part I</title><content type='html'>Recently I did some new development on our software. As a standard process, I submitted my work to a code review. One of my colleagues suggested me, for one of my method, using a reference as the return type instead of an object, for example:&lt;br /&gt;
class toto ()&lt;br /&gt;
...&lt;br /&gt;
&lt;br /&gt;
toto &amp;nbsp; &amp;nbsp;getToto();&lt;br /&gt;
toto &amp;amp;getToto();&lt;br /&gt;
&lt;br /&gt;
His concern is the that return by reference is more efficient than return by value. Personally, I do not think much about efficiency but more on the correctness and clearness of the code, so for a long time, I return everything by value except for very rare cases such as overloading the operator &amp;lt;&amp;lt; , etc. His suggestion triggered me to investigate more this point.&lt;br /&gt;
&lt;br /&gt;
Let&#39;s consider return by value first so we have something to compare with. So how to write a function that returns an object by value? Well it&#39;s simple.&lt;br /&gt;
toto getToto()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; toto myToto;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; // Here I can work with myToto.&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; return myToto;&lt;br /&gt;
}&lt;br /&gt;
Or if I really have very simple work to do with the myToto, I can write also:&lt;br /&gt;
toto getToto()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp; return toto();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Basically, we have the following ways to use the functions:&lt;br /&gt;
toto aToto = getToto(); &amp;nbsp;// get a toto and hold it with aToto&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto aToto(getToto()); &amp;nbsp; // construct aToto with the result of getToto with its copy constructor.&lt;/div&gt;getToto(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// get a toto and do nothing with it. Not useful by valid call.&lt;br /&gt;
toto aToto; aToto = getToto(); // assign the result of getToto to an existing object.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;That is not all the story yet, we still have another way to call the getToto:&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;const toto &amp;amp;aRefToto = getToto();&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;What we are doing here is to bind a reference to a constant to the result of getToto, which is a temporary object so we are not allowed to call it without the &#39;const&#39; like:&amp;nbsp;&amp;nbsp;toto &amp;amp;aRefToto = getToto(); (at least we cannot do it now with most modern compilers. ).&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
So which one is more efficient ? It depends on the compiler.&amp;nbsp;Actually, this is a feature usually named as RVO, Return Value Optimization. C++ standard leaves this to the implementation of the compiler. Modern compilers such as gcc does quite well so all the first three examples and the last one have the same best performance: only one object is created in the function, so the constructor and destructor get called only once. Only the fourth example cost more, which is caused by the assignment.&lt;br /&gt;
&lt;br /&gt;
So here is the output of a test program:&lt;br /&gt;
toto aToto = getToto():&lt;br /&gt;
toto constructor.&lt;br /&gt;
toto destructor.&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto aToto(getToto()); &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;getToto():&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
toto aToto; aToto = getToto();&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto assignment.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;const toto &amp;amp;aRefToto = getToto();&lt;/div&gt;toto constructor.&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;It is amazing to know that when you call&amp;nbsp;toto aToto = getToto() or&amp;nbsp;toto aToto(getToto()), you have actually zero overhead, the constructor and destructor are called only once for the object that is created inside the function. Also it&#39;s amazing to know the difference (the penalty you are paying) when you write something like:&amp;nbsp;toto aToto; aToto = getToto(); once more constructor and destructor and one more assignment!&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;CONCLUSION ONE: Use toto aToto = getToto() or toto aToto(getToto()), don&#39;t write&amp;nbsp;toto aToto; aToto = getToto();&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Now let&#39;s consider the cases when we return by reference.&lt;br /&gt;
Things are a little different when it comes to return by reference. Obviously, you are not allowed to return the reference of a temporary object, so the following form is invalid:&lt;br /&gt;
toto &amp;amp;getTotoRef()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return toto();&lt;br /&gt;
}&lt;br /&gt;
Now we only have on possibility to return a reference of an object:&lt;br /&gt;
toto &amp;amp;getTotoRef()&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;toto aToto;&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return aToto;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
As far as you remember that the reference is &quot;just another name of the referred object&quot;, you may notice that &amp;nbsp;we are doing a&amp;nbsp;dangerous thing here by returning a reference of a local object. Absolutely, you are right. A decent compiler will warn you that you are returning a reference to a local variable. Personally, I never return reference to a local object. This is just to demonstrate the efficiency aspect.&lt;br /&gt;
&lt;br /&gt;
So here we have three ways to use the getTotoRef().&lt;br /&gt;
toto &amp;amp;aRefToto = getTotoRef(); // Don&#39;t do like this!&lt;br /&gt;
toto aToto = getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// construct with result of getTotoRef()&lt;br /&gt;
toto aToto(getTotoRef()); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// construct with copy constructor.&lt;br /&gt;
getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// useless, but valid.&lt;br /&gt;
toto aToto; aToto = getTotoRef(); // assign result of getTotoRef() to an existing variable.&lt;br /&gt;
&lt;br /&gt;
Here is the output of the call sequence when the test program runs:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto &amp;amp;aRefToto = getTotoRef():&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto aToto = getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto copy constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;toto aToto(getTotoRef()); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto copy constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;toto aToto; aToto = getTotoRef();&amp;nbsp;&lt;/div&gt;toto constructor.&lt;br /&gt;
toto constructor.&lt;br /&gt;
toto destructor.&lt;br /&gt;
toto assignment.&lt;br /&gt;
toto destructor.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
So if you use a reference to hold the result of getTotoRef, you have the maximal gain of return by reference: there is no overhead at all. Otherwise, either the copy constructor or the assignment need to be called to construct a new object based on the result of getTotoRef. &amp;nbsp;However, with the first form which is most efficient here, you are bound to have unexpected&amp;nbsp;behavior&amp;nbsp;later when you want to use your reference so don&#39;t do that.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;CONCLUSION TWO: Don&#39;t return a reference to a local variable, except when you know what you are doing :D&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
So why we have this feature to return a reference in C++? Well, this allows you to do something like:&lt;br /&gt;
toto &amp;amp;forwardToto(toto &amp;amp;comingToto)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;return comingToto;&lt;br /&gt;
}&lt;br /&gt;
So here you don&#39;t need to create any object. The interest here is that if a function returns a reference, it can appear at the left side of an operator, so the following is valid:&lt;br /&gt;
forwardToto() = aToto;&lt;br /&gt;
And opens the possibility to do something like cout &amp;lt;&amp;lt; &quot;blabla&quot; &amp;lt;&amp;lt; endl; because &amp;lt;&amp;lt; is overloaded to return a reference. For more on how to overload the operator &amp;lt;&amp;lt;, you should take a/another look at the great book by Prata: &lt;a href=&quot;http://www.amazon.com/Primer-Plus-5th-Stephen-Prata/dp/0672326973?ie=UTF8&amp;amp;tag=aprsnida-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&quot; target=&quot;_blank&quot;&gt;C++ Primer Plus (5th Edition)&lt;/a&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; height=&quot;1&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=aprsnida-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=0672326973&quot; style=&quot;border: none !important; margin: 0px !important; padding: 0px !important;&quot; width=&quot;1&quot; /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
We can also profit this feature in a member function of a class, but with a lot of care. So think about the tradeoff first.&lt;br /&gt;
Suppose we have a class with a member variable, and we have a getter of this variable, so we can make the getter return a reference, like:&lt;br /&gt;
class totoClass&lt;br /&gt;
{&lt;br /&gt;
public:&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;toto &amp;amp;getTotoRef();&lt;br /&gt;
&amp;nbsp;&amp;nbsp; &amp;nbsp;_myToto;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Then you have the following ways to use this getter:&lt;br /&gt;
toto bToto = aToto.getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Construct bToto.&lt;br /&gt;
toto bToto(aToto.getTotoRef()); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;// Construct bToto.&lt;br /&gt;
toto &amp;amp;bToto = aToto.getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; // bToto refers to aToto._myToto.&lt;br /&gt;
bToto = aToto.getTotoRef(); &amp;nbsp; // assign aToto._myToto to an existing bToto.&lt;br /&gt;
&lt;br /&gt;
What do we pay for each call? Here is the result:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto bToto = aToto.getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto copy constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto bToto(aToto.getTotoRef()); &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto copy constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto &amp;amp;bToto = aToto.getTotoRef(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;** Nothing is called.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;bToto = aToto.getTotoRef(); &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto assignment.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;OK, not bad, we call once the copy constructor when we want to save the result in another object, fair enough. If we only get the reference as in the third example, we don&#39;t need to call any toto constructor/copy constructor/assignment/destructor. That is a big saving if these operations are costly themselves. However, you can do this&amp;nbsp;***only if you are able to make sure that your totoClass object lives longer than your variable holding the result.&amp;nbsp;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
In comparison, tradition getters that return a value is not too bad. Here is what we have:&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;class totoClass&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;{&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;public:&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;toto getToto(); // get by value.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;_myToto;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;}&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto bToto = aToto.getToto()&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto copy constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto bToto(aToto.getToto()); &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto copy constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;const toto &amp;amp;bToto = aToto.getToto(); &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto copy constructor.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;toto destructor.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;bToto = aToto.getToto(); &amp;nbsp;&amp;nbsp;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;div&gt;toto copy constructor.&lt;/div&gt;&lt;div&gt;toto assignment.&lt;/div&gt;&lt;div&gt;toto destructor.&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;So the penalty is at least one copy constructor need to be called. We no longer have the amazing gain when we have with ordinary functions that return a local object by value.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;CONCLUSION THREE: Let member function return a reference to a member variable when the performance is really an issue, and call it with toto &amp;amp;aTotoRef = aToto.getTotoRef() to really take advantage of return by reference. Otherwise, return by value. Always take care of the variable holding the reference result, use it only during the life time of the object itself.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Hope this make things clearer, then we can investigate even more in the next part, yes, the story does not end here.&lt;br /&gt;
&lt;br /&gt;
Like to know what you are thinking about this.&amp;nbsp;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/8426439520641253889/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/8426439520641253889' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8426439520641253889'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/8426439520641253889'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2011/03/real-life-story-about-return-by.html' title='A Real-life Story about Return by Reference - Part I'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-2100031552242273209</id><published>2010-11-15T15:53:00.001+01:00</published><updated>2010-11-15T15:53:18.984+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Vim"/><title type='text'>Vim Tabs</title><content type='html'>If you are not used with the buffer concept as the buffers do not necessarily visible, and you also want to focus on the current file, you have the tabs in Vim just as you have in Editplus or Notepad++ or most of modern text editors. Here is my vim with two tabs each holding its own windows. Yes, it&#39;s important that each tab can hold multiple windows, isn&#39;t it cool or what!&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2ZBZt2r7mjIADCAwzcoOu8b20wX8VMz8NmbbtBfdnSvu-g-4B9Ui0i-l2IxKFN7jx1RuQ_PSMIQTG5coIsamIQT_1OsJbBnyRUa6FNyu8Ue6-xlrE5XimRpMjLmsiE2iL9SO0S1QkSAg/s1600/vim2.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;212&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2ZBZt2r7mjIADCAwzcoOu8b20wX8VMz8NmbbtBfdnSvu-g-4B9Ui0i-l2IxKFN7jx1RuQ_PSMIQTG5coIsamIQT_1OsJbBnyRUa6FNyu8Ue6-xlrE5XimRpMjLmsiE2iL9SO0S1QkSAg/s400/vim2.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
&lt;br /&gt;
Here is how you can work with tabs in Vim.&lt;br /&gt;
&lt;br /&gt;
Create a new tab:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;- vim -p file1 file2... : Open multiple files each in one tab.&lt;/li&gt;
&lt;li&gt;- :tabnew [filename] : Crate a new tab, empty or possibly open a file at the same time.&lt;/li&gt;
&lt;li&gt;- Ctrl-w T : If you have multiple windows opened in one tab, use this command to put the current window into a new tab.&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;br /&gt;
Navigate between tabs:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;- :tabs : list all opened tabs&lt;/li&gt;
&lt;li&gt;- :tabn, :tabp: go to next or previous tab. Less convenient.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;- gt : go to next tab. Better.&lt;/li&gt;
&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/2100031552242273209/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/2100031552242273209' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2100031552242273209'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/2100031552242273209'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2010/11/vim-tabs.html' title='Vim Tabs'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj2ZBZt2r7mjIADCAwzcoOu8b20wX8VMz8NmbbtBfdnSvu-g-4B9Ui0i-l2IxKFN7jx1RuQ_PSMIQTG5coIsamIQT_1OsJbBnyRUa6FNyu8Ue6-xlrE5XimRpMjLmsiE2iL9SO0S1QkSAg/s72-c/vim2.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-1035072821400660768</id><published>2010-11-15T15:32:00.002+01:00</published><updated>2010-11-15T15:34:57.078+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="Vim"/><title type='text'>Vim Windows</title><content type='html'>Vim is able to show your files in separated windows which is very handy in many situation. For example, if you are a programmer, very often you need to look at a .cpp file and its header .h at the same time. Or if you are simply writing some text and need to refer to certain part of the same file that can not fill in one screen because your text is quite long.&lt;br /&gt;
&lt;br /&gt;
Then it&#39;s the time to think about multiple windows and it will helps a lot. Here is how vim looks like with multiple windows.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX1BqESBj2Iz8-XOjtn5vKJFuRu8XV7evJy6Ymw5ZicJWRBwKJX1OGjzd81j3xGX0od9dGRHPwIEFfwBKmdi3hBSL1VTsiO9pU3Hl59KNwwDM2uvoaoUaAiHKpUuJyz7Ijgu-HTE4E9gU/s1600/vim1.JPG&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;170&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX1BqESBj2Iz8-XOjtn5vKJFuRu8XV7evJy6Ymw5ZicJWRBwKJX1OGjzd81j3xGX0od9dGRHPwIEFfwBKmdi3hBSL1VTsiO9pU3Hl59KNwwDM2uvoaoUaAiHKpUuJyz7Ijgu-HTE4E9gU/s400/vim1.JPG&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;
Basically, here I created three windows, left top, left bottom and the right one. Several interesting thing to mention here. First, as you can see, we are able to create vertically or horizontally separated windows. Then, we are able to crate multiple windows containing the same file. Finally, we have some important information for each window, the file name, the index of the buffer (the number before the file name), etc.&lt;br /&gt;
&lt;br /&gt;
Here is how I use multiple windows.&lt;br /&gt;
To create new window:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;- Ctrl-w s &amp;nbsp;will split the current window into two containing the same file.&lt;/li&gt;
&lt;li&gt;- :sp same as above.&lt;/li&gt;
&lt;li&gt;- :sp filename will create a new window with the file filename.&lt;/li&gt;
&lt;li&gt;- Ctrl-w v&amp;nbsp;Split the current window vertically.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;- :vs same as above&lt;/li&gt;
&lt;li&gt;- :vs filename Vertical split with file filename.&lt;/li&gt;
&lt;/ul&gt;To move between windows:&amp;nbsp;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: red;&quot;&gt;All commands here start with Ctrl-w.&lt;/span&gt;&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;- h,j,k,l: &amp;nbsp;Navigate: same as move the cursor.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;- x: Switch positions of two neighbor windows.&lt;/li&gt;
&lt;li&gt;- p: Go to previous windows, particularly useful when browsing between .h and .c files.&lt;/li&gt;
&lt;/ul&gt;Close windows:&lt;br /&gt;
&lt;ul&gt;&lt;li&gt;- Ctrl-w c or :q&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/1035072821400660768/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/1035072821400660768' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/1035072821400660768'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/1035072821400660768'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2010/11/vim-windows.html' title='Vim Windows'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX1BqESBj2Iz8-XOjtn5vKJFuRu8XV7evJy6Ymw5ZicJWRBwKJX1OGjzd81j3xGX0od9dGRHPwIEFfwBKmdi3hBSL1VTsiO9pU3Hl59KNwwDM2uvoaoUaAiHKpUuJyz7Ijgu-HTE4E9gU/s72-c/vim1.JPG" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-1424755309064630712</id><published>2010-11-14T22:49:00.000+01:00</published><updated>2010-11-14T22:49:31.571+01:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="software"/><title type='text'>My Favorite Software on Windows</title><content type='html'>I reinstalled my PC this weekend as you all know that Windows tend to slow down after some time.&lt;br /&gt;
&lt;br /&gt;
I take this opportunity to make a collection of my favorite software on a Windows PC.&lt;br /&gt;
&lt;br /&gt;
7-Zip&lt;br /&gt;
Adobe Reader&lt;br /&gt;
Google Chrome&lt;br /&gt;
Mozilla Firefox&lt;br /&gt;
Skype&lt;br /&gt;
PPTV&lt;br /&gt;
SPlayer&lt;br /&gt;
MKPlayer&lt;br /&gt;
Sogou Chinese Input Method&lt;br /&gt;
Thunder Download&lt;br /&gt;
&lt;br /&gt;
...&lt;br /&gt;
To continue.</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/1424755309064630712/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/1424755309064630712' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/1424755309064630712'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/1424755309064630712'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2010/11/my-favorite-software-on-windows.html' title='My Favorite Software on Windows'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-5210754944259635892</id><published>2010-10-10T09:02:00.002+02:00</published><updated>2010-10-10T09:15:28.531+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C/C++"/><category scheme="http://www.blogger.com/atom/ns#" term="DotNet"/><title type='text'>Problem with System.Windows.Forms.WebBrowser</title><content type='html'>I used a lot System.Windows.Forms.WebBrowser in my LSSaver project to process the pages. Recently I got a feed back that LSSaver stops to load the blog titles and is blocked there. The problem seems to happen on a WinXp + IE6 platform. &lt;br /&gt;
&lt;br /&gt;
After investigation, the problem comes from a null pointer exception, but why there is a null pointer? A step by step debug showed me that when using WebBrowser’s DOM to retrieve certain HTML element, the HtmlElement.NextSibling returns an invalid pointer even when there is no more sibling of the calling HtmlElement object. It seems that this is a known issue in the .NET framework:&lt;br /&gt;
&lt;a href=&quot;http://connect.microsoft.com/VisualStudio/feedback/details/304466/htmlelement-nextsibling-returns-incorrect-value&quot;&gt;http://connect.microsoft.com/VisualStudio/feedback/details/304466/htmlelement-nextsibling-returns-incorrect-value&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Anyway, I won’t rely on the NextSibling is null or not, I get the number of children from the parent, then rely on this count when traversing all the children elements. The problem is fixed.</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/5210754944259635892/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/5210754944259635892' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5210754944259635892'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5210754944259635892'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2010/10/problem-with-systemwindowsformswebbrows.html' title='Problem with System.Windows.Forms.WebBrowser'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-5033429047695030133</id><published>2010-09-30T22:55:00.002+02:00</published><updated>2010-09-30T22:55:29.231+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C/C++"/><category scheme="http://www.blogger.com/atom/ns#" term="STL"/><title type='text'>Overloading operator &lt;&lt; for containers</title><content type='html'>Today, I saw a line of code in one of our cpp files, it looks like this: &lt;br /&gt;
marshaller &amp;lt;&amp;lt; obj_list;&lt;br /&gt;
what  interesting is that obj_list is an instance of type  std::vector&lt;std::string&gt;, and one of my colleagues asked me if he  can do similar things with an instance of type std::map&amp;lt;&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
The  answer is &quot;I don&#39;t know&quot;, because it depends on if the operator  &amp;lt;&amp;lt; is overloaded for the marshaller type for std::map&amp;lt;&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
The  secret why &quot;marshaller &amp;lt;&amp;lt; obj_list;&quot; works is that, the  marshaller type has an overloaded operator &amp;lt;&amp;lt; for  std::vector&lt;std::string&gt;. Something looks like this, assuming  marshaller has type T:&lt;br /&gt;
T&amp;nbsp; &amp;amp;operator &amp;lt;&amp;lt; (T &amp;amp;t, const vector &lt;string&gt; avector){...}&lt;br /&gt;
&lt;br /&gt;
Here is an example with cout:&lt;br /&gt;
ostream &amp;amp;operator &amp;lt;&amp;lt; (ostream &amp;amp;out, const vector &lt;string&gt; avector)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; vector &lt;string&gt;::const_iterator itr = avector.begin();&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for (; itr != avector.end(); ++itr)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out &amp;lt;&amp;lt; (*itr);&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
int main(void)&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; vector &lt;string&gt; str_vector;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stringstream str_stream;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; str_stream &amp;lt;&amp;lt; i;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; str_vector.push_back(str_stream.str());&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; cout &amp;lt;&amp;lt; str_vector;&lt;br /&gt;
}&lt;br /&gt;
The output is:&lt;br /&gt;
0123456789&lt;br /&gt;
&lt;br /&gt;
So for this works with std::map&amp;lt;&amp;gt;, we need to overload &amp;lt;&amp;lt; for std::map&amp;lt;&amp;gt;.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Finally, it turned out that there is no such an overload, unfortunately. So we have to do it for each pare in the map.&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/string&gt;&lt;/std::string&gt;&lt;/std::string&gt;</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/5033429047695030133/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/5033429047695030133' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5033429047695030133'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5033429047695030133'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2010/09/overloading-operator-for-containers.html' title='Overloading operator &lt;&lt; for containers'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-4884525182270674189</id><published>2010-09-19T22:28:00.001+02:00</published><updated>2010-09-19T22:29:09.441+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="software"/><title type='text'>Do It in a Programmer&#39;s Way</title><content type='html'>DiPw, I&#39;d like to pronounce it like &quot;deepyou&quot;, is my preferred way of doing things, especially things related with my job as a programmer/developer.&lt;br /&gt;
&lt;br /&gt;
Recently, I was assigned a job to migrate documents from the old system to the new one, and this is to be done for each of our customers. In order to secure the migration, I need to check many configurations on both systems, they need to be similarly configured so the migration is transparent for the customers, ideally. What complicates things is also we have test systems and production systems, and the migration need to be done first on test systems then on production systems. So as a result, I have a lot of things to check and compare. Even worse, the settings are managed by several GUI applications so I have to click many times to find one setting on the old system then, do it on a different GUI on the new system. That is not only boring but also fault prone.&lt;br /&gt;
&lt;br /&gt;
A colleague of mine kindly told me that the content of the GUI applications are finally stored in the database and he also gave me some SQL to retrieve the values. That is a big help, however, I still have to manage two types of databases, Oracle and SqlLite.&lt;br /&gt;
&lt;br /&gt;
Why I have to do it manually? It&#39;s time consuming and error prone. Why not write a program doing all of these jobs for me? With always DiPw in head, I decided to write a Python program doing these boring jobs and finally it took me one day. Voila, now with the program, I retrieve all need information on all systems for a given customer in 20 minutes and when needed, I can retrieve most up-to-date information whenever I want in 20 minutes, this is impossible without the program. And when it comes the time to migrate another customer, I just run the program for this customer.&lt;br /&gt;
&lt;br /&gt;
Well, I admit that the job is not all about getting all the settings, but it is the most boring part, so let a program do it!</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/4884525182270674189/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/4884525182270674189' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4884525182270674189'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/4884525182270674189'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2010/09/do-it-in-programmers-way.html' title='Do It in a Programmer&#39;s Way'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7954696955252676474.post-5325655840814610790</id><published>2010-08-30T22:00:00.003+02:00</published><updated>2010-10-10T08:30:11.778+02:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="C/C++"/><title type='text'>A Real-life Story about Bad Type Cast</title><content type='html'>We have a problem today in our soft. That is, in short, we have 285 lines of text to display but only 231 lines are displayed. The investigation is not so easy because the text to be displayed is converted from structured message and the number of lines are calculated on the fly.&lt;br /&gt;
&lt;br /&gt;
Basically, there is a segment in the message that specifies the maximum allowed number of lines, the sender of the message has set it to 999, assuming it the maximum possible lines in the message.&lt;br /&gt;
&lt;br /&gt;
So if you are really a geek in programming C/C++, you may already know where is going wrong. Yes, an integer 999 when assigned to a unsigned char, the latter will have value 231. In more detail: binary presentation of decimal 999 is 1111100111, when assigned to unsigned char, we have only the lower 8 bits left:&amp;nbsp;11100111, it equals decimal value 231.&lt;br /&gt;
&lt;br /&gt;
The problem is quite clear, but it has been hard to find it out because it hides itself deep in one of the libraries our soft uses.&lt;br /&gt;
&lt;br /&gt;
Another interesting thing is that the library code uses atoi to convert the maximum allowed size in the message to an integer value, but with a parameter like this:&amp;nbsp;&quot;999blabla&quot;, that is the value in string followed by other&amp;nbsp;alphabetic characters.&amp;nbsp;atoi is designed to handle this correctly, take a look at the specification of atoi:&lt;br /&gt;
&lt;br /&gt;
int atoi ( const char * str );&lt;br /&gt;
Convert string to integer&lt;br /&gt;
Parses the C string str interpreting its content as an integral number, which is returned as an int value.&lt;br /&gt;
&lt;br /&gt;
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus orminus sign followed by as many numerical digits as possible, and interprets them as a numerical value.&lt;br /&gt;
&lt;br /&gt;
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.&lt;br /&gt;
&lt;br /&gt;
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.</content><link rel='replies' type='application/atom+xml' href='http://hanbingsy.blogspot.com/feeds/5325655840814610790/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/7954696955252676474/5325655840814610790' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5325655840814610790'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7954696955252676474/posts/default/5325655840814610790'/><link rel='alternate' type='text/html' href='http://hanbingsy.blogspot.com/2010/08/real-life-story-about-bad-type-cast.html' title='A Real-life Story about Bad Type Cast'/><author><name>icequake</name><uri>http://www.blogger.com/profile/05921341689788283829</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>