]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Introduce `IndexVec`
authorKushal Pal <kushalpal109@gmail.com>
Mon, 19 Aug 2024 09:28:25 +0000 (09:28 +0000)
committerP-E-P <32375388+P-E-P@users.noreply.github.com>
Mon, 9 Sep 2024 08:33:02 +0000 (08:33 +0000)
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 728728c08aa2eb66bcf652ef30d976864cfe41d7..6a6519d6d7d149407f9f70391da76bf80ffb15f3 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
 {