Skip to content
/ kmp Public

Go version of Knuth–Morris–Pratt algorithm for custom user types not only strings

License

Notifications You must be signed in to change notification settings

vchezganov/kmp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

kmp

Go version of Knuth–Morris–Pratt algorithm for custom user types, where user type must be a slice implementing the following interface:

type interfaceKMP interface {
	At(i int) interface{}
	Len() int
	EqualTo(i int, to interface{}) bool
}

Example

package main

import (
	"fmt"
	"strings"

	"github.com/vchezganov/kmp"
)

type StringList []string

func (l StringList) At(i int) interface{} {
	return l[i]
}

func (l StringList) Len() int {
	return len(l)
}

func (l StringList) EqualTo(i int, to interface{}) bool {
	return strings.EqualFold(l[i], to.(string))
}

func main() {
	pattern := StringList{
		"hello",
		"WORLD",
	}

	kmpSearch, err := kmp.New(pattern)
	if err != nil {
		panic(err)
	}

	list := StringList{
		"abc",
		"World",
		"hello",
		"hELLo",
		"world",
		"xyz",
	}

	firstIndex := kmpSearch.FindPatternIndex(list)
	fmt.Printf("Index: %d\n", firstIndex)
	fmt.Printf("List: %v\n", list[firstIndex:firstIndex+len(pattern)])
}
Index: 3
List: [hELLo world]

About

Go version of Knuth–Morris–Pratt algorithm for custom user types not only strings

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages