Commit 40b90e4c authored by Kenton Varda's avatar Kenton Varda

Allow initializing kj::Vector from kj::Array and add other methods of Array.

I'm increasingly thinking that maybe kj::Array itself should support Vector semantics, but for now this change makes it easier to write code that uses Vector instead of Array.
parent e92255a0
...@@ -177,6 +177,11 @@ public: ...@@ -177,6 +177,11 @@ public:
inline T& front() { return *ptr; } inline T& front() { return *ptr; }
inline T& back() { return *(ptr + size_ - 1); } inline T& back() { return *(ptr + size_ - 1); }
template <typename U>
inline bool operator==(const U& other) const { return asPtr() == other; }
template <typename U>
inline bool operator!=(const U& other) const { return asPtr() != other; }
inline ArrayPtr<T> slice(size_t start, size_t end) { inline ArrayPtr<T> slice(size_t start, size_t end) {
KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice()."); KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice().");
return ArrayPtr<T>(ptr + start, end - start); return ArrayPtr<T>(ptr + start, end - start);
...@@ -249,6 +254,8 @@ private: ...@@ -249,6 +254,8 @@ private:
template <typename U> template <typename U>
friend class Array; friend class Array;
template <typename U>
friend class ArrayBuilder;
}; };
static_assert(!canMemcpy<Array<char>>(), "canMemcpy<>() is broken"); static_assert(!canMemcpy<Array<char>>(), "canMemcpy<>() is broken");
...@@ -321,6 +328,13 @@ public: ...@@ -321,6 +328,13 @@ public:
other.pos = nullptr; other.pos = nullptr;
other.endPtr = nullptr; other.endPtr = nullptr;
} }
ArrayBuilder(Array<T>&& other)
: ptr(other.ptr), pos(other.ptr + other.size_), endPtr(pos), disposer(other.disposer) {
// Create an already-full ArrayBulider from an Arary of the same type. This constructor
// primarily exists to enable Vector<T> to be constructed from Array<T>.
other.ptr = nullptr;
other.size_ = 0;
}
KJ_DISALLOW_COPY(ArrayBuilder); KJ_DISALLOW_COPY(ArrayBuilder);
inline ~ArrayBuilder() noexcept(false) { dispose(); } inline ~ArrayBuilder() noexcept(false) { dispose(); }
......
...@@ -43,6 +43,7 @@ class Vector { ...@@ -43,6 +43,7 @@ class Vector {
public: public:
inline Vector() = default; inline Vector() = default;
inline explicit Vector(size_t capacity): builder(heapArrayBuilder<T>(capacity)) {} inline explicit Vector(size_t capacity): builder(heapArrayBuilder<T>(capacity)) {}
inline Vector(Array<T>&& array): builder(kj::mv(array)) {}
inline operator ArrayPtr<T>() { return builder; } inline operator ArrayPtr<T>() { return builder; }
inline operator ArrayPtr<const T>() const { return builder; } inline operator ArrayPtr<const T>() const { return builder; }
...@@ -71,6 +72,18 @@ public: ...@@ -71,6 +72,18 @@ public:
return builder.finish(); return builder.finish();
} }
template <typename U>
inline bool operator==(const U& other) const { return asPtr() == other; }
template <typename U>
inline bool operator!=(const U& other) const { return asPtr() != other; }
inline ArrayPtr<T> slice(size_t start, size_t end) {
return asPtr().slice(start, end);
}
inline ArrayPtr<const T> slice(size_t start, size_t end) const {
return asPtr().slice(start, end);
}
template <typename... Params> template <typename... Params>
inline T& add(Params&&... params) { inline T& add(Params&&... params) {
if (builder.isFull()) grow(); if (builder.isFull()) grow();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment