comparison osx/include/kj/vector.h @ 49:3ab5a40c4e3b

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