Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions algorithm.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,53 @@ namespace firefly::std {
return largest;
}

template <typename ForwardIt, typename UnaryPredicate>
ForwardIt find_if(InputIt first, const InputIt last,
UnaryPredicate func) {

for (; first != last; first++) {
if (func(*first))
return first;
}

return last;
}

template <typename ForwardIt, typename T>
ForwardIt find(InputIt first, const InputIt last,
const T& val) {

for (; first != last; first++) {
if (*first == val)
return first;
}

return last;
}

template <typename BidirectionalIterator>
void reverse(BidirectionalIterator first, BidirectionalIterator last) {
while ((first != last) && (first != last--)) {
std::swap(*first, *last);
first++;
}
}

template <typename ForwardIt>
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 <class RandomIt>
void sort(RandomIt first, RandomIt last) {
Expand Down