https://gcc.gnu.org/g:d91f1148d85ae0801387975364a5338237ac6efc

commit r15-8366-gd91f1148d85ae0801387975364a5338237ac6efc
Author: Kushal Pal <kushalpal...@gmail.com>
Date:   Mon Aug 19 09:28:25 2024 +0000

    gccrs: Introduce `IndexVec`
    
    gcc/rust/ChangeLog:
    
            * checks/errors/borrowck/rust-bir-place.h (struct Loan):
            Introduce new class `IndexVec` inspired from IndexVec of rust.
            It acts as a wrapper around `std::vector` and lets user specify
            a strong type to use as index.
    
    Signed-off-by: Kushal Pal <kushalpal...@gmail.com>

Diff:
---
 gcc/rust/checks/errors/borrowck/rust-bir-place.h | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h 
b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
index f7018d3af7f0..235646088920 100644
--- a/gcc/rust/checks/errors/borrowck/rust-bir-place.h
+++ b/gcc/rust/checks/errors/borrowck/rust-bir-place.h
@@ -199,6 +199,25 @@ struct Loan
   location_t location;
 };
 
+// I is the index type, T is the contained type
+template <typename I, typename T> class IndexVec
+{
+  std::vector<T> internal_vector;
+
+public:
+  T &at (I pid) { return internal_vector[pid.value]; }
+  const T &at (I pid) const { return internal_vector[pid.value]; }
+  T &operator[] (I pid) { return internal_vector[pid.value]; }
+  const T &operator[] (I pid) const { return internal_vector[pid.value]; }
+
+  void push_back (T &&param) { internal_vector.push_back (std::move (param)); }
+  template <typename... Args> void emplace_back (Args &&... args)
+  {
+    internal_vector.emplace_back (std::forward<Args> (args)...);
+  }
+  size_t size () const { return internal_vector.size (); }
+};
+
 /** Allocated places and keeps track of paths. */
 class PlaceDB
 {

Reply via email to