Last active
August 29, 2015 14:14
-
-
Save UplinkCoder/822c6f6795b109ec2463 to your computer and use it in GitHub Desktop.
small patternMatching template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// returns where the pattern matched | |
uint[] match(T)(T[] elems, T[] pattern) { | |
uint[] positionsMatched; | |
uint posInPattern; | |
uint posInElems; | |
while (elems.length - posInElems >= pattern.length - posInPattern) { | |
if (pattern[posInPattern] == elems[posInElems]) { | |
if (posInPattern == pattern.length-1) { | |
positionsMatched ~= (posInElems - posInPattern); | |
posInPattern = 0; | |
posInElems++; | |
} else { | |
posInElems++; | |
posInPattern++; | |
} | |
} else { | |
posInPattern = 0; | |
posInElems++; | |
} | |
} | |
return positionsMatched; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment