annotate win64-msvc/include/kj/vector.h @ 70:9e21af8f0420

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