annotate osx/include/kj/vector.h @ 146:206f0eb279b8

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