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