tagging release
[dasher.git] / trunk / Src / DasherCore / DasherNode.h
blob1918a7b288fbc6336926c50e36d909bc0cc50118
1 // DasherNode.h
2 //
3 // Copyright (c) 2007 David Ward
4 //
5 // This file is part of Dasher.
6 //
7 // Dasher is free software; you can redistribute it and/or modify
8 // it under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 2 of the License, or
10 // (at your option) any later version.
12 // Dasher is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with Dasher; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef __DasherNode_h__
22 #define __DasherNode_h__
24 #include "../Common/Common.h"
25 #include "../Common/NoClones.h"
26 #include "DasherTypes.h"
27 #include "LanguageModelling/LanguageModel.h"
28 #include "NodeManager.h"
29 #include "Alphabet/GroupInfo.h"
31 #include <deque>
32 #include <iostream>
34 // Node flag constants
35 #define NF_COMMITTED 1
36 #define NF_ACTIVE 2
37 #define NF_ALIVE 4
38 #define NF_SEEN 8
39 #define NF_CONVERTED 16
40 #define NF_GAME 32
41 #define NF_ALLCHILDREN 64
42 #define NF_SUBNODE 128
43 #define NF_SUPER 256
45 namespace Dasher {
46 class CDasherNode;
47 class CDasherModel;
50 /// \ingroup Model
51 /// @{
53 /// @brief A node in the Dasher model
54 ///
55 /// The Dasher node represents a box drawn on the display. This class
56 /// contains the information required to render the node, as well as
57 /// navigation within the model (parents and children
58 /// etc.). Additional information is stored in m_pUserData, which is
59 /// interpreted by the node manager associated with this class. Any
60 /// logic specific to a particular node manager should be stored here.
61 ///
62 /// @todo Encapsulate presentation data in a structure?
63 /// @todo Check that all methods respect the pseudochild attribute
64 class Dasher::CDasherNode:private NoClones {
65 public:
67 /// Display attributes of this node, used for rendering.
68 struct SDisplayInfo {
69 int iColour;
70 bool bShove;
71 bool bVisible;
72 std::string strDisplayText;
75 /// Container type for storing children. Note that it's worth
76 /// optimising this as lookup happens a lot
77 typedef std::deque<CDasherNode*> ChildMap;
79 /// @brief Constructor
80 ///
81 /// @param pParent Parent of the new node
82 /// @param iLbnd Lower bound of node within parent
83 /// @param iHbnd Upper bound of node within parent
84 /// @param pDisplayInfo Struct containing information on how to display the node
85 ///
86 inline CDasherNode(CDasherNode *pParent, int iLbnd, int iHbnd, SDisplayInfo *pDisplayInfo);
88 /// @brief Destructor
89 ///
90 ~CDasherNode();
92 void Trace() const; // diagnostic
94 /// Return display information for this node
95 inline const SDisplayInfo *GetDisplayInfo() const;
97 /// @name Routines for manipulating node status
98 /// @{
100 /// @brief Set a node flag
102 /// Set various flags corresponding to the state of the node. The following flags are defined:
104 /// NF_COMMITTED - Node is 'above' the root, so corresponding symbol
105 /// has been added to text box, language model trained etc
107 /// NF_ACTIVE - Node is a decendent of the root node (TODO: Isnt this everything?)
109 /// NF_ALIVE - Node is large enough to be displayed
111 /// NF_SEEN - Node has already been output
113 /// NF_CONVERTED - Node has been converted (eg Japanese mode)
115 /// NF_GAME - Node is on the path in game mode
117 /// NF_ALLCHILDREN - Node has all children (TODO: obsolete?)
119 /// NF_SUBNODE - Node should be considered an integral subnode of parents (eg groups)
121 /// NF_SUPER - Node covers entire visible area
122 ///
123 /// @param iFlag The flag to set
124 /// @param bValue The new value of the flag
126 void SetFlag(int iFlag, bool bValue);
128 /// @brief Get the value of a flag for this node
130 /// @param iFlag The flag to get
132 /// @return The current value of the flag
134 inline bool GetFlag(int iFlag) const;
136 /// @brief Recursively mark nodes as converted
138 /// Ensure that this node is marked as being converted, together with
139 /// all of its ancestors (assuming that unconverted nodes are
140 /// 'contiguous' at the brances of the tree).
142 /// @todo replace with a generic 'recursive set flag'?
144 void ConvertWithAncestors();
146 /// @}
148 /// @name Routines relating to the size of the node
149 /// @{
151 // Lower and higher bounds, and the range
153 /// @brief Get the lower bound of a node
155 /// @return The lower bound
157 inline int Lbnd() const;
159 /// @brief Get the upper bound of a node
161 /// @return The upper bound
163 inline int Hbnd() const;
165 /// @brief Get the range of a node (upper - lower bound)
167 /// @return The range
169 /// @todo Should this be here (trivial arithmethic of existing methods)
171 inline int Range() const;
173 /// @brief Reset the range of a node
175 /// @param iLower New lower bound
176 /// @param iUpper New upper bound
178 inline void SetRange(int iLower, int iUpper);
180 /// @brief Get the probability of a node
182 /// @param iNormalization Normalisation constant
184 /// @return The probability
186 /// @todo Possibly a bit simplistic to make a full member
187 /// @todo Inline
189 double GetProb(int iNormalization);
191 /// @brief Get the size of the most probable child
193 /// @return The size
195 int MostProbableChild();
196 /// @}
198 /// @name Routines for manipulating relatives
199 /// @{
201 inline ChildMap & Children();
202 inline const ChildMap & GetChildren() const;
203 inline unsigned int ChildCount() const;
204 inline CDasherNode *Parent() const;
205 void SetParent(CDasherNode *pNewParent);
206 bool NodeIsParent(CDasherNode * oldnode) const;
207 // TODO: Should this be here?
208 CDasherNode *const Get_node_under(int, myint y1, myint y2, myint smousex, myint smousey); // find node under given co-ords
210 /// @brief Orphan a child of this node
212 /// Deletes all other children, and the node itself
214 /// @param pChild The child to keep
216 void OrphanChild(CDasherNode * pChild);
218 /// @brief Delete the nephews of a given child
220 /// @param pChild The child to keep
222 void DeleteNephews(CDasherNode *pChild);
224 /// @brief Delete the children of this node
227 void Delete_children();
228 /// @}
230 /// \todo Make private, read only access?
231 CNodeManager *m_pNodeManager;
233 /// Pointer for the node manager to do with as it sees fit.
234 /// Remember to make sure that the node manager deletes anything it
235 /// puts here before the node is destroyed.
237 /// Please put everything in here which isn't required to simply
238 /// render the node - basically the only exceptions should be the
239 /// node size, colour and display text.
240 /// \todo Make private, read only access?
241 void *m_pUserData;
243 private:
244 SDisplayInfo *m_pDisplayInfo;
246 int m_iLbnd;
247 int m_iHbnd; // the cumulative lower and upper bound prob relative to parent
249 int m_iRefCount; // reference count if ancestor of (or equal to) root node
251 ChildMap m_mChildren; // pointer to array of children
252 CDasherNode *m_pParent; // pointer to parent
254 // Binary flags representing the state of the node
255 int m_iFlags;
257 /// @}
259 /////////////////////////////////////////////////////////////////////////////
260 // Inline functions
261 /////////////////////////////////////////////////////////////////////////////
263 using namespace Dasher;
264 using namespace Opts;
265 #include "DasherModel.h"
267 inline CDasherNode::CDasherNode(CDasherNode *pParent, int iLbnd, int iHbnd, SDisplayInfo *pDisplayInfo) {
268 // TODO: Check that these are disabled for debug builds, and that we're not shipping such a build
269 DASHER_ASSERT(iHbnd >= iLbnd);
270 DASHER_ASSERT(pDisplayInfo != NULL);
272 m_pParent = pParent;
273 m_iLbnd = iLbnd;
274 m_iHbnd = iHbnd;
275 m_pDisplayInfo = pDisplayInfo;
277 // Default flags (make a definition somewhere, pass flags to constructor)
278 m_iFlags = NF_ACTIVE | NF_ALIVE;
280 m_iRefCount = 0;
283 inline const CDasherNode::SDisplayInfo *CDasherNode::GetDisplayInfo() const {
284 return m_pDisplayInfo;
287 inline int CDasherNode::Lbnd() const {
288 return m_iLbnd;
291 inline int CDasherNode::Hbnd() const {
292 return m_iHbnd;
295 inline int CDasherNode::Range() const {
296 return m_iHbnd - m_iLbnd;
299 inline CDasherNode::ChildMap &CDasherNode::Children() {
300 return m_mChildren;
303 inline const CDasherNode::ChildMap &CDasherNode::GetChildren() const {
304 return m_mChildren;
307 inline unsigned int CDasherNode::ChildCount() const {
308 return m_mChildren.size();
311 inline bool CDasherNode::GetFlag(int iFlag) const {
312 return (m_iFlags & iFlag);
315 inline CDasherNode *CDasherNode::Parent() const {
316 return m_pParent;
319 inline void CDasherNode::SetRange(int iLower, int iUpper) {
320 m_iLbnd = iLower;
321 m_iHbnd = iUpper;
325 #endif /* #ifndef __DasherNode_h__ */