annotate win32-mingw/include/kj/vector.h @ 62:0994c39f1e94

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