优化算法
This commit is contained in:
parent
8891c44211
commit
8b57abe8ee
1 changed files with 35 additions and 19 deletions
52
srt.go
52
srt.go
|
@ -6,7 +6,6 @@ import (
|
|||
"github.com/Hami-Lemon/lrc2srt/glist"
|
||||
"io"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
@ -109,27 +108,44 @@ func (s *SRT) Merge(other *SRT, mode SRTMergeMode) {
|
|||
}
|
||||
}
|
||||
|
||||
//todo 改进算法,现在的算法太慢了
|
||||
//可以类比为合并两个有序链表,
|
||||
//算法参考:https://leetcode-cn.com/problems/merge-two-sorted-lists/solution/he-bing-liang-ge-you-xu-lian-biao-by-leetcode-solu/
|
||||
func (s *SRT) mergeStack(other *SRT) {
|
||||
size := s.Content.Size() + other.Content.Size()
|
||||
temp := make([]*SRTContent, size, size)
|
||||
index := 0
|
||||
for it := s.Content.Iterator(); it.Has(); {
|
||||
temp[index] = it.Next()
|
||||
index++
|
||||
ls, _ := s.Content.(*glist.LinkedList[*SRTContent])
|
||||
lo, _ := other.Content.(*glist.LinkedList[*SRTContent])
|
||||
lhs, lho := ls.First, lo.First
|
||||
//临时的空结点
|
||||
preHead := &glist.Node[*SRTContent]{}
|
||||
prev := preHead
|
||||
|
||||
for lhs != nil && lho != nil {
|
||||
//副本结点,从而不改变other对象中的内容
|
||||
oCopy := lho.Clone()
|
||||
|
||||
if lhs.Element.Start <= oCopy.Element.Start {
|
||||
prev.Next = lhs
|
||||
lhs.Prev = prev
|
||||
lhs = lhs.Next
|
||||
} else {
|
||||
prev.Next = oCopy
|
||||
oCopy.Prev = prev
|
||||
lho = lho.Next
|
||||
}
|
||||
for it := other.Content.Iterator(); it.Has(); {
|
||||
temp[index] = it.Next()
|
||||
index++
|
||||
prev = prev.Next
|
||||
}
|
||||
sort.SliceStable(temp, func(i, j int) bool {
|
||||
return temp[i].Start < temp[j].Start
|
||||
})
|
||||
list := glist.NewLinkedList[*SRTContent]()
|
||||
for _, v := range temp {
|
||||
list.Append(v)
|
||||
if lhs == nil {
|
||||
//如果剩下的内容是other中的,则依次迭代复制到s中
|
||||
for n := lho; n != nil; n = n.Next {
|
||||
c := n.Clone()
|
||||
prev.Next = c
|
||||
c.Prev = prev
|
||||
prev = prev.Next
|
||||
}
|
||||
s.Content = list
|
||||
} else {
|
||||
prev.Next = lhs
|
||||
}
|
||||
ls.First = preHead.Next
|
||||
ls.First.Prev = nil
|
||||
}
|
||||
|
||||
func (s *SRT) mergeUp(other *SRT) {
|
||||
|
|
Reference in a new issue