this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Guido Tack <tack@gecode.org> 5 * 6 * Copyright: 7 * Guido Tack, 2006 8 * 9 * This file is part of Gecode, the generic constraint 10 * development environment: 11 * http://www.gecode.org 12 * 13 * Permission is hereby granted, free of charge, to any person obtaining 14 * a copy of this software and associated documentation files (the 15 * "Software"), to deal in the Software without restriction, including 16 * without limitation the rights to use, copy, modify, merge, publish, 17 * distribute, sublicense, and/or sell copies of the Software, and to 18 * permit persons to whom the Software is furnished to do so, subject to 19 * the following conditions: 20 * 21 * The above copyright notice and this permission notice shall be 22 * included in all copies or substantial portions of the Software. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 28 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 29 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 30 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 31 * 32 */ 33 34#ifndef GECODE_GIST_NODE_HH 35#define GECODE_GIST_NODE_HH 36 37#include <gecode/kernel.hh> 38 39#include <QHash> 40#include <QString> 41 42namespace Gecode { namespace Gist { 43 44 class VisualNode; 45 46 /// Node allocator 47 template<class T> 48 class NodeAllocatorBase { 49 private: 50 /// Size of each block of nodes 51 static const int NodeBlockSize = 1<<14; 52 /// Blocks of nodes 53 class Block { 54 public: 55 /// The actual nodes 56 T b[NodeBlockSize]; 57 /// The index of the best known previous solution for each node 58 int best[NodeBlockSize]; 59 }; 60 /// Array of blocks 61 Block** b; 62 /// Size of the array 63 int n; 64 /// Current block number 65 int cur_b; 66 /// Current node number in current block 67 int cur_t; 68 /// Allocate new block, potentially reallocate block array 69 void allocate(void); 70 /// Flag whether search uses branch-and-bound 71 bool _bab; 72 /// Hash table mapping nodes to label text 73 QHash<T*,QString> labels; 74 public: 75 /// Constructor 76 NodeAllocatorBase(bool bab); 77 /// Destructor 78 ~NodeAllocatorBase(void); 79 /// Allocate new node with parent \a p 80 int allocate(int p); 81 /// Allocate new root node for space \a root 82 int allocate(Space* root); 83 /// Return node for index \a i 84 T* operator [](int i) const; 85 /// Return index of best node before \a i 86 T* best(int i) const; 87 /// Set index of best node before \a i to \a b 88 void setBest(int i, int b); 89 /// Return branch-and-bound flag 90 bool bab(void) const; 91 /// Return branching label flag 92 bool showLabels(void) const; 93 /// Set branching label flag 94 void showLabels(bool b); 95 /// Return whether node \a n has a label 96 bool hasLabel(T* n) const; 97 /// Set label of node \a n to \a l 98 void setLabel(T* n, const QString& l); 99 /// Remove label of node \a n 100 void clearLabel(T* n); 101 /// Get label of node \a n 102 QString getLabel(T* n) const; 103 }; 104 105 /// \brief Base class for nodes of the search tree 106 class Node { 107 private: 108 /// Tags that are used to encode the number of children 109 enum { 110 UNDET, //< Number of children not determined 111 LEAF, //< Leaf node 112 TWO_CHILDREN, //< Node with at most two children 113 MORE_CHILDREN //< Node with more than two children 114 }; 115 116 /// The children, or in case there are at most two, the first child 117 void* childrenOrFirstChild; 118 119 /** The number of children, in case it is greater than 2, or the first 120 * child (if negative) 121 */ 122 int noOfChildren; 123 124 /// The parent of this node, or nullptr for the root 125 int parent; 126 127 /// Read the tag of childrenOrFirstChild 128 unsigned int getTag(void) const; 129 /// Set the tag of childrenOrFirstChild 130 void setTag(unsigned int tag); 131 /// Return childrenOrFirstChild without tag 132 void* getPtr(void) const; 133 /// Return childrenOrFirstChild as integer 134 int getFirstChild(void) const; 135 136 protected: 137 /// Return whether this node is undetermined 138 bool isUndetermined(void) const; 139 140 /// Return index of child no \a n 141 int getChild(int n) const; 142 public: 143 typedef NodeAllocatorBase<VisualNode> NodeAllocator; 144 145 /// Construct node with parent \a p 146 Node(int p, bool failed = false); 147 148 /// Return the parent 149 int getParent(void) const; 150 /// Return the parent 151 VisualNode* getParent(const NodeAllocator& na) const; 152 /// Return child no \a n 153 VisualNode* getChild(const NodeAllocator& na, int n) const; 154 155 /// Return index of this node 156 int getIndex(const NodeAllocator& na) const; 157 158 /// Check if this node is the root of a tree 159 bool isRoot(void) const; 160 161 /// Set the number of children to \a n and initialize children 162 void setNumberOfChildren(unsigned int n, NodeAllocator& na); 163 164 /// Return the number of children 165 unsigned int getNumberOfChildren(void) const; 166 167 }; 168 169}} 170 171#endif 172 173// STATISTICS: gist-any