Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

map.c

Go to the documentation of this file.
00001 /**********************************************************************
00002 *       FILE:     MAP.c
00003 *       AUTHOR:   E. Otis O
00004 *       DATE:     17 April 1993
00005 *       MODIFIED: 30 April 1993 (5:00 pm)
00006 *       PURPOSE:
00007 *         Functions to store and relay information about the "Scotland
00008 *         Yard" network
00009 ***********************************************************************/
00010 
00011 #include "map.h"
00012 
00013 
00014 /**********************************************************************
00015 *       Private Functions
00016 ***********************************************************************/
00017 
00018 void Map::initialize(int *array, int size)
00019     { for (int i= 0; i < size; i++) array[i]= 0; }
00020 
00021 /************************************************************/
00022 
00023 void Map::get_field (int node, mode transportation) {
00024 // Input a transportation field from the map file.
00025 
00026   char ch;
00027   int  link;
00028 
00029   mapFile >> ch;
00030   if ( ch != FIELD ) {
00031     mapFile.putback(ch);
00032     while ( ch != FIELD ) {
00033       mapFile >> link >> ch;
00034       nodes[node].get_link(link, transportation);
00035       if ( ch == SUB1 ) {  // if a subfield is found...
00036   while ( ch != SUB2 ) {  // ...loop through the subfield until end
00037     mapFile >> link >> ch;
00038     nodes[node].get_join(link);
00039   }
00040         mapFile >> ch;  // pattern always ")," or ");"
00041       }
00042     }
00043   }
00044 }
00045 
00046 
00047 
00048 
00049 
00050 /**********************************************************************
00051 *       Public Functions
00052 ***********************************************************************/
00053 
00054 void Map::show_map (void) {
00055 // Show a tabular representation of the map for testing/debugging
00056 // purposes.
00057 
00058   for (int i= FIRST; i < numNodes; i++) {
00059     cout << "\nNODE #" << i << ": ";
00060     nodes[i].show_node();
00061   }
00062   cout << "\nmax x: " << maxX << "\nmax y: " << maxY << EOLN;
00063 }
00064 
00065 /************************************************************/
00066 
00067 bool Map::load_map (char *mapName) {
00068 // Read in a map and construct a data representation
00069 
00070   const int  BUFFER= 40;    // size of the "node name" input buffer
00071   const char COMMENT= '/';  // lines beginning with COMMENT are ignored
00072 
00073   char text[BUFFER], ch;
00074   int nodeNum= 0;
00075 
00076   mapFile.open(mapName, ios::in);
00077 
00078   if ( !mapFile.fail() ) {
00079     mapFile >> numNodes;
00080 
00081     nodes= new MapNode[numNodes + FIRST];  // store the adjacency list here
00082     assert (nodes != 0);
00083 
00084     while ( nodeNum < numNodes ) {
00085       // ignore lines commented with COMMENT
00086   if (mapFile.peek() == COMMENT) while (mapFile.get() != EOLN) ;
00087       mapFile >> nodeNum >> ch;
00088 
00089       get_field(nodeNum, Taxi);
00090       get_field(nodeNum, Bus);
00091       get_field(nodeNum, Subway);
00092 
00093       mapFile >> nodes[nodeNum].x >> ch;
00094       if (nodes[nodeNum].x > maxX) maxX= nodes[nodeNum].x; 
00095       mapFile >> nodes[nodeNum].y >> ch;
00096       if (nodes[nodeNum].y > maxY) maxY= nodes[nodeNum].y;
00097 
00098       ch= mapFile.get();
00099       while (ch == ' ') ch= mapFile.get();  // ignore whitespace
00100       mapFile.putback(ch);  // put the last read character back into the buffer
00101       mapFile.getline(text, BUFFER, EOLN);  // input node name and...
00102       // ...store name in adjacency list
00103       nodes[nodeNum].get_name(text, mapFile.gcount());
00104     }
00105   }
00106 
00107   mapFile.close();
00108   return (bool) mapFile.good();  // return true if all file operations went well
00109 }
00110 
00111 /************************************************************/
00112 
00113 int Map::connected_to (int node, int link, int join) {
00114 
00115   if ( between(node, FIRST, numNodes) )
00116     if ( join > -1 )
00117       return nodes[node].find_join(link, join);
00118     else {
00119       return nodes[node].find_link(link);
00120 }  
00121 
00122   else {
00123     cerr << "Node " << node << " out of range ("
00124    << FIRST << ".." << numNodes << ")\n";
00125     return 0;  // node 0 points to NULL
00126   }
00127 }
00128 
00129 /************************************************************/
00130 
00131 int Map::num_connections (int node, int join) {
00132 
00133   if ( between(node, FIRST, numNodes) )
00134     if ( join > -1 ) 
00135       return nodes[node].num_joins(join);
00136     else
00137       return nodes[node].numLinks;
00138   else {
00139     cerr << "Node " << node << " out of range ("
00140    << FIRST << ".." << numNodes << ")\n";
00141     return 0;  // node 0 points to NULL
00142   }
00143 }
00144 
00145 /************************************************************/
00146 
00147 int Map::next_short (int, int, bool, bool, bool) {
00148 // This would have been a function to return the next nearest
00149 // link in a path using Dijkstra's shortest path algorithm.
00150 // Design changes, however, have made this function obsolete.
00151 // A nearest-neighbor algorithm is used instead.  We have
00152 // decided to implement this algorithm outside of the map
00153 // module.
00154 
00155    return 0;
00156 }
00157 
00158 
00159 

Generated on Sun Jul 6 23:07:15 2003 for Scotland Yard by doxygen1.2.15