jpayne@69: // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors jpayne@69: // Licensed under the MIT License: jpayne@69: // jpayne@69: // Permission is hereby granted, free of charge, to any person obtaining a copy jpayne@69: // of this software and associated documentation files (the "Software"), to deal jpayne@69: // in the Software without restriction, including without limitation the rights jpayne@69: // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell jpayne@69: // copies of the Software, and to permit persons to whom the Software is jpayne@69: // furnished to do so, subject to the following conditions: jpayne@69: // jpayne@69: // The above copyright notice and this permission notice shall be included in jpayne@69: // all copies or substantial portions of the Software. jpayne@69: // jpayne@69: // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR jpayne@69: // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, jpayne@69: // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE jpayne@69: // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER jpayne@69: // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, jpayne@69: // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN jpayne@69: // THE SOFTWARE. jpayne@69: jpayne@69: #pragma once jpayne@69: jpayne@69: #include "array.h" jpayne@69: jpayne@69: KJ_BEGIN_HEADER jpayne@69: jpayne@69: namespace kj { jpayne@69: jpayne@69: template jpayne@69: class Vector { jpayne@69: // Similar to std::vector, but based on KJ framework. jpayne@69: // jpayne@69: // This implementation always uses move constructors when growing the backing array. If the jpayne@69: // move constructor throws, the Vector is left in an inconsistent state. This is acceptable jpayne@69: // under KJ exception theory which assumes that exceptions leave things in inconsistent states. jpayne@69: jpayne@69: // TODO(someday): Allow specifying a custom allocator. jpayne@69: jpayne@69: public: jpayne@69: inline Vector() = default; jpayne@69: inline explicit Vector(size_t capacity): builder(heapArrayBuilder(capacity)) {} jpayne@69: inline Vector(Array&& array): builder(kj::mv(array)) {} jpayne@69: jpayne@69: inline operator ArrayPtr() KJ_LIFETIMEBOUND { return builder; } jpayne@69: inline operator ArrayPtr() const KJ_LIFETIMEBOUND { return builder; } jpayne@69: inline ArrayPtr asPtr() KJ_LIFETIMEBOUND { return builder.asPtr(); } jpayne@69: inline ArrayPtr asPtr() const KJ_LIFETIMEBOUND { return builder.asPtr(); } jpayne@69: jpayne@69: inline size_t size() const { return builder.size(); } jpayne@69: inline bool empty() const { return size() == 0; } jpayne@69: inline size_t capacity() const { return builder.capacity(); } jpayne@69: inline T& operator[](size_t index) KJ_LIFETIMEBOUND { return builder[index]; } jpayne@69: inline const T& operator[](size_t index) const KJ_LIFETIMEBOUND { return builder[index]; } jpayne@69: jpayne@69: inline const T* begin() const KJ_LIFETIMEBOUND { return builder.begin(); } jpayne@69: inline const T* end() const KJ_LIFETIMEBOUND { return builder.end(); } jpayne@69: inline const T& front() const KJ_LIFETIMEBOUND { return builder.front(); } jpayne@69: inline const T& back() const KJ_LIFETIMEBOUND { return builder.back(); } jpayne@69: inline T* begin() KJ_LIFETIMEBOUND { return builder.begin(); } jpayne@69: inline T* end() KJ_LIFETIMEBOUND { return builder.end(); } jpayne@69: inline T& front() KJ_LIFETIMEBOUND { return builder.front(); } jpayne@69: inline T& back() KJ_LIFETIMEBOUND { return builder.back(); } jpayne@69: jpayne@69: inline Array releaseAsArray() { jpayne@69: // TODO(perf): Avoid a copy/move by allowing Array to point to incomplete space? jpayne@69: if (!builder.isFull()) { jpayne@69: setCapacity(size()); jpayne@69: } jpayne@69: return builder.finish(); jpayne@69: } jpayne@69: jpayne@69: template jpayne@69: inline bool operator==(const U& other) const { return asPtr() == other; } jpayne@69: template jpayne@69: inline bool operator!=(const U& other) const { return asPtr() != other; } jpayne@69: jpayne@69: inline ArrayPtr slice(size_t start, size_t end) KJ_LIFETIMEBOUND { jpayne@69: return asPtr().slice(start, end); jpayne@69: } jpayne@69: inline ArrayPtr slice(size_t start, size_t end) const KJ_LIFETIMEBOUND { jpayne@69: return asPtr().slice(start, end); jpayne@69: } jpayne@69: jpayne@69: template jpayne@69: inline T& add(Params&&... params) KJ_LIFETIMEBOUND { jpayne@69: if (builder.isFull()) grow(); jpayne@69: return builder.add(kj::fwd(params)...); jpayne@69: } jpayne@69: jpayne@69: template jpayne@69: inline void addAll(Iterator begin, Iterator end) { jpayne@69: size_t needed = builder.size() + (end - begin); jpayne@69: if (needed > builder.capacity()) grow(needed); jpayne@69: builder.addAll(begin, end); jpayne@69: } jpayne@69: jpayne@69: template jpayne@69: inline void addAll(Container&& container) { jpayne@69: addAll(container.begin(), container.end()); jpayne@69: } jpayne@69: jpayne@69: inline void removeLast() { jpayne@69: builder.removeLast(); jpayne@69: } jpayne@69: jpayne@69: inline void resize(size_t size) { jpayne@69: if (size > builder.capacity()) grow(size); jpayne@69: builder.resize(size); jpayne@69: } jpayne@69: jpayne@69: inline void operator=(decltype(nullptr)) { jpayne@69: builder = nullptr; jpayne@69: } jpayne@69: jpayne@69: inline void clear() { jpayne@69: builder.clear(); jpayne@69: } jpayne@69: jpayne@69: inline void truncate(size_t size) { jpayne@69: builder.truncate(size); jpayne@69: } jpayne@69: jpayne@69: inline void reserve(size_t size) { jpayne@69: if (size > builder.capacity()) { jpayne@69: grow(size); jpayne@69: } jpayne@69: } jpayne@69: jpayne@69: private: jpayne@69: ArrayBuilder builder; jpayne@69: jpayne@69: void grow(size_t minCapacity = 0) { jpayne@69: setCapacity(kj::max(minCapacity, capacity() == 0 ? 4 : capacity() * 2)); jpayne@69: } jpayne@69: void setCapacity(size_t newSize) { jpayne@69: if (builder.size() > newSize) { jpayne@69: builder.truncate(newSize); jpayne@69: } jpayne@69: ArrayBuilder newBuilder = heapArrayBuilder(newSize); jpayne@69: newBuilder.addAll(kj::mv(builder)); jpayne@69: builder = kj::mv(newBuilder); jpayne@69: } jpayne@69: }; jpayne@69: jpayne@69: template jpayne@69: inline auto KJ_STRINGIFY(const Vector& v) -> decltype(toCharSequence(v.asPtr())) { jpayne@69: return toCharSequence(v.asPtr()); jpayne@69: } jpayne@69: jpayne@69: } // namespace kj jpayne@69: jpayne@69: KJ_END_HEADER