annotate win32-mingw/include/kj/vector.h @ 78:7ea7031c0e5c pa_catalina

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