Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Class Members | File Members

Grid.h

Go to the documentation of this file.
00001 
00002 //
00003 // This file is part of the MADELINE 2 program 
00004 // written by Edward H. Trager and Ritu Khanna
00005 // Copyright (c) 2005 by the
00006 // Regents of the University of Michigan.
00007 // All Rights Reserved.
00008 // 
00009 // The latest version of this program is available from:
00010 // 
00011 //   http://eyegene.ophthy.med.umich.edu/madeline/
00012 //   
00013 // Released under the GNU General Public License.
00014 // A copy of the GPL is included in the distribution
00015 // package of this software, or see:
00016 // 
00017 //   http://www.gnu.org/copyleft/
00018 //   
00019 // ... for licensing details.
00020 // 
00021 // Last modified: 2006.10.11.ET
00022 // 
00024 
00025 //
00026 // Grid.h
00027 //
00028 #ifndef GRID_INCLUDED
00029 #define GRID_INCLUDED
00030 
00031 #include <map>
00032 #include <algorithm>
00033 
00034 // FOR DEBUGGING ONLY: #include <iostream>
00035 
00036 // FOR DEBUGGING ONLY: using namespace std;
00037 
00038 //
00039 // template class Grid : Implements a sparse two-dimensional array
00040 //                       as an STL "map of maps".  The vector of vertical
00041 //                       (Y) indices stores the vectors of horizontal
00042 //                       (X) positions.
00043 //                       
00044 // class N is a number class (i.e., int, double, etc.)
00045 // class pT must be of type "pointer to object of type T"
00046 // 
00047 // Pointers are used: (1) to avoid expensive object copies
00048 //               and: (2) to allow returning NULL (0) instead of a sentinel object 
00049 //                        for empty nodes in the sparse array
00050 template <class N,class pT> class Grid{
00051         private: 
00052                 typedef std::map<N,pT> horizontalVector;
00053                 typedef std::map<N,horizontalVector> verticalVector;
00054                 
00055                 verticalVector _grid;
00056                 
00057         public: 
00058                 void insert(N xPosition,N yPosition,pT pointerToObjectOfTypeT);
00059                 pT   find  (N xPosition,N yPosition);
00060                 
00061                 // FOR DEBUGGING ONLY: 
00062                  void printNames();
00063 };
00064 
00065 //
00066 // Grid::insert() method: Insert a pointer to object of type T
00067 //                        at coordinates xPosition, yPosition
00068 //
00069 template<class N,class pT> void Grid<N,pT>::insert(N xPosition,N yPosition,pT pointerToObjectOfTypeT){
00070         
00071         // Get an iterator to the horizontalVector map in row Y of
00072         // the grid:
00073         typename verticalVector::iterator tempY = _grid.find(yPosition);
00074         // If a horizontalVector map is not yet present, then add one:
00075         if(tempY == _grid.end()){
00076                 horizontalVector hV;
00077                 tempY = _grid.insert( std::pair <N,horizontalVector>(yPosition,hV)).first;
00078         }
00079         // Insert the objectOfTypeT into the horizontalVector:
00080         tempY->second.insert(std::pair<N,pT>(xPosition,pointerToObjectOfTypeT));
00081         
00082 }
00083 
00084 //
00085 // Grid::find() method: Find an object at coordinates xPosition,yPosition
00086 //
00087 // Returns an object of type pT if found.  
00088 // Returns 0 (NULL) if not found.
00089 //
00090 // NOTE BENE: It is the user's responsibility to check if the 
00091 //            returned pointer is NULL before dereferencing!
00092 //            
00093 template<class N,class pT> pT Grid<N,pT>::find(N xPosition,N yPosition){
00094         
00095         // Get an iterator to the horizontalVector map in row Y of
00096         // the grid:
00097         typename verticalVector::iterator tempY = _grid.find(yPosition);
00098         //
00099         // If a horizontalVector map is not yet present, then return
00100         // 
00101         if(tempY == _grid.end()){
00102                 
00103                 return (pT) 0;
00104                 
00105         }
00106         //
00107         // Get here if row Y has entries: look for x:
00108         //
00109         typename horizontalVector::iterator tempX = tempY->second.find(xPosition);
00110         if(tempX != tempY->second.end()){
00111                 return tempX->second;
00112         }
00113         return (pT) 0;
00114         
00115 }
00116 
00117 //
00118 // printNames() is to be used for debugging only:
00119 //
00120 template<class N,class pT> void Grid<N,pT>::printNames(){
00121         
00122         typename verticalVector::const_iterator y;
00123         typename horizontalVector::const_iterator x;
00124         
00125         
00126         std::cout << "Y\tX\tObject" << "\n";
00127         
00128         for(y = _grid.begin(); y != _grid.end() ; y++ ){
00129                 
00130                 for(x = y->second.begin() ; x != y->second.end() ; x++ ){
00131                         std::cout << y->first << "\t" << x->first << "\t";
00132                         // Since we are using a grid of pointers:
00133                         x->second->display();
00134                         std::cout << "\n";
00135                 }
00136         }
00137 }
00138 #endif

Generated on Fri Nov 18 16:24:39 2011 for MADELINE by  doxygen 1.4.4