7#include "prefixstore.h"
9#include "katepartdebug.h"
11void KatePrefixStore::addPrefix(
const QString &prefix)
16 if (m_prefixSet.contains(prefix)) {
19 unsigned long long state = 0;
20 for (
int i = 0; i < prefix.
length(); ++i) {
21 QChar c = prefix.
at(i);
23 CharToOccurrenceStateHash &hash = m_transitionFunction[state];
24 CharToOccurrenceStateHash::iterator it = hash.find(c.
unicode());
25 if (it == hash.end()) {
26 state = nextFreeState();
27 hash[c.
unicode()] = QPair<unsigned int, unsigned long long>(1, state);
35 m_acceptingStates.insert(state);
37 m_prefixSet.insert(prefix);
39 if (prefix.
length() > m_longestPrefixLength) {
40 m_longestPrefixLength = prefix.
length();
44void KatePrefixStore::removePrefix(
const QString &prefix)
49 if (!m_prefixSet.contains(prefix)) {
52 m_prefixSet.remove(prefix);
54 unsigned long long state = 0;
55 for (
int i = 0; i < prefix.
length(); ++i) {
56 QChar c = prefix.
at(i);
58 CharToOccurrenceStateHash &hash = m_transitionFunction[state];
59 CharToOccurrenceStateHash::iterator it = hash.find(c.
unicode());
60 if (it == hash.end()) {
65 if (m_acceptingStates.contains(state) && i == prefix.
length() - 1) {
66 m_acceptingStates.remove(state);
69 if ((*it).first <= 1) {
71 m_stateFreeList.push_back(state);
77 if (prefix.
length() == m_longestPrefixLength) {
78 m_longestPrefixLength = computeLongestPrefixLength();
82void KatePrefixStore::dump()
84 for (
unsigned long long i = 0; i < m_lastAssignedState; ++i) {
85 CharToOccurrenceStateHash &hash = m_transitionFunction[i];
86 for (CharToOccurrenceStateHash::iterator it = hash.begin(); it != hash.end(); ++it) {
87 qCDebug(LOG_KTE) << i <<
"x" << QChar(it.key()) <<
"->" << it.value().first <<
"x" << it.value().second;
90 qCDebug(LOG_KTE) <<
"Accepting states" << m_acceptingStates;
95 unsigned long long state = 0;
98 const CharToOccurrenceStateHash &hash = m_transitionFunction[state];
99 CharToOccurrenceStateHash::const_iterator it = hash.
find(c.
unicode());
100 if (it == hash.
end()) {
104 state = (*it).second;
105 if (m_acceptingStates.contains(state)) {
114 unsigned long long state = 0;
117 const CharToOccurrenceStateHash &hash = m_transitionFunction[state];
118 CharToOccurrenceStateHash::const_iterator it = hash.
find(c.
unicode());
119 if (it == hash.
end()) {
123 state = (*it).second;
124 if (m_acceptingStates.contains(state)) {
131int KatePrefixStore::longestPrefixLength()
const
133 return m_longestPrefixLength;
136void KatePrefixStore::clear()
138 m_longestPrefixLength = 0;
140 m_transitionFunction.clear();
141 m_acceptingStates.clear();
142 m_stateFreeList.clear();
143 m_lastAssignedState = 0;
146int KatePrefixStore::computeLongestPrefixLength()
149 for (QSet<QString>::const_iterator i = m_prefixSet.cbegin(); i != m_prefixSet.cend(); ++i) {
150 qCDebug(LOG_KTE) <<
"length" << (*i).length();
151 toReturn = qMax(toReturn, (*i).length());
156unsigned long long KatePrefixStore::nextFreeState()
158 if (!m_stateFreeList.isEmpty()) {
159 return m_stateFreeList.takeFirst();
161 return ++m_lastAssignedState;
QString findPrefix(const QString &s, int start=0) const
Returns the shortest prefix of the given string that is contained in this prefix store starting at po...
Class representing a single text line.
QString string(int column, int length) const
Returns the substring with length beginning at the given column.
int length() const
Returns the line's length.
QChar at(int column) const
Returns the character at the given column.
Q_SCRIPTABLE QString start(QString train="")
iterator find(const Key &key)
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const