Alexander Stepanov Notes on Programming 10/3/2006 Preface
Total Page:16
File Type:pdf, Size:1020Kb
Alexander Stepanov Notes on Programming 10/3/2006 Preface.............................................................................................................................. 2 Lecture 1. Introduction .............................................................................................. 3 Lecture 2. Designing fvector_int ....................................................................... 8 Lecture 3. Continuing with fvector_int ......................................................... 24 Lecture 4. Implementing swap.............................................................................. 36 Lecture 5. Types and type functions .................................................................. 43 Lecture 6. Regular types and equality............................................................... 51 Lecture 7. Ordering and related algorithms .................................................... 56 Lecture 8. Order selection of up to 5 objects ................................................. 64 Lecture 9. Function objects.................................................................................... 69 Lecture 10. Generic algorithms ............................................................................ 78 10.1. Absolute value ............................................................................................. 78 10.2. Greatest common divisor........................................................................ 83 10.2.1. Euclid’s algorithm............................................................................... 83 10.2.2. Stein’s algorithm ................................................................................ 88 10.3. Exponentiation............................................................................................. 94 Lecture 11. Locations and addresses ............................................................... 110 Lecture 12. Actions and their orbits ................................................................. 113 Lecture 13. Iterators............................................................................................... 129 Lecture 14. Elementary optimizations ............................................................. 136 Lecture 15. Iterator type-functions .................................................................. 139 Lecture 16. Equality of ranges and copying algorithms ........................... 139 Lecture 17. Permutation algorithms................................................................. 139 Lecture 18. Reverse ................................................................................................ 143 Lecture 19. Rotate ................................................................................................... 154 Lecture 20. Partition ............................................................................................... 167 Lecture 21. Optimizing partition ........................................................................ 178 Lecture 22. Algorithms on Linked Iterators................................................... 183 Lecture 23. Stable partition ................................................................................. 192 Lecture 24. Reduction and balanced reduction............................................ 199 Lecture 25. 3-partition........................................................................................... 207 Lecture 26. Finding the partition point............................................................ 211 Lecture 27. Conclusions......................................................................................... 215 1 Alexander Stepanov Notes on Programming 10/3/2006 Preface This is a selection from the notes that I have used in teaching programming courses at SGI and Adobe over the last 10 years. (Some of the material goes back even further to the courses I taught in the 80s at Polytechnic University.) The purpose of these courses was to teach experienced engineers to design better interfaces and reason about code. In general, the book presupposes a certain fluency in computer science and some familiarity with C++. This book does not present a scholarly consensus. It presents my personal opinions and should, therefore, be taken with a grain of salt. Programming is a wonderful activity that goes well beyond the range of what a single programmer can experience in a lifetime. This is not a book about C++. Although it uses C++ and would be difficult to write the focus is on programming rather than programming language. This is not a book about STL. I often refer to STL as a source of examples both good and (more often than I would like) bad. This book will not help one become a fluent user of STL, but it explains the principles used to design STL. This book does not attempt to solve complicated problems. It will attempt to solve very simple problems which most people find trivial: minimum and maximum, linear search and swap. These problems are not, however, as simple as they seem. I have been forced to go back and revisit my view of them many times. And I am not alone. I often have to argue about different aspects of the interface and implementation of such simple functions with my old friends and collaborators. There is more to it, than many people think. I do understand that most people have to design systems somewhat more complex than maximum and minimum. But I urge them to consider the following: unless they can design a three line program well, why would they be able to design a three hundred thousand line program. We have to build our design skills by following through simple exercises, the way a pianist has to work through simple finger exercises before attempting to play a complicated piece. This book would never have been written without the constant encouragement of Sean Parent, who has been my manager for the last three years. Paul McJones and Mark Ruzon had been reviewing every single page of every single version of the notes and came up with many major improvements. I was also helped by many others who assisted me in developing my courses and writing the notes. I have to mention especially the following: Dave Musser, Jim Dehnert, John Wilkinson, John Banning, Greg Gilley, Mat Marcus, Russell Williams, Scott Byer, Seetharaman Narayanan, Vineet Batra, Martin Newell, Jon Brandt, Lubomir Bourdev, Scott Cohen. (Names are listed in a roughly chronological order of appearance.) It is to them and to my many other students who had to suffer for years through my attempts to understand how to program that I dedicate my book. 2 Alexander Stepanov Notes on Programming 10/3/2006 Lecture 1. Introduction I have been programming for over 30 years. I wrote my first program in 1969 and became a full time programmer in 1972. My first major project was writing a debugger. I spent two whole months writing it. It almost worked. Sadly, it had some fundamental design flaws. I had to throw away all the code and write it again from scratch. Then I had to put hundreds of patches onto the code, but eventually I made it work. For several more years I stuck to this process: writing a huge blob of code and then putting lots of patches to make it work. My management1 was very happy with me. In 3 years I had 4 promotions and at the age of 25 obtained a title of a Senior Researcher – much earlier than all of my college friends. Life seemed so good. By the end of 1975 my youthful happiness was permanently lost. The belief that I was a great programmer was shattered. (For better or for worse, I never regained the belief. Since that time I have been a perplexed programmer searching for a guide. This book is an attempt to share some of the things I learned during my quest.) The first idea was a result of reading the works of the Structured Programming School: Dijkstra, Wirth, Hoare, Dahl. By 1975 I became a fanatical disciple. I read every book and paper authored by the giants. I was, however, saddened by the fact that I could not follow their advice. I had to write my code in assembly language and had to use goto statement. I was so ashamed. And then in the beginning of 1976 I had my first revelation: the ideas of the Structured Programming had nothing to do with the language. One could write beautiful code even in assembly. And if I could, I must. (After all I reached the top of the technical ladder and had to either aspire to something unattainable or go into management.) I decided that I will use my new insight while doing my next project: implementing an assembler. Specifically I decided to use the following principles: 1. the code should be partitioned into functions; 2. every function should be most 20 lines of code; 3. functions should not depend on the global state but only on the arguments; 4. every function is either general or application specific, where general function is useful to other applications; 5. every function that could be made general – should be made general; 6. the interface to every function should be documented; 7. the global state should be documented by describing both semantics of individual variables and the global invariants. The result of my experiment was quite astonishing. The code did not contain serious bugs. There were typos: I had to change AND to OR, etc. But I did not need patches. And over 95% of the code was in general functions! I felt quite proud. There remained a problem that I could not yet precisely figure out what it meant that a function was 1 Natalya Davydovskaya, Ilya Neistadt and Aleksandr Gurevich – these