10 #include "llvm/ADT/STLExtras.h"
11 #include "llvm/ADT/SmallVector.h"
21 size_t size()
const {
return slices.size(); }
25 bool empty()
const {
return slices.empty(); }
30 if (slices[pos].first ==
static_cast<size_t>(-1))
32 return ArrayRef<T>(storage).slice(slices[pos].first, slices[pos].second);
36 if (slices[pos].first ==
static_cast<size_t>(-1))
44 :
public llvm::iterator_facade_base<
45 iterator, std::forward_iterator_tag, MutableArrayRef<T>,
46 std::ptrdiff_t, MutableArrayRef<T> *, MutableArrayRef<T>> {
59 if (pos < ragged.slices.size())
67 return &ragged == &other.ragged && pos == other.pos;
77 :
public llvm::iterator_facade_base<
78 const_iterator, std::forward_iterator_tag, ArrayRef<T>,
79 std::ptrdiff_t, ArrayRef<T> *, ArrayRef<T>> {
83 : ragged(ragged), pos(0) {}
87 : ragged(ragged), pos(pos) {}
94 if (pos < ragged.slices.size())
102 return &ragged == &other.ragged && pos == other.pos;
118 slices.reserve(
size);
119 storage.reserve(
size * nestedSize);
124 template <
typename Range>
126 slices.push_back(appendToStorage(std::forward<Range>(elements)));
132 template <
typename Range>
134 if (slices[pos].first !=
static_cast<size_t>(-1)) {
135 auto from = std::next(storage.begin(), slices[pos].first);
136 auto to = std::next(from, slices[pos].second);
137 auto newFrom = storage.erase(from, to);
139 for (
size_t i = pos + 1, e =
size(); i < e; ++i) {
140 slices[i] = std::make_pair(std::distance(storage.begin(), newFrom),
142 std::advance(newFrom, slices[i].second);
145 slices[pos] = appendToStorage(std::forward<Range>(elements));
150 slices.resize(slices.size() + num, std::pair<size_t, size_t>(-1, 0));
159 template <
typename Range>
160 std::pair<size_t, size_t> appendToStorage(
Range &&elements) {
161 size_t start = storage.size();
162 llvm::append_range(storage, std::forward<Range>(elements));
163 return std::make_pair(start, storage.size() - start);
171 SmallVector<std::pair<size_t, size_t>> slices;
174 SmallVector<T> storage;
Constant iterator over the rows.
bool operator==(const const_iterator &other) const
Compares the two iterators.
const_iterator & operator++()
Increments the iterator.
ArrayRef< T > operator*() const
Dereferences the current iterator. Assumes in-bounds.
const_iterator(const RaggedArray &ragged, size_t pos)
Creates the end iterator.
const_iterator(const RaggedArray &ragged)
Creates the start iterator.
bool operator==(const iterator &other) const
Compares the two iterators.
MutableArrayRef< T > operator*() const
Dereferences the current iterator. Assumes in-bounds.
iterator & operator++()
Increments the iterator.
iterator(RaggedArray &ragged, size_t pos)
Creates the end iterator.
iterator(RaggedArray &ragged)
Creates the start iterator.
A 2D array where each row may have different length.
ArrayRef< T > at(size_t pos) const
void replace(size_t pos, Range &&elements)
Replaces the pos-th row in the 2D array with the given range of elements.
MutableArrayRef< T > operator[](size_t pos)
ArrayRef< T > operator[](size_t pos) const
Accesses pos-th row.
void appendEmptyRows(size_t num)
Appends num empty rows to the array.
void reserve(size_t size, size_t nestedSize=0)
Reserve space to store size rows with nestedSize elements each.
void removeFront()
Removes the first subarray in-place. Invalidates iterators to all rows.
const_iterator begin() const
Iterator over rows.
size_t size() const
Returns the number of rows in the 2D array.
MutableArrayRef< T > at(size_t pos)
const_iterator end() const
bool empty() const
Returns true if the are no rows in the 2D array.
void push_back(Range &&elements)
Appends the given range of elements as a new row to the 2D array.
Include the generated interface declarations.
Represents a range (offset, size, and stride) where each element of the triple may be dynamic or stat...