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