mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-02-07 06:53:33 +00:00
Fix issue with ctl::vector constructor
This commit is contained in:
parent
4cb5e21ba8
commit
387310c659
8 changed files with 70 additions and 24 deletions
|
@ -37,24 +37,28 @@ struct array
|
||||||
constexpr reference at(size_type pos)
|
constexpr reference at(size_type pos)
|
||||||
{
|
{
|
||||||
if (pos >= N)
|
if (pos >= N)
|
||||||
throw ctl::out_of_range("out of range");
|
throw ctl::out_of_range();
|
||||||
return elems[pos];
|
return elems[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const_reference at(size_type pos) const
|
constexpr const_reference at(size_type pos) const
|
||||||
{
|
{
|
||||||
if (pos >= N)
|
if (pos >= N)
|
||||||
throw ctl::out_of_range("out of range");
|
throw ctl::out_of_range();
|
||||||
return elems[pos];
|
return elems[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr reference operator[](size_type pos)
|
constexpr reference operator[](size_type pos)
|
||||||
{
|
{
|
||||||
|
if (pos >= N)
|
||||||
|
__builtin_trap();
|
||||||
return elems[pos];
|
return elems[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
constexpr const_reference operator[](size_type pos) const
|
constexpr const_reference operator[](size_type pos) const
|
||||||
{
|
{
|
||||||
|
if (pos >= N)
|
||||||
|
__builtin_trap();
|
||||||
return elems[pos];
|
return elems[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,12 +2,33 @@
|
||||||
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
|
||||||
#ifndef CTL_ITERATOR_TRAITS_H_
|
#ifndef CTL_ITERATOR_TRAITS_H_
|
||||||
#define CTL_ITERATOR_TRAITS_H_
|
#define CTL_ITERATOR_TRAITS_H_
|
||||||
|
#include "iterator.h"
|
||||||
#include "utility.h"
|
#include "utility.h"
|
||||||
|
#include "void_t.h"
|
||||||
|
|
||||||
namespace ctl {
|
namespace ctl {
|
||||||
|
|
||||||
template<class Iterator>
|
template<typename Iterator, typename = void>
|
||||||
struct iterator_traits
|
struct iterator_traits
|
||||||
|
{};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct iterator_traits<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 iterator_category = typename Iterator::iterator_category;
|
||||||
using value_type = typename Iterator::value_type;
|
using value_type = typename Iterator::value_type;
|
||||||
|
@ -16,16 +37,6 @@ struct iterator_traits
|
||||||
using reference = typename Iterator::reference;
|
using reference = typename Iterator::reference;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<class 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 pointer = T*;
|
|
||||||
using reference = T&;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace ctl
|
} // namespace ctl
|
||||||
|
|
||||||
#endif // CTL_ITERATOR_TRAITS_H_
|
#endif // CTL_ITERATOR_TRAITS_H_
|
||||||
|
|
|
@ -165,7 +165,7 @@ class map
|
||||||
{
|
{
|
||||||
auto it = find(key);
|
auto it = find(key);
|
||||||
if (it == end())
|
if (it == end())
|
||||||
throw ctl::out_of_range("out of range");
|
throw ctl::out_of_range();
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -173,7 +173,7 @@ class map
|
||||||
{
|
{
|
||||||
auto it = find(key);
|
auto it = find(key);
|
||||||
if (it == end())
|
if (it == end())
|
||||||
throw ctl::out_of_range("out of range");
|
throw ctl::out_of_range();
|
||||||
return it->second;
|
return it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
19
ctl/require_input_iterator.h
Normal file
19
ctl/require_input_iterator.h
Normal file
|
@ -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
ctl/vector.h
15
ctl/vector.h
|
@ -15,6 +15,7 @@
|
||||||
#include "move_backward.h"
|
#include "move_backward.h"
|
||||||
#include "move_iterator.h"
|
#include "move_iterator.h"
|
||||||
#include "out_of_range.h"
|
#include "out_of_range.h"
|
||||||
|
#include "require_input_iterator.h"
|
||||||
#include "reverse_iterator.h"
|
#include "reverse_iterator.h"
|
||||||
#include "uninitialized_fill.h"
|
#include "uninitialized_fill.h"
|
||||||
#include "uninitialized_fill_n.h"
|
#include "uninitialized_fill_n.h"
|
||||||
|
@ -65,7 +66,7 @@ class vector
|
||||||
resize(count);
|
resize(count);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class InputIt>
|
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
|
||||||
vector(InputIt first, InputIt last, const Allocator& alloc = Allocator())
|
vector(InputIt first, InputIt last, const Allocator& alloc = Allocator())
|
||||||
: alloc_(alloc), data_(nullptr), size_(0), capacity_(0)
|
: alloc_(alloc), data_(nullptr), size_(0), capacity_(0)
|
||||||
{
|
{
|
||||||
|
@ -173,7 +174,7 @@ class vector
|
||||||
size_ = count;
|
size_ = count;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class InputIt>
|
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
|
||||||
void assign(InputIt first, InputIt last)
|
void assign(InputIt first, InputIt last)
|
||||||
{
|
{
|
||||||
clear();
|
clear();
|
||||||
|
@ -189,24 +190,28 @@ class vector
|
||||||
reference at(size_type pos)
|
reference at(size_type pos)
|
||||||
{
|
{
|
||||||
if (pos >= size_)
|
if (pos >= size_)
|
||||||
throw ctl::out_of_range("out of range");
|
throw ctl::out_of_range();
|
||||||
return data_[pos];
|
return data_[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
const_reference at(size_type pos) const
|
const_reference at(size_type pos) const
|
||||||
{
|
{
|
||||||
if (pos >= size_)
|
if (pos >= size_)
|
||||||
throw ctl::out_of_range("out of range");
|
throw ctl::out_of_range();
|
||||||
return data_[pos];
|
return data_[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
reference operator[](size_type pos)
|
reference operator[](size_type pos)
|
||||||
{
|
{
|
||||||
|
if (pos >= size_)
|
||||||
|
__builtin_trap();
|
||||||
return data_[pos];
|
return data_[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
const_reference operator[](size_type pos) const
|
const_reference operator[](size_type pos) const
|
||||||
{
|
{
|
||||||
|
if (pos >= size_)
|
||||||
|
__builtin_trap();
|
||||||
return data_[pos];
|
return data_[pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -368,7 +373,7 @@ class vector
|
||||||
return it;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class InputIt>
|
template<class InputIt, typename = ctl::require_input_iterator<InputIt>>
|
||||||
iterator insert(const_iterator pos, InputIt first, InputIt last)
|
iterator insert(const_iterator pos, InputIt first, InputIt last)
|
||||||
{
|
{
|
||||||
difference_type count = ctl::distance(first, last);
|
difference_type count = ctl::distance(first, last);
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_
|
#ifndef COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_
|
||||||
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_
|
#define COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_
|
||||||
#include "libc/mem/alloca.h"
|
#include "libc/mem/alloca.h"
|
||||||
#include "libc/nexgen32e/stackframe.h"
|
#include "libc/nexgen32e/stackframe.h"
|
||||||
COSMOPOLITAN_C_START_
|
COSMOPOLITAN_C_START_
|
||||||
|
@ -8,4 +8,4 @@ const char *DescribeBacktrace(char[160], const struct StackFrame *) libcesque;
|
||||||
#define DescribeBacktrace(x) DescribeBacktrace(alloca(160), x)
|
#define DescribeBacktrace(x) DescribeBacktrace(alloca(160), x)
|
||||||
|
|
||||||
COSMOPOLITAN_C_END_
|
COSMOPOLITAN_C_END_
|
||||||
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_INTERNAL_H_ */
|
#endif /* COSMOPOLITAN_LIBC_INTRIN_DESCRIBEBACKTRACE_H_ */
|
||||||
|
|
|
@ -42,4 +42,3 @@
|
||||||
void *malloc(size_t n) {
|
void *malloc(size_t n) {
|
||||||
return dlmalloc(n);
|
return dlmalloc(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -352,5 +352,13 @@ main()
|
||||||
return 80;
|
return 80;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
ctl::vector<int> dog(8, 0);
|
||||||
|
if (dog.size() != 8)
|
||||||
|
return 81;
|
||||||
|
if (dog[0] != 0)
|
||||||
|
return 82;
|
||||||
|
}
|
||||||
|
|
||||||
CheckForMemoryLeaks();
|
CheckForMemoryLeaks();
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue