Skip to content

Commit

Permalink
Start implementations of <algorithm>, <array>, <iterator>, <utility> (l…
Browse files Browse the repository at this point in the history
…lvm-mos#95)

* Add partial implementations of <algorithm>, <array>, <iterator>, <utility>

See llvm-mos#52.

* Avoid #pragma once.
  • Loading branch information
pfusik committed Dec 21, 2022
1 parent a198cd9 commit b790334
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 0 deletions.
60 changes: 60 additions & 0 deletions mos-platform/common/include/algorithm
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__
47 changes: 47 additions & 0 deletions mos-platform/common/include/array
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__
50 changes: 50 additions & 0 deletions mos-platform/common/include/iterator
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__
61 changes: 61 additions & 0 deletions mos-platform/common/include/utility
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__

0 comments on commit b790334

Please sign in to comment.