authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-17 21:38:50-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-10-17 21:38:50-04:00
log644400054c5769a397ded4f569e2ac0600d65305
treeb4f340abf3922fd0ccbf0110ee5e3f2c8af77b70
parente55244c4c64830d2830dea81ab6011b61c188d25
parentff58f09b68de08a5fe33177f1874c677c762c1c0
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6259 from dec05eba/master

Use boyer-moore-horspool algorithm for indexOfPos and lastIndexOf unless the haystack or needle is very small

1 files changed, 85 insertions(+), 8 deletions(-)

lib/std/mem.zig+85-8
...@@ -853,10 +853,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize...@@ -853,10 +853,7 @@ pub fn indexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize
853853
854/// Find the index in a slice of a sub-slice, searching from the end backwards.854/// Find the index in a slice of a sub-slice, searching from the end backwards.
855/// To start looking at a different index, slice the haystack first.855/// To start looking at a different index, slice the haystack first.
856/// TODO is there even a better algorithm for this?856fn lastIndexOfLinear(comptime T: type, haystack: []const T, needle: []const T) ?usize {
857pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
858 if (needle.len > haystack.len) return null;
859
860 var i: usize = haystack.len - needle.len;857 var i: usize = haystack.len - needle.len;
861 while (true) : (i -= 1) {858 while (true) : (i -= 1) {
862 if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i;859 if (mem.eql(T, haystack[i .. i + needle.len], needle)) return i;
...@@ -864,10 +861,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us...@@ -864,10 +861,7 @@ pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?us
864 }861 }
865}862}
866863
867// TODO boyer-moore algorithm864fn indexOfPosLinear(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
868pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
869 if (needle.len > haystack.len) return null;
870
871 var i: usize = start_index;865 var i: usize = start_index;
872 const end = haystack.len - needle.len;866 const end = haystack.len - needle.len;
873 while (i <= end) : (i += 1) {867 while (i <= end) : (i += 1) {
...@@ -876,7 +870,90 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee...@@ -876,7 +870,90 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee
876 return null;870 return null;
877}871}
878872
873fn boyerMooreHorspoolPreprocessReverse(pattern: []const u8, table: *[256]usize) void {
874 for (table) |*c| {
875 c.* = pattern.len;
876 }
877
878 var i: usize = pattern.len - 1;
879 // The first item is intentionally ignored and the skip size will be pattern.len.
880 // This is the standard way boyer-moore-horspool is implemented.
881 while (i > 0) : (i -= 1) {
882 table[pattern[i]] = i;
883 }
884}
885
886fn boyerMooreHorspoolPreprocess(pattern: []const u8, table: *[256]usize) void {
887 for (table) |*c| {
888 c.* = pattern.len;
889 }
890
891 var i: usize = 0;
892 // The last item is intentionally ignored and the skip size will be pattern.len.
893 // This is the standard way boyer-moore-horspool is implemented.
894 while (i < pattern.len - 1) : (i += 1) {
895 table[pattern[i]] = pattern.len - 1 - i;
896 }
897}
898/// Find the index in a slice of a sub-slice, searching from the end backwards.
899/// To start looking at a different index, slice the haystack first.
900// Reverse boyer-moore-horspool algorithm
901pub fn lastIndexOf(comptime T: type, haystack: []const T, needle: []const T) ?usize {
902 if (needle.len > haystack.len) return null;
903 if (needle.len == 0) return haystack.len;
904
905 if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 32 or needle.len <= 2)
906 return lastIndexOfLinear(T, haystack, needle);
907
908 const haystack_bytes = sliceAsBytes(haystack);
909 const needle_bytes = sliceAsBytes(needle);
910
911 var skip_table: [256]usize = undefined;
912 boyerMooreHorspoolPreprocessReverse(needle_bytes, skip_table[0..]);
913
914 var i: usize = haystack_bytes.len - needle_bytes.len;
915 while (true) {
916 if (mem.eql(u8, haystack_bytes[i .. i + needle_bytes.len], needle_bytes)) return i;
917 const skip = skip_table[haystack_bytes[i]];
918 if (skip > i) break;
919 i -= skip;
920 }
921
922 return null;
923}
924
925// Boyer-moore-horspool algorithm
926pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, needle: []const T) ?usize {
927 if (needle.len > haystack.len) return null;
928 if (needle.len == 0) return 0;
929
930 if (!meta.trait.hasUniqueRepresentation(T) or haystack.len < 52 or needle.len <= 4)
931 return indexOfPosLinear(T, haystack, start_index, needle);
932
933 const haystack_bytes = sliceAsBytes(haystack);
934 const needle_bytes = sliceAsBytes(needle);
935
936 var skip_table: [256]usize = undefined;
937 boyerMooreHorspoolPreprocess(needle_bytes, skip_table[0..]);
938
939 var i: usize = start_index * @sizeOf(T);
940 while (i <= haystack_bytes.len - needle_bytes.len) {
941 if (mem.eql(u8, haystack_bytes[i .. i + needle_bytes.len], needle_bytes)) return i;
942 i += skip_table[haystack_bytes[i + needle_bytes.len - 1]];
943 }
944
945 return null;
946}
947
879test "mem.indexOf" {948test "mem.indexOf" {
949 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "three four").? == 8);
950 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "three four").? == 8);
951 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "two two") == null);
952 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "two two") == null);
953
954 testing.expect(indexOf(u8, "one two three four five six seven eight nine ten", "").? == 0);
955 testing.expect(lastIndexOf(u8, "one two three four five six seven eight nine ten", "").? == 48);
956
880 testing.expect(indexOf(u8, "one two three four", "four").? == 14);957 testing.expect(indexOf(u8, "one two three four", "four").? == 14);
881 testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14);958 testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14);
882 testing.expect(indexOf(u8, "one two three four", "gour") == null);959 testing.expect(indexOf(u8, "one two three four", "gour") == null);