Diff2.st
author Claus Gittinger <cg@exept.de>
Wed, 05 Feb 2014 19:59:20 +0100
changeset 13845 5092330709c7
child 13885 d01470638c24
permissions -rw-r--r--
initial checkin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
13845
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     1
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     2
 Copyright (c) 2007-2012 Tony Garnock-Jones
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     3
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     4
 This code is based on Squeak's DiffMerge package
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     5
 written by Tony Garnock-Jones. Original project's web site:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     6
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     7
 http://www.squeaksource.com/DiffMerge
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     8
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
     9
 Permission is hereby granted, free of charge, to any person
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    10
 obtaining a copy of this software and associated documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    11
 files (the 'Software'), to deal in the Software without
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    12
 restriction, including without limitation the rights to use,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
 copy, modify, merge, publish, distribute, sublicense, and/or sell
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
 copies of the Software, and to permit persons to whom the
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
 Software is furnished to do so, subject to the following
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    16
 conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
 The above copyright notice and this permission notice shall be
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
 included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    22
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    23
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    24
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    25
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    26
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    27
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    28
 OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    29
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    30
"{ Package: 'stx:libtool' }"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    31
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    32
Object subclass:#Diff2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    33
	instanceVariableNames:'file1 file2'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    34
	classVariableNames:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    35
	poolDictionaries:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    36
	category:'Collections-Sequenceable-Diff2'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    37
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    38
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    39
Object subclass:#Chunk
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    40
	instanceVariableNames:'offset length'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    41
	classVariableNames:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    42
	poolDictionaries:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    43
	privateIn:Diff2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    44
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    45
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    46
Diff2 subclass:#HuntMcilroy
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    47
	instanceVariableNames:'lcs'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    48
	classVariableNames:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    49
	poolDictionaries:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    50
	privateIn:Diff2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    51
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    52
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    53
Object subclass:#Candidate
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    54
	instanceVariableNames:'file1index file2index chain'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    55
	classVariableNames:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    56
	poolDictionaries:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    57
	privateIn:Diff2::HuntMcilroy
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    58
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    59
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    60
Diff2 subclass:#MyersUkkonen
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
	instanceVariableNames:'lcs'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    62
	classVariableNames:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    63
	poolDictionaries:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    64
	privateIn:Diff2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    65
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    66
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    67
Object subclass:#Patch
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    68
	instanceVariableNames:'chunks snippets'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    69
	classVariableNames:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    70
	poolDictionaries:''
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    71
	privateIn:Diff2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    72
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    73
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    74
Diff2 comment:'Generic diff/comm utilities. Agnostic as to the longestCommonSubsequence algorithm used.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    75
Instance Variables
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    76
	file1:		<SequenceableCollection> One of the two files to compare.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    77
	file2:		<SequenceableCollection> The other of the files to compare.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    78
-- 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    79
Copyright (c) 2008 Tony Garnock-Jones <tonyg@lshift.net>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    80
Copyright (c) 2008 LShift Ltd. <query@lshift.net>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    81
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction,including without limitation the rights to use, copy, modify, merge,publish, distribute, sublicense, and/or sell copies of the Software,and to permit persons to whom the Software is furnished to do so,subject to the following conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    82
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    83
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    84
'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    85
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    86
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    87
!Diff2 class methodsFor:'documentation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    88
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    89
copyright
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    90
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    91
 Copyright (c) 2007-2012 Tony Garnock-Jones
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    92
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    93
 This code is based on Squeak's DiffMerge package
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    94
 written by Tony Garnock-Jones. Original project's web site:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    95
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    96
 http://www.squeaksource.com/DiffMerge
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    97
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    98
 Permission is hereby granted, free of charge, to any person
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    99
 obtaining a copy of this software and associated documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   100
 files (the 'Software'), to deal in the Software without
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   101
 restriction, including without limitation the rights to use,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   102
 copy, modify, merge, publish, distribute, sublicense, and/or sell
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   103
 copies of the Software, and to permit persons to whom the
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   104
 Software is furnished to do so, subject to the following
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   105
 conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   106
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   107
 The above copyright notice and this permission notice shall be
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   108
 included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   109
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   110
 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   111
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   112
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   113
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   114
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   115
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   116
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   117
 OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   118
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   119
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   120
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   121
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   122
documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   123
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   124
Generic diff/comm utilities. Agnostic as to the longestCommonSubsequence algorithm used.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   125
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   126
Instance Variables
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   127
        file1:          <SequenceableCollection> One of the two files to compare.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   128
        file2:          <SequenceableCollection> The other of the files to compare.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   129
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   130
    [author:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   131
        Tony Garnock-Jones <tonyg@lshift.com>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   132
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   133
    [instance variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   134
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   135
    [class variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   136
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   137
    [see also:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   138
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   139
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   140
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   141
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   142
!Diff2 class methodsFor:'instance creation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   143
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   144
new
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   145
    "I'm abstract, so instantiate some default here"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   146
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   147
    ^self == Diff2 ifTrue:[
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   148
        HuntMcilroy new
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   149
    ] ifFalse:[
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   150
        self basicNew initialize
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   151
    ]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   152
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   153
    "Created: / 16-03-2012 / 20:16:52 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   154
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   155
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   156
!Diff2 class methodsFor:'accessing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   157
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   158
HuntMcilroy
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   159
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   160
    ^HuntMcilroy
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   161
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   162
    "Created: / 16-03-2012 / 18:54:30 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   163
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   164
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   165
MyersUkkonen
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   166
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   167
    ^MyersUkkonen
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   168
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   169
    "Created: / 16-03-2012 / 18:54:40 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   170
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   171
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   172
!Diff2 methodsFor:'accessing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   173
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   174
file1
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   175
	^ file1
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   176
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   177
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   178
file1: anObject
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   179
	file1 := anObject.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   180
	self emptyCaches.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   181
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   182
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   183
file2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   184
	^ file2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   185
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   186
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   187
file2: anObject
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   188
	file2 := anObject.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   189
	self emptyCaches.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   190
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   191
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   192
!Diff2 methodsFor:'diffing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   193
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   194
comm
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   195
	"Returns a collection of similarities and differences between the two files. Each entry in the resulting collection is either (#common -> {...}) or (#different -> ({...} -> {...}))."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   196
	| result common p1 p2 |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   197
	result := OrderedCollection new.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   198
	p1 := 0.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   199
	p2 := 0.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   200
	common := OrderedCollection new.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   201
	self longestCommonSubsequence do: [:entry |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   202
		common := self maybeAddCommonBlock: common to: result
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   203
						p1: p1 p2: p2 limit1: entry key limit2: entry value.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   204
		common add: (self file1 at: entry key).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   205
		p1 := entry key.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   206
		p2 := entry value.].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   207
	common := self maybeAddCommonBlock: common to: result
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   208
					p1: p1 p2: p2 limit1: file1 size + 1 limit2: file2 size + 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   209
	self addCommonBlock: common ifNonEmptyTo: result.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   210
	^ result asArray
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   211
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   212
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   213
diff
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   214
        "Returns a DiffPatch instance that can be used in future to transform file1 into file2."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   215
        | p |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   216
        p := Diff2::Patch new.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   217
        p initChunks: self diffIndices file1: file1 file2: file2.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   218
        ^ p
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   219
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   220
    "Modified: / 16-03-2012 / 19:16:55 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   221
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   222
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   223
diffIndices
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   224
	"Returns a collection of (DiffChunk -> DiffChunk) associations mapping differing regions of file1 and file2 onto each other."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   225
	| result p1 p2 |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   226
	result := OrderedCollection new.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   227
	p1 := 0.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   228
	p2 := 0.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   229
	self longestCommonSubsequence do: [:entry |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   230
		self maybeAddDiffChunkTo: result p1: p1 p2: p2 limit1: entry key limit2: entry value.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   231
		p1 := entry key.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   232
		p2 := entry value.].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   233
	self maybeAddDiffChunkTo: result p1: p1 p2: p2 limit1: file1 size + 1 limit2: file2 size + 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   234
	^ result asArray
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   235
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   236
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   237
longestCommonSubsequence
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   238
	"The longestCommonSubsequence (LCS) algorithm is at the heart of a diff/comm algorithm."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   239
	self subclassResponsibility.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   240
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   241
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   242
!Diff2 methodsFor:'private'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   243
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   244
addCommonBlock: aSubCollection ifNonEmptyTo: aCollection
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   245
	^ aSubCollection isEmpty
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   246
		ifFalse: [aCollection add: #common -> aSubCollection asArray. OrderedCollection new]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   247
		ifTrue: [aSubCollection]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   248
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   249
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   250
emptyCaches
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   251
	"Subclasses should implement this to clear any cached state they may have built up."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   252
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   253
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   254
maybeAddCommonBlock: common to: result p1: p1 p2: p2 limit1: limit1 limit2: limit2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   255
	"For internal use by comm."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   256
	((p1 + 1 ~= limit1) or: [p2 + 1 ~= limit2])
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   257
			ifTrue: [| newCommon |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   258
					newCommon := self addCommonBlock: common ifNonEmptyTo: result.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   259
					result add: #different -> ((file1 copyFrom: p1 + 1 to: limit1 - 1) ->
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   260
											(file2 copyFrom: p2 + 1 to: limit2 - 1)).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   261
					^ newCommon]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   262
			ifFalse: [^ common].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   263
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   264
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   265
maybeAddDiffChunkTo: result p1: p1 p2: p2 limit1: limit1 limit2: limit2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   266
        "For internal use by diffIndices."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   267
        ((p1 + 1 ~= limit1) or: [p2 + 1 ~= limit2])
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   268
                        ifTrue: [result add: ((Diff2::Chunk offset: p1 + 1 length: limit1 - p1 - 1) ->
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   269
                                                                (Diff2::Chunk offset: p2 + 1 length: limit2 - p2 - 1))].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   270
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   271
    "Modified: / 16-03-2012 / 19:13:01 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   272
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   273
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   274
!Diff2::Chunk class methodsFor:'as yet unclassified'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   275
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   276
negativeSize: s
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   277
	"Returns a pseudo-chunk with *negative* length, useful as a kind of zero for destructiveMergeWith: operations intended to build up coverage over some set of chunks."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   278
	^ self new offset: s + 1; length: s negated
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   279
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   280
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   281
offset: o length: l
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   282
	^ self new offset: o; length: l
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   283
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   284
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   285
!Diff2::Chunk class methodsFor:'documentation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   286
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   287
copyright
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   288
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   289
 Copyright (c) 2007-2012 Tony Garnock-Jones
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   290
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   291
 This code is based on Squeak's DiffMerge package
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   292
 written by Tony Garnock-Jones. Original project's web site:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   293
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   294
 http://www.squeaksource.com/DiffMerge
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   295
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   296
 Permission is hereby granted, free of charge, to any person
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   297
 obtaining a copy of this software and associated documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   298
 files (the 'Software'), to deal in the Software without
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   299
 restriction, including without limitation the rights to use,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   300
 copy, modify, merge, publish, distribute, sublicense, and/or sell
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   301
 copies of the Software, and to permit persons to whom the
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   302
 Software is furnished to do so, subject to the following
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   303
 conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   304
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   305
 The above copyright notice and this permission notice shall be
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   306
 included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   307
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   308
 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   309
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   310
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   311
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   312
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   313
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   314
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   315
 OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   316
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   317
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   318
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   319
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   320
documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   321
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   322
A DiffChunk represents a span of items within a collection (e.g. a collection of lines representing a text file).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   323
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   324
Instance Variables
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   325
        length:                 <Integer> Count of lines within the chunk; 0 is permitted
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   326
        offset:                 <Integer> Index of first line within the chunk; 1-based
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   327
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   328
    [author:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   329
        Tony Garnock-Jones <tonyg@lshift.com>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   330
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   331
    [instance variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   332
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   333
    [class variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   334
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   335
    [see also:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   336
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   337
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   338
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   339
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   340
!Diff2::Chunk methodsFor:'accessing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   341
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   342
correctForSkewFrom: smallerChunk to: biggerChunk
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   343
	"Given a biggerChunk that definitely contains smallerChunk but might have an extra head or tail, updates the receiver to include such an extra head or tail."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   344
	| headSize tailSize |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   345
	headSize := smallerChunk offset - biggerChunk offset.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   346
	tailSize := biggerChunk lastIndex - smallerChunk lastIndex.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   347
	offset := offset - headSize.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   348
	length := length + headSize + tailSize.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   349
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   350
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   351
destructiveMergeWith: aChunk
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   352
	| newLastIndex |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   353
	newLastIndex := self lastIndex max: aChunk lastIndex.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   354
	offset := offset min: aChunk offset.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   355
	length := newLastIndex - offset + 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   356
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   357
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   358
lastIndex
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   359
	"Returns the rightmost index contained in my range. (Offset is the leftmost index.) If my length is zero, will return an index lower than my offset."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   360
	^ offset + length - 1
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   361
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   362
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   363
length
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   364
	^ length
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   365
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   366
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   367
length: anObject
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   368
	length := anObject
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   369
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   370
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   371
offset
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   372
	^ offset
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   373
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   374
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   375
offset: anObject
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   376
	offset := anObject
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   377
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   378
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   379
printOn: aStream
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   380
	aStream
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   381
		nextPut: $(;
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   382
		nextPutAll: self class name;
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   383
		nextPutAll: ' offset: ';
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   384
		nextPutAll: self offset asString;
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   385
		nextPutAll: ' length: ';
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   386
		nextPutAll: self length asString;
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   387
		nextPut: $).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   388
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   389
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   390
!Diff2::Chunk methodsFor:'as yet unclassified'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   391
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   392
extractFrom: aCollection
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   393
	"Extracts a subcollection from aCollection corresponding to my offset and length."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   394
	^ aCollection copyFrom: offset to: offset + length - 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   395
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   396
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   397
extractSafeFrom: aCollection
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   398
    "Extracts a subcollection from aCollection corresponding to my offset and length.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   399
     Returns nil if extraction fails (out of bounds)"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   400
    ^((offset <= aCollection size) and:[(offset + length - 1) <= aCollection size]) ifTrue:[
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   401
        aCollection copyFrom: offset to: offset + length - 1
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   402
    ] ifFalse:[
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   403
        nil
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   404
    ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   405
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
    "Created: / 06-04-2012 / 12:37:28 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   409
!Diff2::Chunk methodsFor:'comparing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   411
< aDiffChunk
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   412
	"Used to sort changed chunks during three-way merge; see Diff3"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   413
	^ self offset < aDiffChunk offset
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   414
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   415
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   416
= otherChunk
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   417
        ^ (otherChunk isKindOf: Diff2::Chunk) and:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   418
        [(self offset = otherChunk offset) and:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   419
        [(self length = otherChunk length)]]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   420
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   421
    "Modified: / 16-03-2012 / 19:13:07 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   422
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   423
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   424
!Diff2::HuntMcilroy class methodsFor:'documentation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   425
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   426
copyright
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   427
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   428
 Copyright (c) 2007-2012 Tony Garnock-Jones
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   429
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   430
 This code is based on Squeak's DiffMerge package
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   431
 written by Tony Garnock-Jones. Original project's web site:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   432
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   433
 http://www.squeaksource.com/DiffMerge
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   434
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   435
 Permission is hereby granted, free of charge, to any person
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   436
 obtaining a copy of this software and associated documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   437
 files (the 'Software'), to deal in the Software without
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   438
 restriction, including without limitation the rights to use,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   439
 copy, modify, merge, publish, distribute, sublicense, and/or sell
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   440
 copies of the Software, and to permit persons to whom the
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   441
 Software is furnished to do so, subject to the following
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   442
 conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   443
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   444
 The above copyright notice and this permission notice shall be
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   445
 included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   446
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   447
 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   448
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   449
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   450
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   451
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   452
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   453
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   454
 OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   455
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   456
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   457
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   458
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   459
documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   460
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   461
A HuntMcilroyDiff provides a longestCommonSubsequence algorithm following Hunt and McIlroy 1976 for use by the methods on GenericDiff.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   462
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   463
J. W. Hunt and M. D. McIlroy, An algorithm for differential file comparison, Bell Telephone Laboratories CSTR #41 (1976).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   464
http://www.cs.dartmouth.edu/~doug/
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   465
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   466
Instance Variables
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   467
        lcs:            cached longest common subsequence
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   468
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   469
    [author:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   470
        Tony Garnock-Jones <tonyg@lshift.com>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   471
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   472
    [instance variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   473
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   474
    [class variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   475
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   476
    [see also:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   477
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   478
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   479
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   480
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   481
!Diff2::HuntMcilroy methodsFor:'accessing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   482
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   483
emptyCaches
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   484
	lcs := nil.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   485
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   486
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   487
!Diff2::HuntMcilroy methodsFor:'diffing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   488
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   489
longestCommonSubsequence
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   490
        | equivalenceClasses candidates |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   491
        lcs ifNotNil: [ ^ lcs ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   492
        equivalenceClasses := self computeEquivalenceClasses.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   493
        candidates := OrderedCollection with: (Candidate new 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   494
                        file1index: 0
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   495
                        file2index: 0
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   496
                        chain: nil).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   497
        file1 withIndexDo: 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   498
                [ :line :file1index | 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   499
                self 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   500
                        mergeCandidates: candidates
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   501
                        file1index: file1index
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   502
                        file2indices: (equivalenceClasses 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   503
                                        at: line
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   504
                                        ifAbsent: #()) ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   505
        lcs := self postprocessCandidateChain: candidates.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   506
        ^ lcs
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   507
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   508
    "Modified: / 16-03-2012 / 19:14:04 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   509
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   510
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   511
!Diff2::HuntMcilroy methodsFor:'private'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   512
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   513
computeEquivalenceClasses
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   514
	| result |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   515
	result := Dictionary new.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   516
	file2 withIndexDo: 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   517
		[ :line :index | 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   518
		(result 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   519
			at: line
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   520
			ifAbsentPut: [ OrderedCollection new ]) add: index ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   521
	^ result
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   522
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   523
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   524
findCandidateFrom: candidates forLine: file2index startingAt: lowIndex
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   525
	"Find the index k in the given subrange of candidates where file2index falls strictly between the file2indexes of the kth and k+1th candidates. If no such k exists, return 0."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   526
	(lowIndex to: candidates size) do: [ :k |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   527
		(candidates at: k) file2index >= file2index ifTrue: [^ 0].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   528
		(k = candidates size or: [ (candidates at: k + 1) file2index > file2index ])
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   529
			ifTrue: [^ k] ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   530
	^ 0
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   531
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   532
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   533
mergeCandidates: candidates file1index: file1index file2indices: file2indices 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   534
        | r c s newCandidate |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   535
        r := 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   536
        c := candidates at: r.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   537
        file2indices do: 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   538
                [ :file2index | 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   539
                s := self findCandidateFrom: candidates forLine: file2index startingAt: r.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   540
                s > 0 ifTrue: 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   541
                        [ newCandidate := Candidate new   
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   542
                                file1index: file1index
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   543
                                file2index: file2index
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   544
                                chain: (candidates at: s).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   545
                        self storeCandidate: c at: r in: candidates.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   546
                        c := newCandidate.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   547
                        r := s + 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   548
                        "optimise by leaving early if s was the end of the candidates list, since none of the subsequent file2indices will have a place to go"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   549
                        s = candidates size ifTrue: 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   550
                                [ self storeCandidate: c at: r in: candidates. ^ self ] ] ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   551
        self storeCandidate: c at: r in: candidates.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   552
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   553
    "Modified (format): / 16-03-2012 / 19:08:50 / Jan Vrany <jan.vrany@fit.cvut.cz>"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   554
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   555
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   556
postprocessCandidateChain: candidates 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   557
	| result c |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   558
	result := OrderedCollection new.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   559
	c := candidates at: candidates size.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   560
	[ c chain notNil ] whileTrue: 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   561
		[ result add: c file1index -> c file2index.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   562
		c := c chain ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   563
	^ result reversed.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   564
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   565
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   566
storeCandidate: c at: r in: candidates
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   567
	r > candidates size ifTrue: [candidates add: c] ifFalse: [candidates at: r put: c].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   568
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   569
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   570
!Diff2::HuntMcilroy::Candidate class methodsFor:'documentation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   571
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   572
copyright
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   573
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   574
 Copyright (c) 2007-2012 Tony Garnock-Jones
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   575
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   576
 This code is based on Squeak's DiffMerge package
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   577
 written by Tony Garnock-Jones. Original project's web site:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   578
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   579
 http://www.squeaksource.com/DiffMerge
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   580
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   581
 Permission is hereby granted, free of charge, to any person
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   582
 obtaining a copy of this software and associated documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   583
 files (the 'Software'), to deal in the Software without
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   584
 restriction, including without limitation the rights to use,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   585
 copy, modify, merge, publish, distribute, sublicense, and/or sell
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   586
 copies of the Software, and to permit persons to whom the
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   587
 Software is furnished to do so, subject to the following
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   588
 conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   589
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   590
 The above copyright notice and this permission notice shall be
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   591
 included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   592
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   593
 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   594
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   595
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   596
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   597
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   598
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   599
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   600
 OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   601
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   602
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   603
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   604
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   605
documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   606
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   607
HuntMcilroyDiffCandidate is used internally by HuntMcilroyDiff.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   608
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   609
    [author:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   610
        Tony Garnock-Jones <tonyg@kcbbs.gen.nz>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   611
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   612
    [instance variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   613
        chain:                  Link to next candidate in chain.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   614
        file1index:             Position in file1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   615
        file2index:             Position in file2.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   616
    [class variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   617
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   618
    [see also:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   619
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   620
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   621
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   622
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   623
!Diff2::HuntMcilroy::Candidate methodsFor:'accessing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   624
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   625
chain
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   626
	^ chain
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   627
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   628
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   629
file1index
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   630
	^ file1index
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   631
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   632
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   633
file2index
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   634
	^ file2index
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   635
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   636
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   637
!Diff2::HuntMcilroy::Candidate methodsFor:'as yet unclassified'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   638
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   639
file1index: f1 file2index: f2 chain: c
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   640
	file1index := f1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   641
	file2index := f2.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   642
	chain := c.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   643
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   644
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   645
!Diff2::MyersUkkonen class methodsFor:'documentation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   646
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   647
copyright
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   648
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   649
 Copyright (c) 2007-2012 Tony Garnock-Jones
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   650
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   651
 This code is based on Squeak's DiffMerge package
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   652
 written by Tony Garnock-Jones. Original project's web site:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   653
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   654
 http://www.squeaksource.com/DiffMerge
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   655
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   656
 Permission is hereby granted, free of charge, to any person
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   657
 obtaining a copy of this software and associated documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   658
 files (the 'Software'), to deal in the Software without
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   659
 restriction, including without limitation the rights to use,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   660
 copy, modify, merge, publish, distribute, sublicense, and/or sell
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   661
 copies of the Software, and to permit persons to whom the
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   662
 Software is furnished to do so, subject to the following
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   663
 conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   664
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   665
 The above copyright notice and this permission notice shall be
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   666
 included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   667
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   668
 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   669
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   670
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   671
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   672
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   673
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   674
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   675
 OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   676
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   677
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   678
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   679
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   680
documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   681
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   682
I implement a modified version of Myers' greedy lcs algorithm described in http://xmailserver.org/diff2.pdf. A similar version written in C can be found here http://research.janelia.org/myers/Papers/file.comparison.pdf. Ukkonen's version can be found here http://www.cs.helsinki.fi/u/ukkonen/InfCont85.PDF.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   683
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   684
    [author:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   685
        Tony Garnock-Jones <tonyg@lshift.com>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   686
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   687
    [instance variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   688
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   689
    [class variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   690
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   691
    [see also:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   692
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   693
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   694
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   695
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   696
!Diff2::MyersUkkonen methodsFor:'accessing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   697
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   698
emptyCaches
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   699
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   700
	lcs := nil
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   701
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   702
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   703
!Diff2::MyersUkkonen methodsFor:'diffing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   704
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   705
longestCommonSubsequence
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   706
	
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   707
	^lcs ifNil: [ 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   708
		lcs := (Array streamContents: [ :stream |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   709
			| list |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   710
			list := self calculateLcs.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   711
			[ list == nil ] whileFalse: [
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   712
				stream nextPut: (list at: 1) -> (list at: 2).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   713
				list := list at: 3 ] ]) reverse ]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   714
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   715
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   716
!Diff2::MyersUkkonen methodsFor:'private'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   717
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   718
calculateLcs
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   719
	"I find one of the longest common subsequences of my the arguments. I assume that none of my arguments are empty. I return nil or an Array which represents a list. The first two elements are the matching line numbers, the last is the next node in the list or nil if there are no more elements. The list containts the longest common subsequence. I'm a modified version of the greedy lcs algorithm from the 6th page of 'An O(ND) Difference Algorithm and Its Variations (1986)' by Eugene W. Myers"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   720
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   721
    | n m v lcss max |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   722
    n := file1 size.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   723
    m := file2 size.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   724
    max := m + n.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   725
    v := Array new: 2 * max + 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   726
    v at: max + 2 put: 0.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   727
    lcss := Array new: 2 * max + 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   728
    0 to: max do: [ :d |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   729
	d negated to: d by: 2 do: [ :k |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   730
	    | index chain x y |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   731
	    (k + d = 0 or: [ k ~= d and: [ (v at: max + k ) < (v at: max + k + 2) ] ])
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   732
				ifTrue: [ 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   733
					index := max + k + 2.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   734
					x := v at: index ]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   735
				ifFalse: [ 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   736
					index := max + k.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   737
					x := (v at: index) + 1 ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   738
			chain := lcss at: index.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   739
			y := x - k.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   740
			[ x < n and: [ y < m and: [ (file1 at: x + 1) = (file2 at: y + 1) ] ] ] whileTrue: [
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   741
			    chain := Array with: (x := x + 1) with: (y := y + 1) with: chain.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   742
			].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   743
			(x >= n and: [ y >= m ]) ifTrue: [
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   744
				^chain ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   745
			v at: max + k + 1 put: x.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   746
			lcss at: max + k + 1 put: chain ] ].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   747
	self error
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   748
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   749
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   750
!Diff2::Patch class methodsFor:'documentation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   751
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   752
copyright
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   753
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   754
 Copyright (c) 2007-2012 Tony Garnock-Jones
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   755
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   756
 This code is based on Squeak's DiffMerge package
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   757
 written by Tony Garnock-Jones. Original project's web site:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   758
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   759
 http://www.squeaksource.com/DiffMerge
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   760
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   761
 Permission is hereby granted, free of charge, to any person
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   762
 obtaining a copy of this software and associated documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   763
 files (the 'Software'), to deal in the Software without
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   764
 restriction, including without limitation the rights to use,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   765
 copy, modify, merge, publish, distribute, sublicense, and/or sell
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   766
 copies of the Software, and to permit persons to whom the
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   767
 Software is furnished to do so, subject to the following
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   768
 conditions:
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   769
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   770
 The above copyright notice and this permission notice shall be
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   771
 included in all copies or substantial portions of the Software.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   772
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   773
 THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   774
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   775
 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   776
 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   777
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   778
 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   779
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   780
 OTHER DEALINGS IN THE SOFTWARE.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   781
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   782
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   783
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   784
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   785
documentation
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   786
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   787
    A DiffPatch has a collection of DiffChunks, and a collection of corresponding 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   788
    SequenceableCollection snippets. It can be used to patch a file (= SequenceableCollection) 
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   789
    forwards or backwards.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   790
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   791
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   792
    [author:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   793
        Tony Garnock-Jones <tonyg@lshift.com>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   794
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   795
    [instance variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   796
        chunks:         <SequenceableCollection of DiffChunk->DiffChunk>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   797
        snippets:       <SequenceableCollection of SequenceableCollection->SequenceableCollection>
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   798
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   799
    [class variables:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   800
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   801
    [see also:]
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   802
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   803
"
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   804
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   805
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   806
!Diff2::Patch methodsFor:'accessing'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   807
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   808
applyTo: file
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   809
	"Applies this patch to the given collection. Makes no sanity checks on the contents of the collection - simply blindly applies the chunks and snippets to its argument."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   810
	| result commonOffset |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   811
	result := OrderedCollection new.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   812
	commonOffset := 1.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   813
	chunks with: snippets do: [:chunk :snippet |
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   814
		result addAll: (file copyFrom: commonOffset to: chunk key offset - 1).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   815
		result addAll: (snippet value).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   816
		commonOffset := chunk key offset + chunk key length].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   817
	result addAll: (file copyFrom: commonOffset to: file size).
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   818
	^ result as: file species.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   819
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   820
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   821
!Diff2::Patch methodsFor:'as yet unclassified'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   822
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   823
initChunks: c file1: f1 file2: f2
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   824
	chunks := c.
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   825
	snippets := c collect: [:entry | (entry key extractFrom: f1) -> (entry value extractFrom: f2)].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   826
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   827
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   828
!Diff2::Patch methodsFor:'selecting'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   829
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   830
invert
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   831
	"Causes this patch to invert itself; if previously it represented the changes from file1 to file2, after being sent #invert, it will represent the changes from file2 to file1. After inversion, calling #applyTo: on file2 will yield file1, rather than the other way around."
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   832
	chunks do: [:entry | entry key: entry value value: entry key].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   833
	snippets do: [:entry | entry key: entry value value: entry key].
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   834
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   835
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   836
!Diff2 class methodsFor:'documentation'!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   837
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   838
version
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   839
    ^ '$Header: /cvs/stx/stx/libtool/Diff2.st,v 1.1 2014-02-05 18:59:20 cg Exp $'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   840
!
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   841
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   842
version_CVS
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   843
    ^ '$Header: /cvs/stx/stx/libtool/Diff2.st,v 1.1 2014-02-05 18:59:20 cg Exp $'
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   844
! !
5092330709c7 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   845