annotate win32-mingw/include/kj/vector.h @ 140:59a8758c56b1

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