From 8e313e5a8c431fbea059988659d3cadcda476e7c Mon Sep 17 00:00:00 2001 From: Phil Cowans Date: Fri, 12 Jan 2007 16:06:27 +0000 Subject: [PATCH] Finished generalised node flags * Finished generalised node flags svn path=/trunk/; revision=2967 --- ChangeLog | 4 ++ Src/DasherCore/AlphabetManager.cpp | 16 ++--- Src/DasherCore/ControlManager.cpp | 6 +- Src/DasherCore/ConversionManager.cpp | 6 +- Src/DasherCore/DasherModel.cpp | 29 +++++---- Src/DasherCore/DasherNode.cpp | 19 +++--- Src/DasherCore/DasherNode.h | 122 +++++++++++------------------------ Src/DasherCore/DasherViewSquare.cpp | 12 ++-- 8 files changed, 85 insertions(+), 129 deletions(-) diff --git a/ChangeLog b/ChangeLog index 441de4d6..a67120d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2007-01-12 Phil Cowans + + * Finished generalised node flags + 2007-01-09 Phil Cowans * Merged Will's Chinese stuff diff --git a/Src/DasherCore/AlphabetManager.cpp b/Src/DasherCore/AlphabetManager.cpp index 5ceaef3a..5cec7be7 100644 --- a/Src/DasherCore/AlphabetManager.cpp +++ b/Src/DasherCore/AlphabetManager.cpp @@ -54,10 +54,10 @@ CDasherNode *CAlphabetManager::GetRoot(CDasherNode *pParent, int iLower, int iUp // pNewNode->SetContext(m_pLanguageModel->CreateEmptyContext()); // FIXME - handle context properly pNewNode->m_pNodeManager = this; - pNewNode->m_bShove = true; + pNewNode->SetShove(true); pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup; pNewNode->m_strDisplayText = m_pNCManager->GetAlphabet()->GetDisplayText(iSymbol); - pNewNode->Seen(true); + pNewNode->SetFlag(NF_SEEN, true); SAlphabetData *pNodeUserData = new SAlphabetData; pNewNode->m_pUserData = pNodeUserData; @@ -84,7 +84,7 @@ CDasherNode *CAlphabetManager::GetRoot(CDasherNode *pParent, int iLower, int iUp if(m_bGameMode) { pNodeUserData->iGameOffset = -1; - pNewNode->SetGame(true); + pNewNode->SetFlag(NF_GAME, true); } return pNewNode; @@ -157,7 +157,7 @@ void CAlphabetManager::PopulateChildrenWithSymbol( CDasherNode *pNode, int iExis pNewNode = new CDasherNode(pNode, newchars[j], 0, Nodes1, iLbnd, cum[j], pParentUserData->pLanguageModel, iColour); pNewNode->m_pNodeManager = this; - pNewNode->m_bShove = true; + pNewNode->SetShove(true); pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup; SAlphabetData *pNodeUserData = new SAlphabetData; @@ -172,7 +172,7 @@ void CAlphabetManager::PopulateChildrenWithSymbol( CDasherNode *pNode, int iExis if((iCurrentGameOffset != -2) && ((iCurrentGameOffset + 1) < static_cast(m_strGameString.size())) && ((m_pNCManager->GetAlphabet()->GetText(newchars[j]))[0] == m_strGameString[iCurrentGameOffset + 1])) { static_cast(pNewNode->m_pUserData)->iGameOffset = iCurrentGameOffset + 1; - pNewNode->SetGame(true); + pNewNode->SetFlag(NF_GAME, true); } else static_cast(pNewNode->m_pUserData)->iGameOffset = -2; @@ -194,7 +194,7 @@ void CAlphabetManager::PopulateChildrenWithSymbol( CDasherNode *pNode, int iExis iLbnd = cum[j]; } - pNode->SetHasAllChildren(true); + pNode->SetFlag(NF_ALLCHILDREN, true); } void CAlphabetManager::ClearNode( CDasherNode *pNode ) { @@ -283,8 +283,8 @@ CDasherNode *CAlphabetManager::RebuildParent(CDasherNode *pNode, int iGeneration } pNewNode->m_pNodeManager = this; - pNewNode->m_bShove = true; - pNewNode->Seen(true); + pNewNode->SetShove(true); + pNewNode->SetFlag(NF_SEEN, true); pNewNode->m_pBaseGroup = m_pNCManager->GetAlphabet()->m_pBaseGroup; SAlphabetData *pNodeUserData = new SAlphabetData; diff --git a/Src/DasherCore/ControlManager.cpp b/Src/DasherCore/ControlManager.cpp index 8de2fb43..f80e689a 100644 --- a/Src/DasherCore/ControlManager.cpp +++ b/Src/DasherCore/ControlManager.cpp @@ -255,7 +255,7 @@ CDasherNode *CControlManager::GetRoot(CDasherNode *pParent, int iLower, int iUpp pNewNode->m_pNodeManager = this; pNewNode->m_pUserData = m_mapControlMap[0]; pNewNode->m_strDisplayText = static_cast(pNewNode->m_pUserData)->strLabel; - pNewNode->m_bShove = false; + pNewNode->SetShove(false); pNewNode->m_pBaseGroup = 0; return pNewNode; @@ -280,7 +280,7 @@ void CControlManager::PopulateChildren( CDasherNode *pNode ) { if( *it == NULL ) { // Escape back to alphabet pNewNode = m_pNCManager->GetRoot(0, pNode, iLbnd, iHbnd, NULL); - pNewNode->Seen(false); + pNewNode->SetFlag(NF_SEEN, false); } else { @@ -294,7 +294,7 @@ void CControlManager::PopulateChildren( CDasherNode *pNode ) { pNewNode->m_pNodeManager = this; pNewNode->m_pUserData = *it; pNewNode->m_strDisplayText = (*it)->strLabel; - pNewNode->m_bShove = false; + pNewNode->SetShove(false); pNewNode->m_pBaseGroup = 0; } pNode->Children().push_back(pNewNode); diff --git a/Src/DasherCore/ConversionManager.cpp b/Src/DasherCore/ConversionManager.cpp index c7eca593..1cc565a3 100644 --- a/Src/DasherCore/ConversionManager.cpp +++ b/Src/DasherCore/ConversionManager.cpp @@ -59,7 +59,7 @@ CDasherNode *CConversionManager::GetRoot(CDasherNode *pParent, int iLower, int i pNodeUserData->pLanguageModel = m_pHelper->GetLanguageModel(); pNewNode->m_strDisplayText = "Convert"; - pNewNode->m_bShove = false; + pNewNode->SetShove(false); pNewNode->m_pBaseGroup = 0; CLanguageModel::Context iContext; @@ -327,7 +327,7 @@ void CConversionManager::PopulateChildren( CDasherNode *pNode ) { //pNodeUserData->iSymbol = iIdx; pNewNode->m_strDisplayText = pCurrentSCEChild->pszConversion; - pNewNode->m_bShove = true; + pNewNode->SetShove(false); pNewNode->m_pBaseGroup = 0; pNode->Children().push_back(pNewNode); @@ -347,7 +347,7 @@ void CConversionManager::PopulateChildren( CDasherNode *pNode ) { int iHbnd(m_pNCManager->GetLongParameter(LP_NORMALIZATION)); pNewNode = m_pNCManager->GetRoot(0, pNode, iLbnd, iHbnd, NULL); - pNewNode->Seen(false); + pNewNode->SetFlag(NF_SEEN, false); pNode->Children().push_back(pNewNode); // pNode->SetHasAllChildren(false); diff --git a/Src/DasherCore/DasherModel.cpp b/Src/DasherCore/DasherModel.cpp index b7189afb..5986895b 100644 --- a/Src/DasherCore/DasherModel.cpp +++ b/Src/DasherCore/DasherModel.cpp @@ -143,7 +143,7 @@ void CDasherModel::Make_root(CDasherNode *pNewRoot) { oldroots.push_back(m_Root); - while((oldroots.size() > 10) && (!m_bRequireConversion || (oldroots[0]->GetConverted()))) { + while((oldroots.size() > 10) && (!m_bRequireConversion || (oldroots[0]->GetFlag(NF_CONVERTED)))) { oldroots[0]->OrphanChild(oldroots[1]); delete oldroots[0]; oldroots.pop_front(); @@ -515,7 +515,7 @@ void CDasherModel::OldPush(myint iMousex, myint iMousey) { } void CDasherModel::RecursiveOutput(CDasherNode *pNode, Dasher::VECTOR_SYMBOL_PROB* pAdded) { - if(pNode->Parent() && (!pNode->Parent()->isSeen())) + if(pNode->Parent() && (!pNode->Parent()->GetFlag(NF_SEEN))) RecursiveOutput(pNode->Parent(), pAdded); if(pNode->Parent()) @@ -523,7 +523,7 @@ void CDasherModel::RecursiveOutput(CDasherNode *pNode, Dasher::VECTOR_SYMBOL_PRO pNode->m_pNodeManager->Enter(pNode); - pNode->Seen(true); + pNode->SetFlag(NF_SEEN, true); pNode->m_pNodeManager->Output(pNode, pAdded, GetLongParameter(LP_NORMALIZATION)); } @@ -644,7 +644,7 @@ void CDasherModel::HandleOutput(CDasherNode *pNewNode, CDasherNode *pOldNode, Da if(pNewNode != pOldNode) DeleteCharacters(pNewNode, pOldNode, pNumDeleted); - if(pNewNode->isSeen()) + if(pNewNode->GetFlag(NF_SEEN)) return; RecursiveOutput(pNewNode, pAdded); @@ -660,13 +660,13 @@ bool CDasherModel::DeleteCharacters(CDasherNode *newnode, CDasherNode *oldnode, // This deals with the trivial instance - we're reversing back over // text that we've seen already - if(newnode->isSeen() == true) { + if(newnode->GetFlag(NF_SEEN)) { if(oldnode->Parent() == newnode) { oldnode->m_pNodeManager->Undo(oldnode); oldnode->Parent()->m_pNodeManager->Enter(oldnode->Parent()); if (pNumDeleted != NULL) (*pNumDeleted)++; - oldnode->Seen(false); + oldnode->SetFlag(NF_SEEN, false); return true; } if(DeleteCharacters(newnode, oldnode->Parent(), pNumDeleted) == true) { @@ -674,7 +674,7 @@ bool CDasherModel::DeleteCharacters(CDasherNode *newnode, CDasherNode *oldnode, oldnode->Parent()->m_pNodeManager->Enter(oldnode->Parent()); if (pNumDeleted != NULL) (*pNumDeleted)++; - oldnode->Seen(false); + oldnode->SetFlag(NF_SEEN, false); return true; } } @@ -683,13 +683,13 @@ bool CDasherModel::DeleteCharacters(CDasherNode *newnode, CDasherNode *oldnode, // Find the last seen node on the new branch CDasherNode *lastseen = newnode->Parent(); - while(lastseen != NULL && lastseen->isSeen() == false) { + while(lastseen != NULL && !(lastseen->GetFlag(NF_SEEN))) { lastseen = lastseen->Parent(); }; // Delete back to last seen node while(oldnode != lastseen) { - oldnode->Seen(false); + oldnode->SetFlag(NF_SEEN, false); oldnode->m_pNodeManager->Undo(oldnode); @@ -717,21 +717,22 @@ void CDasherModel::EnterText(CLanguageModel::Context context, string TheText) co void CDasherModel::Push_Node(CDasherNode *pNode) { - if(pNode->HasAllChildren()) { + if(pNode->GetFlag(NF_ALLCHILDREN)) { DASHER_ASSERT(pNode->Children().size() > 0); // if there are children just give them a poke CDasherNode::ChildMap::iterator i; for(i = pNode->Children().begin(); i != pNode->Children().end(); i++) - (*i)->Alive(true); + (*i)->SetFlag(NF_ALIVE, true); return; } // TODO: Do we really need to delete all of the children at this point? pNode->Delete_children(); - pNode->Alive(true); + pNode->SetFlag(NF_ALIVE, true); + pNode->m_pNodeManager->PopulateChildren(pNode); - pNode->SetHasAllChildren(true); + pNode->SetFlag(NF_ALLCHILDREN, true); } void CDasherModel::Recursive_Push_Node(CDasherNode *pNode, int iDepth) { @@ -794,7 +795,7 @@ bool CDasherModel::CheckForNewRoot(CDasherView *pView) { // Find whether there is exactly one alive child; if more, we don't care. CDasherNode::ChildMap::iterator i; for(i = children.begin(); i != children.end(); i++) { - if((*i)->Alive()) { + if((*i)->GetFlag(NF_ALIVE)) { alive++; theone = *i; if(alive > 1) diff --git a/Src/DasherCore/DasherNode.cpp b/Src/DasherCore/DasherNode.cpp index 4e85bee8..32d649b5 100644 --- a/Src/DasherCore/DasherNode.cpp +++ b/Src/DasherCore/DasherNode.cpp @@ -22,13 +22,8 @@ static char THIS_FILE[] = __FILE__; // TODO: put this back to being inlined CDasherNode::~CDasherNode() { - // Release any storage that the node manager has allocated, // unreference ref counted stuff etc. - - if(m_bWatchDelete) - std::cout << "Deleting " << this << std::endl; - m_pNodeManager->ClearNode( this ); Delete_children(); @@ -63,8 +58,10 @@ bool CDasherNode::NodeIsParent(CDasherNode *oldnode) const { CDasherNode *const CDasherNode::Get_node_under(int iNormalization, myint miY1, myint miY2, myint miMousex, myint miMousey) { myint miRange = miY2 - miY1; -// m_iAge=0; - m_bAlive = true; + + // TODO: Manipulating flags in a 'get' method? + SetFlag(NF_ALIVE, true); + ChildMap::const_iterator i; for(i = GetChildren().begin(); i != GetChildren().end(); i++) { CDasherNode *pChild = *i; @@ -94,7 +91,7 @@ void CDasherNode::OrphanChild(CDasherNode *pChild) { pChild->SetParent(NULL); Children().clear(); - SetHasAllChildren(false); + SetFlag(NF_ALLCHILDREN, false); } // Delete nephews of the child which has the specified symbol @@ -117,7 +114,7 @@ void CDasherNode::Delete_children() { delete (*i); } Children().clear(); - SetHasAllChildren(false); + SetFlag(NF_ALLCHILDREN, false); } // Gets the probability of this node, conditioned on the parent @@ -126,10 +123,10 @@ double CDasherNode::GetProb(int iNormalization) { } void CDasherNode::ConvertWithAncestors() { - if(m_bConverted) + if(GetFlag(NF_CONVERTED)) return; - m_bConverted = true; + SetFlag(NF_CONVERTED, true); if(m_pParent) m_pParent->ConvertWithAncestors(); diff --git a/Src/DasherCore/DasherNode.h b/Src/DasherCore/DasherNode.h index 060f1541..c5ea6af5 100644 --- a/Src/DasherCore/DasherNode.h +++ b/Src/DasherCore/DasherNode.h @@ -17,6 +17,12 @@ // Node flag constants #define NF_COMMITTED 1 +#define NF_ACTIVE 2 +#define NF_ALIVE 4 +#define NF_SEEN 8 +#define NF_CONVERTED 16 +#define NF_GAME 32 +#define NF_ALLCHILDREN 64 // CDasherNode represents a rectangle and character @@ -25,6 +31,8 @@ namespace Dasher { class CDasherModel; } +// TODO: Move rendering code into node? + class Dasher::CDasherNode:private NoClones { public: CDasherNode(CDasherNode * parent, symbol Symbol, int iphase, Opts::ColorSchemes ColorScheme, int ilbnd, int ihbnd, CLanguageModel * lm, int Colour); @@ -61,36 +69,12 @@ class Dasher::CDasherNode:private NoClones { inline int Hbnd() const; void SetRange(int iLower, int iUpper) { - // std::cout << "Setting range: " << iLower << " " << iUpper << std::endl; - m_iLbnd = iLower; m_iHbnd = iUpper; }; inline int Range() const; - // 'Alive' - this could do with an overhaul - bool Alive() const { - return m_bAlive; - } - - void Alive(bool b) { - m_bAlive = b; - } - - void Kill() { - m_bAlive = 0; - } - - // 'Seen' - this could do with an overhaul - bool isSeen() const { - return m_bSeen; - } - - void Seen(bool seen) { - m_bSeen = seen; - } - int Colour() const { return m_iColour; } @@ -99,29 +83,10 @@ class Dasher::CDasherNode:private NoClones { m_iColour = iColour; } - void SetGame(bool bInGame) { - m_bInGame = bInGame; - } - - bool GetGame() { - return m_bInGame; - } - - bool HasAllChildren() const { - return m_bHasAllChildren; - }; - - void SetHasAllChildren(bool val) { - m_bHasAllChildren = val; - }; // Get the probability of this node double GetProb(int iNormalization); - bool GetConverted() { - return m_bConverted; - }; - /// Ensure that this node is marked as being converted, together with /// all of its ancestors (assuming that unconverted nodes are /// 'contiguous' at the brances of the tree). @@ -129,7 +94,6 @@ class Dasher::CDasherNode:private NoClones { void ConvertWithAncestors(); - // New stuff CNodeManager *m_pNodeManager; /// Pointer for the node manager to do with as it sees fit :-) @@ -148,11 +112,6 @@ class Dasher::CDasherNode:private NoClones { std::string m_strDisplayText; - /// - /// Whether this node shoves or not - /// - - bool m_bShove; int MostProbableChild() { @@ -171,24 +130,23 @@ class Dasher::CDasherNode:private NoClones { SGroupInfo *m_pBaseGroup; - // Members only useful for debugging purposes - bool m_bWatchDelete; // Notify when this node is deleted - /// /// Set various flags corresponding to the state of the node. The following flags are defined: /// /// NF_COMMITTED - Node is 'above' the root, so corresponding symbol /// has been added to text box, language model trained etc /// - /// NF_ACTIVE - Not yet implemented + /// NF_ACTIVE - Node is a decendent of the root node (TODO: Isnt this everything?) + /// + /// NF_ALIVE - Node is large enough to be displayed /// - /// NF_ALIVE - Not yet implemented + /// NF_SEEN - Node has already been output /// - /// NF_SEEN - Not yet implemented + /// NF_CONVERTED - Node has been converted (eg Japanese mode) /// - /// NF_CONVERTED - Not yet implemented + /// NF_GAME - Node is on the path in game mode /// - /// NF_GAME - Not yet implemented + /// NF_ALLCHILDREN - Node has all children (TODO: obsolete?) /// void SetFlag(int iFlag, bool bValue) { @@ -204,34 +162,38 @@ class Dasher::CDasherNode:private NoClones { /// Get the value of a flag for this node /// - bool GetFlag(int iFlag) { + bool GetFlag(int iFlag) const { return (m_iFlags & iFlag); } + // Temporary placeholders: + + void SetShove(bool bShove) { + m_bShove = bShove; + } + + bool GetShove() { + return m_bShove; + } + private: int m_iColour; // for the advanced colour mode int m_iLbnd; int m_iHbnd; // the cumulative lower and upper bound prob relative to parent - // Information concerning the behaviour of the node - bool m_bIsActive; // true if descendent of a root node int m_iRefCount; // reference count if ancestor of (or equal to) root node - bool m_bAlive; // if true, then display node, else dont bother - bool m_bSeen; // if true, node has been output already - - // Whether this node has been converted (and can therefore be safely deleted) - bool m_bConverted; - // Whether this is on the game mode path - bool m_bInGame; - - // Information internal to the data structure ChildMap m_mChildren; // pointer to array of children - bool m_bHasAllChildren; // true if we haven't deleted any children after instantiating them CDasherNode *m_pParent; // pointer to parent // Binary flags representing the state of the node int m_iFlags; + + /// + /// Whether this node shoves or not + /// + + bool m_bShove; }; ///////////////////////////////////////////////////////////////////////////// @@ -242,22 +204,17 @@ using namespace Dasher; using namespace Opts; #include "DasherModel.h" -inline CDasherNode::CDasherNode(CDasherNode *pParent, symbol Symbol, int iphase, ColorSchemes ColorScheme, int ilbnd, int ihbnd, CLanguageModel *lm, int Colour =-1) - : m_mChildren() { - +inline CDasherNode::CDasherNode(CDasherNode *pParent, symbol Symbol, int iphase, ColorSchemes ColorScheme, int ilbnd, int ihbnd, CLanguageModel *lm, int Colour =-1) { + m_iLbnd = ilbnd; m_iHbnd = ihbnd; - m_bIsActive = true; - m_bHasAllChildren = false; - m_iRefCount = 0; - m_bAlive = true; m_iColour = Colour; - m_bSeen = false; m_pParent = pParent; - m_bConverted = false; - m_bInGame = false; - m_bWatchDelete = false; + // Default flags + m_iFlags = NF_ACTIVE | NF_ALIVE; + + m_iRefCount = 0; } inline int CDasherNode::Lbnd() const { @@ -273,9 +230,6 @@ inline int CDasherNode::Range() const { } inline CDasherNode::ChildMap &CDasherNode::Children() { - /* // DJW - please make sure DASHER_ASSERT is implemented on your platform - DASHER_ASSERT(m_mChildren.size()==0); - m_mChildren = mChildren; */ return m_mChildren; } diff --git a/Src/DasherCore/DasherViewSquare.cpp b/Src/DasherCore/DasherViewSquare.cpp index 37d4a036..4a3b3842 100644 --- a/Src/DasherCore/DasherViewSquare.cpp +++ b/Src/DasherCore/DasherViewSquare.cpp @@ -120,9 +120,9 @@ int CDasherViewSquare::RecursiveRender(CDasherNode *pRender, myint y1, myint y2, ++m_iRenderCount; - if(bDraw && !RenderNode(pRender->Colour(), y1, y2, mostleft, pRender->m_strDisplayText, pRender->m_bShove) && !(pRender->GetGame())) { + if(bDraw && !RenderNode(pRender->Colour(), y1, y2, mostleft, pRender->m_strDisplayText, pRender->GetShove()) && !(pRender->GetFlag(NF_GAME))) { vDeleteList.push_back(pRender); - pRender->Kill(); + pRender->SetFlag(NF_ALIVE, false); return 0; } @@ -131,7 +131,7 @@ int CDasherViewSquare::RecursiveRender(CDasherNode *pRender, myint y1, myint y2, return 0; } - if(pRender->GetGame()) + if(pRender->GetFlag(NF_GAME)) *iGamePointer = (y1 + y2) / 2; // Render groups @@ -150,11 +150,11 @@ int CDasherViewSquare::RecursiveRender(CDasherNode *pRender, myint y1, myint y2, // FIXME - make the threshold a parameter - if((newy2 - newy1 > 50) || (pChild->Alive())) { - pChild->Alive(true); + if((newy2 - newy1 > 50) || (pChild->GetFlag(NF_ALIVE))) { + pChild->SetFlag(NF_ALIVE, true); RecursiveRender(pChild, newy1, newy2, mostleft, vNodeList, vDeleteList, iGamePointer, true); } - else if(pRender->GetGame()) { + else if(pRender->GetFlag(NF_GAME)) { RecursiveRender(pChild, newy1, newy2, mostleft, vNodeList, vDeleteList, iGamePointer, false); } } -- 2.11.4.GIT