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