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