array.h 22.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
// Copyright (c) 2013, Kenton Varda <temporal@gmail.com>
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef KJ_ARRAY_H_
#define KJ_ARRAY_H_

#include "common.h"
#include <string.h>
29
#include <initializer_list>
30 31 32

namespace kj {

Kenton Varda's avatar
Kenton Varda committed
33 34 35 36 37 38 39
// =======================================================================================
// ArrayDisposer -- Implementation details.

class ArrayDisposer {
  // Much like Disposer from memory.h.

protected:
40 41
  // Do not declare a destructor, as doing so will force a global initializer for
  // HeapArrayDisposer::instance.
Kenton Varda's avatar
Kenton Varda committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

  virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
                           size_t capacity, void (*destroyElement)(void*)) const = 0;
  // Disposes of the array.  `destroyElement` invokes the destructor of each element, or is nullptr
  // if the elements have trivial destructors.  `capacity` is the amount of space that was
  // allocated while `elementCount` is the number of elements that were actually constructed;
  // these are always the same number for Array<T> but may be different when using ArrayBuilder<T>.

public:

  template <typename T>
  void dispose(T* firstElement, size_t elementCount, size_t capacity) const;
  // Helper wrapper around disposeImpl().
  //
  // Callers must not call dispose() on the same array twice, even if the first call throws
  // an exception.

private:
  template <typename T, bool hasTrivialDestructor = __has_trivial_destructor(T)>
  struct Dispose_;
};

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
class ExceptionSafeArrayUtil {
  // Utility class that assists in constructing or destroying elements of an array, where the
  // constructor or destructor could throw exceptions.  In case of an exception,
  // ExceptionSafeArrayUtil's destructor will call destructors on all elements that have been
  // constructed but not destroyed.  Remember that destructors that throw exceptions are required
  // to use UnwindDetector to detect unwind and avoid exceptions in this case.  Therefore, no more
  // than one exception will be thrown (and the program will not terminate).

public:
  inline ExceptionSafeArrayUtil(void* ptr, size_t elementSize, size_t constructedElementCount,
                                void (*destroyElement)(void*))
      : pos(reinterpret_cast<byte*>(ptr) + elementSize * constructedElementCount),
        elementSize(elementSize), constructedElementCount(constructedElementCount),
        destroyElement(destroyElement) {}
  KJ_DISALLOW_COPY(ExceptionSafeArrayUtil);

  inline ~ExceptionSafeArrayUtil() noexcept(false) {
    if (constructedElementCount > 0) destroyAll();
  }

  void construct(size_t count, void (*constructElement)(void*));
  // Construct the given number of elements.

  void destroyAll();
  // Destroy all elements.  Call this immediately before ExceptionSafeArrayUtil goes out-of-scope
  // to ensure that one element throwing an exception does not prevent the others from being
  // destroyed.

  void release() { constructedElementCount = 0; }
  // Prevent ExceptionSafeArrayUtil's destructor from destroying the constructed elements.
  // Call this after you've successfully finished constructing.

private:
  byte* pos;
  size_t elementSize;
  size_t constructedElementCount;
  void (*destroyElement)(void*);
};

103 104 105 106 107 108 109 110
class DestructorOnlyArrayDisposer: public ArrayDisposer {
public:
  static const DestructorOnlyArrayDisposer instance;

  void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
                   size_t capacity, void (*destroyElement)(void*)) const override;
};

111 112 113 114 115 116 117 118 119 120 121
class NullArrayDisposer: public ArrayDisposer {
  // An ArrayDisposer that does nothing.  Can be used to construct a fake Arrays that doesn't
  // actually own its content.

public:
  static const NullArrayDisposer instance;

  void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
                   size_t capacity, void (*destroyElement)(void*)) const override;
};

122 123 124 125 126
// =======================================================================================
// Array

template <typename T>
class Array {
Kenton Varda's avatar
Kenton Varda committed
127 128 129
  // An owned array which will automatically be disposed of (using an ArrayDisposer) in the
  // destructor.  Can be moved, but not copied.  Much like Own<T>, but for arrays rather than
  // single objects.
130 131

public:
Kenton Varda's avatar
Kenton Varda committed
132 133
  inline Array(): ptr(nullptr), size_(0), disposer(nullptr) {}
  inline Array(decltype(nullptr)): ptr(nullptr), size_(0), disposer(nullptr) {}
Kenton Varda's avatar
Kenton Varda committed
134 135
  inline Array(Array&& other) noexcept
      : ptr(other.ptr), size_(other.size_), disposer(other.disposer) {
136 137 138
    other.ptr = nullptr;
    other.size_ = 0;
  }
139
  inline Array(Array<RemoveConstOrDisable<T>>&& other) noexcept
140 141 142 143
      : ptr(other.ptr), size_(other.size_), disposer(other.disposer) {
    other.ptr = nullptr;
    other.size_ = 0;
  }
Kenton Varda's avatar
Kenton Varda committed
144 145
  inline Array(T* firstElement, size_t size, const ArrayDisposer& disposer)
      : ptr(firstElement), size_(size), disposer(&disposer) {}
146 147

  KJ_DISALLOW_COPY(Array);
Kenton Varda's avatar
Kenton Varda committed
148
  inline ~Array() noexcept { dispose(); }
149 150 151 152 153 154 155 156 157 158

  inline operator ArrayPtr<T>() {
    return ArrayPtr<T>(ptr, size_);
  }
  inline operator ArrayPtr<const T>() const {
    return ArrayPtr<T>(ptr, size_);
  }
  inline ArrayPtr<T> asPtr() {
    return ArrayPtr<T>(ptr, size_);
  }
Kenton Varda's avatar
Kenton Varda committed
159 160 161
  inline ArrayPtr<const T> asPtr() const {
    return ArrayPtr<T>(ptr, size_);
  }
162 163 164

  inline size_t size() const { return size_; }
  inline T& operator[](size_t index) const {
Kenton Varda's avatar
Kenton Varda committed
165
    KJ_IREQUIRE(index < size_, "Out-of-bounds Array access.");
166 167 168
    return ptr[index];
  }

Kenton Varda's avatar
Kenton Varda committed
169 170 171 172 173 174 175 176
  inline const T* begin() const { return ptr; }
  inline const T* end() const { return ptr + size_; }
  inline const T& front() const { return *ptr; }
  inline const T& back() const { return *(ptr + size_ - 1); }
  inline T* begin() { return ptr; }
  inline T* end() { return ptr + size_; }
  inline T& front() { return *ptr; }
  inline T& back() { return *(ptr + size_ - 1); }
177 178

  inline ArrayPtr<T> slice(size_t start, size_t end) {
Kenton Varda's avatar
Kenton Varda committed
179
    KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice().");
180 181 182
    return ArrayPtr<T>(ptr + start, end - start);
  }
  inline ArrayPtr<const T> slice(size_t start, size_t end) const {
Kenton Varda's avatar
Kenton Varda committed
183
    KJ_IREQUIRE(start <= end && end <= size_, "Out-of-bounds Array::slice().");
184 185 186 187 188 189 190
    return ArrayPtr<const T>(ptr + start, end - start);
  }

  inline bool operator==(decltype(nullptr)) const { return size_ == 0; }
  inline bool operator!=(decltype(nullptr)) const { return size_ != 0; }

  inline Array& operator=(decltype(nullptr)) {
Kenton Varda's avatar
Kenton Varda committed
191
    dispose();
192 193 194 195
    return *this;
  }

  inline Array& operator=(Array&& other) {
Kenton Varda's avatar
Kenton Varda committed
196
    dispose();
197 198
    ptr = other.ptr;
    size_ = other.size_;
Kenton Varda's avatar
Kenton Varda committed
199
    disposer = other.disposer;
200 201 202 203 204 205 206 207
    other.ptr = nullptr;
    other.size_ = 0;
    return *this;
  }

private:
  T* ptr;
  size_t size_;
Kenton Varda's avatar
Kenton Varda committed
208 209 210 211 212 213 214 215 216 217 218 219 220
  const ArrayDisposer* disposer;

  inline void dispose() {
    // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly
    // dispose again.
    T* ptrCopy = ptr;
    size_t sizeCopy = size_;
    if (ptrCopy != nullptr) {
      ptr = nullptr;
      size_ = 0;
      disposer->dispose(ptrCopy, sizeCopy, sizeCopy);
    }
  }
221 222 223

  template <typename U>
  friend class Array;
Kenton Varda's avatar
Kenton Varda committed
224 225
};

226
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
227 228 229 230 231 232 233

class HeapArrayDisposer final: public ArrayDisposer {
public:
  template <typename T>
  static T* allocate(size_t count);
  template <typename T>
  static T* allocateUninitialized(size_t count);
234

Kenton Varda's avatar
Kenton Varda committed
235 236 237
  static const HeapArrayDisposer instance;

private:
Kenton Varda's avatar
Kenton Varda committed
238 239 240 241 242 243 244 245
  static void* allocateImpl(size_t elementSize, size_t elementCount, size_t capacity,
                            void (*constructElement)(void*), void (*destroyElement)(void*));
  // Allocates and constructs the array.  Both function pointers are null if the constructor is
  // trivial, otherwise destroyElement is null if the constructor doesn't throw.

  virtual void disposeImpl(void* firstElement, size_t elementSize, size_t elementCount,
                           size_t capacity, void (*destroyElement)(void*)) const override;

Kenton Varda's avatar
Kenton Varda committed
246 247 248
  template <typename T, bool hasTrivialConstructor = __has_trivial_constructor(T),
                        bool hasNothrowConstructor = __has_nothrow_constructor(T)>
  struct Allocate_;
249 250
};

251
}  // namespace _ (private)
Kenton Varda's avatar
Kenton Varda committed
252

253
template <typename T>
Kenton Varda's avatar
Kenton Varda committed
254 255 256
inline Array<T> heapArray(size_t size) {
  // Much like `heap<T>()` from memory.h, allocates a new array on the heap.

257 258
  return Array<T>(_::HeapArrayDisposer::allocate<T>(size), size,
                  _::HeapArrayDisposer::instance);
259 260
}

Kenton Varda's avatar
Kenton Varda committed
261
template <typename T> Array<T> heapArray(const T* content, size_t size);
262
template <typename T> Array<T> heapArray(ArrayPtr<T> content);
Kenton Varda's avatar
Kenton Varda committed
263 264
template <typename T> Array<T> heapArray(ArrayPtr<const T> content);
template <typename T, typename Iterator> Array<T> heapArray(Iterator begin, Iterator end);
265 266
template <typename T> Array<T> heapArray(std::initializer_list<T> init);
// Allocate a heap array containing a copy of the given content.
Kenton Varda's avatar
Kenton Varda committed
267

268 269 270 271 272
template <typename T, typename Container>
Array<T> heapArrayFromIterable(Container&& a) { return heapArray(a.begin(), a.end()); }
template <typename T>
Array<T> heapArrayFromIterable(Array<T>&& a) { return mv(a); }

273 274 275 276 277
// =======================================================================================
// ArrayBuilder

template <typename T>
class ArrayBuilder {
Kenton Varda's avatar
Kenton Varda committed
278 279
  // Class which lets you build an Array<T> specifying the exact constructor arguments for each
  // element, rather than starting by default-constructing them.
280 281

public:
Kenton Varda's avatar
Kenton Varda committed
282 283
  ArrayBuilder(): ptr(nullptr), pos(nullptr), endPtr(nullptr) {}
  ArrayBuilder(decltype(nullptr)): ptr(nullptr), pos(nullptr), endPtr(nullptr) {}
284 285
  explicit ArrayBuilder(RemoveConst<T>* firstElement, size_t capacity,
                        const ArrayDisposer& disposer)
Kenton Varda's avatar
Kenton Varda committed
286 287 288 289 290 291 292 293 294
      : ptr(firstElement), pos(firstElement), endPtr(firstElement + capacity),
        disposer(&disposer) {}
  ArrayBuilder(ArrayBuilder&& other)
      : ptr(other.ptr), pos(other.pos), endPtr(other.endPtr), disposer(other.disposer) {
    other.ptr = nullptr;
    other.pos = nullptr;
    other.endPtr = nullptr;
  }
  KJ_DISALLOW_COPY(ArrayBuilder);
295
  inline ~ArrayBuilder() noexcept(false) { dispose(); }
Kenton Varda's avatar
Kenton Varda committed
296 297 298 299 300 301 302 303 304 305

  inline operator ArrayPtr<T>() {
    return arrayPtr(ptr, pos);
  }
  inline operator ArrayPtr<const T>() const {
    return arrayPtr(ptr, pos);
  }
  inline ArrayPtr<T> asPtr() {
    return arrayPtr(ptr, pos);
  }
306 307 308
  inline ArrayPtr<const T> asPtr() const {
    return arrayPtr(ptr, pos);
  }
Kenton Varda's avatar
Kenton Varda committed
309 310 311 312

  inline size_t size() const { return pos - ptr; }
  inline size_t capacity() const { return endPtr - ptr; }
  inline T& operator[](size_t index) const {
Kenton Varda's avatar
Kenton Varda committed
313
    KJ_IREQUIRE(index < implicitCast<size_t>(pos - ptr), "Out-of-bounds Array access.");
Kenton Varda's avatar
Kenton Varda committed
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
    return ptr[index];
  }

  inline const T* begin() const { return ptr; }
  inline const T* end() const { return pos; }
  inline const T& front() const { return *ptr; }
  inline const T& back() const { return *(pos - 1); }
  inline T* begin() { return ptr; }
  inline T* end() { return pos; }
  inline T& front() { return *ptr; }
  inline T& back() { return *(pos - 1); }

  ArrayBuilder& operator=(ArrayBuilder&& other) {
    dispose();
    ptr = other.ptr;
    pos = other.pos;
    endPtr = other.endPtr;
    disposer = other.disposer;
    other.ptr = nullptr;
    other.pos = nullptr;
    other.endPtr = nullptr;
    return *this;
  }
  ArrayBuilder& operator=(decltype(nullptr)) {
    dispose();
    return *this;
340 341 342
  }

  template <typename... Params>
Kenton Varda's avatar
Kenton Varda committed
343
  T& add(Params&&... params) {
Kenton Varda's avatar
Kenton Varda committed
344
    KJ_IREQUIRE(pos < endPtr, "Added too many elements to ArrayBuilder.");
Kenton Varda's avatar
Kenton Varda committed
345
    ctor(*pos, kj::fwd<Params>(params)...);
Kenton Varda's avatar
Kenton Varda committed
346
    return *pos++;
347 348 349 350
  }

  template <typename Container>
  void addAll(Container&& container) {
Kenton Varda's avatar
Kenton Varda committed
351
    addAll(container.begin(), container.end());
352 353
  }

Kenton Varda's avatar
Kenton Varda committed
354 355 356
  template <typename Iterator>
  void addAll(Iterator start, Iterator end);

357 358 359 360 361
  void removeLast() {
    KJ_IREQUIRE(pos > ptr, "No elements present to remove.");
    kj::dtor(*--pos);
  }

362
  Array<T> finish() {
363 364 365 366 367 368
    // We could safely remove this check if we assume that the disposer implementation doesn't
    // need to know the original capacity, as is thes case with HeapArrayDisposer since it uses
    // operator new() or if we created a custom disposer for ArrayBuilder which stores the capacity
    // in a prefix.  But that would make it hard to write cleverer heap allocators, and anyway this
    // check might catch bugs.  Probably people should use Vector if they want to build arrays
    // without knowing the final size in advance.
Kenton Varda's avatar
Kenton Varda committed
369
    KJ_IREQUIRE(pos == endPtr, "ArrayBuilder::finish() called prematurely.");
370
    Array<T> result(reinterpret_cast<T*>(ptr), pos - ptr, *disposer);
371 372 373 374 375 376
    ptr = nullptr;
    pos = nullptr;
    endPtr = nullptr;
    return result;
  }

377 378 379 380
  inline bool isFull() const {
    return pos == endPtr;
  }

381
private:
Kenton Varda's avatar
Kenton Varda committed
382
  T* ptr;
383
  RemoveConst<T>* pos;
Kenton Varda's avatar
Kenton Varda committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  T* endPtr;
  const ArrayDisposer* disposer;

  inline void dispose() {
    // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly
    // dispose again.
    T* ptrCopy = ptr;
    T* posCopy = pos;
    T* endCopy = endPtr;
    if (ptrCopy != nullptr) {
      ptr = nullptr;
      pos = nullptr;
      endPtr = nullptr;
      disposer->dispose(ptrCopy, posCopy - ptrCopy, endCopy - ptrCopy);
    }
  }
400 401
};

Kenton Varda's avatar
Kenton Varda committed
402 403 404 405 406
template <typename T>
inline ArrayBuilder<T> heapArrayBuilder(size_t size) {
  // Like `heapArray<T>()` but does not default-construct the elements.  You must construct them
  // manually by calling `add()`.

407 408
  return ArrayBuilder<T>(_::HeapArrayDisposer::allocateUninitialized<RemoveConst<T>>(size),
                         size, _::HeapArrayDisposer::instance);
Kenton Varda's avatar
Kenton Varda committed
409 410 411
}

// =======================================================================================
412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
// Inline Arrays

template <typename T, size_t fixedSize>
class FixedArray {
  // A fixed-width array whose storage is allocated inline rather than on the heap.

public:
  inline size_t size() const { return fixedSize; }
  inline T* begin() { return content; }
  inline T* end() { return content + fixedSize; }
  inline const T* begin() const { return content; }
  inline const T* end() const { return content + fixedSize; }

  inline operator ArrayPtr<T>() {
    return arrayPtr(content, fixedSize);
  }
  inline operator ArrayPtr<const T>() const {
    return arrayPtr(content, fixedSize);
  }

  inline T& operator[](size_t index) { return content[index]; }
  inline const T& operator[](size_t index) const { return content[index]; }

private:
  T content[fixedSize];
};

template <typename T, size_t fixedSize>
class CappedArray {
  // Like `FixedArray` but can be dynamically resized as long as the size does not exceed the limit
  // specified by the template parameter.
  //
  // TODO(someday):  Don't construct elements past currentSize?

public:
  inline constexpr CappedArray(): currentSize(fixedSize) {}
  inline explicit constexpr CappedArray(size_t s): currentSize(s) {}

  inline size_t size() const { return currentSize; }
451
  inline void setSize(size_t s) { KJ_IREQUIRE(s <= fixedSize); currentSize = s; }
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
  inline T* begin() { return content; }
  inline T* end() { return content + currentSize; }
  inline const T* begin() const { return content; }
  inline const T* end() const { return content + currentSize; }

  inline operator ArrayPtr<T>() {
    return arrayPtr(content, currentSize);
  }
  inline operator ArrayPtr<const T>() const {
    return arrayPtr(content, currentSize);
  }

  inline T& operator[](size_t index) { return content[index]; }
  inline const T& operator[](size_t index) const { return content[index]; }

private:
  size_t currentSize;
  T content[fixedSize];
};

472
// =======================================================================================
473
// KJ_MAP
474

475
#define KJ_MAP(elementName, array) \
Kenton Varda's avatar
Kenton Varda committed
476
  ::kj::_::Mapper<KJ_DECLTYPE_REF(array)>(array) * [&](decltype(*(array).begin()) elementName)
477 478 479 480
// Applies some function to every element of an array, returning an Array of the results,  with
// nice syntax.  Example:
//
//     StringPtr foo = "abcd";
481
//     Array<char> bar = KJ_MAP(c, foo) -> char { return c + 1; };
482 483 484 485 486 487 488
//     KJ_ASSERT(str(bar) == "bcde");

namespace _ {  // private

template <typename T>
struct Mapper {
  T array;
Kenton Varda's avatar
Kenton Varda committed
489
  Mapper(T&& array): array(kj::fwd<T>(array)) {}
490 491 492 493 494 495 496 497 498 499 500 501
  template <typename Func>
  auto operator*(Func&& func) -> Array<decltype(func(*array.begin()))> {
    auto builder = heapArrayBuilder<decltype(func(*array.begin()))>(array.size());
    for (auto iter = array.begin(); iter != array.end(); ++iter) {
      builder.add(func(*iter));
    }
    return builder.finish();
  }
};

}  // namespace _ (private)

502
// =======================================================================================
Kenton Varda's avatar
Kenton Varda committed
503 504 505 506 507 508
// Inline implementation details

template <typename T>
struct ArrayDisposer::Dispose_<T, true> {
  static void dispose(T* firstElement, size_t elementCount, size_t capacity,
                      const ArrayDisposer& disposer) {
509 510
    disposer.disposeImpl(const_cast<RemoveConst<T>*>(firstElement),
                         sizeof(T), elementCount, capacity, nullptr);
Kenton Varda's avatar
Kenton Varda committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
  }
};
template <typename T>
struct ArrayDisposer::Dispose_<T, false> {
  static void destruct(void* ptr) {
    kj::dtor(*reinterpret_cast<T*>(ptr));
  }

  static void dispose(T* firstElement, size_t elementCount, size_t capacity,
                      const ArrayDisposer& disposer) {
    disposer.disposeImpl(firstElement, sizeof(T), elementCount, capacity, &destruct);
  }
};

template <typename T>
void ArrayDisposer::dispose(T* firstElement, size_t elementCount, size_t capacity) const {
  Dispose_<T>::dispose(firstElement, elementCount, capacity, *this);
}

530
namespace _ {  // private
Kenton Varda's avatar
Kenton Varda committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572

template <typename T>
struct HeapArrayDisposer::Allocate_<T, true, true> {
  static T* allocate(size_t elementCount, size_t capacity) {
    return reinterpret_cast<T*>(allocateImpl(
        sizeof(T), elementCount, capacity, nullptr, nullptr));
  }
};
template <typename T>
struct HeapArrayDisposer::Allocate_<T, false, true> {
  static void construct(void* ptr) {
    kj::ctor(*reinterpret_cast<T*>(ptr));
  }
  static T* allocate(size_t elementCount, size_t capacity) {
    return reinterpret_cast<T*>(allocateImpl(
        sizeof(T), elementCount, capacity, &construct, nullptr));
  }
};
template <typename T>
struct HeapArrayDisposer::Allocate_<T, false, false> {
  static void construct(void* ptr) {
    kj::ctor(*reinterpret_cast<T*>(ptr));
  }
  static void destruct(void* ptr) {
    kj::dtor(*reinterpret_cast<T*>(ptr));
  }
  static T* allocate(size_t elementCount, size_t capacity) {
    return reinterpret_cast<T*>(allocateImpl(
        sizeof(T), elementCount, capacity, &construct, &destruct));
  }
};

template <typename T>
T* HeapArrayDisposer::allocate(size_t count) {
  return Allocate_<T>::allocate(count, count);
}

template <typename T>
T* HeapArrayDisposer::allocateUninitialized(size_t count) {
  return Allocate_<T, true, true>::allocate(0, count);
}

573
template <typename Element, typename Iterator, bool = canMemcpy<Element>()>
Kenton Varda's avatar
Kenton Varda committed
574 575 576 577 578
struct CopyConstructArray_;

template <typename T>
struct CopyConstructArray_<T, T*, true> {
  static inline T* apply(T* __restrict__ pos, T* start, T* end) {
579
    memcpy(pos, start, reinterpret_cast<byte*>(end) - reinterpret_cast<byte*>(start));
Kenton Varda's avatar
Kenton Varda committed
580 581 582 583 584 585 586
    return pos + (end - start);
  }
};

template <typename T>
struct CopyConstructArray_<T, const T*, true> {
  static inline T* apply(T* __restrict__ pos, const T* start, const T* end) {
587
    memcpy(pos, start, reinterpret_cast<const byte*>(end) - reinterpret_cast<const byte*>(start));
Kenton Varda's avatar
Kenton Varda committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
    return pos + (end - start);
  }
};

template <typename T, typename Iterator>
struct CopyConstructArray_<T, Iterator, true> {
  static inline T* apply(T* __restrict__ pos, Iterator start, Iterator end) {
    // Since both the copy constructor and assignment operator are trivial, we know that assignment
    // is equivalent to copy-constructing.  So we can make this case somewhat easier for the
    // compiler to optimize.
    while (start != end) {
      *pos++ = *start++;
    }
    return pos;
  }
};

template <typename T, typename Iterator>
struct CopyConstructArray_<T, Iterator, false> {
  struct ExceptionGuard {
    T* start;
    T* pos;
    inline explicit ExceptionGuard(T* pos): start(pos), pos(pos) {}
611
    ~ExceptionGuard() noexcept(false) {
Kenton Varda's avatar
Kenton Varda committed
612 613 614 615 616 617 618 619 620
      while (pos > start) {
        dtor(*--pos);
      }
    }
  };

  static T* apply(T* __restrict__ pos, Iterator start, Iterator end) {
    if (noexcept(T(instance<const T&>()))) {
      while (start != end) {
621
        ctor(*pos++, implicitCast<const T&>(*start++));
Kenton Varda's avatar
Kenton Varda committed
622 623 624 625 626 627
      }
      return pos;
    } else {
      // Crap.  This is complicated.
      ExceptionGuard guard(pos);
      while (start != end) {
628
        ctor(*guard.pos, implicitCast<const T&>(*start++));
Kenton Varda's avatar
Kenton Varda committed
629 630 631 632 633 634 635 636 637 638
        ++guard.pos;
      }
      guard.start = guard.pos;
      return guard.pos;
    }
  }
};

template <typename T, typename Iterator>
inline T* copyConstructArray(T* dst, Iterator start, Iterator end) {
639
  return CopyConstructArray_<T, Decay<Iterator>>::apply(dst, start, end);
Kenton Varda's avatar
Kenton Varda committed
640 641
}

642
}  // namespace _ (private)
Kenton Varda's avatar
Kenton Varda committed
643 644 645 646

template <typename T>
template <typename Iterator>
void ArrayBuilder<T>::addAll(Iterator start, Iterator end) {
647
  pos = _::copyConstructArray(pos, start, end);
Kenton Varda's avatar
Kenton Varda committed
648 649
}

Kenton Varda's avatar
Kenton Varda committed
650 651 652 653 654 655 656
template <typename T>
Array<T> heapArray(const T* content, size_t size) {
  ArrayBuilder<T> builder = heapArrayBuilder<T>(size);
  builder.addAll(content, content + size);
  return builder.finish();
}

657 658 659 660 661 662 663
template <typename T>
Array<T> heapArray(ArrayPtr<T> content) {
  ArrayBuilder<T> builder = heapArrayBuilder<T>(content.size());
  builder.addAll(content);
  return builder.finish();
}

Kenton Varda's avatar
Kenton Varda committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677
template <typename T>
Array<T> heapArray(ArrayPtr<const T> content) {
  ArrayBuilder<T> builder = heapArrayBuilder<T>(content.size());
  builder.addAll(content);
  return builder.finish();
}

template <typename T, typename Iterator> Array<T>
heapArray(Iterator begin, Iterator end) {
  ArrayBuilder<T> builder = heapArrayBuilder<T>(end - begin);
  builder.addAll(begin, end);
  return builder.finish();
}

678 679 680 681 682
template <typename T>
inline Array<T> heapArray(std::initializer_list<T> init) {
  return heapArray<T>(init.begin(), init.end());
}

683 684 685
}  // namespace kj

#endif  // KJ_ARRAY_H_