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