Code Examples
  Home arrow Code Examples arrow Page 6 - 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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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 - Summary


    (Page 6 of 6 )

    Let’s preserve the original code’s basic interface choice instead of straying far afield and proposing alternate design choices.  Limiting our critique just to correcting the code for mechanical errors and basic style, then, consider the three alternative improved versions below. Each has its own benefits, drawbacks, and style preferences as explained in the accompanying comments. What all three versions have in common is that they are clearer, more understandable, and more portable code—and that ought to count for something, in your company and in mine.

    // An improved version of the code originally published in [Hicks00].
    //
    #include <vector>
    #include <map>
    #include <algorithm>
    // Solution 1 does some basic cleanup but still preserves the general structure
    // of the original’s approach. We’re down to 17 lines (even if you count “public:”
    // and “private:” as lines), where the original had 23.
    //
    namespace Solution1 {
      template<class Iter>
      class sort_idxtbl_pair {
      public:
        void set( const Iter& it, int i ) { it_ = it; i_ = i; }
        bool operator<( const sort_idxtbl_pair& other ) const
          { return *it_ < *other.it_; }
        operator int() const { return i_; }
      private:
        Iter it_;
        int i_;
      };
      // This function is where most of the clarity savings came from; it has 5 lines,
      // where the original had 13. After each code line, I’ll show the corresponding
      // original code for comparison. Prefer to write code that is clear and concise,
      // not unnecessarily complex or obscure!
      //
      template<class IterIn, class IterOut>
      void sort_idxtbl( IterIn first, IterIn last, IterOut out ) {
        std::vector<sort_idxtbl_pair<IterIn> > v(last-first);
          // int iDst = last-first;
          // typedef std::vector< sort_idxtbl_pair<RAIter> > V;
          // V v(iDst);
        for( int i=0; i < last-first; ++i )
          v[i].set( first+i, i );
          // 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() );
          // std::sort(v.begin(), v.end());
        std::copy( v.begin(), v.end(), out );
          // int *pi = pidxtbl;
          // vit = v.begin();
          // for (; vit<v.end(); pi++, vit++)
          // *pi = (*vit).i;
      }
    }

    // Solution 2 uses a pair instead of reinventing a pair-like helper class. Now we’re
    // down to 13 lines, from the original 23. Of the 14 lines, 9 are purpose-specific,
    // and 5 are directly reusable in other contexts.
    //
    namespace Solution2 {
      template<class T, class U>
      struct ComparePair1stDeref {
        bool operator()( const std::pair<T,U>& a, const std::pair<T,U>& b ) const
          { return *a.first < *b.first; }
      };
      template<class IterIn, class IterOut>
      void sort_idxtbl( IterIn first, IterIn last, IterOut out ) {
        std::vector< std::pair<IterIn,int> > s( last-first );
        for( int i=0; i < s.size(); ++i )
          s[i] = std::make_pair( first+i, i );
        std::sort( s.begin(), s.end(), ComparePair1stDeref<IterIn,int>() );
        for( int i=0; i < s.size(); ++i, ++out )
        *out = s[i].second;
      }
    }
    // Solution 3 just shows a couple of alternative details—it uses a map to avoid a
    // separate sorting step, and it uses std::transform() instead of a handcrafted loop.
    // Here we still have 15 lines, but more are reusable. This version uses more space
    // overhead and probably more time overhead too, so I prefer Solution 2, but this
    // is an example of finding alternative approaches to a problem.
    //
    namespace Solution3 {
      template<class T>
      struct CompareDeref {
        bool operator()( const T& a, const T& b ) const
          { return *a < *b; }
      };
      template<class T, class U>
      struct Pair2nd {
        const U& operator()( const std::pair<T,U>& a ) const { return a.second; }
      };
      template<class IterIn, class IterOut>
      void sort_idxtbl( IterIn first, IterIn last, IterOut out ) {
        std::multimap<IterIn, int, CompareDeref<IterIn> > v;
        for( int i=0; first != last; ++i, ++first )
          v.insert( std::make_pair( first, i ) );
        std::transform( v.begin(), v.end(), out, Pair2nd<IterIn const,int>() );
      }
    }
    // I left the test harness essentially unchanged, except to demonstrate putting
    // the output in an output iterator (instead of necessarily an int*) and using the
    // source array directly as a container.
    //
    #include <iostream>
    int main() {
      int ai[10] = { 15,12,13,14,18,11,10,17,16,19 };
      std::cout << “#################” << std::endl;
      std::vector<int> aidxtbl( 10 );
      // use another namespace name to test a different solution
      Solution3::sort_idxtbl( ai, ai+10, aidxtbl.begin() );
      for( int i=0; i<10; ++i )
      std::cout << “i=“ << i
     << “, aidxtbl[i]=“ << aidxtbl[i]
     << “, ai[aidxtbl[i]]=“ << ai[aidxtbl[i]]
     << std::endl;
      std::cout << “#################” << std::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.

     


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

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

    CODE EXAMPLES ARTICLES

    - Handling Animations and Bitmaps Using GDI+ f...
    - Download a Web Page using the WebClient
    - Creating a Chart using Data from a Database ...
    - The Basics of Charting with the MS Chart Con...
    - Searching Body Text with textRange: Enter th...
    - Searching Body Text with textRange: Building...
    - Searching Body Text with textRange, part 1: ...
    - First Steps in Programming
    - Programming in C
    - Quick Introduction to ASF,ASX, and Networkin...
    - SatView: Pointer Perfect, Part 2: Constructi...
    - SatView: Pointer Perfect, Part 1
    - Style Case Studies: Construction Unions
    - Creating an Engine for Games for Windows
    - Style Case Studies: Generic Callbacks

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT