mirror of
				https://github.com/golang/go.git
				synced 2025-10-31 00:30:57 +00:00 
			
		
		
		
	suffixarray: a package for creating suffixarray-based indexes
This is a replacement for pending CL 2219042. It only contains the raw suffixarray functionality with two methods: - New create a new index from some data - Lookup lookup occurences of a bytes slice in the data Any other functionality (dealing with multiple data sets and the corresponding position lists) is generic and doesn't have to be part of this package. Known performance bug: This implementation works fine for data sets up to several megabytes as long as it doesn't contain very long contiguous sequences of equal bytes. For instance, index creation for all .go files under GOROOT (250KLOCs, approx. 9MB) takes ~50s on 2.66 GHz Intel Xeon as long as test/fixedbugs/257.go is excluded. With that file, index creation times takes several days. 257.go contains a string of 1M smiley faces. There are more sophisticated suffixarray creation algorithms which can handle very long common prefixes. The implementation can be updated w/o the need to change the interface. R=rsc, r, PeterGo CC=golang-dev https://golang.org/cl/2265041
This commit is contained in:
		
							parent
							
								
									176364900e
								
							
						
					
					
						commit
						22974fbe8e
					
				
					 4 changed files with 290 additions and 0 deletions
				
			
		
							
								
								
									
										161
									
								
								src/pkg/index/suffixarray/suffixarray_test.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										161
									
								
								src/pkg/index/suffixarray/suffixarray_test.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,161 @@ | |||
| // Copyright 2010 The Go Authors. All rights reserved. | ||||
| // Use of this source code is governed by a BSD-style | ||||
| // license that can be found in the LICENSE file. | ||||
| 
 | ||||
| package suffixarray | ||||
| 
 | ||||
| import ( | ||||
| 	"container/vector" | ||||
| 	"sort" | ||||
| 	"strings" | ||||
| 	"testing" | ||||
| ) | ||||
| 
 | ||||
| 
 | ||||
| type testCase struct { | ||||
| 	name    string   // name of test case | ||||
| 	source  string   // source to index | ||||
| 	lookups []string // strings to lookup | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| var testCases = []testCase{ | ||||
| 	testCase{ | ||||
| 		"empty string", | ||||
| 		"", | ||||
| 		[]string{ | ||||
| 			"", | ||||
| 			"foo", | ||||
| 		}, | ||||
| 	}, | ||||
| 
 | ||||
| 	testCase{ | ||||
| 		"all a's", | ||||
| 		"aaaaaaaaaa", // 10 a's | ||||
| 		[]string{ | ||||
| 			"", | ||||
| 			"a", | ||||
| 			"aa", | ||||
| 			"aaa", | ||||
| 			"aaaa", | ||||
| 			"aaaaa", | ||||
| 			"aaaaaa", | ||||
| 			"aaaaaaa", | ||||
| 			"aaaaaaaa", | ||||
| 			"aaaaaaaaa", | ||||
| 			"aaaaaaaaaa", | ||||
| 			"aaaaaaaaaaa", // 11 a's | ||||
| 		}, | ||||
| 	}, | ||||
| 
 | ||||
| 	testCase{ | ||||
| 		"abc", | ||||
| 		"abc", | ||||
| 		[]string{ | ||||
| 			"a", | ||||
| 			"b", | ||||
| 			"c", | ||||
| 			"ab", | ||||
| 			"bc", | ||||
| 			"abc", | ||||
| 		}, | ||||
| 	}, | ||||
| 
 | ||||
| 	testCase{ | ||||
| 		"barbara*3", | ||||
| 		"barbarabarbarabarbara", | ||||
| 		[]string{ | ||||
| 			"a", | ||||
| 			"bar", | ||||
| 			"rab", | ||||
| 			"arab", | ||||
| 			"barbar", | ||||
| 		}, | ||||
| 	}, | ||||
| 
 | ||||
| 	testCase{ | ||||
| 		"typing drill", | ||||
| 		"Now is the time for all good men to come to the aid of their country.", | ||||
| 		[]string{ | ||||
| 			"Now", | ||||
| 			"the time", | ||||
| 			"to come the aid", | ||||
| 			"is the time for all good men to come to the aid of their", | ||||
| 		}, | ||||
| 	}, | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| // find all occurences of s in source; report at most n occurences | ||||
| func find(src, s string, n int) []int { | ||||
| 	var res vector.IntVector | ||||
| 	if s != "" && n != 0 { | ||||
| 		// find at most n occurences of s in src | ||||
| 		for i := -1; n < 0 || len(res) < n; { | ||||
| 			j := strings.Index(src[i+1:], s) | ||||
| 			if j < 0 { | ||||
| 				break | ||||
| 			} | ||||
| 			i += j + 1 | ||||
| 			res.Push(i) | ||||
| 		} | ||||
| 	} | ||||
| 	return res | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| func testLookups(t *testing.T, src string, x *Index, tc *testCase, n int) { | ||||
| 	for _, s := range tc.lookups { | ||||
| 		res := x.Lookup([]byte(s), n) | ||||
| 		exp := find(tc.source, s, n) | ||||
| 
 | ||||
| 		// check that the lengths match | ||||
| 		if len(res) != len(exp) { | ||||
| 			t.Errorf("test %q, lookup %q (n = %d): expected %d results; got %d", tc.name, s, n, len(exp), len(res)) | ||||
| 		} | ||||
| 
 | ||||
| 		// if n >= 0 the number of results is limited --- unless n >= all results, | ||||
| 		// we may obtain different positions from the Index and from find (because | ||||
| 		// Index may not find the results in the same order as find) => in general | ||||
| 		// we cannot simply check that the res and exp lists are equal | ||||
| 
 | ||||
| 		// check that there are no duplicates | ||||
| 		sort.SortInts(res) | ||||
| 		for i, r := range res { | ||||
| 			if i > 0 && res[i-1] == r { | ||||
| 				t.Errorf("test %q, lookup %q, result %d (n = %d): found duplicate index %d", tc.name, s, i, n, r) | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		// check that each result is in fact a correct match | ||||
| 		for i, r := range res { | ||||
| 			if r < 0 || len(src) <= r { | ||||
| 				t.Errorf("test %q, lookup %q, result %d (n = %d): index %d out of range [0, %d[", tc.name, s, i, n, r, len(src)) | ||||
| 			} else if !strings.HasPrefix(src[r:], s) { | ||||
| 				t.Errorf("test %q, lookup %q, result %d (n = %d): index %d not a match", tc.name, s, i, n, r) | ||||
| 			} | ||||
| 		} | ||||
| 
 | ||||
| 		if n < 0 { | ||||
| 			// all results computed - sorted res and exp must be equal | ||||
| 			for i, r := range res { | ||||
| 				e := exp[i] | ||||
| 				if r != e { | ||||
| 					t.Errorf("test %q, lookup %q, result %d: expected index %d; got %d", tc.name, s, i, e, r) | ||||
| 					continue | ||||
| 				} | ||||
| 			} | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| func TestIndex(t *testing.T) { | ||||
| 	for _, tc := range testCases { | ||||
| 		x := New([]byte(tc.source)) | ||||
| 		testLookups(t, tc.source, x, &tc, 0) | ||||
| 		testLookups(t, tc.source, x, &tc, 1) | ||||
| 		testLookups(t, tc.source, x, &tc, 10) | ||||
| 		testLookups(t, tc.source, x, &tc, -1) | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Robert Griesemer
						Robert Griesemer