diff options
author | Jacques Lucke <mail@jlucke.com> | 2019-09-14 16:03:25 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2019-09-14 16:03:25 +0300 |
commit | d6057b919dca589be97e3b0be15d6e61b04172ea (patch) | |
tree | 3014f7a1d4856a02a716bbf50e5242b673e10f4a /source/blender/blenlib/BLI_vector_set.h | |
parent | a98760f7dad7948fc50613d2c39cb9cbe8a4fb08 (diff) |
BLI: add utility to print VectorSet stats
Diffstat (limited to 'source/blender/blenlib/BLI_vector_set.h')
-rw-r--r-- | source/blender/blenlib/BLI_vector_set.h | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/source/blender/blenlib/BLI_vector_set.h b/source/blender/blenlib/BLI_vector_set.h index 5de3ae298a7..fb21f7ed987 100644 --- a/source/blender/blenlib/BLI_vector_set.h +++ b/source/blender/blenlib/BLI_vector_set.h @@ -302,6 +302,15 @@ template<typename T, typename Allocator = GuardedAllocator> class VectorSet { return m_elements; } + void print_stats() const + { + std::cout << "VectorSet at " << (void *)this << ":\n"; + std::cout << " Size: " << this->size() << "\n"; + std::cout << " Usable Slots: " << m_array.slots_usable() << "\n"; + std::cout << " Total Slots: " << m_array.slots_total() << "\n"; + std::cout << " Average Collisions: " << this->compute_average_collisions() << "\n"; + } + private: void update_slot_index(T &value, uint old_index, uint new_index) { @@ -354,6 +363,31 @@ template<typename T, typename Allocator = GuardedAllocator> class VectorSet { ITER_SLOTS_END; } + float compute_average_collisions() const + { + if (m_elements.size() == 0) { + return 0.0f; + } + + uint collisions_sum = 0; + for (const T &value : m_elements) { + collisions_sum += this->count_collisions(value); + } + return (float)collisions_sum / (float)m_elements.size(); + } + + uint count_collisions(const T &value) const + { + uint collisions = 0; + ITER_SLOTS_BEGIN (value, m_array, const, slot) { + if (slot.is_empty() || slot.has_value(value, m_elements)) { + return collisions; + } + collisions++; + } + ITER_SLOTS_END; + } + template<typename ForwardT> void add_new__impl(ForwardT &&value) { BLI_assert(!this->contains(value)); |