vendor/github.com/ysmood/got/lib/lcs/lcs.go

157 lines · 2785 bytes

  1// Package lcs ...
  2package lcs
  3
  4import (
  5	"context"
  6)
  7
  8// Indices is the index list of items in xs that forms the LCS between xs and ys.
  9type Indices []int
 10
 11// YadLCS returns the x index of each Comparable that are in the YadLCS between x and y.
 12// The complexity is O(M * log(L)), M is the number of char matches between x and y, L is the length of LCS.
 13// The worst memory complexity is O(M), but usually it's much less.
 14//
 15// The advantage of this algorithm is it's easy to understand and implement. It converts the LCS
 16// problem into problems that are familiar to us, such as LIS, binary-search, object-recycle, etc., which give us
 17// more room to do the optimization for each streamline.
 18func (xs Sequence) YadLCS(ctx context.Context, ys Sequence) Indices {
 19	o := xs.Occurrence(ys)
 20	r := result{list: make([]*node, 0, min(len(xs), len(ys)))}
 21	rest := len(ys)
 22
 23	for _, xi := range o {
 24		if ctx.Err() != nil {
 25			break
 26		}
 27
 28		from := len(r.list)
 29		for _, i := range xi {
 30			from = r.add(from, i, rest)
 31		}
 32
 33		rest--
 34	}
 35
 36	return r.lcs()
 37}
 38
 39type node struct {
 40	x int
 41	p *node
 42
 43	c int // pointer count for node recycle
 44}
 45
 46func (n *node) link(x int, m *node) {
 47	if m != nil {
 48		m.c++
 49	}
 50
 51	n.p = m
 52	n.x = x
 53}
 54
 55type result struct {
 56	list []*node
 57
 58	// reuse node to reduce memory allocation
 59	recycle []*node
 60}
 61
 62func (r *result) new(x int, n *node) *node {
 63	var m *node
 64
 65	// reuse node if possible
 66	l := len(r.recycle)
 67	if l > 0 {
 68		m = r.recycle[l-1]
 69		r.recycle = r.recycle[:l-1]
 70	} else {
 71		m = &node{}
 72	}
 73
 74	m.link(x, n)
 75
 76	return m
 77}
 78
 79func (r *result) replace(i, x int, n *node) {
 80	// recycle nodes
 81	if m := r.list[i]; m.c == 0 {
 82		for p := m.p; p != nil && p != n; p = p.p {
 83			p.c--
 84			if p.c == 0 {
 85				r.recycle = append(r.recycle, p)
 86			} else {
 87				break
 88			}
 89		}
 90
 91		m.link(x, n)
 92		return
 93	}
 94
 95	r.list[i] = r.new(x, n)
 96}
 97
 98func (r *result) add(from, x, rest int) int {
 99	l := len(r.list)
100
101	next, n := r.find(from, x)
102	if n != nil {
103		if l-next < rest { // only when we have enough rest xs
104			if next == l {
105				r.list = append(r.list, r.new(x, n))
106			} else if x < r.list[next].x {
107				r.replace(next, x, n)
108			}
109			return next
110		}
111	}
112
113	if l == 0 {
114		r.list = append(r.list, r.new(x, n))
115		return 1
116	}
117
118	if l-1 < rest && x < r.list[0].x {
119		r.replace(0, x, nil)
120	}
121
122	return 0
123}
124
125// binary search to find the largest r.list[i].x that is smaller than x
126func (r *result) find(from, x int) (int, *node) {
127	var found *node
128	for i, j := 0, from; i < j; {
129		h := (i + j) >> 1
130		n := r.list[h]
131		if n.x < x {
132			from = h
133			found = n
134			i = h + 1
135		} else {
136			j = h
137		}
138	}
139	return from + 1, found
140}
141
142func (r *result) lcs() Indices {
143	l := len(r.list)
144
145	idx := make(Indices, l)
146
147	if l == 0 {
148		return idx
149	}
150
151	for p := r.list[l-1]; p != nil; p = p.p {
152		l--
153		idx[l] = p.x
154	}
155
156	return idx
157}