From d91f1148d85ae0801387975364a5338237ac6efc Mon Sep 17 00:00:00 2001 From: Kushal Pal Date: Mon, 19 Aug 2024 09:28:25 +0000 Subject: [PATCH] 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 --- .../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 f7018d3af7f..23564608892 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 class IndexVec +{ + std::vector 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 &¶m) { internal_vector.push_back (std::move (param)); } + template void emplace_back (Args &&... args) + { + internal_vector.emplace_back (std::forward (args)...); + } + size_t size () const { return internal_vector.size (); } +}; + /** Allocated places and keeps track of paths. */ class PlaceDB { -- 2.47.2