//===- PreallocatedArray.h - Fixed size array -------------------*- C++ -*-===// // // This source file is part of the Swift.org open source project // // Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors // Licensed under Apache License v2.0 with Runtime Library Exception // // See http://swift.org/LICENSE.txt for license information // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors // //===----------------------------------------------------------------------===// #ifndef SWIFT_BASIC_PREALLOCATEDARRAY_H_ #define SWIFT_BASIC_PREALLOCATEDARRAY_H_ #include "llvm/Support/Allocator.h" #include namespace swift { /// An array meant for large collections of default constructible elements of /// fixed size. It does one large allocation on construction. template class PreallocatedArray { static_assert(std::is_default_constructible::value, "EltTy must be default constructable."); unsigned NumElts; Optional Allocator; EltTy *Elt; public: PreallocatedArray(unsigned NumElts) : NumElts(NumElts), Allocator(llvm::MallocAllocator()), Elt(new (Allocator->template Allocate(NumElts)) EltTy[NumElts]) {} PreallocatedArray(unsigned NumElts, AllocatorTy &Allocator) : NumElts(NumElts), Elt(new (Allocator.template Allocate(NumElts)) EltTy[NumElts]) {} // Call destructors of data. ~PreallocatedArray() { for (unsigned i = 0; i < NumElts; ++i) { Elt[i].~EltTy(); } } MutableArrayRef getArray() { return MutableArrayRef(Elt, NumElts); } ArrayRef getArray() const { return ArrayRef(Elt, NumElts); } using iterator = typename MutableArrayRef::iterator; using const_iterator = typename ArrayRef::iterator; iterator begin() { return getArray().begin(); } iterator end() { return getArray().end(); } const_iterator begin() const { return getArray().begin(); } const_iterator end() const { return getArray().end(); } Range getRange() { return {begin(), end()}; } Range getRange() const { return {begin(), end()}; } unsigned size() const { return NumElts; } EltTy &operator[](size_t Index) { return getArray()[Index]; } }; } // end namespace swift #endif