annotate win64-msvc/include/kj/vector.h @ 142:75bf92aa2d1f

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