annotate win64-msvc/include/kj/vector.h @ 49:3ab5a40c4e3b

Add Capnp and KJ builds for OSX
author Chris Cannam <cannam@all-day-breakfast.com>
date Tue, 25 Oct 2016 14:48:23 +0100
parents d93140aac40b
children 0f2d93caa50c
rev   line source
Chris@47 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
Chris@47 2 // Licensed under the MIT License:
Chris@47 3 //
Chris@47 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
Chris@47 5 // of this software and associated documentation files (the "Software"), to deal
Chris@47 6 // in the Software without restriction, including without limitation the rights
Chris@47 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Chris@47 8 // copies of the Software, and to permit persons to whom the Software is
Chris@47 9 // furnished to do so, subject to the following conditions:
Chris@47 10 //
Chris@47 11 // The above copyright notice and this permission notice shall be included in
Chris@47 12 // all copies or substantial portions of the Software.
Chris@47 13 //
Chris@47 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Chris@47 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Chris@47 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Chris@47 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Chris@47 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Chris@47 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Chris@47 20 // THE SOFTWARE.
Chris@47 21
Chris@47 22 #ifndef KJ_VECTOR_H_
Chris@47 23 #define KJ_VECTOR_H_
Chris@47 24
Chris@47 25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
Chris@47 26 #pragma GCC system_header
Chris@47 27 #endif
Chris@47 28
Chris@47 29 #include "array.h"
Chris@47 30
Chris@47 31 namespace kj {
Chris@47 32
Chris@47 33 template <typename T>
Chris@47 34 class Vector {
Chris@47 35 // Similar to std::vector, but based on KJ framework.
Chris@47 36 //
Chris@47 37 // This implementation always uses move constructors when growing the backing array. If the
Chris@47 38 // move constructor throws, the Vector is left in an inconsistent state. This is acceptable
Chris@47 39 // under KJ exception theory which assumes that exceptions leave things in inconsistent states.
Chris@47 40
Chris@47 41 // TODO(someday): Allow specifying a custom allocator.
Chris@47 42
Chris@47 43 public:
Chris@47 44 inline Vector() = default;
Chris@47 45 inline explicit Vector(size_t capacity): builder(heapArrayBuilder<T>(capacity)) {}
Chris@47 46
Chris@47 47 inline operator ArrayPtr<T>() { return builder; }
Chris@47 48 inline operator ArrayPtr<const T>() const { return builder; }
Chris@47 49 inline ArrayPtr<T> asPtr() { return builder.asPtr(); }
Chris@47 50 inline ArrayPtr<const T> asPtr() const { return builder.asPtr(); }
Chris@47 51
Chris@47 52 inline size_t size() const { return builder.size(); }
Chris@47 53 inline bool empty() const { return size() == 0; }
Chris@47 54 inline size_t capacity() const { return builder.capacity(); }
Chris@47 55 inline T& operator[](size_t index) const { return builder[index]; }
Chris@47 56
Chris@47 57 inline const T* begin() const { return builder.begin(); }
Chris@47 58 inline const T* end() const { return builder.end(); }
Chris@47 59 inline const T& front() const { return builder.front(); }
Chris@47 60 inline const T& back() const { return builder.back(); }
Chris@47 61 inline T* begin() { return builder.begin(); }
Chris@47 62 inline T* end() { return builder.end(); }
Chris@47 63 inline T& front() { return builder.front(); }
Chris@47 64 inline T& back() { return builder.back(); }
Chris@47 65
Chris@47 66 inline Array<T> releaseAsArray() {
Chris@47 67 // TODO(perf): Avoid a copy/move by allowing Array<T> to point to incomplete space?
Chris@47 68 if (!builder.isFull()) {
Chris@47 69 setCapacity(size());
Chris@47 70 }
Chris@47 71 return builder.finish();
Chris@47 72 }
Chris@47 73
Chris@47 74 template <typename... Params>
Chris@47 75 inline T& add(Params&&... params) {
Chris@47 76 if (builder.isFull()) grow();
Chris@47 77 return builder.add(kj::fwd<Params>(params)...);
Chris@47 78 }
Chris@47 79
Chris@47 80 template <typename Iterator>
Chris@47 81 inline void addAll(Iterator begin, Iterator end) {
Chris@47 82 size_t needed = builder.size() + (end - begin);
Chris@47 83 if (needed > builder.capacity()) grow(needed);
Chris@47 84 builder.addAll(begin, end);
Chris@47 85 }
Chris@47 86
Chris@47 87 template <typename Container>
Chris@47 88 inline void addAll(Container&& container) {
Chris@47 89 addAll(container.begin(), container.end());
Chris@47 90 }
Chris@47 91
Chris@47 92 inline void removeLast() {
Chris@47 93 builder.removeLast();
Chris@47 94 }
Chris@47 95
Chris@47 96 inline void resize(size_t size) {
Chris@47 97 if (size > builder.capacity()) grow(size);
Chris@47 98 while (builder.size() < size) {
Chris@47 99 builder.add(T());
Chris@47 100 }
Chris@47 101 while (builder.size() > size) {
Chris@47 102 builder.removeLast();
Chris@47 103 }
Chris@47 104 }
Chris@47 105
Chris@47 106 inline void operator=(decltype(nullptr)) {
Chris@47 107 builder = nullptr;
Chris@47 108 }
Chris@47 109
Chris@47 110 inline void clear() {
Chris@47 111 while (builder.size() > 0) {
Chris@47 112 builder.removeLast();
Chris@47 113 }
Chris@47 114 }
Chris@47 115
Chris@47 116 inline void truncate(size_t size) {
Chris@47 117 while (builder.size() > size) {
Chris@47 118 builder.removeLast();
Chris@47 119 }
Chris@47 120 }
Chris@47 121
Chris@47 122 private:
Chris@47 123 ArrayBuilder<T> builder;
Chris@47 124
Chris@47 125 void grow(size_t minCapacity = 0) {
Chris@47 126 setCapacity(kj::max(minCapacity, capacity() == 0 ? 4 : capacity() * 2));
Chris@47 127 }
Chris@47 128 void setCapacity(size_t newSize) {
Chris@47 129 ArrayBuilder<T> newBuilder = heapArrayBuilder<T>(newSize);
Chris@47 130 size_t moveCount = kj::min(newSize, builder.size());
Chris@47 131 for (size_t i = 0; i < moveCount; i++) {
Chris@47 132 newBuilder.add(kj::mv(builder[i]));
Chris@47 133 }
Chris@47 134 builder = kj::mv(newBuilder);
Chris@47 135 }
Chris@47 136 };
Chris@47 137
Chris@47 138 template <typename T>
Chris@47 139 inline auto KJ_STRINGIFY(const Vector<T>& v) -> decltype(toCharSequence(v.asPtr())) {
Chris@47 140 return toCharSequence(v.asPtr());
Chris@47 141 }
Chris@47 142
Chris@47 143 } // namespace kj
Chris@47 144
Chris@47 145 #endif // KJ_VECTOR_H_