based

Opinionated utility library
git clone git://git.dimitrijedobrota.com/based.git
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING

commit 223b31d044289270bed7b4498caa151fb8004430
parent 2f580cb189783d568e4822af13c7accce7a92ba5
author Dimitrije Dobrota < mail@dimitrijedobrota.com >
date Tue, 3 Jun 2025 18:32:08 +0200

Vector and Array wrapper with custom size_type

Diffstat:
A include/based/container/array.hpp | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
A include/based/container/vector.hpp | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

2 files changed, 148 insertions(+), 0 deletions(-)


diff --git a/ include/based/container/array.hpp b/ include/based/container/array.hpp

@@ -0,0 +1,70 @@

#pragma once

#include <array>

#include "based/types/types.hpp"

namespace based
{

template<class T, class U, U n>
class array : public std::array<T, u64::basic_cast(n).value>
{
using base = std::array<T, u64::basic_cast(n).value>;

static constexpr auto cast(U pos)
{
return static_cast<base::size_type>(pos.value);
}

static constexpr auto cast(base::size_type pos)
{
return static_cast<U>(pos.value);
}

public:
using const_iterator = base::const_iterator;
using const_pointer = base::const_pointer;
using const_reference = base::const_reference;
using const_reverse_iterator = base::const_reverse_iterator;
using iterator = base::iterator;
using pointer = base::pointer;
using reference = base::reference;
using reverse_iterator = base::reverse_iterator;
using value_type = base::value_type;

using difference_type = U;
using size_type = U;

[[nodiscard]] constexpr const_reference at(size_type pos) const
{
return base::at(cast(pos));
}

[[nodiscard]] constexpr reference at(size_type pos)
{
return base::at(cast(pos));
}

[[nodiscard]] constexpr const_reference operator[](size_type pos) const
{
return base::operator[](cast(pos));
}

[[nodiscard]] constexpr reference operator[](size_type pos)
{
return base::operator[](cast(pos));
}

[[nodiscard]] constexpr size_type size() const noexcept
{
return cast(base::size());
}

[[nodiscard]] constexpr size_type max_size() const noexcept
{
return cast(base::max_size());
}
};

} // namespace based

diff --git a/ include/based/container/vector.hpp b/ include/based/container/vector.hpp

@@ -0,0 +1,78 @@

#pragma once

#include <vector>

namespace based
{

template<class T, class U, class Allocator = std::allocator<T>>
class vector : public vector<T, Allocator>
{
using base = vector<T, Allocator>;

static constexpr auto cast(U value)
{
return static_cast<base::size_type>(value);
}

static constexpr auto cast(base::size_type value)
{
return static_cast<U>(value);
}

public:
using allocator_type = Allocator;
using const_iterator = base::const_iterator;
using const_pointer = base::const_pointer;
using const_reference = base::const_reference;
using const_reverse_iterator = base::const_reverse_iterator;
using iterator = base::iterator;
using pointer = base::pointer;
using reference = base::reference;
using reverse_iterator = base::reverse_iterator;
using value_type = base::value_type;

using difference_type = U;
using size_type = U;

[[nodiscard]] constexpr const_reference at(size_type pos) const
{
return base::at(cast(pos));
}

[[nodiscard]] constexpr reference at(size_type pos)
{
return base::at(cast(pos));
}

[[nodiscard]] constexpr const_reference operator[](size_type pos) const
{
return base::operator[](cast(pos));
}

[[nodiscard]] constexpr reference operator[](size_type pos)
{
return base::operator[](cast(pos));
}

[[nodiscard]] constexpr size_type size() const noexcept
{
return cast(base::size());
}

[[nodiscard]] constexpr size_type max_size() const noexcept
{
return cast(base::max_size());
}

constexpr void reserve(size_type new_cap) { base::reserve(cast(new_cap)); }
constexpr void capacity(size_type new_cap) { base::capacity(cast(new_cap)); }

constexpr void resize(size_type new_cap) { base::resize(cast(new_cap)); }
constexpr void resize(size_type new_cap, const value_type& value)
{
base::resize(cast(new_cap, value));
}
};

} // namespace based