3 // Copyright (c) 2007 David Ward
5 // This file is part of Dasher.
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"
34 // Node flag constants
35 #define NF_COMMITTED 1
39 #define NF_CONVERTED 16
41 #define NF_ALLCHILDREN 64
42 #define NF_SUBNODE 128
53 /// @brief A node in the Dasher model
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.
62 /// @todo Encapsulate presentation data in a structure?
63 /// @todo Check that all methods respect the pseudochild attribute
64 class Dasher::CDasherNode
:private NoClones
{
67 /// Display attributes of this node, used for rendering.
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
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
86 inline CDasherNode(CDasherNode
*pParent
, int iLbnd
, int iHbnd
, SDisplayInfo
*pDisplayInfo
);
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
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
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();
148 /// @name Routines relating to the size of the node
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
189 double GetProb(int iNormalization
);
191 /// @brief Get the size of the most probable child
195 int MostProbableChild();
198 /// @name Routines for manipulating relatives
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();
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?
244 SDisplayInfo
*m_pDisplayInfo
;
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
259 /////////////////////////////////////////////////////////////////////////////
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
);
275 m_pDisplayInfo
= pDisplayInfo
;
277 // Default flags (make a definition somewhere, pass flags to constructor)
278 m_iFlags
= NF_ACTIVE
| NF_ALIVE
;
283 inline const CDasherNode::SDisplayInfo
*CDasherNode::GetDisplayInfo() const {
284 return m_pDisplayInfo
;
287 inline int CDasherNode::Lbnd() const {
291 inline int CDasherNode::Hbnd() const {
295 inline int CDasherNode::Range() const {
296 return m_iHbnd
- m_iLbnd
;
299 inline CDasherNode::ChildMap
&CDasherNode::Children() {
303 inline const CDasherNode::ChildMap
&CDasherNode::GetChildren() const {
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 {
319 inline void CDasherNode::SetRange(int iLower
, int iUpper
) {
325 #endif /* #ifndef __DasherNode_h__ */