path.cc

Go to the documentation of this file.
00001 /*                            Package   : libhex
00002  * path.cc                    Created   : 2007/10/11
00003  *                            Author    : Alex Tingle
00004  *
00005  *    Copyright (C) 2007-2008, Alex Tingle.
00006  *
00007  *    This file is part of the libhex application.
00008  *
00009  *    libhex is free software; you can redistribute it and/or
00010  *    modify it under the terms of the GNU Lesser General Public
00011  *    License as published by the Free Software Foundation; either
00012  *    version 2.1 of the License, or (at your option) any later version.
00013  *
00014  *    libhex is distributed in the hope that it will be useful,
00015  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  *    Lesser General Public License for more details.
00018  *
00019  *    You should have received a copy of the GNU Lesser General Public
00020  *    License along with this library; if not, write to the Free Software
00021  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  */
00023 
00024 #include "hex.h"
00025 
00026 #include <cassert>
00027 
00028 namespace hex {
00029 
00030 
00031 Area
00032 Path::to_area(void) const
00033 {
00034   std::set<Hex*> result;
00035   std::copy(_hexes.begin(), _hexes.end(), std::inserter(result,result.end()));
00036   return result;
00037 }
00038 
00039 
00040 int
00041 Path::length(void) const
00042 {
00043   return int( _hexes.size() );
00044 }
00045 
00046 
00047 std::string
00048 Path::steps(void) const
00049 {
00050   std::string result ="";
00051   const Hex* curr =NULL;
00052   for(std::list<Hex*>::const_iterator h=_hexes.begin(); h!=_hexes.end(); ++h)
00053   {
00054     if(curr)
00055         result += hex::steps(curr,*h);
00056     curr = *h;
00057   }
00058   return result;
00059 }
00060 
00061 
00062 std::string
00063 Path::str(void) const
00064 {
00065   assert(!this->_hexes.empty());
00066   std::string result =this->_hexes.front()->str();
00067   result+=":"+this->steps();
00068   return result;
00069 }
00070 
00071 
00073 inline std::list<Hex*>
00074 path(Hex* start, const std::string& steps)
00075   throw(hex::out_of_range,hex::invalid_argument)
00076 {
00077   // See also: go(int&,int&,const std::string&)
00078   std::list<Hex*> hexes;
00079   hexes.push_back(start);
00080   std::string::size_type cur =0;
00081   while(cur<steps.size() && steps[cur]!='?')
00082   {
00083     // Find direction
00084     Direction dir =to_direction( steps[cur] );
00085     ++cur;
00086     bool repeat =(cur<steps.size() && steps[cur]=='*');
00087     do{
00088         Hex* next =hexes.back()->go( dir );
00089         if(next)
00090             hexes.push_back( next );
00091         else if(*steps.rbegin()=='?' || repeat)
00092             return hexes; // bail out instead of throwing
00093         else
00094             throw hex::out_of_range(start->str()+":"+steps);
00095     } while(repeat);
00096   }
00097   return hexes;
00098 }
00099 
00100 
00101 Path::Path(Hex* start, const std::string& steps)
00102   throw(hex::out_of_range,hex::invalid_argument)
00103   : _hexes( path(start,steps) )
00104 {}
00105 
00106 
00107 Path::Path(Hex* from, const Hex* to) throw()
00108   : _hexes( path(from,hex::steps(from,to)) )
00109 {}
00110 
00111 
00112 } // end namespace hex

Generated on Thu Feb 21 00:00:54 2008 for libhex by  doxygen 1.5.1