HowManyAreAnalyzed(): use status_user_agent to report progress
[linguistica.git] / TerminalRuleCollection.cpp
blobf9508f2872b287d34e01fb9f2e5bae2887f1fd24
1 // Implementation of CTerminalRuleCollection methods
2 // Copyright © 2009 The University of Chicago
3 #include "TerminalRuleCollection.h"
4 #include <Q3PtrList>
5 #include <QString>
6 #include <QList>
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 )
26 CNode* pTerminal;
27 int Result;
28 QString Key;
29 CStringSurrogate cssKey;
31 cssKey = pRule->GetKey();
32 pTerminal = Insert( cssKey, &Result );
33 if( Result == 1 )
35 pTerminal->SetPointer( pRule );
37 else
39 Q_ASSERT( FALSE && "Rule should not already exist, ignoring it." );
42 m_SortValidFlag = FALSE;
43 m_HashHasChangedFlag = TRUE;
45 return pRule;
49 void CTerminalRuleCollection::FindSubstrings( CStringSurrogate& string, Q3PtrList<CTerminalRule>& substrings )
51 CTerminalRule* substring;
52 CStringSurrogate ss = string;
53 int step;
55 CNode* pNode = m_Root,
56 * qNode = pNode->FindLetter( ss[0] );
58 while( qNode )
60 substring = (CTerminalRule*) qNode->Get_T_Pointer();
61 if( substring ) substrings.append( substring );
63 step = qNode->m_BreakPoint - pNode->m_BreakPoint;
64 ss = ss.Mid( step );
66 pNode = qNode;
67 qNode = pNode->FindLetter( ss[0] );
72 int CTerminalRuleCollection::GetShortest()
74 Sort( LENGTH );
75 CTerminalRule* pTR = GetAtSort( GetCount() - 1 );
76 return pTR->GetKeyLength();
80 // Breadth first search in trie
82 int shortest = -1, depth;
83 int* pInt;
85 QPtrQueue<CNode> nodeQ;
86 QPtrQueue<int> depthQ;
88 CNode* pNode;
90 nodeQ.enqueue( m_Root );
91 depthQ.enqueue( new int( 0 ) );
92 pNode = nodeQ.dequeue();
93 pInt = depthQ.dequeue();
94 depth = *pInt;
95 delete pInt;
97 while( pNode )
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 );
108 return shortest;
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;
122 else depth = -1;
123 delete pInt;
126 depthQ.setAutoDelete( TRUE );
127 return -1;