Introduce more CTL content

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.
This commit is contained in:
Justine Tunney 2024-06-27 22:18:55 -07:00
parent 054da021d0
commit 38921dc46b
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
52 changed files with 2980 additions and 193 deletions

173
ctl/move_iterator.h Normal file
View file

@ -0,0 +1,173 @@
#ifndef CTL_MOVE_ITERATOR_H_
#define CTL_MOVE_ITERATOR_H_
#include "iterator_traits.h"
#include "type_traits.h"
namespace ctl {
template<typename Iterator>
class move_iterator
{
public:
using iterator_type = Iterator;
using iterator_category =
typename ctl::iterator_traits<Iterator>::iterator_category;
using value_type = typename ctl::iterator_traits<Iterator>::value_type;
using difference_type =
typename ctl::iterator_traits<Iterator>::difference_type;
using pointer = Iterator;
using reference = value_type&&;
constexpr move_iterator() : current()
{
}
explicit constexpr move_iterator(Iterator i) : current(i)
{
}
template<class U>
constexpr move_iterator(const move_iterator<U>& u) : current(u.base())
{
}
constexpr Iterator base() const
{
return current;
}
constexpr reference operator*() const
{
return static_cast<reference>(*current);
}
constexpr pointer operator->() const
{
return current;
}
constexpr move_iterator& operator++()
{
++current;
return *this;
}
constexpr move_iterator operator++(int)
{
move_iterator tmp = *this;
++current;
return tmp;
}
constexpr move_iterator& operator--()
{
--current;
return *this;
}
constexpr move_iterator operator--(int)
{
move_iterator tmp = *this;
--current;
return tmp;
}
constexpr move_iterator operator+(difference_type n) const
{
return move_iterator(current + n);
}
constexpr move_iterator& operator+=(difference_type n)
{
current += n;
return *this;
}
constexpr move_iterator operator-(difference_type n) const
{
return move_iterator(current - n);
}
constexpr move_iterator& operator-=(difference_type n)
{
current -= n;
return *this;
}
constexpr reference operator[](difference_type n) const
{
return ctl::move(current[n]);
}
private:
Iterator current;
};
template<typename Iterator>
__attribute__((__always_inline__)) constexpr move_iterator<Iterator>
make_move_iterator(Iterator i)
{
return move_iterator<Iterator>(i);
}
template<typename Iterator1, typename Iterator2>
constexpr bool
operator==(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y)
{
return x.base() == y.base();
}
template<typename Iterator1, typename Iterator2>
constexpr bool
operator!=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y)
{
return !(x == y);
}
template<typename Iterator1, typename Iterator2>
constexpr bool
operator<(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y)
{
return x.base() < y.base();
}
template<typename Iterator1, typename Iterator2>
constexpr bool
operator<=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y)
{
return !(y < x);
}
template<typename Iterator1, typename Iterator2>
constexpr bool
operator>(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y)
{
return y < x;
}
template<typename Iterator1, typename Iterator2>
constexpr bool
operator>=(const move_iterator<Iterator1>& x, const move_iterator<Iterator2>& y)
{
return !(x < y);
}
template<typename Iterator>
constexpr move_iterator<Iterator>
operator+(typename move_iterator<Iterator>::difference_type n,
const move_iterator<Iterator>& x)
{
return x + n;
}
template<typename Iterator1, typename Iterator2>
constexpr auto
operator-(const move_iterator<Iterator1>& x,
const move_iterator<Iterator2>& y) -> decltype(x.base() - y.base())
{
return x.base() - y.base();
}
} // namespace ctl
#endif // CTL_MOVE_ITERATOR_H_