• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Kate

  • kde-4.14
  • applications
  • kate
  • part
  • spellcheck
prefixstore.cpp
Go to the documentation of this file.
1 /* This file is part of the KDE libraries and the Kate part.
2  *
3  * Copyright (C) 2009 by Michel Ludwig <michel.ludwig@kdemail.net>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Library General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  *
15  * You should have received a copy of the GNU Library General Public License
16  * along with this library; see the file COPYING.LIB. If not, write to
17  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  */
20 
21 #include "prefixstore.h"
22 
23 #include <kdebug.h>
24 
25 #include "katetextline.h"
26 
27 KatePrefixStore::KatePrefixStore()
28 : m_longestPrefixLength(0),
29  m_lastAssignedState(0)
30 {
31 }
32 
33 KatePrefixStore::~KatePrefixStore()
34 {
35 }
36 
37 void KatePrefixStore::addPrefix(const QString& prefix)
38 {
39  if(prefix.isEmpty()) {
40  return;
41  }
42  if(m_prefixSet.contains(prefix)) {
43  return;
44  }
45  unsigned long long state = 0;
46  for(int i = 0; i < prefix.length(); ++i) {
47  QChar c = prefix.at(i);
48 
49  CharToOccurrenceStateHash& hash = m_transitionFunction[state];
50  CharToOccurrenceStateHash::iterator it = hash.find(c.unicode());
51  if(it == hash.end()) {
52  state = nextFreeState();
53  hash[c.unicode()] = QPair<unsigned int, unsigned long long>(1, state);
54  continue;
55  }
56 
57  ++(*it).first;
58  state = (*it).second;
59  }
60  // add the last state as accepting state
61  m_acceptingStates.insert(state);
62 
63  m_prefixSet.insert(prefix);
64 
65  if(prefix.length() > m_longestPrefixLength) {
66  m_longestPrefixLength = prefix.length();
67  }
68 }
69 
70 void KatePrefixStore::removePrefix(const QString& prefix)
71 {
72  if(prefix.isEmpty()) {
73  return;
74  }
75  if(!m_prefixSet.contains(prefix)) {
76  return;
77  }
78  m_prefixSet.remove(prefix);
79 
80  unsigned long long state = 0;
81  for(int i = 0; i < prefix.length(); ++i) {
82  QChar c = prefix.at(i);
83 
84  CharToOccurrenceStateHash& hash = m_transitionFunction[state];
85  CharToOccurrenceStateHash::iterator it = hash.find(c.unicode());
86  if(it == hash.end()) {
87  continue;
88  }
89 
90  state = (*it).second;
91  if(m_acceptingStates.contains(state) && i == prefix.length() - 1) {
92  m_acceptingStates.remove(state);
93  }
94 
95  if((*it).first <= 1) {
96  hash.erase(it);
97  m_stateFreeList.push_back(state);
98  }
99  else {
100  --(*it).first;
101  }
102  }
103 
104  if(prefix.length() == m_longestPrefixLength) {
105  m_longestPrefixLength = computeLongestPrefixLength();
106  }
107 }
108 
109 void KatePrefixStore::dump()
110 {
111  for(unsigned long long i = 0; i < m_lastAssignedState; ++i) {
112  CharToOccurrenceStateHash& hash = m_transitionFunction[i];
113  for(CharToOccurrenceStateHash::iterator it = hash.begin(); it != hash.end(); ++it) {
114  kDebug() << i << "x" << QChar(it.key()) << "->" << it.value().first << "x" << it.value().second;
115  }
116  }
117  kDebug() << "Accepting states" << m_acceptingStates;
118 }
119 
120 QString KatePrefixStore::findPrefix(const QString& s, int start) const
121 {
122  unsigned long long state = 0;
123  for(int i = start; i < s.length(); ++i) {
124  QChar c = s.at(i);
125  const CharToOccurrenceStateHash& hash = m_transitionFunction[state];
126  CharToOccurrenceStateHash::const_iterator it = hash.find(c.unicode());
127  if(it == hash.end()) {
128  return QString();
129  }
130 
131  state = (*it).second;
132  if(m_acceptingStates.contains(state)) {
133  return s.mid(start, i + 1 - start);
134  }
135  }
136  return QString();
137 }
138 
139 QString KatePrefixStore::findPrefix(const Kate::TextLine& line, int start) const
140 {
141  unsigned long long state = 0;
142  for(int i = start; i < line->length(); ++i) {
143  QChar c = line->at(i);
144  const CharToOccurrenceStateHash& hash = m_transitionFunction[state];
145  CharToOccurrenceStateHash::const_iterator it = hash.find(c.unicode());
146  if(it == hash.end()) {
147  return QString();
148  }
149 
150  state = (*it).second;
151  if(m_acceptingStates.contains(state)) {
152  return line->string(start, i + 1 - start);
153  }
154  }
155  return QString();
156 }
157 
158 int KatePrefixStore::longestPrefixLength() const
159 {
160  return m_longestPrefixLength;
161 }
162 
163 void KatePrefixStore::clear()
164 {
165  m_longestPrefixLength = 0;
166  m_prefixSet.clear();
167  m_transitionFunction.clear();
168  m_acceptingStates.clear();
169  m_stateFreeList.clear();
170  m_lastAssignedState = 0;
171 }
172 
173 int KatePrefixStore::computeLongestPrefixLength()
174 {
175  int toReturn = 0;
176  for(QSet<QString>::iterator i = m_prefixSet.begin();
177  i != m_prefixSet.end(); ++i) {
178  kDebug() << "length" << (*i).length();
179  toReturn = qMax(toReturn, (*i).length());
180  }
181  return toReturn;
182 }
183 
184 unsigned long long KatePrefixStore::nextFreeState()
185 {
186  if(!m_stateFreeList.isEmpty()) {
187  return m_stateFreeList.takeFirst();
188  }
189  return ++m_lastAssignedState;
190 }
KatePrefixStore::m_transitionFunction
TransitionFunction m_transitionFunction
Definition: prefixstore.h:76
QList::clear
void clear()
katetextline.h
KatePrefixStore::longestPrefixLength
int longestPrefixLength() const
Definition: prefixstore.cpp:158
KatePrefixStore::m_longestPrefixLength
int m_longestPrefixLength
Definition: prefixstore.h:70
KatePrefixStore::clear
void clear()
Definition: prefixstore.cpp:163
QList::push_back
void push_back(const T &value)
QChar
KatePrefixStore::m_stateFreeList
QList< unsigned long long > m_stateFreeList
Definition: prefixstore.h:78
KatePrefixStore::removePrefix
void removePrefix(const QString &prefix)
Definition: prefixstore.cpp:70
QSet::insert
const_iterator insert(const T &value)
KatePrefixStore::KatePrefixStore
KatePrefixStore()
Definition: prefixstore.cpp:27
QHash::iterator
QSharedPointer
QHash
QList::isEmpty
bool isEmpty() const
KatePrefixStore::nextFreeState
unsigned long long nextFreeState()
Definition: prefixstore.cpp:184
QString::isEmpty
bool isEmpty() const
QHash::begin
iterator begin()
QSet
QString
QChar::unicode
ushort unicode() const
KatePrefixStore::m_lastAssignedState
unsigned long long m_lastAssignedState
Definition: prefixstore.h:79
QHash::erase
iterator erase(iterator pos)
QPair
QHash::clear
void clear()
QHash::find
iterator find(const Key &key)
QSet::begin
iterator begin()
KatePrefixStore::dump
void dump()
Definition: prefixstore.cpp:109
QSet::contains
bool contains(const T &value) const
QHash::const_iterator
KatePrefixStore::addPrefix
void addPrefix(const QString &prefix)
Definition: prefixstore.cpp:37
QString::mid
QString mid(int position, int n) const
QSet::remove
bool remove(const T &value)
QList::takeFirst
T takeFirst()
QSet::end
iterator end()
QString::at
const QChar at(int position) const
KatePrefixStore::computeLongestPrefixLength
int computeLongestPrefixLength()
Definition: prefixstore.cpp:173
QString::length
int length() const
KatePrefixStore::m_acceptingStates
QSet< unsigned long long > m_acceptingStates
Definition: prefixstore.h:77
QHash::end
iterator end()
QSet::clear
void clear()
KatePrefixStore::findPrefix
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...
Definition: prefixstore.cpp:120
KatePrefixStore::~KatePrefixStore
virtual ~KatePrefixStore()
Definition: prefixstore.cpp:33
prefixstore.h
KatePrefixStore::m_prefixSet
QSet< QString > m_prefixSet
Definition: prefixstore.h:71
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:59 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kate

Skip menu "Kate"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal