Get rid of compressed_pair

This felt so good. Omg. Bye. You won’t be missed.
This commit is contained in:
Steven Dee (Jōshin) 2024-06-15 19:00:04 -07:00
parent 393f024a4d
commit a9fb3193d1
No known key found for this signature in database
3 changed files with 23 additions and 140 deletions

View file

@ -1,19 +0,0 @@
// -*- mode:c++; indent-tabs-mode:nil; c-basic-offset:4; coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
// Copyright 2024 Justine Alexandra Roberts Tunney
//
// Permission to use, copy, modify, and/or distribute this software for
// any purpose with or without fee is hereby granted, provided that the
// above copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
// AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
// DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
// PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
// TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
#include "compressed_pair.h"

View file

@ -1,101 +0,0 @@
// -*-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 COSMOPOLITAN_CTL_COMPRESSED_PAIR_H_
#define COSMOPOLITAN_CTL_COMPRESSED_PAIR_H_
#include <__type_traits/is_empty.h>
#include <__type_traits/is_final.h>
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/swap.h>
namespace ctl {
namespace __ {
template<typename T>
concept EmptyBase = std::is_empty_v<T> && !std::is_final_v<T>;
template<typename T, int I>
struct pair_elem
{
using value_type = T;
constexpr pair_elem(auto&& u) : t(std::forward<decltype(u)>(u))
{
}
constexpr T& get() noexcept
{
return t;
}
constexpr const T& get() const noexcept
{
return t;
}
private:
T t;
};
template<EmptyBase T, int I>
struct pair_elem<T, I> : private T
{
using value_type = T;
constexpr pair_elem(auto&& u) : T(std::forward<decltype(u)>(u))
{
}
constexpr T& get() noexcept
{
return static_cast<T&>(*this);
}
constexpr const T& get() const noexcept
{
return static_cast<const T&>(*this);
}
protected:
};
} // namespace __
template<typename A, typename B>
struct compressed_pair
: private __::pair_elem<A, 0>
, private __::pair_elem<B, 1>
{
using first_type = __::pair_elem<A, 0>;
using second_type = __::pair_elem<B, 1>;
constexpr compressed_pair(auto&& a, auto&& b)
: first_type(std::forward<decltype(a)>(a))
, second_type(std::forward<decltype(b)>(b))
{
}
constexpr typename first_type::value_type& first() noexcept
{
return static_cast<first_type&>(*this).get();
}
constexpr const typename first_type::value_type& first() const noexcept
{
return static_cast<const first_type&>(*this).get();
}
constexpr typename second_type::value_type& second() noexcept
{
return static_cast<second_type&>(*this).get();
}
constexpr const typename second_type::value_type& second() const noexcept
{
return static_cast<const second_type&>(*this).get();
}
};
} // namespace ctl
#endif // COSMOPOLITAN_CTL_COMPRESSED_PAIR_H_

View file

@ -2,7 +2,9 @@
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef COSMOPOLITAN_CTL_UNIQUE_PTR_H_
#define COSMOPOLITAN_CTL_UNIQUE_PTR_H_
#include "compressed_pair.h"
#include <__utility/forward.h>
#include <__utility/move.h>
#include <__utility/swap.h>
namespace ctl {
@ -22,25 +24,25 @@ struct unique_ptr
using element_type = T;
using deleter_type = D;
compressed_pair<pointer, D> d;
pointer p;
[[no_unique_address]] deleter_type d;
constexpr unique_ptr(nullptr_t = nullptr) noexcept : d(nullptr, D{})
constexpr unique_ptr(nullptr_t = nullptr) noexcept : p(nullptr)
{
}
constexpr unique_ptr(pointer p) noexcept : d(p, D{})
constexpr unique_ptr(pointer p) noexcept : p(p)
{
}
constexpr unique_ptr(pointer p, auto&& d) noexcept
: d(p, std::forward<decltype(d)>(d))
: p(p), d(std::forward<decltype(d)>(d))
{
}
constexpr unique_ptr(unique_ptr&& u) noexcept
: d(u.d.first(), std::move(u.d.second()))
constexpr unique_ptr(unique_ptr&& u) noexcept : p(u.p), d(std::move(u.d))
{
u.d.first() = nullptr;
u.p = nullptr;
}
// TODO(mrdomino):
@ -62,16 +64,16 @@ struct unique_ptr
inline pointer release() noexcept
{
pointer r = d.first();
d.first() = nullptr;
pointer r = p;
p = nullptr;
return r;
}
inline void reset(nullptr_t = nullptr) noexcept
{
if (*this)
d.second()(d.first());
d.first() = nullptr;
d(p);
p = nullptr;
}
template<typename U>
@ -80,35 +82,36 @@ struct unique_ptr
inline void reset(U* p2)
{
if (*this) {
d.second()(d.first());
d(p);
}
d.first() = static_cast<pointer>(p2);
p = static_cast<pointer>(p2);
}
inline void swap(unique_ptr& r) noexcept
{
using std::swap;
swap(p, r.p);
swap(d, r.d);
}
inline pointer get() const noexcept
{
return d.first();
return p;
}
inline deleter_type& get_deleter() noexcept
{
return d.second();
return d;
}
inline const deleter_type& get_deleter() const noexcept
{
return d.second();
return d;
}
inline explicit operator bool() const noexcept
{
return d.first();
return p;
}
inline element_type& operator*() const
@ -116,14 +119,14 @@ struct unique_ptr
{
if (!*this)
__builtin_trap();
return *d.first();
return *p;
}
inline pointer operator->() const noexcept
{
if (!*this)
__builtin_trap();
return d.first();
return p;
}
};