1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
/*******************************************************************************
**
** Filename : levenshteintable.h
** Created on : 08 november, 2003
** Copyright 2003 Otto Bruggeman <bruggie@home.nl>
** Copyright 2011 Dmitry Risenberg <dmitry.risenberg@gmail.com>
**
*******************************************************************************/
/*******************************************************************************
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
*******************************************************************************/
#ifndef LEVENSHTEIN_H
#define LEVENSHTEIN_H
#include <iostream>
#include <QtCore/QString>
#include <kdebug.h>
namespace Diff2 {
class Marker;
class Marker;
/**
* Computes the Levenshtein distance between two sequences.
* The actual sequence contents must be prepended with one virtual item each for easier index access.
*/
template<class SequencePair> class LevenshteinTable
{
public:
LevenshteinTable();
LevenshteinTable( unsigned int width, unsigned int height );
~LevenshteinTable();
public:
int getContent( unsigned int posX, unsigned int posY ) const;
int setContent( unsigned int posX, unsigned int posY, int value );
bool setSize ( unsigned int width, unsigned int height );
unsigned int width() const { return m_width; };
unsigned int height() const { return m_height; };
/** Debug method to check if the table is properly filled */
void dumpLevenshteinTable( void );
/**
* This calculates the levenshtein distance of 2 sequences.
* This object takes ownership of the argument
*/
unsigned int createTable( SequencePair* sequences );
void createListsOfMarkers( void );
int chooseRoute( int c1, int c2, int c3, int current );
protected:
LevenshteinTable( const LevenshteinTable& table );
const LevenshteinTable& operator = ( const LevenshteinTable& table );
private:
unsigned int m_width;
unsigned int m_height;
unsigned int m_size;
unsigned int* m_table;
SequencePair* m_sequences;
};
template<class SequencePair> LevenshteinTable<SequencePair>::LevenshteinTable()
: m_width( 256 ),
m_height( 256 ),
m_size( m_height * m_width ),
m_table( new unsigned int[ m_size ] ),
m_sequences(0)
{
}
template<class SequencePair> LevenshteinTable<SequencePair>::LevenshteinTable( unsigned int width, unsigned int height )
: m_width( width ),
m_height( height ),
m_size( m_width * m_height ),
m_table( new unsigned int[ m_size ] ),
m_sequences(0)
{
}
template<class SequencePair> LevenshteinTable<SequencePair>::~LevenshteinTable()
{
delete[] m_table;
delete m_sequences;
}
template<class SequencePair> int LevenshteinTable<SequencePair>::getContent( unsigned int posX, unsigned int posY ) const
{
// kDebug(8101) << "Width = " << m_width << ", height = " << m_height << ", posX = " << posX << ", posY = " << posY;
return m_table[ posY * m_width + posX ];
}
template<class SequencePair> int LevenshteinTable<SequencePair>::setContent( unsigned int posX, unsigned int posY, int value )
{
m_table[ posY * m_width + posX ] = value;
return 0;
}
template<class SequencePair> bool LevenshteinTable<SequencePair>::setSize( unsigned int width, unsigned int height )
{
// Set a limit of 16.7 million entries, will be about 64 MB of ram, that should be plenty
if ( ( ( width ) * ( height ) ) > ( 256 * 256 * 256 ) )
return false;
if ( ( ( width ) * ( height ) ) > m_size )
{
delete[] m_table;
m_size = width * height;
m_table = new unsigned int[ m_size ];
}
m_width = width;
m_height = height;
return true;
}
template<class SequencePair> void LevenshteinTable<SequencePair>::dumpLevenshteinTable()
{
for ( unsigned int i = 0; i < m_height; ++i )
{
for ( unsigned int j = 0; j < m_width; ++j )
{
std::cout.width( 3 );
std::cout << getContent( j, i );
}
std::cout << std::endl;
}
}
template<class SequencePair> unsigned int LevenshteinTable<SequencePair>::createTable( SequencePair* sequences )
{
m_sequences = sequences;
unsigned int m = m_sequences->lengthFirst();
unsigned int n = m_sequences->lengthSecond();
if ( !setSize( m, n ) )
return 0;
unsigned int i;
unsigned int j;
// initialize first row
for ( i = 0; i < m; ++i )
setContent( i, 0, i );
// initialize first column
for ( j = 0; j < n; ++j )
setContent( 0, j, j );
int cost = 0, north = 0, west = 0, northwest = 0;
QChar si, dj;
// Optimization, calculate row wise instead of column wise, wont trash the cache so much with large strings
for ( j = 1; j < n; ++j )
{
for ( i = 1; i < m; ++i )
{
if (m_sequences->equal(i, j))
cost = 0;
else
cost = SequencePair::allowReplace? 1 : 2;
north = getContent( i, j-1 ) + 1;
west = getContent( i-1, j ) + 1;
northwest = getContent( i-1, j-1 ) + cost;
setContent( i, j, qMin( north, qMin( west, northwest ) ) );
}
}
return getContent( m-1, n-1 );
}
template<class SequencePair> int LevenshteinTable<SequencePair>::chooseRoute( int c1, int c2, int c3, int current )
{
// kDebug(8101) << "c1 = " << c1 << ", c2 = " << c2 << ", c3 = " << c3;
// preference order: c2, c3, c1, hopefully this will work out for me
if ( c2 <= c1 && c2 <= c3 )
{
if ( SequencePair::allowReplace || (c2 == current) )
{
return 1;
}
}
if ( c3 <= c2 && c3 <= c1 )
return 2;
return 0;
}
template<class SequencePair> void LevenshteinTable<SequencePair>::createListsOfMarkers()
{
// kDebug(8101) << source;
// kDebug(8101) << destination;
// dumpLevenshteinTable();
unsigned int x = m_width-1;
unsigned int y = m_height-1;
unsigned int difference = getContent(x, y);
// If the number of differences is more than half the length of the largest string
// dont bother to mark the individual changes
// Patch based on work by Felix Berger as put as attachment to bug 75794
if ( !m_sequences->needFineGrainedOutput(difference) )
{
m_sequences->prependFirst( new Marker( Marker::End, x ) );
m_sequences->prependFirst( new Marker( Marker::Start, 0 ) );
m_sequences->prependSecond( new Marker( Marker::End, y ) );
m_sequences->prependSecond( new Marker( Marker::Start, 0 ) );
return;
}
Marker* c = 0;
int n, nw, w, direction, currentValue;
while ( x > 0 && y > 0 )
{
currentValue = getContent( x, y );
n = getContent( x, y - 1 );
w = getContent( x - 1, y );
nw = getContent( x - 1, y - 1 );
direction = chooseRoute( n, nw, w, currentValue );
switch ( direction )
{
case 0: // north
// kDebug(8101) << "Picking north";
// kDebug(8101) << "Source[" << ( x - 1 ) << "] = " << QString( source[ x-1 ] ) << ", destination[" << ( y - 1 ) << "] = " << QString( destination[ y-1 ] );
if ( !m_sequences->markerListSecond().isEmpty() )
c = m_sequences->markerListSecond().first();
else
c = 0;
if ( c && c->type() == Marker::End )
{
// kDebug(8101) << "CurrentValue: " << currentValue;
if ( n == currentValue )
m_sequences->prependSecond( new Marker( Marker::Start, y ) );
// else: the change continues, do not do anything
}
else
{
// kDebug(8101) << "CurrentValue: " << currentValue;
if ( n < currentValue )
m_sequences->prependSecond( new Marker( Marker::End, y ) );
}
--y;
if ( y == 0 ) {
m_sequences->prependSecond( new Marker( Marker::Start, 0 ) );
}
break;
case 1: // northwest
// kDebug(8101) << "Picking northwest";
// kDebug(8101) << "Source[" << ( x - 1 ) << "] = " << QString( source[ x-1 ] ) << ", destination[" << ( y - 1 ) << "] = " << QString( destination[ y-1 ] );
if ( !m_sequences->markerListSecond().isEmpty() )
c = m_sequences->markerListSecond().first();
else
c = 0;
if ( c && c->type() == Marker::End )
{
// kDebug(8101) << "End found: CurrentValue: " << currentValue;
if ( nw == currentValue )
m_sequences->prependSecond( new Marker( Marker::Start, y ) );
// else: the change continues, do not do anything
}
else
{
// kDebug(8101) << "CurrentValue: " << currentValue;
if ( nw < currentValue )
m_sequences->prependSecond( new Marker( Marker::End, y ) );
}
if ( !m_sequences->markerListFirst().isEmpty() )
c = m_sequences->markerListFirst().first();
else
c = 0;
if ( c && c->type() == Marker::End )
{
// kDebug(8101) << "End found: CurrentValue: " << currentValue;
if ( nw == currentValue )
m_sequences->prependFirst( new Marker( Marker::Start, x ) );
// else: the change continues, do not do anything
}
else
{
// kDebug(8101) << "CurrentValue: " << currentValue;
if ( nw < currentValue )
m_sequences->prependFirst( new Marker( Marker::End, x ) );
}
--y;
--x;
break;
case 2: // west
// kDebug(8101) << "Picking west";
// kDebug(8101) << "Source[" << ( x - 1 ) << "] = " << QString( source[ x-1 ] ) << ", destination[" << ( y - 1 ) << "] = " << QString( destination[ y-1 ] );
if ( !m_sequences->markerListFirst().isEmpty() )
c = m_sequences->markerListFirst().first();
else
c = 0;
if ( c && c->type() == Marker::End )
{
// kDebug(8101) << "End found: CurrentValue: " << currentValue;
if ( w == currentValue )
m_sequences->prependFirst( new Marker( Marker::Start, x ) );
// else: the change continues, do not do anything
}
else
{
// kDebug(8101) << "CurrentValue: " << currentValue;
if ( w < currentValue )
m_sequences->prependFirst( new Marker( Marker::End, x ) );
}
--x;
if (x == 0) {
m_sequences->prependFirst( new Marker( Marker::Start, 0 ) );
}
break;
}
}
// When leaving the loop it does not mean both are 0! If not there is still a change at the beginning of the line we missed so adding now.
if ( x != 0 )
{
m_sequences->prependFirst( new Marker( Marker::End, x ) );
m_sequences->prependFirst( new Marker( Marker::Start, 0 ) );
}
if ( y != 0 )
{
m_sequences->prependSecond( new Marker( Marker::End, y ) );
m_sequences->prependSecond( new Marker( Marker::Start, 0 ) );
}
// kDebug(8101) << "Source string: " << source;
// QStringList list;
// int prevValue = 0;
// MarkerListConstIterator mit = m_sequences->markerListFirst().begin();
// MarkerListConstIterator end = m_sequences->markerListFirst().end();
// for ( ; mit != end; ++mit )
// {
// c = *mit;
// kDebug(8101) << "Source Marker Entry : Type: " << c->type() << ", Offset: " << c->offset();
// list.append( source.mid( prevValue, c->offset() - prevValue ) );
// prevValue = c->offset();
// }
// if ( prevValue < source.length() - 1 )
// {
// list.append( source.mid( prevValue, source.length() - prevValue ) );
// }
// kDebug(8101) << "Source Resulting stringlist : " << list.join("\n");
//
// list.clear();
// prevValue = 0;
//
// kDebug(8101) << "Destination string: " << destination;
// mit = m_sequences->markerListSecond().begin();
// end = m_sequences->markerListSecond().end();
// for ( ; mit != end; ++mit )
// {
// c = *mit;
// kDebug(8101) << "Destination Marker Entry : Type: " << c->type() << ", Offset: " << c->offset();
// list.append( destination.mid( prevValue, c->offset() - prevValue ) );
// prevValue = c->offset();
// }
// if ( prevValue < destination.length() - 1 )
// {
// list.append( destination.mid( prevValue, destination.length() - prevValue ) );
// }
// kDebug(8101) << "Destination Resulting string : " << list.join("\n");
}
} // namespace Diff2
#endif // LEVENSHTEIN_H
|