Skip to content

Commit beb2b9a

Browse files
authored
Start implementations of <algorithm>, <array>, <iterator>, <utility> (#95)
* Add partial implementations of <algorithm>, <array>, <iterator>, <utility> See #52. * Avoid #pragma once.
1 parent ec527b7 commit beb2b9a

File tree

4 files changed

+218
-0
lines changed

4 files changed

+218
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#ifndef __ALGORITHM__
2+
#define __ALGORITHM__
3+
4+
namespace std {
5+
6+
template <class ForwardIt>
7+
ForwardIt min_element(ForwardIt first, ForwardIt last)
8+
{
9+
if (first == last)
10+
return last;
11+
ForwardIt smallest = first;
12+
while (++first != last) {
13+
if (*first < *smallest)
14+
smallest = first;
15+
}
16+
return smallest;
17+
}
18+
19+
template <class ForwardIt, class Compare>
20+
ForwardIt min_element(ForwardIt first, ForwardIt last, Compare comp)
21+
{
22+
if (first == last)
23+
return last;
24+
ForwardIt smallest = first;
25+
while (++first != last) {
26+
if (comp(*first, *smallest))
27+
smallest = first;
28+
}
29+
return smallest;
30+
}
31+
32+
template <class ForwardIt>
33+
ForwardIt max_element(ForwardIt first, ForwardIt last)
34+
{
35+
if (first == last)
36+
return last;
37+
ForwardIt largest = first;
38+
while (++first != last) {
39+
if (*largest < *first)
40+
largest = first;
41+
}
42+
return largest;
43+
}
44+
45+
template <class ForwardIt, class Compare>
46+
ForwardIt max_element(ForwardIt first, ForwardIt last, Compare comp)
47+
{
48+
if (first == last)
49+
return last;
50+
ForwardIt largest = first;
51+
while (++first != last) {
52+
if (comp(*largest, *first))
53+
largest = first;
54+
}
55+
return largest;
56+
}
57+
58+
}
59+
60+
#endif // __ALGORITHM__

mos-platform/common/include/array

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef __ARRAY__
2+
#define __ARRAY__
3+
4+
#include <cstddef>
5+
#include <iterator>
6+
7+
namespace std {
8+
9+
template <class T, size_t _N>
10+
struct array {
11+
using value_type = T;
12+
using pointer = T*;
13+
using const_pointer = const T*;
14+
using reference = T&;
15+
using const_reference = const T&;
16+
using size_type = size_t;
17+
using difference_type = ptrdiff_t;
18+
using iterator = T*;
19+
using const_iterator = const T*;
20+
21+
constexpr iterator begin() noexcept { return contents; }
22+
constexpr const_iterator begin() const noexcept { return contents; }
23+
constexpr iterator end() noexcept { return contents + _N; }
24+
constexpr const_iterator end() const noexcept { return contents + _N; }
25+
26+
constexpr const_iterator cbegin() const noexcept { return contents; }
27+
constexpr const_iterator cend() const noexcept { return contents + _N; }
28+
29+
[[nodiscard]] constexpr bool empty() const noexcept { return _N == 0; }
30+
constexpr size_type size() const noexcept { return _N; }
31+
constexpr size_type max_size() const noexcept { return _N; }
32+
33+
constexpr reference operator[](size_type n) { return contents[n]; }
34+
constexpr const_reference operator[](size_type n) const { return contents[n]; }
35+
constexpr reference front() { return contents[0]; }
36+
constexpr const_reference front() const { return contents[0]; }
37+
constexpr reference back() { return contents[_N - 1]; }
38+
constexpr const_reference back() const { return contents[_N - 1]; }
39+
constexpr pointer data() noexcept { return contents; }
40+
constexpr const_pointer data() const noexcept { return contents; }
41+
private:
42+
T contents[_N];
43+
};
44+
45+
}
46+
47+
#endif // __ARRAY__
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef __ITERATOR__
2+
#define __ITERATOR__
3+
4+
#include <cstddef>
5+
6+
namespace std {
7+
8+
template <class C>
9+
constexpr auto begin(C &c) -> decltype(c.begin()) {
10+
return c.begin();
11+
}
12+
13+
template <class C>
14+
constexpr auto begin(const C &c) -> decltype(c.begin()) {
15+
return c.begin();
16+
}
17+
18+
template <class T, std::size_t _N>
19+
constexpr T* begin(T (&array)[_N]) noexcept {
20+
return array;
21+
}
22+
23+
template <class C>
24+
constexpr auto cbegin(const C &c) noexcept(noexcept(std::begin(c))) -> decltype(c.begin()) {
25+
return c.begin();
26+
}
27+
28+
template <class C>
29+
constexpr auto end(C &c) -> decltype(c.end()) {
30+
return c.end();
31+
}
32+
33+
template <class C>
34+
constexpr auto end(const C &c) -> decltype(c.end()) {
35+
return c.end();
36+
}
37+
38+
template <class T, std::size_t _N>
39+
constexpr T* end(T (&array)[_N]) noexcept {
40+
return array + _N;
41+
}
42+
43+
template <class C>
44+
constexpr auto cend(const C &c) noexcept(noexcept(std::end(c))) -> decltype(c.end()) {
45+
return c.end();
46+
}
47+
48+
}
49+
50+
#endif // __ITERATOR__
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef __UTILITY__
2+
#define __UTILITY__
3+
4+
#include <type_traits>
5+
6+
namespace std {
7+
8+
template <class T>
9+
constexpr T&& forward(std::remove_reference_t<T> &t) noexcept {
10+
return static_cast<T &&>(t);
11+
}
12+
13+
template <class T>
14+
constexpr T&& forward(std::remove_reference_t<T> &&t) noexcept {
15+
return static_cast<T &&>(t);
16+
}
17+
18+
template <class T1, class T2>
19+
struct pair {
20+
using first_type = T1;
21+
using second_type = T2;
22+
T1 first;
23+
T2 second;
24+
25+
pair(const pair &p) = default;
26+
pair(pair &&p) = default;
27+
28+
constexpr pair(const T1 &x, const T2 &y) :
29+
first(x),
30+
second(y) {
31+
}
32+
33+
template <class U1 = T1, class U2 = T2>
34+
constexpr pair(U1 &&x, U2 &&y) :
35+
first(std::forward<U1>(x)),
36+
second(std::forward<U2>(y)) {
37+
}
38+
39+
template <class U1, class U2>
40+
constexpr pair& operator=(const pair<U1, U2> &p) {
41+
first = p.first;
42+
second = p.second;
43+
return *this;
44+
}
45+
46+
template <class U1, class U2>
47+
constexpr pair& operator=(pair<U1, U2> &&p) {
48+
first = std::forward<U1>(p.first);
49+
second = std::forward<U2>(p.second);
50+
return *this;
51+
}
52+
};
53+
54+
template <class T1, class T2>
55+
std::pair<T1, T2> make_pair(T1 x, T2 y) {
56+
return std::pair<T1, T2>(x, y);
57+
}
58+
59+
}
60+
61+
#endif // __UTILITY__

0 commit comments

Comments
 (0)