annotate osx/include/kj/vector.h @ 79:91c729825bca pa_catalina

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