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