Skip to content

Instantly share code, notes, and snippets.

@UplinkCoder
Last active August 29, 2015 14:14
Show Gist options
  • Save UplinkCoder/822c6f6795b109ec2463 to your computer and use it in GitHub Desktop.
Save UplinkCoder/822c6f6795b109ec2463 to your computer and use it in GitHub Desktop.
small patternMatching template
// 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