[Bf-blender-cvs] [225b5a3491d] master: BLI: Add utility functions to generic spans

Hans Goudey noreply at git.blender.org
Mon Sep 12 18:39:50 CEST 2022


Commit: 225b5a3491d9593639a80c9a34bcc017862eb2b2
Author: Hans Goudey
Date:   Mon Sep 12 10:38:31 2022 -0500
Branches: master
https://developer.blender.org/rB225b5a3491d9593639a80c9a34bcc017862eb2b2

BLI: Add utility functions to generic spans

Generally we don't want to do per-element operations on these spans
because of the overhead of the runtime type system, but these operations
on the whole span avoid ugly pointer arithmetic in other areas.

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

M	source/blender/blenlib/BLI_generic_span.hh

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

diff --git a/source/blender/blenlib/BLI_generic_span.hh b/source/blender/blenlib/BLI_generic_span.hh
index 143ab235d2e..e7a08988c46 100644
--- a/source/blender/blenlib/BLI_generic_span.hh
+++ b/source/blender/blenlib/BLI_generic_span.hh
@@ -100,6 +100,34 @@ class GSpan {
   {
     return this->slice(range.start(), range.size());
   }
+
+  GSpan drop_front(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::max<int64_t>(0, size_ - n);
+    return GSpan(*type_, POINTER_OFFSET(data_, type_->size() * n), new_size);
+  }
+
+  GSpan drop_back(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::max<int64_t>(0, size_ - n);
+    return GSpan(*type_, data_, new_size);
+  }
+
+  GSpan take_front(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::min<int64_t>(size_, n);
+    return GSpan(*type_, data_, new_size);
+  }
+
+  GSpan take_back(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::min<int64_t>(size_, n);
+    return GSpan(*type_, POINTER_OFFSET(data_, type_->size() * (size_ - new_size)), new_size);
+  }
 };
 
 /**
@@ -199,6 +227,35 @@ class GMutableSpan {
     return this->slice(range.start(), range.size());
   }
 
+  GMutableSpan drop_front(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::max<int64_t>(0, size_ - n);
+    return GMutableSpan(*type_, POINTER_OFFSET(data_, type_->size() * n), new_size);
+  }
+
+  GMutableSpan drop_back(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::max<int64_t>(0, size_ - n);
+    return GMutableSpan(*type_, data_, new_size);
+  }
+
+  GMutableSpan take_front(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::min<int64_t>(size_, n);
+    return GMutableSpan(*type_, data_, new_size);
+  }
+
+  GMutableSpan take_back(const int64_t n) const
+  {
+    BLI_assert(n >= 0);
+    const int64_t new_size = std::min<int64_t>(size_, n);
+    return GMutableSpan(
+        *type_, POINTER_OFFSET(data_, type_->size() * (size_ - new_size)), new_size);
+  }
+
   /**
    * Copy all values from another span into this span. This invokes undefined behavior when the
    * destination contains uninitialized data and T is not trivially copy constructible.



More information about the Bf-blender-cvs mailing list