Code Examples
  Home arrow Code Examples arrow Style Case Studies: Index Tables
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Style Case Studies: Index Tables
By: Addison-Wesley/Prentice Hall PTR
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 14
    2004-09-01

    Table of Contents:
  • Style Case Studies: Index Tables
  • Solution to Item 34: Dissecting Index Tables
  • Correcting Mechanical Errors
  • Improving Style
  • Reuse and Preincrement
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Style Case Studies: Index Tables


    (Page 1 of 6 )

    This book evaluates style versus structure in C++ programming, emphasizing using the C++ standard library effectively. This item on index tables focuses on how to implement them not just effectively but exceptionally. (This book excerpt is from Exceptional C++ Style by Herb Sutter, ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional.)

    Editor's Note: This book presents puzzles or problems. A question is posed by JG, a term for new junior-grade military officer, and an answer from a Guru.


    Item 34. Index Tables

    sutterIndex tables are a genuinely useful idiom and a technique that’s worth being aware of. But how can we implement the technique effectively… nay, even better than that, exceptionally?

    JG Question

    1. Who benefits from clear, understandable code?

    Guru Question

    2. The following code presents an interesting and genuinely useful idiom for creating index tables into existing containers. For a more detailed explanation, see the original article [Hicks00].

    Critique this code and identify:

    a) Mechanical errors, such as invalid syntax or nonportable conventions.

    b) Stylistic improvements that would improve code clarity, reusability, and maintainability.

    // program sort_idxtbl(…) to make a permuted array of indices
    #include <vector>
    #include <algorith>
    template <class RAIter>
    struct sort_idxtbl_pair
    {
      RAIter it;
      int i;
      bool operator<( const sort_idxtbl_pair& s )
        { return (*it) < (*(s.it)); }
      void set( const RAIter& _it, int _i ) { it=_it; i=_i; }
      sort_idxtbl_pair() {}
    };
    template <class RAIter>
    void sort_idxtbl( RAIter first, RAIter last, int* pidxtbl )
    {
      int iDst = last-first;
      typedef std::vector< sort_idxtbl_pair<RAIter> > V;
      V v( iDst );
      int i=0;
      RAIter it = first;
      V::iterator vit = v.begin();
      for( i=0; it<last; it++, vit++, i++ )
        (*vit).set(it,i);
      std::sort(v.begin(), v.end());
      int *pi = pidxtbl;
      vit = v.begin();
      for( ; vit<v.end(); pi++, vit++ )
        *pi = (*vit).i;
    }
    main()
    {
      int ai[10] = { 15,12,13,14,18,11,10,17,16,19 };
      cout << “#################” << endl;
      std::vector<int> vecai(ai, ai+10);
      int aidxtbl[10];
      sort_idxtbl(vecai.begin(), vecai.end(), aidxtbl);
      for (int i=0; i<10; i++)
      cout << “i=“ << i
           << “, aidxtbl[i]=“ << aidxtbl[i]
           << “, ai[aidxtbl[i]]=“ << ai[aidxtbl[i]]
           << endl;
      cout << “#################” << endl;
    }

    This chapter is from Exceptional C++ Style, by Herb Sutter (ISBN 0201760428, copyright 2005. All rights reserved. It is reprinted with permission from Addison-Wesley Professional). Check it out at your favorite bookstore today.

    Buy this book now.

    More Code Examples Articles
    More By Addison-Wesley/Prentice Hall PTR


       · Although the article was about programming style, I was also very pleased to read...
     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek