Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_chunked_list.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e0702e55bc90aa7d1b2390542e19acc2d27b5591 (plain)
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* SPDX-License-Identifier: GPL-2.0-or-later */

#pragma once

/** \file
 * \ingroup bli
 *
 * A `blender::ChunkedList` is a dynamically growing ordered container for values of type T.
 * It is *not* guaranteed that all values will be stored in one contiguous array. Instead, multiple
 * arrays may be used.
 *
 * Comparison to `blender::Vector`:
 * - `ChunkedList` has better performance when appending many elements, because it does not have to
 *   move existing values.
 * - This also means that `ChunkedList` can be used with types that cannot be moved.
 * - A `ChunkedList` can not be indexed efficiently. So while the container is ordered, one can not
 *   efficiently get the value at a specific index.
 * - Iterating over a `ChunkedList` is a little bit slower, because it may have to iterate over
 *   multiple arrays. That is likely negligible in most cases.
 *
 * `ChunkedList` should be used instead of `Vector` when the following two statements are true:
 * - The elements do not have to be in a contiguous array.
 * - The elements do not have to be accessed with an index.
 */

#include "BLI_allocator.hh"
#include "BLI_math_base.hh"
#include "BLI_memory_utils.hh"
#include "BLI_vector.hh"

namespace blender {

namespace detail {
template<typename T> struct alignas(std::max<size_t>(alignof(T), 8)) ChunkListAllocInfo {
  Vector<MutableSpan<T>> spans;
};
}  // namespace detail

template<typename T,
         int64_t InlineBufferCapacity = default_inline_buffer_capacity(sizeof(T)),
         typename Allocator = GuardedAllocator>
class ChunkedList {
 private:
  using AllocInfo = detail::ChunkListAllocInfo<T>;

  T *end_;
  T *capacity_end_;
  AllocInfo *alloc_info_ = nullptr;
  BLI_NO_UNIQUE_ADDRESS TypedBuffer<T, InlineBufferCapacity> inline_buffer_;
  BLI_NO_UNIQUE_ADDRESS Allocator allocator_;

 public:
  ChunkedList(Allocator allocator = {}) noexcept : allocator_(allocator)
  {
    end_ = inline_buffer_;
    capacity_end_ = end_ + InlineBufferCapacity;
  }

  ChunkedList(NoExceptConstructor, Allocator allocator = {}) noexcept : ChunkedList(allocator)
  {
  }

  ChunkedList(const ChunkedList &other)
  {
    this->extend(other);
  }

  template<int64_t OtherInlineBufferCapacity>
  ChunkedList(const ChunkedList<T, OtherInlineBufferCapacity, Allocator> &other)
      : ChunkedList(other.allocator())
  {
    this->extend(other);
  }

  const Allocator &allocator() const
  {
    return allocator_;
  }

  template<typename Fn> void foreach_span(Fn &&fn) const
  {
    const T *inline_begin = inline_buffer_;
    if (alloc_info_ == nullptr) {
      if (end_ > inline_begin) {
        fn(Span<T>(inline_begin, end_ - inline_begin));
      }
    }
    else {
      if (InlineBufferCapacity > 0) {
        fn(Span<T>(inline_begin, InlineBufferCapacity));
      }
      for (const Span<T> span : alloc_info_->spans) {
        fn(span);
      }
    }
  }

  template<typename Fn> void foreach_span(Fn &&fn)
  {
    const_cast<const ChunkedList *>(this)->foreach_span(
        [&](const Span<T> span) { fn(MutableSpan(const_cast<T *>(span.data()), span.size())); });
  }

  std::optional<Span<T>> get_span(const int64_t index) const
  {
    /* TODO: Define the indices of individual spans. */
    return {};
  }

  void append(const T &value)
  {
    this->append_as(value);
  }
  void append(T &&value)
  {
    this->append_as(std::move(value));
  }
  template<typename... Args> void append_as(Args &&...args)
  {
    this->ensure_space_for_one();
    BLI_assert(end_ < capacity_end_);
    new (end_) T(std::forward<Args>(args)...);
    end_++;
  }

  template<int64_t OtherInlineBufferCapacity>
  void extend(const ChunkedList<T, OtherInlineBufferCapacity, Allocator> &list)
  {
    list.foreach_span([&](const Span<T> span) { this->extend(span); });
  }

  void extend(const Span<T> values)
  {
    const T *src_data = values.data();
    const int64_t remaining_capacity = capacity_end_ - end_;
    const int64_t copy_to_current_chunk = std::min(values.size(), remaining_capacity);
    const int64_t copy_to_next_chunk = values.size() - copy_to_current_chunk;

    uninitialized_copy_n(src_data, copy_to_current_chunk, end_);
    end_ += copy_to_current_chunk;
    if (copy_to_next_chunk == 0) {
      return;
    }
    this->add_chunk(copy_to_next_chunk);
    try {
      uninitialized_copy_n(src_data + copy_to_current_chunk, copy_to_next_chunk, end_);
    }
    catch (...) {
      /* TODO:
       * - Destruct data in previous chunk.
       * - Free newly allocated chunk.
       * - Reset `end_`. */
      throw;
    }
    end_ += copy_to_next_chunk;
  }

  class Iterator {
   private:
    using iterator_category = std::forward_iterator_tag;
    using value_type = T;
    using pointer = const T *;
    using reference = const T &;

    const T *begin_;
    const T *current_;
    const T *end_;
    int64_t span_index_;
    const ChunkedList *chunked_list_;

    friend ChunkedList;

   public:
    constexpr Iterator &operator++()
    {
      current_++;
      if (current_ == end_) {
        span_index_++;
        if (const std::optional<Span<T>> span = chunked_list_->get_span(span_index_)) {
          begin_ = span.data();
          current_ = begin_;
          end_ = begin_ + span.size();
        }
      }
    }

    constexpr friend bool operator!=(const Iterator &a, const Iterator &b)
    {
      BLI_assert(a.chunked_list_ == b.chunked_list_);
      return a.current_ == b.current_;
    }

    constexpr const T &operator*() const
    {
      return *current_;
    }
  };

 private:
  void ensure_space_for_one()
  {
    if (UNLIKELY(end_ >= capacity_end_)) {
      this->add_chunk(1);
    }
  }

  BLI_NOINLINE void add_chunk(const int64_t min_chunk_size)
  {
    int64_t new_chunk_size = min_chunk_size;
    T *new_chunk_begin;
    if (alloc_info_ == nullptr) {
      math::max_inplace(new_chunk_size, std::max<int64_t>(InlineBufferCapacity * 2, 8));
      const size_t allocation_size = sizeof(AllocInfo) +
                                     sizeof(T) * static_cast<size_t>(new_chunk_size);
      void *buffer = allocator_.allocate(allocation_size, alignof(AllocInfo), __func__);
      alloc_info_ = new (buffer) AllocInfo();
      new_chunk_begin = static_cast<T *>(POINTER_OFFSET(buffer, sizeof(AllocInfo)));
    }
    else {
      math::max_inplace(new_chunk_size,
                        std::min<int64_t>(alloc_info_->spans.last().size() * 2, 4096));
      const size_t allocation_size = sizeof(T) * static_cast<size_t>(new_chunk_size);
      new_chunk_begin = static_cast<T *>(
          allocator_.allocate(allocation_size, alignof(T), __func__));
    }
    MutableSpan<T> new_chunk{new_chunk_begin, new_chunk_size};
    alloc_info_->spans.append(new_chunk);
    end_ = new_chunk.data();
    capacity_end_ = new_chunk.data() + new_chunk_size;
  }
};

}  // namespace blender