[Bf-blender-cvs] [d6057b919dc] master: BLI: add utility to print VectorSet stats

Jacques Lucke noreply at git.blender.org
Sat Sep 14 15:03:33 CEST 2019


Commit: d6057b919dca589be97e3b0be15d6e61b04172ea
Author: Jacques Lucke
Date:   Sat Sep 14 15:03:25 2019 +0200
Branches: master
https://developer.blender.org/rBd6057b919dca589be97e3b0be15d6e61b04172ea

BLI: add utility to print VectorSet stats

===================================================================

M	source/blender/blenlib/BLI_vector_set.h

===================================================================

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));



More information about the Bf-blender-cvs mailing list