1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-30 02:47:35 +00:00

More moving towards using signed types.

I'm still feeling this out, but I am starting to like the general idea.
This commit is contained in:
Andreas Kling 2019-02-25 22:06:55 +01:00
parent beda478821
commit 9624b54703
48 changed files with 234 additions and 250 deletions

View file

@ -29,7 +29,7 @@ public:
{
}
String(const char* cstring, size_t length, ShouldChomp shouldChomp = NoChomp)
String(const char* cstring, ssize_t length, ShouldChomp shouldChomp = NoChomp)
: m_impl(StringImpl::create(cstring, length, shouldChomp))
{
}
@ -66,13 +66,13 @@ public:
}
Vector<String> split(char separator) const;
String substring(size_t start, size_t length) const;
String substring(ssize_t start, ssize_t length) const;
bool is_null() const { return !m_impl; }
bool is_empty() const { return length() == 0; }
size_t length() const { return m_impl ? m_impl->length() : 0; }
ssize_t length() const { return m_impl ? m_impl->length() : 0; }
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
char operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
char operator[](ssize_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
bool operator==(const String&) const;
bool operator!=(const String& other) const { return !(*this == other); }

View file

@ -10,12 +10,12 @@ namespace AK {
class Bitmap {
public:
// NOTE: A wrapping Bitmap won't try to free the wrapped data.
static Bitmap wrap(byte* data, unsigned size)
static Bitmap wrap(byte* data, int size)
{
return Bitmap(data, size);
}
static Bitmap create(unsigned size, bool default_value = 0)
static Bitmap create(int size, bool default_value = 0)
{
return Bitmap(size, default_value);
}
@ -27,13 +27,13 @@ public:
m_data = nullptr;
}
unsigned size() const { return m_size; }
bool get(unsigned index) const
int size() const { return m_size; }
bool get(int index) const
{
ASSERT(index < m_size);
return 0 != (m_data[index / 8] & (1u << (index % 8)));
}
void set(unsigned index, bool value) const
void set(int index, bool value) const
{
ASSERT(index < m_size);
if (value)
@ -46,17 +46,17 @@ public:
const byte* data() const { return m_data; }
private:
explicit Bitmap(unsigned size, bool default_value)
explicit Bitmap(int size, bool default_value)
: m_size(size)
, m_owned(true)
{
ASSERT(m_size != 0);
size_t size_to_allocate = ceil_div(size, 8u);
int size_to_allocate = ceil_div(size, 8);
m_data = reinterpret_cast<byte*>(kmalloc(size_to_allocate));
memset(m_data, default_value ? 0xff : 0x00, size_to_allocate);
}
Bitmap(byte* data, unsigned size)
Bitmap(byte* data, int size)
: m_data(data)
, m_size(size)
, m_owned(false)
@ -64,7 +64,7 @@ private:
}
byte* m_data { nullptr };
unsigned m_size { 0 };
int m_size { 0 };
bool m_owned { false };
};

View file

@ -11,10 +11,10 @@ namespace AK {
template<typename T>
class Buffer : public Retainable<Buffer<T>> {
public:
static Retained<Buffer> create_uninitialized(size_t count);
static Retained<Buffer> copy(const T*, size_t count);
static Retained<Buffer> wrap(T*, size_t count);
static Retained<Buffer> adopt(T*, size_t count);
static Retained<Buffer> create_uninitialized(ssize_t count);
static Retained<Buffer> copy(const T*, ssize_t count);
static Retained<Buffer> wrap(T*, ssize_t count);
static Retained<Buffer> adopt(T*, ssize_t count);
~Buffer() { clear(); }
@ -27,42 +27,42 @@ public:
m_elements = nullptr;
}
T& operator[](size_t i) { ASSERT(i < m_size); return m_elements[i]; }
const T& operator[](size_t i) const { ASSERT(i < m_size); return m_elements[i]; }
T& operator[](ssize_t i) { ASSERT(i < m_size); return m_elements[i]; }
const T& operator[](ssize_t i) const { ASSERT(i < m_size); return m_elements[i]; }
bool is_empty() const { return !m_size; }
size_t size() const { return m_size; }
ssize_t size() const { return m_size; }
T* pointer() { return m_elements; }
const T* pointer() const { return m_elements; }
T* offset_pointer(size_t offset) { return m_elements + offset; }
const T* offset_pointer(size_t offset) const { return m_elements + offset; }
T* offset_pointer(ssize_t offset) { return m_elements + offset; }
const T* offset_pointer(ssize_t offset) const { return m_elements + offset; }
const void* end_pointer() const { return m_elements + m_size; }
// NOTE: trim() does not reallocate.
void trim(size_t size)
void trim(ssize_t size)
{
ASSERT(size <= m_size);
m_size = size;
}
void grow(size_t size);
void grow(ssize_t size);
private:
enum ConstructionMode { Uninitialized, Copy, Wrap, Adopt };
explicit Buffer(size_t); // For ConstructionMode=Uninitialized
Buffer(const T*, size_t, ConstructionMode); // For ConstructionMode=Copy
Buffer(T*, size_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt
explicit Buffer(ssize_t); // For ConstructionMode=Uninitialized
Buffer(const T*, ssize_t, ConstructionMode); // For ConstructionMode=Copy
Buffer(T*, ssize_t, ConstructionMode); // For ConstructionMode=Wrap/Adopt
Buffer() { }
T* m_elements { nullptr };
size_t m_size { 0 };
ssize_t m_size { 0 };
bool m_owned { false };
};
template<typename T>
inline Buffer<T>::Buffer(size_t size)
inline Buffer<T>::Buffer(ssize_t size)
: m_size(size)
{
m_elements = static_cast<T*>(kmalloc(size * sizeof(T)));
@ -70,7 +70,7 @@ inline Buffer<T>::Buffer(size_t size)
}
template<typename T>
inline Buffer<T>::Buffer(const T* elements, size_t size, ConstructionMode mode)
inline Buffer<T>::Buffer(const T* elements, ssize_t size, ConstructionMode mode)
: m_size(size)
{
ASSERT(mode == Copy);
@ -80,7 +80,7 @@ inline Buffer<T>::Buffer(const T* elements, size_t size, ConstructionMode mode)
}
template<typename T>
inline Buffer<T>::Buffer(T* elements, size_t size, ConstructionMode mode)
inline Buffer<T>::Buffer(T* elements, ssize_t size, ConstructionMode mode)
: m_elements(elements)
, m_size(size)
{
@ -92,7 +92,7 @@ inline Buffer<T>::Buffer(T* elements, size_t size, ConstructionMode mode)
}
template<typename T>
inline void Buffer<T>::grow(size_t size)
inline void Buffer<T>::grow(ssize_t size)
{
ASSERT(size > m_size);
ASSERT(m_owned);
@ -105,25 +105,25 @@ inline void Buffer<T>::grow(size_t size)
}
template<typename T>
inline Retained<Buffer<T>> Buffer<T>::create_uninitialized(size_t size)
inline Retained<Buffer<T>> Buffer<T>::create_uninitialized(ssize_t size)
{
return ::adopt(*new Buffer<T>(size));
}
template<typename T>
inline Retained<Buffer<T>> Buffer<T>::copy(const T* elements, size_t size)
inline Retained<Buffer<T>> Buffer<T>::copy(const T* elements, ssize_t size)
{
return ::adopt(*new Buffer<T>(elements, size, Copy));
}
template<typename T>
inline Retained<Buffer<T>> Buffer<T>::wrap(T* elements, size_t size)
inline Retained<Buffer<T>> Buffer<T>::wrap(T* elements, ssize_t size)
{
return ::adopt(*new Buffer<T>(elements, size, Wrap));
}
template<typename T>
inline Retained<Buffer<T>> Buffer<T>::adopt(T* elements, size_t size)
inline Retained<Buffer<T>> Buffer<T>::adopt(T* elements, ssize_t size)
{
return ::adopt(*new Buffer<T>(elements, size, Adopt));
}

View file

@ -37,20 +37,21 @@ public:
void operator<<(const char* str)
{
size_t len = strlen(str);
for (unsigned i = 0; i < len; ++i)
ssize_t len = strlen(str);
ASSERT(len >= 0);
for (ssize_t i = 0; i < len; ++i)
m_buffer[m_offset++] = str[i];
}
void operator<<(const String& value)
{
for (unsigned i = 0; i < value.length(); ++i)
for (ssize_t i = 0; i < value.length(); ++i)
m_buffer[m_offset++] = value[i];
}
void operator<<(const ByteBuffer& value)
{
for (size_t i = 0; i < value.size(); ++i)
for (ssize_t i = 0; i < value.size(); ++i)
m_buffer[m_offset++] = value[i];
}
@ -65,11 +66,11 @@ public:
m_buffer[m_offset++] = ch;
}
size_t offset() const { return m_offset; }
ssize_t offset() const { return m_offset; }
private:
ByteBuffer& m_buffer;
size_t m_offset { 0 };
ssize_t m_offset { 0 };
};
}

View file

@ -30,10 +30,10 @@ public:
return *this;
}
static ByteBuffer create_uninitialized(size_t size) { return ByteBuffer(Buffer<byte>::create_uninitialized(size)); }
static ByteBuffer copy(const byte* data, size_t size) { return ByteBuffer(Buffer<byte>::copy(data, size)); }
static ByteBuffer wrap(byte* data, size_t size) { return ByteBuffer(Buffer<byte>::wrap(data, size)); }
static ByteBuffer adopt(byte* data, size_t size) { return ByteBuffer(Buffer<byte>::adopt(data, size)); }
static ByteBuffer create_uninitialized(ssize_t size) { return ByteBuffer(Buffer<byte>::create_uninitialized(size)); }
static ByteBuffer copy(const byte* data, ssize_t size) { return ByteBuffer(Buffer<byte>::copy(data, size)); }
static ByteBuffer wrap(byte* data, ssize_t size) { return ByteBuffer(Buffer<byte>::wrap(data, size)); }
static ByteBuffer adopt(byte* data, ssize_t size) { return ByteBuffer(Buffer<byte>::adopt(data, size)); }
~ByteBuffer() { clear(); }
void clear() { m_impl = nullptr; }
@ -42,27 +42,27 @@ public:
bool operator!() const { return is_null(); }
bool is_null() const { return m_impl == nullptr; }
byte& operator[](size_t i) { ASSERT(m_impl); return (*m_impl)[i]; }
byte operator[](size_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
byte& operator[](ssize_t i) { ASSERT(m_impl); return (*m_impl)[i]; }
byte operator[](ssize_t i) const { ASSERT(m_impl); return (*m_impl)[i]; }
bool is_empty() const { return !m_impl || m_impl->is_empty(); }
size_t size() const { return m_impl ? m_impl->size() : 0; }
ssize_t size() const { return m_impl ? m_impl->size() : 0; }
byte* pointer() { return m_impl ? m_impl->pointer() : nullptr; }
const byte* pointer() const { return m_impl ? m_impl->pointer() : nullptr; }
byte* offset_pointer(size_t offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const byte* offset_pointer(size_t offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
byte* offset_pointer(ssize_t offset) { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const byte* offset_pointer(ssize_t offset) const { return m_impl ? m_impl->offset_pointer(offset) : nullptr; }
const void* end_pointer() const { return m_impl ? m_impl->end_pointer() : nullptr; }
// NOTE: trim() does not reallocate.
void trim(size_t size)
void trim(ssize_t size)
{
if (m_impl)
m_impl->trim(size);
}
ByteBuffer slice(size_t offset, size_t size) const
ByteBuffer slice(ssize_t offset, ssize_t size) const
{
if (is_null())
return { };
@ -73,7 +73,7 @@ public:
return copy(offset_pointer(offset), size);
}
void grow(size_t size)
void grow(ssize_t size)
{
if (!m_impl)
m_impl = Buffer<byte>::create_uninitialized(size);

View file

@ -36,7 +36,7 @@ String String::isolated_copy() const
return String(move(*impl));
}
String String::substring(size_t start, size_t length) const
String String::substring(ssize_t start, ssize_t length) const
{
ASSERT(m_impl);
ASSERT(start + length <= m_impl->length());
@ -54,17 +54,17 @@ Vector<String> String::split(const char separator) const
return { };
Vector<String> v;
size_t substart = 0;
for (size_t i = 0; i < length(); ++i) {
ssize_t substart = 0;
for (ssize_t i = 0; i < length(); ++i) {
char ch = characters()[i];
if (ch == separator) {
size_t sublen = i - substart;
ssize_t sublen = i - substart;
if (sublen != 0)
v.append(substring(substart, sublen));
substart = i + 1;
}
}
size_t taillen = length() - substart;
ssize_t taillen = length() - substart;
if (taillen != 0)
v.append(substring(substart, taillen));
if (characters()[length() - 1] == separator)
@ -82,7 +82,7 @@ ByteBuffer String::to_byte_buffer() const
unsigned String::to_uint(bool& ok) const
{
unsigned value = 0;
for (size_t i = 0; i < length(); ++i) {
for (ssize_t i = 0; i < length(); ++i) {
if (characters()[i] < '0' || characters()[i] > '9') {
ok = false;
return 0;

View file

@ -5,13 +5,13 @@
namespace AK {
inline void StringBuilder::will_append(size_t size)
inline void StringBuilder::will_append(ssize_t size)
{
if ((m_length + size) > m_buffer.size())
m_buffer.grow(max((size_t)16, m_buffer.size() * 2 + size));
m_buffer.grow(max((ssize_t)16, m_buffer.size() * 2 + size));
}
StringBuilder::StringBuilder(size_t initial_capacity)
StringBuilder::StringBuilder(ssize_t initial_capacity)
{
m_buffer.grow(initial_capacity);
}
@ -25,7 +25,7 @@ void StringBuilder::append(const String& str)
m_length += str.length();
}
void StringBuilder::append(const char* characters, size_t length)
void StringBuilder::append(const char* characters, ssize_t length)
{
if (!length)
return;

View file

@ -8,12 +8,12 @@ namespace AK {
class StringBuilder {
public:
explicit StringBuilder(size_t initial_capacity = 16);
explicit StringBuilder(ssize_t initial_capacity = 16);
~StringBuilder() { }
void append(const String&);
void append(char);
void append(const char*, size_t);
void append(const char*, ssize_t);
void appendf(const char*, ...);
void appendvf(const char*, va_list);
@ -21,10 +21,10 @@ public:
ByteBuffer to_byte_buffer();
private:
void will_append(size_t);
void will_append(ssize_t);
ByteBuffer m_buffer;
size_t m_length { 0 };
ssize_t m_length { 0 };
};
}

View file

@ -30,7 +30,7 @@ StringImpl& StringImpl::the_empty_stringimpl()
return *s_the_empty_stringimpl;
}
StringImpl::StringImpl(ConstructWithInlineBufferTag, size_t length)
StringImpl::StringImpl(ConstructWithInlineBufferTag, ssize_t length)
: m_length(length)
, m_characters(m_inline_buffer)
{
@ -50,12 +50,12 @@ StringImpl::~StringImpl()
#endif
}
static inline size_t allocation_size_for_stringimpl(size_t length)
static inline ssize_t allocation_size_for_stringimpl(ssize_t length)
{
return sizeof(StringImpl) + (sizeof(char) * length) + sizeof(char);
}
Retained<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buffer)
Retained<StringImpl> StringImpl::create_uninitialized(ssize_t length, char*& buffer)
{
ASSERT(length);
void* slot = kmalloc(allocation_size_for_stringimpl(length));
@ -66,7 +66,7 @@ Retained<StringImpl> StringImpl::create_uninitialized(size_t length, char*& buff
return new_stringimpl;
}
RetainPtr<StringImpl> StringImpl::create(const char* cstring, size_t length, ShouldChomp shouldChomp)
RetainPtr<StringImpl> StringImpl::create(const char* cstring, ssize_t length, ShouldChomp shouldChomp)
{
if (!cstring)
return nullptr;
@ -123,7 +123,7 @@ static inline char to_ascii_uppercase(char c)
Retained<StringImpl> StringImpl::to_lowercase() const
{
for (size_t i = 0; i < m_length; ++i) {
for (ssize_t i = 0; i < m_length; ++i) {
if (!is_ascii_lowercase(m_characters[i]))
goto slow_path;
}
@ -132,14 +132,14 @@ Retained<StringImpl> StringImpl::to_lowercase() const
slow_path:
char* buffer;
auto lowercased = create_uninitialized(m_length, buffer);
for (size_t i = 0; i < m_length; ++i)
for (ssize_t i = 0; i < m_length; ++i)
buffer[i] = to_ascii_lowercase(m_characters[i]);
return lowercased;
}
Retained<StringImpl> StringImpl::to_uppercase() const
{
for (size_t i = 0; i < m_length; ++i) {
for (ssize_t i = 0; i < m_length; ++i) {
if (!is_ascii_uppercase(m_characters[i]))
goto slow_path;
}
@ -148,7 +148,7 @@ Retained<StringImpl> StringImpl::to_uppercase() const
slow_path:
char* buffer;
auto uppercased = create_uninitialized(m_length, buffer);
for (size_t i = 0; i < m_length; ++i)
for (ssize_t i = 0; i < m_length; ++i)
buffer[i] = to_ascii_uppercase(m_characters[i]);
return uppercased;
}
@ -159,7 +159,7 @@ void StringImpl::compute_hash() const
m_hash = 0;
} else {
unsigned hash = 0;
for (size_t i = 0; i < m_length; ++i) {
for (ssize_t i = 0; i < m_length; ++i) {
hash += m_characters[i];
hash += (hash << 10);
hash ^= (hash >> 6);

View file

@ -10,9 +10,9 @@ enum ShouldChomp { NoChomp, Chomp };
class StringImpl : public Retainable<StringImpl> {
public:
static Retained<StringImpl> create_uninitialized(size_t length, char*& buffer);
static Retained<StringImpl> create_uninitialized(ssize_t length, char*& buffer);
static RetainPtr<StringImpl> create(const char* cstring, ShouldChomp = NoChomp);
static RetainPtr<StringImpl> create(const char* cstring, size_t length, ShouldChomp = NoChomp);
static RetainPtr<StringImpl> create(const char* cstring, ssize_t length, ShouldChomp = NoChomp);
Retained<StringImpl> to_lowercase() const;
Retained<StringImpl> to_uppercase() const;
@ -20,9 +20,9 @@ public:
~StringImpl();
size_t length() const { return m_length; }
ssize_t length() const { return m_length; }
const char* characters() const { return m_characters; }
char operator[](size_t i) const { ASSERT(i < m_length); return m_characters[i]; }
char operator[](ssize_t i) const { ASSERT(i >= 0 && i < m_length); return m_characters[i]; }
unsigned hash() const
{
@ -36,11 +36,11 @@ private:
explicit StringImpl(ConstructTheEmptyStringImplTag) : m_characters("") { }
enum ConstructWithInlineBufferTag { ConstructWithInlineBuffer };
StringImpl(ConstructWithInlineBufferTag, size_t length);
StringImpl(ConstructWithInlineBufferTag, ssize_t length);
void compute_hash() const;
size_t m_length { 0 };
ssize_t m_length { 0 };
mutable bool m_hasHash { false };
const char* m_characters { nullptr };
mutable unsigned m_hash { 0 };

View file

@ -9,12 +9,12 @@ namespace AK {
template<typename T, typename Allocator> class Vector;
struct KmallocAllocator {
static void* allocate(size_t size) { return kmalloc(size); }
static void* allocate(ssize_t size) { return kmalloc(size); }
static void deallocate(void* ptr) { kfree(ptr); }
};
struct KmallocEternalAllocator {
static void* allocate(size_t size) { return kmalloc_eternal(size); }
static void* allocate(ssize_t size) { return kmalloc_eternal(size); }
static void deallocate(void*) { }
};
@ -22,25 +22,25 @@ template<typename T, typename Allocator>
class VectorImpl {
public:
~VectorImpl() { }
static VectorImpl* create(size_t capacity)
static VectorImpl* create(ssize_t capacity)
{
size_t size = sizeof(VectorImpl) + sizeof(T) * capacity;
ssize_t size = sizeof(VectorImpl) + sizeof(T) * capacity;
void* slot = Allocator::allocate(size);
new (slot) VectorImpl(capacity);
return (VectorImpl*)slot;
}
size_t size() const { return m_size; }
size_t capacity() const { return m_capacity; }
ssize_t size() const { return m_size; }
ssize_t capacity() const { return m_capacity; }
T& at(size_t i) { ASSERT(i < m_size); return *slot(i); }
const T& at(size_t i) const { ASSERT(i < m_size); return *slot(i); }
T& at(ssize_t i) { ASSERT(i < m_size); return *slot(i); }
const T& at(ssize_t i) const { ASSERT(i < m_size); return *slot(i); }
void remove(size_t index)
void remove(ssize_t index)
{
ASSERT(index < m_size);
at(index).~T();
for (size_t i = index + 1; i < m_size; ++i) {
for (ssize_t i = index + 1; i < m_size; ++i) {
new (slot(i - 1)) T(move(at(i)));
at(i).~T();
}
@ -51,16 +51,16 @@ public:
//private:
friend class Vector<T, Allocator>;
VectorImpl(size_t capacity) : m_capacity(capacity) { }
VectorImpl(ssize_t capacity) : m_capacity(capacity) { }
T* tail() { return reinterpret_cast<T*>(this + 1); }
T* slot(size_t i) { return &tail()[i]; }
T* slot(ssize_t i) { return &tail()[i]; }
const T* tail() const { return reinterpret_cast<const T*>(this + 1); }
const T* slot(size_t i) const { return &tail()[i]; }
const T* slot(ssize_t i) const { return &tail()[i]; }
size_t m_size { 0 };
size_t m_capacity;
ssize_t m_size { 0 };
ssize_t m_capacity;
};
template<typename T, typename Allocator = KmallocAllocator>
@ -78,7 +78,7 @@ public:
Vector(const Vector& other)
{
ensure_capacity(other.size());
for (size_t i = 0; i < other.size(); ++i)
for (ssize_t i = 0; i < other.size(); ++i)
unchecked_append(other[i]);
}
@ -93,7 +93,7 @@ public:
void clear()
{
for (size_t i = 0; i < size(); ++i) {
for (ssize_t i = 0; i < size(); ++i) {
at(i).~T();
}
Allocator::deallocate(m_impl);
@ -104,14 +104,14 @@ public:
{
if (!m_impl)
return;
for (size_t i = 0; i < size(); ++i)
for (ssize_t i = 0; i < size(); ++i)
at(i).~T();
m_impl->m_size = 0;
}
bool contains_slow(const T& value) const
{
for (size_t i = 0; i < size(); ++i) {
for (ssize_t i = 0; i < size(); ++i) {
if (at(i) == value)
return true;
}
@ -119,17 +119,17 @@ public:
}
bool is_empty() const { return size() == 0; }
size_t size() const { return m_impl ? m_impl->size() : 0; }
size_t capacity() const { return m_impl ? m_impl->capacity() : 0; }
ssize_t size() const { return m_impl ? m_impl->size() : 0; }
ssize_t capacity() const { return m_impl ? m_impl->capacity() : 0; }
T* data() { return m_impl ? m_impl->slot(0) : nullptr; }
const T* data() const { return m_impl ? m_impl->slot(0) : nullptr; }
const T& at(size_t i) const { return m_impl->at(i); }
T& at(size_t i) { return m_impl->at(i); }
const T& at(ssize_t i) const { return m_impl->at(i); }
T& at(ssize_t i) { return m_impl->at(i); }
const T& operator[](size_t i) const { return at(i); }
T& operator[](size_t i) { return at(i); }
const T& operator[](ssize_t i) const { return at(i); }
T& operator[](ssize_t i) { return at(i); }
const T& first() const { return at(0); }
T& first() { return at(0); }
@ -154,7 +154,7 @@ public:
return value;
}
void remove(size_t index)
void remove(ssize_t index)
{
m_impl->remove(index);
}
@ -211,23 +211,23 @@ public:
++m_impl->m_size;
}
void append(const T* values, size_t count)
void append(const T* values, ssize_t count)
{
ensure_capacity(size() + count);
for (size_t i = 0; i < count; ++i)
for (ssize_t i = 0; i < count; ++i)
new (m_impl->slot(m_impl->m_size + i)) T(values[i]);
m_impl->m_size += count;
}
void ensure_capacity(size_t neededCapacity)
void ensure_capacity(ssize_t neededCapacity)
{
if (capacity() >= neededCapacity)
return;
size_t new_capacity = padded_capacity(neededCapacity);
ssize_t new_capacity = padded_capacity(neededCapacity);
auto new_impl = VectorImpl<T, Allocator>::create(new_capacity);
if (m_impl) {
new_impl->m_size = m_impl->m_size;
for (size_t i = 0; i < size(); ++i) {
for (ssize_t i = 0; i < size(); ++i) {
new (new_impl->slot(i)) T(move(m_impl->at(i)));
m_impl->at(i).~T();
}
@ -236,13 +236,13 @@ public:
m_impl = new_impl;
}
void resize(size_t new_size)
void resize(ssize_t new_size)
{
ASSERT(new_size >= size());
if (!new_size)
return;
ensure_capacity(new_size);
for (size_t i = size(); i < new_size; ++i)
for (ssize_t i = size(); i < new_size; ++i)
new (m_impl->slot(i)) T;
m_impl->m_size = new_size;
}
@ -254,9 +254,9 @@ public:
T& operator*() { return m_vector[m_index]; }
private:
friend class Vector;
Iterator(Vector& vector, size_t index) : m_vector(vector), m_index(index) { }
Iterator(Vector& vector, ssize_t index) : m_vector(vector), m_index(index) { }
Vector& m_vector;
size_t m_index { 0 };
ssize_t m_index { 0 };
};
Iterator begin() { return Iterator(*this, 0); }
@ -269,18 +269,18 @@ public:
const T& operator*() const { return m_vector[m_index]; }
private:
friend class Vector;
ConstIterator(const Vector& vector, const size_t index) : m_vector(vector), m_index(index) { }
ConstIterator(const Vector& vector, const ssize_t index) : m_vector(vector), m_index(index) { }
const Vector& m_vector;
size_t m_index { 0 };
ssize_t m_index { 0 };
};
ConstIterator begin() const { return ConstIterator(*this, 0); }
ConstIterator end() const { return ConstIterator(*this, size()); }
//private:
static size_t padded_capacity(size_t capacity)
static ssize_t padded_capacity(ssize_t capacity)
{
return max(size_t(4), capacity + (capacity / 4) + 4);
return max(ssize_t(4), capacity + (capacity / 4) + 4);
}
VectorImpl<T, Allocator>* m_impl { nullptr };