1 // Implementation of CTerminalRuleCollection methods
2 // Copyright © 2009 The University of Chicago
3 #include "TerminalRuleCollection.h"
7 #include "StringSurrogate.h"
8 #include "GrammarRule.h"
10 //////////////////////////////////////////////////////////////////////
11 // Construction/Destruction
12 //////////////////////////////////////////////////////////////////////
14 CTerminalRuleCollection::CTerminalRuleCollection()
18 CTerminalRuleCollection::~CTerminalRuleCollection()
20 // TODO : This should delete it's own rules if flag set
24 CTerminalRule
* CTerminalRuleCollection::operator<< ( CTerminalRule
* pRule
)
29 CStringSurrogate cssKey
;
31 cssKey
= pRule
->GetKey();
32 pTerminal
= Insert( cssKey
, &Result
);
35 pTerminal
->SetPointer( pRule
);
39 Q_ASSERT( FALSE
&& "Rule should not already exist, ignoring it." );
42 m_SortValidFlag
= FALSE
;
43 m_HashHasChangedFlag
= TRUE
;
49 void CTerminalRuleCollection::FindSubstrings( CStringSurrogate
& string
, Q3PtrList
<CTerminalRule
>& substrings
)
51 CTerminalRule
* substring
;
52 CStringSurrogate ss
= string
;
55 CNode
* pNode
= m_Root
,
56 * qNode
= pNode
->FindLetter( ss
[0] );
60 substring
= (CTerminalRule
*) qNode
->Get_T_Pointer();
61 if( substring
) substrings
.append( substring
);
63 step
= qNode
->m_BreakPoint
- pNode
->m_BreakPoint
;
67 qNode
= pNode
->FindLetter( ss
[0] );
72 int CTerminalRuleCollection::GetShortest()
75 CTerminalRule
* pTR
= GetAtSort( GetCount() - 1 );
76 return pTR
->GetKeyLength();
80 // Breadth first search in trie
82 int shortest = -1, depth;
85 QPtrQueue<CNode> nodeQ;
86 QPtrQueue<int> depthQ;
90 nodeQ.enqueue( m_Root );
91 depthQ.enqueue( new int( 0 ) );
92 pNode = nodeQ.dequeue();
93 pInt = depthQ.dequeue();
99 if( pNode->m_ExistenceFlag )
101 if( shortest < 0 || shortest > ( (CTerminalRule*)pNode->Get_T_Pointer() )->GetKeyLength() )
103 shortest = ( (CTerminalRule*)pNode->Get_T_Pointer() )->GetKeyLength();
105 if( depth >= shortest )
107 depthQ.setAutoDelete( TRUE );
112 int numberOfBranches = pNode->GetNumberOfBranches();
113 for( int i = 0; i < numberOfBranches; i++ )
115 nodeQ.enqueue( pNode->GetBranch(i) );
116 depthQ.enqueue( new int( depth+1 ) );
119 pNode = nodeQ.dequeue();
120 pInt = depthQ.dequeue();
121 if( pInt ) depth = *pInt;
126 depthQ.setAutoDelete( TRUE );