kmp

package module
v0.0.0-...-7effc39 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 7, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

README

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]

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(pattern interfaceKMP) (*kmp, error)

New creates instance for provided pattern where pattern has to be a slice

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL