本文整理匯總了Golang中index/suffixarray.New函數的典型用法代碼示例。如果您正苦於以下問題:Golang New函數的具體用法?Golang New怎麽用?Golang New使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了New函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: search
// Searches the given Nucleotide-Sequence file-path for the given 'searchString' using sophisticated compression
func search(fileName string, sequenceToSearchFor string) (bool, []int) {
var isPresent bool = false
var master []byte
var offsets []int
lines, err := readLines(fileName)
if err != nil {
log.Fatalf("readLines: %s", err)
} else {
for _, line := range lines {
master = *(compress(&line))
}
}
// dat, _ := ioutil.ReadFile("./encoded.txt")
index := suffixarray.New(master)
searchString := sequenceToSearchFor
searchBytes := *(compress(&searchString))
//https://code.google.com/p/go/source/browse/src/pkg/index/suffixarray/suffixarray.go?name=release#190
//Gets exactly the first match alone
// offsets = index.Lookup([]byte(searchBytes), -1)
//https://code.google.com/p/go/source/browse/src/pkg/index/suffixarray/suffixarray.go?name=release#174
//Gets all matches
offsets = index.Lookup([]byte(searchBytes), 1)
if len(offsets) == 1 {
isPresent = true
}
return isPresent, offsets
}
示例2: main
func main() {
//A set of words delimited by space
words := "a apple sphere atom atmosphere"
//A suffix array created in golang by converting the given string into bytes
index := suffixarray.New([]byte(words))
//Lookup Time complexity = O(log(N)*len(s) + len(result))
// N : the size of the indexed data
// s : substring to be seached for
// result : array containing integers which represent the index of the given substring 's' in the suffix array
//NOTE
// Let's take the following example
// var s string = "apple"
// fmt.Println([]byte(s)) would print = [97 112 112 108 101].
// Golang Suffix array uses the byte representation of the sub-string "s" in order to perform most optimal comutation
offsets1 := index.Lookup([]byte("sphere"), -1) // the list of all indices where s occurs in data
//Prints unsorted array of integers which are the indices of the given substring
fmt.Println(offsets1)
}
示例3: _indexSuffixArray
func _indexSuffixArray() string {
docs := []string{
"mercury", "venus", "earth", "mars",
"jupiter", "saturn", "uranus", "pluto",
}
var data []byte
var offsets []int
for _, d := range docs {
data = append(data, []byte(d)...)
offsets = append(offsets, len(data))
}
sfx := suffixarray.New(data)
query := "earth"
idxs := sfx.Lookup([]byte(query), -1)
var results []int
for _, idx := range idxs {
i := sort.Search(len(offsets), func(i int) bool { return offsets[i] > idx })
if idx+len(query) <= offsets[i] {
results = append(results, i)
}
}
return fmt.Sprintf("%q is in documents %v\n", query, results)
}
示例4: NewTruncIndex
func NewTruncIndex() *TruncIndex {
return &TruncIndex{
index: suffixarray.New([]byte{' '}),
ids: make(map[string]bool),
bytes: []byte{' '},
}
}
示例5: main
func main() {
const N = 6e5
const M = 100
const I = 20
const J = 10
const P = 2
data := make([]byte, N)
for i := 0; i < N; i++ {
data[i] = byte(rand.Intn(255))
}
done := make(chan bool, P)
for p := 0; p < P; p++ {
go func() {
for i := 0; i < I; i++ {
suffix := suffixarray.New(data)
for j := 0; j < J; j++ {
str := make([]byte, M)
for m := 0; m < M; m++ {
str[m] = byte(rand.Intn(255))
}
_ = suffix.Lookup(str, 10)
}
}
done <- true
}()
}
for p := 0; p < P; p++ {
<-done
}
}
示例6: Add
func (idx *TruncIndex) Add(id string) error {
idx.Lock()
defer idx.Unlock()
if err := idx.addId(id); err != nil {
return err
}
idx.index = suffixarray.New(idx.bytes)
return nil
}
示例7: BenchmarkBuildSuffixArray
func BenchmarkBuildSuffixArray(b *testing.B) {
words := getDictWords()
b.ResetTimer()
for i := 0; i < b.N; i++ {
data := []byte("\x00" + strings.Join(words, "\x00") + "\x00")
_ = suffixarray.New(data)
}
}
示例8: GoSufArray_UserAgentIsClean
func (waf *WAF) GoSufArray_UserAgentIsClean(UA []byte) bool {
index := suffixarray.New(UA)
for _, robot := range waf.bad_robots {
if len(index.Lookup(robot, 1)) > 0 {
return false
}
}
return true
}
示例9: check_url
func check_url(substr, str string) bool {
var dst []byte
substr_bytes := strconv.AppendQuoteToASCII(dst, substr)
str_bytes := strconv.AppendQuoteToASCII(dst, str)
index := suffixarray.New(str_bytes)
offsets := index.Lookup(substr_bytes, -1)
if offsets == nil {
return false
}
return offsets[0] == 0
}
示例10: benchFast
func benchFast(line string, substr string) time.Duration {
index := fs.New([]byte(line))
start := time.Now()
for i := 0; i < LOOPS; i++ {
index.Lookup([]byte(substr), 1)
}
end := time.Now()
delta := end.Sub(start)
fmt.Printf("%10s: %20s\t%16s %10s\n", "fast", substr, delta, delta/LOOPS)
return delta
}
示例11: NewTruncIndex
func NewTruncIndex(ids []string) (idx *TruncIndex) {
idx = &TruncIndex{
ids: make(map[string]bool),
bytes: []byte{' '},
}
for _, id := range ids {
idx.ids[id] = true
idx.bytes = append(idx.bytes, []byte(id+" ")...)
}
idx.index = suffixarray.New(idx.bytes)
return
}
示例12: indexFile
func (s *Searcher) indexFile(path string, info os.FileInfo, err error) error {
// only index 1/4 of the files per server
if int(path[len(path)-1])%4 != s.id {
return nil
}
if info.Mode().IsRegular() && info.Size() < (1<<20) {
name := strings.TrimPrefix(path, s.base_path)
data, _ := ioutil.ReadFile(path)
s.files[name] = suffixarray.New(data)
}
return nil
}
示例13: Add
func (idx *TruncIndex) Add(id string) error {
if strings.Contains(id, " ") {
return fmt.Errorf("Illegal character: ' '")
}
if _, exists := idx.ids[id]; exists {
return fmt.Errorf("Id already exists: %s", id)
}
idx.ids[id] = true
idx.bytes = append(idx.bytes, []byte(id+" ")...)
idx.index = suffixarray.New(idx.bytes)
return nil
}
示例14: BenchmarkLookupXX
func BenchmarkLookupXX(b *testing.B) {
words := getDictWords()
data := []byte("\x00" + strings.Join(words, "\x00") + "\x00")
sa := suffixarray.New(data)
b.ResetTimer()
for i := 0; i < b.N; i++ {
indices := sa.Lookup([]byte(XXwordToTry), 1)
if len(indices) > 0 {
_ = getStringFromIndex(data, indices[0])
}
}
}
示例15: Delete
func (idx *TruncIndex) Delete(id string) error {
if _, exists := idx.ids[id]; !exists {
return fmt.Errorf("No such id: %s", id)
}
before, after, err := idx.lookup(id)
if err != nil {
return err
}
delete(idx.ids, id)
idx.bytes = append(idx.bytes[:before], idx.bytes[after:]...)
idx.index = suffixarray.New(idx.bytes)
return nil
}