Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simhash #6

Closed
wants to merge 10 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use uint32
  • Loading branch information
aslakhellesoy committed Jun 20, 2023
commit 2b0c5174c846704aed51604bfcdc2fdca61ff8dc
20 changes: 10 additions & 10 deletions lhdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ const SimilarityThreshold = 0.45
* Then, for each line in the right file, the most similar line in the left file is found.
* The most similar line is the line with the highest combined similarity.
*/
func Lhdiff(left string, right string, contextSize int, includeIdenticalLines bool) ([][]int, error) {
func Lhdiff(left string, right string, contextSize int, includeIdenticalLines bool) ([][]uint32, error) {
leftLines := ConvertToLinesWithoutNewLine(left)
rightLines := ConvertToLinesWithoutNewLine(right)

Expand Down Expand Up @@ -144,20 +144,20 @@ func Lhdiff(left string, right string, contextSize int, includeIdenticalLines bo
return makeLineMappings(allPairs, len(leftLines), rightLineNumbers, includeIdenticalLines), nil
}

func makeLineMappings(linePairs map[int]LinePair, leftLineCount int, newRightLines []int, includeIdenticalLines bool) [][]int {
lineMappings := make([][]int, 0)
func makeLineMappings(linePairs map[int]LinePair, leftLineCount int, newRightLines []int, includeIdenticalLines bool) [][]uint32 {
lineMappings := make([][]uint32, 0)
for leftLineNumber := 0; leftLineNumber < leftLineCount; leftLineNumber++ {
pair, exists := linePairs[leftLineNumber]
if !exists {
lineMappings = append(lineMappings, []int{leftLineNumber + 1, -1})
lineMappings = append(lineMappings, []uint32{uint32(leftLineNumber + 1), 0})
} else {
if includeIdenticalLines || !(pair.left.content == pair.right.content && leftLineNumber == pair.right.lineNumber) {
lineMappings = append(lineMappings, []int{leftLineNumber + 1, pair.right.lineNumber + 1})
lineMappings = append(lineMappings, []uint32{uint32(leftLineNumber + 1), uint32(pair.right.lineNumber + 1)})
}
}
}
for _, rightLine := range newRightLines {
lineMappings = append(lineMappings, []int{-1, rightLine + 1})
lineMappings = append(lineMappings, []uint32{0, uint32(rightLine + 1)})
}
return lineMappings
}
Expand Down Expand Up @@ -316,7 +316,7 @@ func RemoveMultipleSpaceAndTrim(s string) string {
return strings.TrimSpace(re.ReplaceAllString(s, " ")) + "\n"
}

func PrintMappings(mappings [][]int) error {
func PrintMappings(mappings [][]uint32) error {
for _, mapping := range mappings {
_, err := fmt.Printf("%s,%s\n", toString(mapping[0]), toString(mapping[1]))
if err != nil {
Expand All @@ -326,12 +326,12 @@ func PrintMappings(mappings [][]int) error {
return nil
}

func toString(i int) string {
func toString(i uint32) string {
var left string
if i == -1 {
if i == 0 {
left = "_"
} else {
left = strconv.Itoa(i)
left = strconv.FormatUint(uint64(i), 10)
}
return left
}