7#include "prefixstore.h"
9#include "katepartdebug.h"
11void KatePrefixStore::addPrefix(
const QString &prefix)
19 unsigned long long state = 0;
20 for (
int i = 0; i < prefix.
length(); ++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)
52 m_prefixSet.
remove(prefix);
54 unsigned long long state = 0;
55 for (
int i = 0; i < prefix.
length(); ++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) {
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;
100 if (it == hash.
end()) {
104 state = (*it).second;
105 if (m_acceptingStates.
contains(state)) {
114 unsigned long long state = 0;
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()
150 qCDebug(LOG_KTE) <<
"length" << (*i).length();
151 toReturn = qMax(toReturn, (*i).length());
156unsigned long long KatePrefixStore::nextFreeState()
158 if (!m_stateFreeList.
isEmpty()) {
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 Q_NOREPLY void start()
iterator find(const Key &key)
bool isEmpty() const const
void push_back(parameter_type value)
const_iterator cbegin() const const
const_iterator cend() const const
bool contains(const QSet< T > &other) const const
iterator insert(const T &value)
bool remove(const T &value)
const QChar at(qsizetype position) const const
bool isEmpty() const const
qsizetype length() const const
QString mid(qsizetype position, qsizetype n) const const