blob: 9c36a98f82d2679a410ec3bedeacb5f4b6ebe406 [file] [log] [blame]
Eric Eisnere3c95652011-01-11 21:46:50 -08001// Copyright 2011 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// This algorithm is based on "Faster Suffix Sorting"
6// by N. Jesper Larsson and Kunihiko Sadakane
7// paper: http://www.larsson.dogma.net/ssrev-tr.pdf
8// code: http://www.larsson.dogma.net/qsufsort.c
9
10// This algorithm computes the suffix array sa by computing its inverse.
11// Consecutive groups of suffixes in sa are labeled as sorted groups or
12// unsorted groups. For a given pass of the sorter, all suffixes are ordered
13// up to their first h characters, and sa is h-ordered. Suffixes in their
Robert Griesemerd7246312012-03-13 17:29:07 -070014// final positions and unambiguously sorted in h-order are in a sorted group.
Eric Eisnere3c95652011-01-11 21:46:50 -080015// Consecutive groups of suffixes with identical first h characters are an
16// unsorted group. In each pass of the algorithm, unsorted groups are sorted
17// according to the group number of their following suffix.
18
19// In the implementation, if sa[i] is negative, it indicates that i is
20// the first element of a sorted group of length -sa[i], and can be skipped.
21// An unsorted group sa[i:k] is given the group number of the index of its
22// last element, k-1. The group numbers are stored in the inverse slice (inv),
23// and when all groups are sorted, this slice is the inverse suffix array.
24
25package suffixarray
26
27import "sort"
28
Robert Griesemer71557712011-09-27 16:21:28 -070029func qsufsort(data []byte) []int {
Eric Eisnere3c95652011-01-11 21:46:50 -080030 // initial sorting by first byte of suffix
31 sa := sortedByFirstByte(data)
32 if len(sa) < 2 {
33 return sa
34 }
35 // initialize the group lookup table
36 // this becomes the inverse of the suffix array when all groups are sorted
37 inv := initGroups(sa, data)
38
39 // the index starts 1-ordered
Eric Eisner6f182332011-09-19 11:03:43 -070040 sufSortable := &suffixSortable{sa: sa, inv: inv, h: 1}
Eric Eisnere3c95652011-01-11 21:46:50 -080041
Robert Griesemer71557712011-09-27 16:21:28 -070042 for sa[0] > -len(sa) { // until all suffixes are one big sorted group
Eric Eisnere3c95652011-01-11 21:46:50 -080043 // The suffixes are h-ordered, make them 2*h-ordered
44 pi := 0 // pi is first position of first group
45 sl := 0 // sl is negated length of sorted groups
46 for pi < len(sa) {
Robert Griesemer71557712011-09-27 16:21:28 -070047 if s := sa[pi]; s < 0 { // if pi starts sorted group
Eric Eisnere3c95652011-01-11 21:46:50 -080048 pi -= s // skip over sorted group
49 sl += s // add negated length to sl
50 } else { // if pi starts unsorted group
51 if sl != 0 {
Robert Griesemer71557712011-09-27 16:21:28 -070052 sa[pi+sl] = sl // combine sorted groups before pi
Eric Eisnere3c95652011-01-11 21:46:50 -080053 sl = 0
54 }
Robert Griesemer71557712011-09-27 16:21:28 -070055 pk := inv[s] + 1 // pk-1 is last position of unsorted group
Eric Eisnere3c95652011-01-11 21:46:50 -080056 sufSortable.sa = sa[pi:pk]
57 sort.Sort(sufSortable)
58 sufSortable.updateGroups(pi)
59 pi = pk // next group
60 }
61 }
62 if sl != 0 { // if the array ends with a sorted group
Robert Griesemer71557712011-09-27 16:21:28 -070063 sa[pi+sl] = sl // combine sorted groups at end of sa
Eric Eisnere3c95652011-01-11 21:46:50 -080064 }
65
66 sufSortable.h *= 2 // double sorted depth
67 }
68
69 for i := range sa { // reconstruct suffix array from inverse
Robert Griesemer71557712011-09-27 16:21:28 -070070 sa[inv[i]] = i
Eric Eisnere3c95652011-01-11 21:46:50 -080071 }
72 return sa
73}
74
Robert Griesemer71557712011-09-27 16:21:28 -070075func sortedByFirstByte(data []byte) []int {
Eric Eisnere3c95652011-01-11 21:46:50 -080076 // total byte counts
77 var count [256]int
78 for _, b := range data {
79 count[b]++
80 }
Robert Griesemerd7246312012-03-13 17:29:07 -070081 // make count[b] equal index of first occurrence of b in sorted array
Eric Eisnere3c95652011-01-11 21:46:50 -080082 sum := 0
83 for b := range count {
84 count[b], sum = sum, count[b]+sum
85 }
86 // iterate through bytes, placing index into the correct spot in sa
Robert Griesemer71557712011-09-27 16:21:28 -070087 sa := make([]int, len(data))
Eric Eisnere3c95652011-01-11 21:46:50 -080088 for i, b := range data {
Robert Griesemer71557712011-09-27 16:21:28 -070089 sa[count[b]] = i
Eric Eisnere3c95652011-01-11 21:46:50 -080090 count[b]++
91 }
92 return sa
93}
94
Robert Griesemer71557712011-09-27 16:21:28 -070095func initGroups(sa []int, data []byte) []int {
Eric Eisnere3c95652011-01-11 21:46:50 -080096 // label contiguous same-letter groups with the same group number
Robert Griesemer71557712011-09-27 16:21:28 -070097 inv := make([]int, len(data))
Eric Eisnere3c95652011-01-11 21:46:50 -080098 prevGroup := len(sa) - 1
99 groupByte := data[sa[prevGroup]]
100 for i := len(sa) - 1; i >= 0; i-- {
101 if b := data[sa[i]]; b < groupByte {
102 if prevGroup == i+1 {
103 sa[i+1] = -1
104 }
105 groupByte = b
106 prevGroup = i
107 }
Robert Griesemer71557712011-09-27 16:21:28 -0700108 inv[sa[i]] = prevGroup
Eric Eisnere3c95652011-01-11 21:46:50 -0800109 if prevGroup == 0 {
110 sa[0] = -1
111 }
112 }
113 // Separate out the final suffix to the start of its group.
114 // This is necessary to ensure the suffix "a" is before "aba"
115 // when using a potentially unstable sort.
116 lastByte := data[len(data)-1]
117 s := -1
118 for i := range sa {
119 if sa[i] >= 0 {
120 if data[sa[i]] == lastByte && s == -1 {
121 s = i
122 }
Robert Griesemer71557712011-09-27 16:21:28 -0700123 if sa[i] == len(sa)-1 {
Eric Eisnere3c95652011-01-11 21:46:50 -0800124 sa[i], sa[s] = sa[s], sa[i]
Robert Griesemer71557712011-09-27 16:21:28 -0700125 inv[sa[s]] = s
Eric Eisnere3c95652011-01-11 21:46:50 -0800126 sa[s] = -1 // mark it as an isolated sorted group
127 break
128 }
129 }
130 }
131 return inv
132}
133
Eric Eisnere3c95652011-01-11 21:46:50 -0800134type suffixSortable struct {
Robert Griesemer71557712011-09-27 16:21:28 -0700135 sa []int
136 inv []int
137 h int
Eric Eisner6f182332011-09-19 11:03:43 -0700138 buf []int // common scratch space
Eric Eisnere3c95652011-01-11 21:46:50 -0800139}
140
141func (x *suffixSortable) Len() int { return len(x.sa) }
142func (x *suffixSortable) Less(i, j int) bool { return x.inv[x.sa[i]+x.h] < x.inv[x.sa[j]+x.h] }
143func (x *suffixSortable) Swap(i, j int) { x.sa[i], x.sa[j] = x.sa[j], x.sa[i] }
144
Eric Eisnere3c95652011-01-11 21:46:50 -0800145func (x *suffixSortable) updateGroups(offset int) {
Eric Eisner6f182332011-09-19 11:03:43 -0700146 bounds := x.buf[0:0]
Eric Eisnerd93b2f32011-01-31 13:13:02 -0800147 group := x.inv[x.sa[0]+x.h]
148 for i := 1; i < len(x.sa); i++ {
149 if g := x.inv[x.sa[i]+x.h]; g > group {
150 bounds = append(bounds, i)
Eric Eisnere3c95652011-01-11 21:46:50 -0800151 group = g
Eric Eisnere3c95652011-01-11 21:46:50 -0800152 }
Eric Eisnerd93b2f32011-01-31 13:13:02 -0800153 }
154 bounds = append(bounds, len(x.sa))
Eric Eisner6f182332011-09-19 11:03:43 -0700155 x.buf = bounds
Eric Eisnerd93b2f32011-01-31 13:13:02 -0800156
157 // update the group numberings after all new groups are determined
158 prev := 0
159 for _, b := range bounds {
160 for i := prev; i < b; i++ {
Robert Griesemer71557712011-09-27 16:21:28 -0700161 x.inv[x.sa[i]] = offset + b - 1
Eric Eisnere3c95652011-01-11 21:46:50 -0800162 }
Eric Eisnerd93b2f32011-01-31 13:13:02 -0800163 if b-prev == 1 {
164 x.sa[prev] = -1
165 }
166 prev = b
Eric Eisnere3c95652011-01-11 21:46:50 -0800167 }
168}