Giter Site home page Giter Site logo

kmp-algorithm's Introduction

Overview

Build Status algorithm-force MyGet Build Status

KMP Algorithm .NET is the .NET implementation of Knuth–Morris–Pratt algorithm. The project defines a set of extension methods that apply the algorithm to strings and lists.

Unlike traditional KMP algorithm uses which are focused on string instances, the project provides a set of generic APIs that apply KMP algorithm to IEnumerable(T), IList(T) and IReadOnlyList(T), as long as type T is equatable. This expands the applicability of the algorithm, making searching an array of bytes in a longer array, or a collection of floats in an array of floats with same algorithm possible. In some cases, you may specify optional parameter IEqualityComparer(T) instance to provide different comparison behavior for type T.

The project also includes a "backward" version of KMP algorithm that searches the last occurrence of the target within the instance.

Getting Started

Installation

PM> Install-Package AlgorithmForce.Searching -Source https://www.myget.org/F/algorithm-force/api/v3/index.json

First and Last Index Search

Using the extension method is similar to String.IndexOf, as following example shows.

    var s = Enumerable.Range(0, 100).ToList();
    var t = new[] { 5, 6, 7, 8, 9 };

    Console.WriteLine(s.IndexOf(t)); // 5

Because List(T) and array implements IReadOnlyList(T) interface, and Int32 is equatable, the extension method IndexOf is available for search.

Starting at a specified position is also supported. The following example searches an array of integer in collection, starting at index 6.

    var s = Enumerable.Range(0, 100).ToArray();
    var t = new List<int> { 10, 11, 12, 13, 14 };

    Console.WriteLine(s.IndexOf(t, 6)); // 10

Similar to String.LastIndexOf, you can also search the last occurrence of target array in the collection. The backward version of KMP algorithm is used in the following example.

    var s = Enumerable.Range(0, 100).ToList();
    var t = new List<int> { 15, 16, 17, 18, 19 };

    Console.WriteLine(s.LastIndexOf(t)); // 15

Index Enumeration

The project provides iterator pattern for forward and backward index enumeration. The following example enumerates each of indexes by calling IndexesOf and LastIndexesOf extension method.

    var s = "1231abcdabcd123231abcdabcdabcdtrefabc";
    var t = new List<char> { 'a', 'b', 'c', 'd', 'a', 'b', 'c', 'd'};

    foreach (var index in s.IndexesOf(t))
    {
        Console.WriteLine(index); // 4, 18, 22
    }

    foreach (var index in s.LastIndexesOf(t))
    {
        Console.WriteLine(index); // 22, 18, 4
    }

In this example, caller can start enumerating each index before all indexes are found.

Searching in Deferred Execution

The project provides a set of IndexOf APIs optimized for IEnumerable(T) instance that is produced in deferred execution. That is, the search can start before whole collection is ready. The APIs are defined in AlgorithmForce.Searching.Deferred namespace.

The following example shows how to search an array of numbers in Enumerable.Range().

    // using AlgorithmForce.Searching.Deferred

    var s = Enumerable.Range(0, 100); // deferred execution 
    var t = new[] { 95, 96, 97, 98, 99 };

    Console.WriteLine(s.IndexOf(t)); // 95

A set of APIs that wrap TextReader and BinaryReader as IEnumerable(T) instances can be used together in order to search the collection in streamed content. The following code snippet shows how to search an array in StringReader by applying AsCharEnumerable() API.

    var s = "Vrogros, the Underlord, is a melee strength hero.";
    var t = new[] { 'U', 'n', 'd', 'e', 'r', 'l', 'o', 'r', 'd' };

    using (var reader = new StringReader(s))
    {
        Console.WriteLine(reader.AsCharEnumerable().IndexOf(t)); 
        // 13
    }

Table of Features

Search In \ Search With IEnumerable(T) IReadOnlyList(T) string
IEnumerable(T) Not Supported IndexOf() IndexOf()
IReadOnlyList(T) Not Supported IndexOf(), LastIndexOf(), IndexesOf(), LastIndexesOf() IndexOf(), LastIndexOf(), IndexesOf(), LastIndexesOf()
string Not Supported IndexOf(). LastIndexOf(), IndexesOf(), LastIndexesOf() Native APIs, IndexesOf(), LastIndexesOf()

Future Works

  • IndexOfAny() and IndexesOfAll() implementation
  • Span(T) and/or ReadOnlySpan(T) support
  • Vertical Search in a collecton of collections

Platform

This project targets .NET Standard 1.6 currently.

License

The project is licesed under the MIT license. Feel free to use and modify the code in your algorithm homeworks.

kmp-algorithm's People

Contributors

rvhuang avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

kabirutd

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.