mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-01-31 03:27:39 +00:00
38921dc46b
This change introduces accumulate, addressof, advance, all_of, distance, array, enable_if, allocator_traits, back_inserter, bad_alloc, is_signed, any_of, copy, exception, fill, fill_n, is_same, is_same_v, out_of_range, lexicographical_compare, is_integral, uninitialized_fill_n, is_unsigned, numeric_limits, uninitialized_fill, iterator_traits, move_backward, min, max, iterator_tag, move_iterator, reverse_iterator, uninitialized_move_n This change experiments with rewriting the ctl::vector class to make the CTL design more similar to the STL. So far it has not slowed things down to have 42 #include lines rather than 2, since it's still almost nothing compared to LLVM's code. In fact the closer we can flirt with being just like libcxx, the better chance we might have of discovering exactly what makes it so slow to compile. It would be an enormous discovery if we can find one simple trick to solving the issue there instead. This also fixes a bug in `ctl::string(const string &s)` when `s` is big.
183 lines
4.2 KiB
C++
183 lines
4.2 KiB
C++
// -*-mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8-*-
|
|
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
|
#ifndef CTL_REVERSE_ITERATOR_H_
|
|
#define CTL_REVERSE_ITERATOR_H_
|
|
#include "iterator_traits.h"
|
|
#include "utility.h"
|
|
|
|
namespace ctl {
|
|
|
|
template<class Iterator>
|
|
class reverse_iterator
|
|
{
|
|
public:
|
|
using iterator_type = Iterator;
|
|
using iterator_category =
|
|
typename iterator_traits<Iterator>::iterator_category;
|
|
using value_type = typename iterator_traits<Iterator>::value_type;
|
|
using difference_type = typename iterator_traits<Iterator>::difference_type;
|
|
using pointer = typename iterator_traits<Iterator>::pointer;
|
|
using reference = typename iterator_traits<Iterator>::reference;
|
|
|
|
constexpr reverse_iterator() : current()
|
|
{
|
|
}
|
|
|
|
constexpr explicit reverse_iterator(Iterator x) : current(x)
|
|
{
|
|
}
|
|
|
|
template<class U>
|
|
constexpr reverse_iterator(const reverse_iterator<U>& other)
|
|
: current(other.base())
|
|
{
|
|
}
|
|
|
|
template<class U>
|
|
constexpr reverse_iterator& operator=(const reverse_iterator<U>& other)
|
|
{
|
|
current = other.base();
|
|
return *this;
|
|
}
|
|
|
|
constexpr Iterator base() const
|
|
{
|
|
return current;
|
|
}
|
|
|
|
constexpr reference operator*() const
|
|
{
|
|
Iterator tmp = current;
|
|
return *--tmp;
|
|
}
|
|
|
|
constexpr pointer operator->() const
|
|
{
|
|
return &(operator*());
|
|
}
|
|
|
|
constexpr reverse_iterator& operator++()
|
|
{
|
|
--current;
|
|
return *this;
|
|
}
|
|
|
|
constexpr reverse_iterator operator++(int)
|
|
{
|
|
reverse_iterator tmp = *this;
|
|
--current;
|
|
return tmp;
|
|
}
|
|
|
|
constexpr reverse_iterator& operator--()
|
|
{
|
|
++current;
|
|
return *this;
|
|
}
|
|
|
|
constexpr reverse_iterator operator--(int)
|
|
{
|
|
reverse_iterator tmp = *this;
|
|
++current;
|
|
return tmp;
|
|
}
|
|
|
|
constexpr reverse_iterator operator+(difference_type n) const
|
|
{
|
|
return reverse_iterator(current - n);
|
|
}
|
|
|
|
constexpr reverse_iterator& operator+=(difference_type n)
|
|
{
|
|
current -= n;
|
|
return *this;
|
|
}
|
|
|
|
constexpr reverse_iterator operator-(difference_type n) const
|
|
{
|
|
return reverse_iterator(current + n);
|
|
}
|
|
|
|
constexpr reverse_iterator& operator-=(difference_type n)
|
|
{
|
|
current += n;
|
|
return *this;
|
|
}
|
|
|
|
constexpr reference operator[](difference_type n) const
|
|
{
|
|
return *(*this + n);
|
|
}
|
|
|
|
protected:
|
|
Iterator current;
|
|
};
|
|
|
|
template<class Iterator1, class Iterator2>
|
|
constexpr bool
|
|
operator==(const reverse_iterator<Iterator1>& lhs,
|
|
const reverse_iterator<Iterator2>& rhs)
|
|
{
|
|
return lhs.base() == rhs.base();
|
|
}
|
|
|
|
template<class Iterator1, class Iterator2>
|
|
constexpr bool
|
|
operator!=(const reverse_iterator<Iterator1>& lhs,
|
|
const reverse_iterator<Iterator2>& rhs)
|
|
{
|
|
return lhs.base() != rhs.base();
|
|
}
|
|
|
|
template<class Iterator1, class Iterator2>
|
|
constexpr bool
|
|
operator<(const reverse_iterator<Iterator1>& lhs,
|
|
const reverse_iterator<Iterator2>& rhs)
|
|
{
|
|
return rhs.base() < lhs.base();
|
|
}
|
|
|
|
template<class Iterator1, class Iterator2>
|
|
constexpr bool
|
|
operator<=(const reverse_iterator<Iterator1>& lhs,
|
|
const reverse_iterator<Iterator2>& rhs)
|
|
{
|
|
return !(rhs < lhs);
|
|
}
|
|
|
|
template<class Iterator1, class Iterator2>
|
|
constexpr bool
|
|
operator>(const reverse_iterator<Iterator1>& lhs,
|
|
const reverse_iterator<Iterator2>& rhs)
|
|
{
|
|
return rhs < lhs;
|
|
}
|
|
|
|
template<class Iterator1, class Iterator2>
|
|
constexpr bool
|
|
operator>=(const reverse_iterator<Iterator1>& lhs,
|
|
const reverse_iterator<Iterator2>& rhs)
|
|
{
|
|
return !(lhs < rhs);
|
|
}
|
|
|
|
template<class Iterator>
|
|
constexpr reverse_iterator<Iterator>
|
|
operator+(typename reverse_iterator<Iterator>::difference_type n,
|
|
const reverse_iterator<Iterator>& it)
|
|
{
|
|
return reverse_iterator<Iterator>(it.base() - n);
|
|
}
|
|
|
|
template<class Iterator1, class Iterator2>
|
|
constexpr auto
|
|
operator-(const reverse_iterator<Iterator1>& lhs,
|
|
const reverse_iterator<Iterator2>& rhs) -> decltype(rhs.base() -
|
|
lhs.base())
|
|
{
|
|
return rhs.base() - lhs.base();
|
|
}
|
|
|
|
} // namespace ctl
|
|
|
|
#endif // CTL_REVERSE_ITERATOR_H_
|