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