Initial commit and a working version of KarpRabin

parents
use nix
.spago/
node_modules/
output/
import (
builtins.fetchTarball {
url = "https://github.com/NixOS/nixpkgs/archive/22.11.tar.gz";
}
)
let upstream =
https://github.com/garganscript/package-sets/releases/download/v0.1.7/release.dhall
sha256:52886309e1f0158a85427f80c1e3d47ce747c5f14fcec671a81fe5c2c711a6db
in upstream
{ pkgs ? import ./nix/pinned.nix {} }:
let
default-shell = import (
pkgs.fetchFromGitHub {
owner = "garganscript";
repo = "package-sets";
rev = "master";
sha256 = "RYFsTna5cg7EjynLvkFg87zTLSMeZw7DNIgPu9iXokk=";
} + "/default-shell.nix");
build = pkgs.writeShellScriptBin "build" ''
#!/usr/bin/env bash
set -e
echo "Installing JS Dependencies"
yarn
echo "Compiling"
# 0.15
spago build
#spago bundle-module --main Main --to dist/bundle.js
'';
test-ps = pkgs.writeShellScriptBin "test-ps" ''
#!/usr/bin/env bash
set -e
echo "Compiling"
yarn
echo "Testing"
spago -x test.dhall test --main Test.Main
'';
in
pkgs.mkShell {
name = "purescript-string-search";
buildInputs = [
build
test-ps
default-shell.purs
default-shell.easy-ps.psc-package
default-shell.easy-ps.spago
default-shell.build
default-shell.pkgs.dhall-json
default-shell.pkgs.nodejs
];
}
{-
Welcome to a Spago project!
You can edit this file as you like.
Need help? See the following resources:
- Spago documentation: https://github.com/purescript/spago
- Dhall language tour: https://docs.dhall-lang.org/tutorials/Language-Tour.html
When creating a new Spago project, you can use
`spago init --no-comments` or `spago init -C`
to generate this file without the comments in this block.
-}
{ name = "string-search"
, dependencies =
[ "arrays"
, "debug"
, "enums"
, "foldable-traversable"
, "integers"
, "lists"
, "maybe"
, "ordered-collections"
, "partial"
, "prelude"
, "strings"
, "tuples"
, "uint"
]
, packages = ./packages.dhall
, sources = [ "src/**/*.purs" ]
}
module Data.String.Search where
This diff is collapsed.
module Data.String.Search.Utils where
import Data.Array
import Prelude
slidingWindow :: forall a. Array a -> Int -> Array (Array a)
slidingWindow lst len =
let diff = length lst - len
in
if diff < 0 then []
else (\idx -> slice idx (idx + len) lst) <$> 0 .. diff
let conf = ./spago.dhall
in conf
// { sources = conf.sources # [ "test/**/*.purs" ]
, dependencies =
conf.dependencies
# [ "aff", "effect", "spec", "spec-discovery", "spec-quickcheck" ]
}
module Data.String.Search.KarpRabin.Spec where
import Prelude
import Data.Array (index)
import Data.Foldable (all)
import Data.Maybe (Maybe(..), isJust)
import Data.String (drop, stripPrefix, Pattern(..))
import Data.String.Search.KarpRabin (indicesOfAnyLegacy, indicesOfAny)
import Data.Tuple (Tuple(..))
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual)
import Test.Spec.QuickCheck (quickCheck')
validIndices :: Array String -> String -> Boolean
validIndices pats input = all validIndex (indicesOfAny pats input)
where
validIndex (Tuple i ps) = all validPat ps
where
input' = drop i input
validPat p =
case index pats p of
Just pat -> isJust (stripPrefix (Pattern pat) input')
-- <?> (show input' <> " should start with " <> show pat)
Nothing -> false -- Failed "out of bounds pattern"
-- indicesOfAny :: Array String -> String -> Array (Tuple Int (Array Int))
-- indicesOfAny = indicesOfAnyLegacy
spec :: Spec Unit
spec =
describe "KarpRabin" do
it "works on a single pattern matching two times" do
let pats = ["ab"]
let input = "abcbab"
let output = [Tuple 0 [0], Tuple 4 [0]]
indicesOfAny pats input `shouldEqual` output
-- hashes: 1650, 1667, 1682, 1665, 1650
it "works on a many unmatching patterns" do
let pats = ["abd","e","bac","abcbabe"]
let input = "abcbab"
let output = []
indicesOfAny pats input `shouldEqual` output
it "works on a simple case" do
let pats = ["ab","cb","bc","bca"]
let input = "abcbab"
let output = [Tuple 0 [0]
,Tuple 1 [2]
,Tuple 2 [1]
,Tuple 4 [0]
]
indicesOfAny pats input `shouldEqual` output
it "works with overlaps" do
let pats = ["aba"]
let input = "ababa"
let output = [Tuple 0 [0]
,Tuple 2 [0]
]
indicesOfAny pats input `shouldEqual` output
it "returns valid indices" do
validIndices ["a","ab","ba","abc","aba","abab","abcde"]
"ababarbabacbbababcaccacabbababa"
`shouldEqual` true
it "returns valid indices 2000 random samples" do
quickCheck' 2000 validIndices
module Test.Main where
import Prelude
import Effect (Effect)
import Effect.Aff (launchAff_)
import Test.Spec.Discovery (discover)
import Test.Spec.Reporter.Console (consoleReporter)
import Test.Spec.Runner (runSpec)
main :: Effect Unit
main = launchAff_ do
specs <- discover "Data\\.String\\.Search\\..*Spec"
runSpec [consoleReporter] specs
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment