BLI: add index_range method to some data structures

This can be used to iterate over all indices with less code.
This commit is contained in:
Jacques Lucke 2020-02-07 17:23:25 +01:00
parent f3db5a0965
commit c2e80cfaa3
4 changed files with 23 additions and 0 deletions

View File

@ -30,6 +30,7 @@
*/
#include <stdlib.h>
#include <algorithm>
#include "MEM_guardedalloc.h"

View File

@ -27,6 +27,7 @@
#include "BLI_allocator.h"
#include "BLI_array_ref.h"
#include "BLI_memory_utils_cxx.h"
#include "BLI_index_range.h"
namespace BLI {
@ -182,6 +183,11 @@ template<typename T, typename Allocator = GuardedAllocator> class Array {
return m_data + m_size;
}
IndexRange index_range() const
{
return IndexRange(m_size);
}
private:
T *allocate(uint size)
{

View File

@ -246,6 +246,11 @@ template<typename T> class ArrayRef {
return fallback;
}
IndexRange index_range() const
{
return IndexRange(m_size);
}
/**
* Get a new array ref to the same underlying memory buffer. No conversions are done.
* Asserts when the sizes of the types don't match.
@ -411,6 +416,11 @@ template<typename T> class MutableArrayRef {
{
return ArrayRef<T>(m_start, m_size);
}
IndexRange index_range() const
{
return IndexRange(m_size);
}
};
/**

View File

@ -37,6 +37,7 @@
#include "BLI_listbase_wrapper.h"
#include "BLI_math_base.h"
#include "BLI_allocator.h"
#include "BLI_index_range.h"
#include "MEM_guardedalloc.h"
@ -520,6 +521,11 @@ template<typename T, uint N = 4, typename Allocator = GuardedAllocator> class Ve
return (uint)(m_capacity_end - m_begin);
}
IndexRange index_range() const
{
return IndexRange(this->size());
}
void print_stats() const
{
std::cout << "Small Vector at " << (void *)this << ":" << std::endl;