annotate win32-mingw/include/kj/vector.h @ 163:5cc1366da2e9

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