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