Application Context Implementation #4

Closed
kayomn wants to merge 93 commits from event-loop-dev into main
1 changed files with 12 additions and 12 deletions
Showing only changes of commit 14e5e18228 - Show all commits

View File

@ -63,10 +63,12 @@ pub fn Spliterator(comptime Element: type) type {
fn testEquality(this: Element, that: Element) bool { fn testEquality(this: Element, that: Element) bool {
return this == that; return this == that;
} }
}.testEquality)) |index| { }.testEquality)) |head| {
defer self.source = self.source[(index + self.delimiter.len) .. self.source.len]; const tail = (head + self.delimiter.len);
return self.source[0 .. index]; defer self.source = self.source[tail .. self.source.len];
return self.source[0 .. (tail - 1)];
} }
defer self.source = self.source[self.source.len .. self.source.len]; defer self.source = self.source[self.source.len .. self.source.len];
@ -254,17 +256,15 @@ test "Find first of element" {
pub fn findFirstOf(comptime Element: type, haystack: []const Element, pub fn findFirstOf(comptime Element: type, haystack: []const Element,
needle: []const Element, comptime testEquality: fn (Element, Element) bool) ?usize { needle: []const Element, comptime testEquality: fn (Element, Element) bool) ?usize {
kayomn marked this conversation as resolved Outdated

Worth mentioning that it performs linear time, making it O(n) time complexity?

Worth mentioning that it performs linear time, making it O(n) time complexity?
var cursor: usize = 0; var head: usize = 0;
const end = (haystack.len - needle.len); const tail = (haystack.len - needle.len);
walk_haystack: while (cursor <= end) : (cursor += 1) { walk_haystack: while (head <= tail) : (head += 1) {
const range = (cursor + needle.len); for (needle) |element, index| {
var index = cursor; if (!testEquality(haystack[head + index], element)) continue: walk_haystack;
}
while (index < range) : (index += 1) return head;
if (testEquality(haystack[index], needle[index])) continue: walk_haystack;
return cursor;
} }
return null; return null;