Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
use separated value for counting duplicated/uniq lines instead of map.
Browse files Browse the repository at this point in the history
  • Loading branch information
tamada committed Mar 10, 2020
1 parent 9b9705c commit 495b1f0
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/uniq2.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ type Arguments struct {
}

type entry struct {
line string
count map[bool]int
line string
duplicatedCount int
uniqCount int
}

func closeImpl(value interface{}) {
Expand Down Expand Up @@ -106,10 +107,18 @@ func isPrint(uniqFlag bool, deleteLineFlag bool) bool {
uniqFlag && deleteLineFlag
}

func (e *entry) increment(flag bool) {
if flag {
e.uniqCount++
} else {
e.duplicatedCount++
}
}

func updateDatabase(line string, uniqFlag bool, entries []*entry) []*entry {
for i, entry := range entries {
if entry.line == line {
entries[i].count[uniqFlag]++
entries[i].increment(uniqFlag)
}
}
return entries
Expand All @@ -119,7 +128,8 @@ func upsertDatabase(line string, uniqFlag bool, entries []*entry) []*entry {
if uniqFlag {
return updateDatabase(line, uniqFlag, entries)
}
var entry = &entry{line: line, count: map[bool]int{uniqFlag: 1}}
var entry = &entry{line: line}
entry.increment(uniqFlag)
return append(entries, entry)
}

Expand Down

0 comments on commit 495b1f0

Please sign in to comment.