-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start implementations of <algorithm>, <array>, <iterator>, <utility> (#…
…95) * Add partial implementations of <algorithm>, <array>, <iterator>, <utility> See #52. * Avoid #pragma once.
- Loading branch information
Showing
4 changed files
with
218 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
#ifndef __ALGORITHM__ | ||
#define __ALGORITHM__ | ||
|
||
namespace std { | ||
|
||
template <class ForwardIt> | ||
ForwardIt min_element(ForwardIt first, ForwardIt last) | ||
{ | ||
if (first == last) | ||
return last; | ||
ForwardIt smallest = first; | ||
while (++first != last) { | ||
if (*first < *smallest) | ||
smallest = first; | ||
} | ||
return smallest; | ||
} | ||
|
||
template <class ForwardIt, class Compare> | ||
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp) | ||
{ | ||
if (first == last) | ||
return last; | ||
ForwardIt smallest = first; | ||
while (++first != last) { | ||
if (comp(*first, *smallest)) | ||
smallest = first; | ||
} | ||
return smallest; | ||
} | ||
|
||
template <class ForwardIt> | ||
ForwardIt max_element(ForwardIt first, ForwardIt last) | ||
{ | ||
if (first == last) | ||
return last; | ||
ForwardIt largest = first; | ||
while (++first != last) { | ||
if (*largest < *first) | ||
largest = first; | ||
} | ||
return largest; | ||
} | ||
|
||
template <class ForwardIt, class Compare> | ||
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp) | ||
{ | ||
if (first == last) | ||
return last; | ||
ForwardIt largest = first; | ||
while (++first != last) { | ||
if (comp(*largest, *first)) | ||
largest = first; | ||
} | ||
return largest; | ||
} | ||
|
||
} | ||
|
||
#endif // __ALGORITHM__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#ifndef __ARRAY__ | ||
#define __ARRAY__ | ||
|
||
#include <cstddef> | ||
#include <iterator> | ||
|
||
namespace std { | ||
|
||
template <class T, size_t _N> | ||
struct array { | ||
using value_type = T; | ||
using pointer = T*; | ||
using const_pointer = const T*; | ||
using reference = T&; | ||
using const_reference = const T&; | ||
using size_type = size_t; | ||
using difference_type = ptrdiff_t; | ||
using iterator = T*; | ||
using const_iterator = const T*; | ||
|
||
constexpr iterator begin() noexcept { return contents; } | ||
constexpr const_iterator begin() const noexcept { return contents; } | ||
constexpr iterator end() noexcept { return contents + _N; } | ||
constexpr const_iterator end() const noexcept { return contents + _N; } | ||
|
||
constexpr const_iterator cbegin() const noexcept { return contents; } | ||
constexpr const_iterator cend() const noexcept { return contents + _N; } | ||
|
||
[[nodiscard]] constexpr bool empty() const noexcept { return _N == 0; } | ||
constexpr size_type size() const noexcept { return _N; } | ||
constexpr size_type max_size() const noexcept { return _N; } | ||
|
||
constexpr reference operator[](size_type n) { return contents[n]; } | ||
constexpr const_reference operator[](size_type n) const { return contents[n]; } | ||
constexpr reference front() { return contents[0]; } | ||
constexpr const_reference front() const { return contents[0]; } | ||
constexpr reference back() { return contents[_N - 1]; } | ||
constexpr const_reference back() const { return contents[_N - 1]; } | ||
constexpr pointer data() noexcept { return contents; } | ||
constexpr const_pointer data() const noexcept { return contents; } | ||
private: | ||
T contents[_N]; | ||
}; | ||
|
||
} | ||
|
||
#endif // __ARRAY__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#ifndef __ITERATOR__ | ||
#define __ITERATOR__ | ||
|
||
#include <cstddef> | ||
|
||
namespace std { | ||
|
||
template <class C> | ||
constexpr auto begin(C &c) -> decltype(c.begin()) { | ||
return c.begin(); | ||
} | ||
|
||
template <class C> | ||
constexpr auto begin(const C &c) -> decltype(c.begin()) { | ||
return c.begin(); | ||
} | ||
|
||
template <class T, std::size_t _N> | ||
constexpr T* begin(T (&array)[_N]) noexcept { | ||
return array; | ||
} | ||
|
||
template <class C> | ||
constexpr auto cbegin(const C &c) noexcept(noexcept(std::begin(c))) -> decltype(c.begin()) { | ||
return c.begin(); | ||
} | ||
|
||
template <class C> | ||
constexpr auto end(C &c) -> decltype(c.end()) { | ||
return c.end(); | ||
} | ||
|
||
template <class C> | ||
constexpr auto end(const C &c) -> decltype(c.end()) { | ||
return c.end(); | ||
} | ||
|
||
template <class T, std::size_t _N> | ||
constexpr T* end(T (&array)[_N]) noexcept { | ||
return array + _N; | ||
} | ||
|
||
template <class C> | ||
constexpr auto cend(const C &c) noexcept(noexcept(std::end(c))) -> decltype(c.end()) { | ||
return c.end(); | ||
} | ||
|
||
} | ||
|
||
#endif // __ITERATOR__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
#ifndef __UTILITY__ | ||
#define __UTILITY__ | ||
|
||
#include <type_traits> | ||
|
||
namespace std { | ||
|
||
template <class T> | ||
constexpr T&& forward(std::remove_reference_t<T> &t) noexcept { | ||
return static_cast<T &&>(t); | ||
} | ||
|
||
template <class T> | ||
constexpr T&& forward(std::remove_reference_t<T> &&t) noexcept { | ||
return static_cast<T &&>(t); | ||
} | ||
|
||
template <class T1, class T2> | ||
struct pair { | ||
using first_type = T1; | ||
using second_type = T2; | ||
T1 first; | ||
T2 second; | ||
|
||
pair(const pair &p) = default; | ||
pair(pair &&p) = default; | ||
|
||
constexpr pair(const T1 &x, const T2 &y) : | ||
first(x), | ||
second(y) { | ||
} | ||
|
||
template <class U1 = T1, class U2 = T2> | ||
constexpr pair(U1 &&x, U2 &&y) : | ||
first(std::forward<U1>(x)), | ||
second(std::forward<U2>(y)) { | ||
} | ||
|
||
template <class U1, class U2> | ||
constexpr pair& operator=(const pair<U1, U2> &p) { | ||
first = p.first; | ||
second = p.second; | ||
return *this; | ||
} | ||
|
||
template <class U1, class U2> | ||
constexpr pair& operator=(pair<U1, U2> &&p) { | ||
first = std::forward<U1>(p.first); | ||
second = std::forward<U2>(p.second); | ||
return *this; | ||
} | ||
}; | ||
|
||
template <class T1, class T2> | ||
std::pair<T1, T2> make_pair(T1 x, T2 y) { | ||
return std::pair<T1, T2>(x, y); | ||
} | ||
|
||
} | ||
|
||
#endif // __UTILITY__ |