rabinkarp

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 1, 2019 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package rabinkarp provides the Rabin-Karp string pattern matching algorithm.

Example
package main

import (
	"fmt"
	"rabinkarp"
	"sort"
)

func main() {
	pattern := "bro"
	inputs := []string{"slowbro", "browser", "bob", "st", "bebrobaum"}
	matcher := rabinkarp.Make(rabinkarp.NaiveHasher)
	matches := matcher.MatchAll(pattern, inputs)

	sort.Sort(matches)
	fmt.Println(matches)

}
Output:
[{0 browser} {2 bebrobaum} {4 slowbro}]

Index

Examples

Constants

View Source
const (
	NaiveHasher = naiveHasher(0)
)

Variables

View Source
var DefaultMatcher = Make(NaiveHasher)

DefaultMatcher provides a instance of RabinKarp with the naive hasher implementation.

Functions

This section is empty.

Types

type Hasher

type Hasher interface {
	Hash(string) int
}

Hasher is used to hash values in a RabinKarp func

type Match

type Match struct {
	Index uint
	Value string
}

Match is return when a RabinKarp function founds a match

type Matches

type Matches []Match

Matches is a slice of match. The type implements sort.Interface

func (Matches) Len

func (m Matches) Len() int

func (Matches) Less

func (m Matches) Less(i, j int) bool

func (Matches) Swap

func (m Matches) Swap(i, j int)

type RabinKarp

type RabinKarp func(string, string) (bool, Match)

RabinKarp is a function that runs the rabin karp algorithm

func Make

func Make(h Hasher) RabinKarp

Make a closure of RabinKarp which uses the specified hasher.

func (RabinKarp) MatchAll

func (f RabinKarp) MatchAll(p string, v []string) Matches

MatchAll runs RabinKarp on every string in the slice and stores all pattern matches in a matches slice and returns it.

Jump to

Keyboard shortcuts

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