nodevisitor.hpp
Go to the documentation of this file.00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 00002 /* 00003 * Main authors: 00004 * Guido Tack <tack@gecode.org> 00005 * 00006 * Copyright: 00007 * Guido Tack, 2006 00008 * 00009 * Last modified: 00010 * $Date: 2010-07-30 17:08:42 +0200 (Fri, 30 Jul 2010) $ by $Author: tack $ 00011 * $Revision: 11315 $ 00012 * 00013 * This file is part of Gecode, the generic constraint 00014 * development environment: 00015 * http://www.gecode.org 00016 * 00017 * Permission is hereby granted, free of charge, to any person obtaining 00018 * a copy of this software and associated documentation files (the 00019 * "Software"), to deal in the Software without restriction, including 00020 * without limitation the rights to use, copy, modify, merge, publish, 00021 * distribute, sublicense, and/or sell copies of the Software, and to 00022 * permit persons to whom the Software is furnished to do so, subject to 00023 * the following conditions: 00024 * 00025 * The above copyright notice and this permission notice shall be 00026 * included in all copies or substantial portions of the Software. 00027 * 00028 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00029 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00030 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00031 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 00032 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 00033 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 00034 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 00035 * 00036 */ 00037 00038 namespace Gecode { namespace Gist { 00039 00040 template<class Cursor> 00041 forceinline 00042 NodeVisitor<Cursor>::NodeVisitor(const Cursor& c0) : c(c0) {} 00043 00044 template<class Cursor> 00045 forceinline void 00046 NodeVisitor<Cursor>::setCursor(const Cursor& c0) { c = c0; } 00047 00048 template<class Cursor> 00049 forceinline Cursor& 00050 NodeVisitor<Cursor>::getCursor(void) { return c; } 00051 00052 template<class Cursor> 00053 forceinline void 00054 PostorderNodeVisitor<Cursor>::moveToLeaf(void) { 00055 while (c.mayMoveDownwards()) { 00056 c.moveDownwards(); 00057 } 00058 } 00059 00060 template<class Cursor> 00061 PostorderNodeVisitor<Cursor>::PostorderNodeVisitor(const Cursor& c0) 00062 : NodeVisitor<Cursor>(c0) { 00063 moveToLeaf(); 00064 } 00065 00066 template<class Cursor> 00067 forceinline bool 00068 PostorderNodeVisitor<Cursor>::next(void) { 00069 c.processCurrentNode(); 00070 if (c.mayMoveSidewards()) { 00071 c.moveSidewards(); 00072 moveToLeaf(); 00073 } else if (c.mayMoveUpwards()) { 00074 c.moveUpwards(); 00075 } else { 00076 return false; 00077 } 00078 return true; 00079 } 00080 00081 template<class Cursor> 00082 forceinline void 00083 PostorderNodeVisitor<Cursor>::run(void) { 00084 while (next()) {} 00085 } 00086 00087 template<class Cursor> 00088 forceinline bool 00089 PreorderNodeVisitor<Cursor>::backtrack(void) { 00090 while (! c.mayMoveSidewards() && c.mayMoveUpwards()) { 00091 c.moveUpwards(); 00092 } 00093 if (! c.mayMoveUpwards()) { 00094 return false; 00095 } else { 00096 c.moveSidewards(); 00097 } 00098 return true; 00099 } 00100 00101 template<class Cursor> 00102 PreorderNodeVisitor<Cursor>::PreorderNodeVisitor(const Cursor& c0) 00103 : NodeVisitor<Cursor>(c0) {} 00104 00105 template<class Cursor> 00106 forceinline bool 00107 PreorderNodeVisitor<Cursor>::next(void) { 00108 c.processCurrentNode(); 00109 if (c.mayMoveDownwards()) { 00110 c.moveDownwards(); 00111 } else if (c.mayMoveSidewards()) { 00112 c.moveSidewards(); 00113 } else { 00114 return backtrack(); 00115 } 00116 return true; 00117 } 00118 00119 template<class Cursor> 00120 forceinline void 00121 PreorderNodeVisitor<Cursor>::run(void) { 00122 while (next()) {} 00123 } 00124 }} 00125 00126 // STATISTICS: gist-any