Skip to content

Commit

Permalink
Fix issue with ctl::vector constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jun 30, 2024
1 parent 4cb5e21 commit 387310c
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 24 deletions.
8 changes: 6 additions & 2 deletions ctl/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,28 @@ struct array
constexpr reference at(size_type pos)
{
if (pos >= N)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return elems[pos];
}

constexpr const_reference at(size_type pos) const
{
if (pos >= N)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return elems[pos];
}

constexpr reference operator[](size_type pos)
{
if (pos >= N)
__builtin_trap();
return elems[pos];
}

constexpr const_reference operator[](size_type pos) const
{
if (pos >= N)
__builtin_trap();
return elems[pos];
}

Expand Down
33 changes: 22 additions & 11 deletions ctl/iterator_traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,39 @@
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#ifndef CTL_ITERATOR_TRAITS_H_
#define CTL_ITERATOR_TRAITS_H_
#include "iterator.h"
#include "utility.h"
#include "void_t.h"

namespace ctl {

template<class Iterator>
template<typename Iterator, typename = void>
struct iterator_traits
{
using iterator_category = typename Iterator::iterator_category;
using value_type = typename Iterator::value_type;
using difference_type = typename Iterator::difference_type;
using pointer = typename Iterator::pointer;
using reference = typename Iterator::reference;
};
{};

template<class T>
template<typename T>
struct iterator_traits<T*>
{
using iterator_category = void*; // We don't actually use this
using value_type = T;
using difference_type = ptrdiff_t;
using value_type = T;
using pointer = T*;
using reference = T&;
using iterator_category = ctl::random_access_iterator_tag;
};

template<typename Iterator>
struct iterator_traits<Iterator,
ctl::void_t<typename Iterator::iterator_category,
typename Iterator::value_type,
typename Iterator::difference_type,
typename Iterator::pointer,
typename Iterator::reference>>
{
using iterator_category = typename Iterator::iterator_category;
using value_type = typename Iterator::value_type;
using difference_type = typename Iterator::difference_type;
using pointer = typename Iterator::pointer;
using reference = typename Iterator::reference;
};

} // namespace ctl
Expand Down
4 changes: 2 additions & 2 deletions ctl/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,15 +165,15 @@ class map
{
auto it = find(key);
if (it == end())
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return it->second;
}

const Value& at(const Key& key) const
{
auto it = find(key);
if (it == end())
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return it->second;
}

Expand Down
19 changes: 19 additions & 0 deletions ctl/require_input_iterator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// -*-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_REQUIRE_INPUT_ITERATOR_H_
#define CTL_REQUIRE_INPUT_ITERATOR_H_
#include "enable_if.h"
#include "is_convertible.h"
#include "iterator.h"
#include "iterator_traits.h"

namespace ctl {

template<typename InputIt>
using require_input_iterator = typename ctl::enable_if<
ctl::is_convertible<typename ctl::iterator_traits<InputIt>::iterator_category,
ctl::input_iterator_tag>::value>::type;

} // namespace ctl

#endif /* CTL_REQUIRE_INPUT_ITERATOR_H_ */
15 changes: 10 additions & 5 deletions ctl/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "move_backward.h"
#include "move_iterator.h"
#include "out_of_range.h"
#include "require_input_iterator.h"
#include "reverse_iterator.h"
#include "uninitialized_fill.h"
#include "uninitialized_fill_n.h"
Expand Down Expand Up @@ -65,7 +66,7 @@ class vector
resize(count);
}

template<class InputIt>
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
vector(InputIt first, InputIt last, const Allocator& alloc = Allocator())
: alloc_(alloc), data_(nullptr), size_(0), capacity_(0)
{
Expand Down Expand Up @@ -173,7 +174,7 @@ class vector
size_ = count;
}

template<class InputIt>
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
void assign(InputIt first, InputIt last)
{
clear();
Expand All @@ -189,24 +190,28 @@ class vector
reference at(size_type pos)
{
if (pos >= size_)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return data_[pos];
}

const_reference at(size_type pos) const
{
if (pos >= size_)
throw ctl::out_of_range("out of range");
throw ctl::out_of_range();
return data_[pos];
}

reference operator[](size_type pos)
{
if (pos >= size_)
__builtin_trap();
return data_[pos];
}

const_reference operator[](size_type pos) const
{
if (pos >= size_)
__builtin_trap();
return data_[pos];
}

Expand Down Expand Up @@ -368,7 +373,7 @@ class vector
return it;
}

template<class InputIt>
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
iterator insert(const_iterator pos, InputIt first, InputIt last)
{
difference_type count = ctl::distance(first, last);
Expand Down
6 changes: 3 additions & 3 deletions libc/intrin/describebacktrace.internal.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_
#include "libc/mem/alloca.h"
#include "libc/nexgen32e/stackframe.h"
COSMOPOLITAN_C_START_
Expand All @@ -8,4 +8,4 @@ const char *DescribeBacktrace(char[160], const struct StackFrame *) libcesque;
#define DescribeBacktrace(x) DescribeBacktrace(alloca(160), x)

COSMOPOLITAN_C_END_
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_ */
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_ */
1 change: 0 additions & 1 deletion libc/mem/malloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,3 @@
void *malloc(size_t n) {
return dlmalloc(n);
}

8 changes: 8 additions & 0 deletions test/ctl/vector_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -352,5 +352,13 @@ main()
return 80;
}

{
ctl::vector<int> dog(8, 0);
if (dog.size() != 8)
return 81;
if (dog[0] != 0)
return 82;
}

CheckForMemoryLeaks();
}

0 comments on commit 387310c

Please sign in to comment.