[Bf-blender-cvs] [8cbbdedaf4d] master: Refactor: Update integer type usage

Jacques Lucke noreply at git.blender.org
Mon Jul 20 12:20:35 CEST 2020


Commit: 8cbbdedaf4dfec9e320e7e2be58b75d256950df1
Author: Jacques Lucke
Date:   Mon Jul 20 12:16:20 2020 +0200
Branches: master
https://developer.blender.org/rB8cbbdedaf4dfec9e320e7e2be58b75d256950df1

Refactor: Update integer type usage

This updates the usage of integer types in code I wrote according to our new style guides.

Major changes:
* Use signed instead of unsigned integers in many places.
* C++ containers in blenlib use `int64_t` for size and indices now (instead of `uint`).
* Hash values for C++ containers are 64 bit wide now (instead of 32 bit).

I do hope that I broke no builds, but it is quite likely that some compiler reports
slightly different errors. Please let me know when there are any errors. If the fix
is small, feel free to commit it yourself.
I compiled successfully on linux with gcc and on windows.

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

M	source/blender/blenlib/BLI_allocator.hh
M	source/blender/blenlib/BLI_array.hh
M	source/blender/blenlib/BLI_color.hh
M	source/blender/blenlib/BLI_disjoint_set.hh
M	source/blender/blenlib/BLI_dot_export.hh
M	source/blender/blenlib/BLI_float3.hh
M	source/blender/blenlib/BLI_float4x4.hh
M	source/blender/blenlib/BLI_hash.hh
M	source/blender/blenlib/BLI_hash_tables.hh
M	source/blender/blenlib/BLI_index_mask.hh
M	source/blender/blenlib/BLI_index_range.hh
M	source/blender/blenlib/BLI_linear_allocator.hh
M	source/blender/blenlib/BLI_listbase_wrapper.hh
M	source/blender/blenlib/BLI_map.hh
M	source/blender/blenlib/BLI_map_slots.hh
M	source/blender/blenlib/BLI_memory_utils.hh
M	source/blender/blenlib/BLI_probing_strategies.hh
M	source/blender/blenlib/BLI_rand.hh
M	source/blender/blenlib/BLI_resource_collector.hh
M	source/blender/blenlib/BLI_set.hh
M	source/blender/blenlib/BLI_set_slots.hh
M	source/blender/blenlib/BLI_span.hh
M	source/blender/blenlib/BLI_stack.hh
M	source/blender/blenlib/BLI_string_ref.hh
M	source/blender/blenlib/BLI_vector.hh
M	source/blender/blenlib/BLI_vector_set.hh
M	source/blender/blenlib/BLI_vector_set_slots.hh
M	source/blender/blenlib/intern/BLI_index_range.cc
M	source/blender/blenlib/intern/dot_export.cc
M	source/blender/blenlib/intern/rand.cc
M	source/blender/depsgraph/intern/builder/deg_builder_cache.cc
M	source/blender/depsgraph/intern/builder/deg_builder_cache.h
M	source/blender/depsgraph/intern/depsgraph_registry.cc
M	source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_modifier.cc
M	source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_modifier.h
M	source/blender/depsgraph/intern/node/deg_node_component.cc
M	source/blender/depsgraph/intern/node/deg_node_component.h
M	source/blender/depsgraph/intern/node/deg_node_id.cc
M	source/blender/depsgraph/intern/node/deg_node_id.h
M	source/blender/functions/FN_array_spans.hh
M	source/blender/functions/FN_attributes_ref.hh
M	source/blender/functions/FN_cpp_type.hh
M	source/blender/functions/FN_generic_vector_array.hh
M	source/blender/functions/FN_multi_function.hh
M	source/blender/functions/FN_multi_function_builder.hh
M	source/blender/functions/FN_multi_function_data_type.hh
M	source/blender/functions/FN_multi_function_network.hh
M	source/blender/functions/FN_multi_function_params.hh
M	source/blender/functions/FN_multi_function_signature.hh
M	source/blender/functions/FN_spans.hh
M	source/blender/functions/intern/attributes_ref.cc
M	source/blender/functions/intern/multi_function_builder.cc
M	source/blender/functions/intern/multi_function_network.cc
M	source/blender/functions/intern/multi_function_network_evaluation.cc
M	source/blender/functions/intern/multi_function_network_optimization.cc
M	source/blender/nodes/NOD_derived_node_tree.hh
M	source/blender/nodes/NOD_node_tree_multi_function.hh
M	source/blender/nodes/NOD_node_tree_ref.hh
M	source/blender/nodes/intern/derived_node_tree.cc
M	source/blender/nodes/intern/node_tree_multi_function.cc
M	source/blender/nodes/shader/nodes/node_shader_map_range.cc
M	source/blender/nodes/shader/nodes/node_shader_sepcombRGB.cc
M	source/blender/nodes/shader/nodes/node_shader_sepcombXYZ.cc
M	source/blender/nodes/shader/nodes/node_shader_valToRgb.cc
M	source/blender/simulation/intern/particle_allocator.cc
M	source/blender/simulation/intern/particle_allocator.hh
M	source/blender/simulation/intern/particle_function.cc
M	source/blender/simulation/intern/particle_function.hh
M	source/blender/simulation/intern/simulation_collect_influences.cc
M	source/blender/simulation/intern/simulation_solver.cc
M	tests/gtests/blenlib/BLI_array_test.cc
M	tests/gtests/blenlib/BLI_disjoint_set_test.cc
M	tests/gtests/blenlib/BLI_index_mask_test.cc
M	tests/gtests/blenlib/BLI_index_range_test.cc
M	tests/gtests/blenlib/BLI_linear_allocator_test.cc
M	tests/gtests/blenlib/BLI_map_test.cc
M	tests/gtests/blenlib/BLI_set_test.cc
M	tests/gtests/blenlib/BLI_span_test.cc
M	tests/gtests/blenlib/BLI_stack_cxx_test.cc
M	tests/gtests/blenlib/BLI_string_ref_test.cc
M	tests/gtests/blenlib/BLI_vector_set_test.cc
M	tests/gtests/blenlib/BLI_vector_test.cc
M	tests/gtests/functions/FN_array_spans_test.cc
M	tests/gtests/functions/FN_attributes_ref_test.cc
M	tests/gtests/functions/FN_cpp_type_test.cc
M	tests/gtests/functions/FN_multi_function_network_test.cc
M	tests/gtests/functions/FN_multi_function_test.cc

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

diff --git a/source/blender/blenlib/BLI_allocator.hh b/source/blender/blenlib/BLI_allocator.hh
index d57703f71bc..ec82e5ab71c 100644
--- a/source/blender/blenlib/BLI_allocator.hh
+++ b/source/blender/blenlib/BLI_allocator.hh
@@ -84,7 +84,7 @@ class RawAllocator {
     void *ptr = malloc(size + alignment + sizeof(MemHead));
     void *used_ptr = (void *)((uintptr_t)POINTER_OFFSET(ptr, alignment + sizeof(MemHead)) &
                               ~((uintptr_t)alignment - 1));
-    uint offset = (uint)((uintptr_t)used_ptr - (uintptr_t)ptr);
+    int offset = (int)((uintptr_t)used_ptr - (uintptr_t)ptr);
     BLI_assert(offset >= sizeof(MemHead));
     ((MemHead *)used_ptr - 1)->offset = (int)offset;
     return used_ptr;
diff --git a/source/blender/blenlib/BLI_array.hh b/source/blender/blenlib/BLI_array.hh
index c411fc50f15..c30893f1337 100644
--- a/source/blender/blenlib/BLI_array.hh
+++ b/source/blender/blenlib/BLI_array.hh
@@ -13,6 +13,7 @@
  * along with this program; if not, write to the Free Software Foundation,
  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
+
 #ifndef __BLI_ARRAY_HH__
 #define __BLI_ARRAY_HH__
 
@@ -56,7 +57,7 @@ template<
      * When T is large, the small buffer optimization is disabled by default to avoid large
      * unexpected allocations on the stack. It can still be enabled explicitly though.
      */
-    uint InlineBufferCapacity = (sizeof(T) < 100) ? 4 : 0,
+    int64_t InlineBufferCapacity = (sizeof(T) < 100) ? 4 : 0,
     /**
      * The allocator used by this array. Should rarely be changed, except when you don't want that
      * MEM_* functions are used internally.
@@ -68,7 +69,7 @@ class Array {
   T *data_;
 
   /** Number of elements in the array. */
-  uint size_;
+  int64_t size_;
 
   /** Used for allocations when the inline buffer is too small. */
   Allocator allocator_;
@@ -117,7 +118,7 @@ class Array {
    * even for non-trivial types. This should not be the default though, because one can easily mess
    * up when dealing with uninitialized memory.
    */
-  explicit Array(uint size)
+  explicit Array(int64_t size)
   {
     size_ = size;
     data_ = this->get_buffer_for_size(size);
@@ -128,8 +129,9 @@ class Array {
    * Create a new array with the given size. All values will be initialized by copying the given
    * default.
    */
-  Array(uint size, const T &value)
+  Array(int64_t size, const T &value)
   {
+    BLI_assert(size >= 0);
     size_ = size;
     data_ = this->get_buffer_for_size(size);
     uninitialized_fill_n(data_, size_, value);
@@ -147,8 +149,9 @@ class Array {
    * Usage:
    *  Array<std::string> my_strings(10, NoInitialization());
    */
-  Array(uint size, NoInitialization)
+  Array(int64_t size, NoInitialization)
   {
+    BLI_assert(size >= 0);
     size_ = size;
     data_ = this->get_buffer_for_size(size);
   }
@@ -203,14 +206,16 @@ class Array {
     return *this;
   }
 
-  T &operator[](uint index)
+  T &operator[](int64_t index)
   {
+    BLI_assert(index >= 0);
     BLI_assert(index < size_);
     return data_[index];
   }
 
-  const T &operator[](uint index) const
+  const T &operator[](int64_t index) const
   {
+    BLI_assert(index >= 0);
     BLI_assert(index < size_);
     return data_[index];
   }
@@ -250,7 +255,7 @@ class Array {
   /**
    * Returns the number of elements in the array.
    */
-  uint size() const
+  int64_t size() const
   {
     return size_;
   }
@@ -263,22 +268,6 @@ class Array {
     return size_ == 0;
   }
 
-  /**
-   * Copies the value to all indices in the array.
-   */
-  void fill(const T &value)
-  {
-    initialized_fill_n(data_, size_, value);
-  }
-
-  /**
-   * Copies the value to the given indices in the array.
-   */
-  void fill_indices(Span<uint> indices, const T &value)
-  {
-    MutableSpan<T>(*this).fill_indices(indices, value);
-  }
-
   /**
    * Get a pointer to the beginning of the array.
    */
@@ -340,13 +329,13 @@ class Array {
    * Get the value of the InlineBufferCapacity template argument. This is the number of elements
    * that can be stored without doing an allocation.
    */
-  static uint inline_buffer_capacity()
+  static int64_t inline_buffer_capacity()
   {
     return InlineBufferCapacity;
   }
 
  private:
-  T *get_buffer_for_size(uint size)
+  T *get_buffer_for_size(int64_t size)
   {
     if (size <= InlineBufferCapacity) {
       return inline_buffer_;
@@ -356,9 +345,9 @@ class Array {
     }
   }
 
-  T *allocate(uint size)
+  T *allocate(int64_t size)
   {
-    return (T *)allocator_.allocate(size * sizeof(T), alignof(T), AT);
+    return (T *)allocator_.allocate((size_t)size * sizeof(T), alignof(T), AT);
   }
 
   bool uses_inline_buffer() const
diff --git a/source/blender/blenlib/BLI_color.hh b/source/blender/blenlib/BLI_color.hh
index 265013c0013..72caa5b1118 100644
--- a/source/blender/blenlib/BLI_color.hh
+++ b/source/blender/blenlib/BLI_color.hh
@@ -62,12 +62,12 @@ struct Color4f {
     return !(a == b);
   }
 
-  uint32_t hash() const
+  uint64_t hash() const
   {
-    uint32_t x1 = *(uint32_t *)&r;
-    uint32_t x2 = *(uint32_t *)&g;
-    uint32_t x3 = *(uint32_t *)&b;
-    uint32_t x4 = *(uint32_t *)&a;
+    uint64_t x1 = *(uint32_t *)&r;
+    uint64_t x2 = *(uint32_t *)&g;
+    uint64_t x3 = *(uint32_t *)&b;
+    uint64_t x4 = *(uint32_t *)&a;
     return (x1 * 1283591) ^ (x2 * 850177) ^ (x3 * 735391) ^ (x4 * 442319);
   }
 };
@@ -119,10 +119,10 @@ struct Color4b {
     return !(a == b);
   }
 
-  uint32_t hash() const
+  uint64_t hash() const
   {
-    return ((uint32_t)r * 1283591) ^ ((uint32_t)g * 850177) ^ ((uint32_t)b * 735391) ^
-           ((uint32_t)a * 442319);
+    return ((uint64_t)r * 1283591) ^ ((uint64_t)g * 850177) ^ ((uint64_t)b * 735391) ^
+           ((uint64_t)a * 442319);
   }
 };
 
diff --git a/source/blender/blenlib/BLI_disjoint_set.hh b/source/blender/blenlib/BLI_disjoint_set.hh
index 3b8453669aa..e0580709a44 100644
--- a/source/blender/blenlib/BLI_disjoint_set.hh
+++ b/source/blender/blenlib/BLI_disjoint_set.hh
@@ -29,16 +29,17 @@ namespace blender {
 
 class DisjointSet {
  private:
-  Array<uint> parents_;
-  Array<uint> ranks_;
+  Array<int64_t> parents_;
+  Array<int64_t> ranks_;
 
  public:
   /**
    * Create a new disjoint set with the given size. Initially, every element is in a separate set.
    */
-  DisjointSet(uint size) : parents_(size), ranks_(size, 0)
+  DisjointSet(int64_t size) : parents_(size), ranks_(size, 0)
   {
-    for (uint i = 0; i < size; i++) {
+    BLI_assert(size >= 0);
+    for (int64_t i = 0; i < size; i++) {
       parents_[i] = i;
     }
   }
@@ -47,10 +48,10 @@ class DisjointSet {
    * Join the sets containing elements x and y. Nothing happens when they have been in the same set
    * before.
    */
-  void join(uint x, uint y)
+  void join(int64_t x, int64_t y)
   {
-    uint root1 = this->find_root(x);
-    uint root2 = this->find_root(y);
+    int64_t root1 = this->find_root(x);
+    int64_t root2 = this->find_root(y);
 
     /* x and y are in the same set already. */
     if (root1 == root2) {
@@ -71,27 +72,27 @@ class DisjointSet {
   /**
    * Return true when x and y are in the same set.
    */
-  bool in_same_set(uint x, uint y)
+  bool in_same_set(int64_t x, int64_t y)
   {
-    uint root1 = this->find_root(x);
-    uint root2 = this->find_root(y);
+    int64_t root1 = this->find_root(x);
+    int64_t root2 = this->find_root(y);
     return root1 == root2;
   }
 
   /**
    * Find the element that represents the set containing x currently.
    */
-  uint find_root(uint x)
+  int64_t find_root(int64_t x)
   {
     /* Find root by following parents. */
-    uint root = x;
+    int64_t root = x;
     while (parents_[root] != root) {
       root = parents_[root];
     }
 
     /* Compress path. */
     while (parents_[x] != root) {
-      uint parent = parents_[x];
+      int64_t parent = parents_[x];
       parents_[x] = root;
       x = parent;
     }
diff --git a/source/blender/blenlib/BLI_dot_export.hh b/source/blender/blenlib/BLI_dot_export.hh
index a7c5f1436d1..0870d8c4c30 100644
--- a/source/blender/blenlib/BLI_dot_export.hh
+++ b/source/blender/blenlib/BLI_dot_export.hh
@@ -274,13 +274,13 @@ class NodeWithSocketsRef {
     return *node_;
   }
 
-  NodePort input(uint index) const
+  NodePort input(int index) const
   {
     std::string port = "\"in" + std::to_string(index) + "\"";
     return NodePort(*node_, port);
   }
 
-  NodePort output(uint index) const
+  NodePort output(int index) const
   {
     std::string port = "\"out" + std::to_string(index) + "\"";
     return NodePort(*node_, port);
diff --git a/source/blender/blenlib/BLI_float3.hh b/source/blender/blenlib/BLI_float3.hh
index a36cedad41d..b2633985ac7 100644
--- a/source/blender/blenlib/BLI_float3.hh
+++ b/source/blender/blenlib/BLI_float3.hh
@@ -188,11 +188,11 @@ struct float3 {
     z = -z;
   }
 
-  uint32_t hash() const
+  uint64_t hash() const
   {
-    uint32_t x1 = *(uint32_t *)&x;
-    uint32_t x2 = *(uint32_t *)&y;
-    uint32_t x3 = *(uint32_t *)&z;
+    uint64_t x1 = *(uint32_t *)&x;
+    uint64_t x2 = *(uint32_t *)&y;
+    uint64_t x3 = *(uint32_t *)&z;
     return (x1 * 435109) ^ (x2 * 380867) ^ (x3 * 1059217);
   }
 
diff --git a/source/blender/blenlib/BLI_float4x4.hh b/source/blender/blenlib/BLI_float4x4.hh
index ef83f9ffc19..185cffd13ac 100644
--- a/source/blender/blenlib/BLI_float4x4.hh
+++ b/source/blender/blenlib/BLI_float4x4.hh
@@ -109,10 +109,10 @@ struct float4x4 {
     return result;
   }
 
-  uint32_t hash() const
+  uint64_t hash() const
   {
-    uint32_t h = 435109;
-    for (uint i = 0; i < 16; i++) {
+    uint64_t h = 435109;
+    for (int i = 0; i < 16; i++) {
       float value = ((const float *)this)[i];
       h = h * 33 + (*(uint32_t *)&value);
     }
diff --git a/source/blender/blenlib/BLI_hash.hh b/source/blender/blenlib/BLI_hash.hh
index 5cd4ce3c1a9..b14a4ca933c 100644
--- a/source/blender/blenlib/BLI_hash.hh
+++ b/source/blender/blenlib/BLI_hash.hh
@@ -38,7 +38,7 @@
  * multiple `operator()` in a specialization of #DefaultHash. All those methods have to compute the
  * same hash for values that compare equal.
  *
- * The computed hash is an unsigned 32 bit integer. Ideally, the 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list