]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
gccrs: Introduce `IndexVec`
authorKushal Pal <kushalpal109@gmail.com>
Mon, 19 Aug 2024 09:28:25 +0000 (09:28 +0000)
committerArthur Cohen <arthur.cohen@embecosm.com>
Wed, 19 Mar 2025 14:32:09 +0000 (15:32 +0100)
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 <kushalpal109@gmail.com>
gcc/rust/checks/errors/borrowck/rust-bir-place.h

index f7018d3af7f026fbadc6ac3cdd02bbb59a3afe4d..2356460889200f60c9ba14a8251c88d73cfd1793 100644 (file)
@@ -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
 {