From 95a040f2daf7b05aa8b1b4d5695026a74d134de3 Mon Sep 17 00:00:00 2001 From: Ethan Date: Sun, 24 Oct 2021 15:49:54 -0400 Subject: [PATCH 1/3] Implement find and find_if --- algorithm.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/algorithm.h b/algorithm.h index f19faa5..aee8aa3 100644 --- a/algorithm.h +++ b/algorithm.h @@ -44,6 +44,32 @@ namespace firefly::std { return largest; } + + template + ForwardIt find_if(InputIt first, const InputIt last, + UnaryPredicate func) { + + for (; first != last; first++) { + if (func(*first)) + return first; + } + + return last; + } + + template + ForwardIt find(InputIt first, const InputIt last, + const T& val) { + + for (; first != last; first++) { + if (*first == val) + return first; + } + + return last; + } + + /* template void sort(RandomIt first, RandomIt last) { From d324aa158dec46e8d682ab815314de0d5e309cc0 Mon Sep 17 00:00:00 2001 From: Ethan Date: Sun, 24 Oct 2021 15:51:03 -0400 Subject: [PATCH 2/3] Implement reverse --- algorithm.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/algorithm.h b/algorithm.h index aee8aa3..a18cb52 100644 --- a/algorithm.h +++ b/algorithm.h @@ -69,6 +69,13 @@ namespace firefly::std { return last; } + template + void reverse(BidirectionalIterator first, BidirectionalIterator last) { + while ((first != last) && (first != last--)) { + std::swap(*first, *last); + first++; + } + } /* template From 4ed1a39342911909faa26f82a851f5cbac63cf88 Mon Sep 17 00:00:00 2001 From: Ethan Date: Mon, 25 Oct 2021 22:23:55 -0400 Subject: [PATCH 3/3] Implement rotate Pretty sure this works. If it doesn't, it's not a big deal, because it's only an operating system standard library. --- algorithm.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/algorithm.h b/algorithm.h index a18cb52..7cd59c7 100644 --- a/algorithm.h +++ b/algorithm.h @@ -44,7 +44,6 @@ namespace firefly::std { return largest; } - template ForwardIt find_if(InputIt first, const InputIt last, UnaryPredicate func) { @@ -77,6 +76,21 @@ namespace firefly::std { } } + template + ForwardIt rotate(const ForwardIt first, ForwardIt n_first, ForwardIt last) { + ForwardIt head = first; + ForwardIt tail = n_first; + + if (first == n_first) return last; + if (last == n_first) return first; + + while (tail != last) { + std::swap(*head++, *tail++); + } + + return n_first; + } + /* template void sort(RandomIt first, RandomIt last) {