File include/jlib/rarray.h changed (mode: 100644) (index e430e04..ceb383e) |
... |
... |
struct jl::rarray |
58 |
58 |
rarray() = default; |
rarray() = default; |
59 |
59 |
/// @brief Initialize read-only rarray as array with single value. |
/// @brief Initialize read-only rarray as array with single value. |
60 |
60 |
/// @param value Single item to be pointed to. |
/// @param value Single item to be pointed to. |
61 |
|
rarray(T &value) : p_begin(&value), p_end_allocated(p_begin+1) { |
|
|
61 |
|
rarray(T &value) : p_begin(&value), p_end(p_begin+1) { |
62 |
62 |
static_assert (std::is_const<T>::value, "only for read-only rarray"); |
static_assert (std::is_const<T>::value, "only for read-only rarray"); |
63 |
63 |
} |
} |
64 |
64 |
/** |
/** |
|
... |
... |
struct jl::rarray |
87 |
87 |
* first byte after an allocation. |
* first byte after an allocation. |
88 |
88 |
*/ |
*/ |
89 |
89 |
rarray(T* p_allocated_mem, T* p_end_alocated_mem) |
rarray(T* p_allocated_mem, T* p_end_alocated_mem) |
90 |
|
: p_begin(p_allocated_mem), p_end_allocated(p_end_alocated_mem) { |
|
91 |
|
jassert(p_end_allocated >= p_begin, "wrong pointers order"); |
|
|
90 |
|
: p_begin(p_allocated_mem), p_end(p_end_alocated_mem) { |
|
91 |
|
jassert(p_end >= p_begin, "wrong pointers order"); |
92 |
92 |
} |
} |
93 |
93 |
/** |
/** |
94 |
94 |
* @brief Initialize with already allocated memory. |
* @brief Initialize with already allocated memory. |
|
... |
... |
struct jl::rarray |
101 |
101 |
*/ |
*/ |
102 |
102 |
rarray(T *p_allocated_mem, size_t allocated_count) |
rarray(T *p_allocated_mem, size_t allocated_count) |
103 |
103 |
: p_begin(p_allocated_mem), |
: p_begin(p_allocated_mem), |
104 |
|
p_end_allocated(p_allocated_mem + allocated_count) {} |
|
|
104 |
|
p_end(p_allocated_mem + allocated_count) {} |
105 |
105 |
/** |
/** |
106 |
106 |
* @brief Initialize with already allocated pointers to range of values. |
* @brief Initialize with already allocated pointers to range of values. |
107 |
107 |
* @tparam R Type with support of begin and end functions. |
* @tparam R Type with support of begin and end functions. |
|
... |
... |
struct jl::rarray |
124 |
124 |
jassert(count, "must not be 0, use init() instead"); |
jassert(count, "must not be 0, use init() instead"); |
125 |
125 |
if (not jl::allocate(&p_begin, count)) |
if (not jl::allocate(&p_begin, count)) |
126 |
126 |
return false; |
return false; |
127 |
|
p_end_allocated = p_begin + count; |
|
|
127 |
|
p_end = p_begin + count; |
128 |
128 |
return true; |
return true; |
129 |
129 |
} |
} |
130 |
130 |
/** |
/** |
|
... |
... |
struct jl::rarray |
166 |
166 |
jassert(new_item_count, "cannot be 0"); |
jassert(new_item_count, "cannot be 0"); |
167 |
167 |
return resize_bytes(sizeof(T) * new_item_count); |
return resize_bytes(sizeof(T) * new_item_count); |
168 |
168 |
} |
} |
|
169 |
|
|
|
170 |
|
/** |
|
171 |
|
* @brief Reduce item count without freeing memory. |
|
172 |
|
* @param new_item_count New item count in range (0;count]. |
|
173 |
|
*/ |
|
174 |
|
void forget(size_t new_item_count) { |
|
175 |
|
jassert(new_item_count > 0 and new_item_count <= count(), |
|
176 |
|
"cannot be 0 and cannot be larger"); |
|
177 |
|
p_end = p_begin + new_item_count; |
|
178 |
|
} |
169 |
179 |
/** |
/** |
170 |
180 |
* @brief Get pointer to item, which is equal to value_to_search. |
* @brief Get pointer to item, which is equal to value_to_search. |
171 |
181 |
* @tparam T_CMP T type must have overloaded "==" operator |
* @tparam T_CMP T type must have overloaded "==" operator |
|
... |
... |
struct jl::rarray |
194 |
204 |
} |
} |
195 |
205 |
///@brief Size of all items (allocated memory) in bytes. |
///@brief Size of all items (allocated memory) in bytes. |
196 |
206 |
[[nodiscard]] size_t size() const { |
[[nodiscard]] size_t size() const { |
197 |
|
return size_t(p_end_allocated - p_begin) * sizeof(T); |
|
|
207 |
|
return size_t(p_end - p_begin) * sizeof(T); |
198 |
208 |
} |
} |
199 |
209 |
///@brief Item count. |
///@brief Item count. |
200 |
210 |
[[nodiscard]] size_t count() const { return size_t(end() - begin()); } |
[[nodiscard]] size_t count() const { return size_t(end() - begin()); } |
|
... |
... |
struct jl::rarray |
210 |
220 |
[[nodiscard]] const T* begin() const { return reinterpret_cast<T*>(p_begin); } |
[[nodiscard]] const T* begin() const { return reinterpret_cast<T*>(p_begin); } |
211 |
221 |
/// @brief Get pointer to the first byte after allocated memory pool. |
/// @brief Get pointer to the first byte after allocated memory pool. |
212 |
222 |
[[nodiscard]] T* end() { |
[[nodiscard]] T* end() { |
213 |
|
return reinterpret_cast<T*>(p_end_allocated); |
|
|
223 |
|
return reinterpret_cast<T*>(p_end); |
214 |
224 |
} |
} |
215 |
225 |
/// @brief Get pointer with read-only access to the first byte after allocation |
/// @brief Get pointer with read-only access to the first byte after allocation |
216 |
226 |
[[nodiscard]] const T* end() const { |
[[nodiscard]] const T* end() const { |
217 |
|
return reinterpret_cast<T*>(p_end_allocated); |
|
|
227 |
|
return reinterpret_cast<T*>(p_end); |
218 |
228 |
} |
} |
219 |
229 |
/// @brief Get read-only refference to an item by index |
/// @brief Get read-only refference to an item by index |
220 |
230 |
[[nodiscard]] const T& operator[](size_t i) const { |
[[nodiscard]] const T& operator[](size_t i) const { |
|
... |
... |
struct jl::rarray |
237 |
247 |
template<typename> friend struct rarray; |
template<typename> friend struct rarray; |
238 |
248 |
/// @brief Convert to read-only version without issues |
/// @brief Convert to read-only version without issues |
239 |
249 |
operator rarray<const T> () const { |
operator rarray<const T> () const { |
240 |
|
jassert(p_end_allocated >= p_begin, "corruption detected"); |
|
241 |
|
return rarray<const T> { p_begin, p_end_allocated }; |
|
|
250 |
|
jassert(p_end >= p_begin, "corruption detected"); |
|
251 |
|
return rarray<const T> { p_begin, p_end }; |
242 |
252 |
} |
} |
243 |
253 |
/// @brief Get index of iterator. |
/// @brief Get index of iterator. |
244 |
254 |
/// @param p pointer in range [begin;end). |
/// @param p pointer in range [begin;end). |
245 |
255 |
/// @return Index of an item on iterator position. |
/// @return Index of an item on iterator position. |
246 |
256 |
[[nodiscard]] size_t iterator_index(T *p) { |
[[nodiscard]] size_t iterator_index(T *p) { |
247 |
|
jassert(p >= p_begin and p < p_end_allocated, |
|
|
257 |
|
jassert(p >= p_begin and p < p_end, |
248 |
258 |
"iterator must be in the range [begin,end)"); |
"iterator must be in the range [begin,end)"); |
249 |
259 |
return size_t(p - p_begin); |
return size_t(p - p_begin); |
250 |
260 |
} |
} |
|
... |
... |
protected: |
253 |
263 |
jassert(new_size, "cannot be 0"); |
jassert(new_size, "cannot be 0"); |
254 |
264 |
if (not reallocate_bytes(&p_begin, new_size)) |
if (not reallocate_bytes(&p_begin, new_size)) |
255 |
265 |
return false; |
return false; |
256 |
|
p_end_allocated = p_begin + new_size; |
|
|
266 |
|
p_end = p_begin + new_size; |
257 |
267 |
return true; |
return true; |
258 |
268 |
} |
} |
259 |
269 |
T *p_begin; |
T *p_begin; |
260 |
|
T *p_end_allocated; |
|
|
270 |
|
T *p_end; |
261 |
271 |
}; |
}; |
262 |
272 |
|
|