From: Kushal Pal Date: Mon, 19 Aug 2024 09:28:25 +0000 (+0000) Subject: Introduce `IndexVec` X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fe780d77dbd7c16076817a43b5f72ce645b2bb48;p=thirdparty%2Fgcc.git 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 --- diff --git a/gcc/rust/checks/errors/borrowck/rust-bir-place.h b/gcc/rust/checks/errors/borrowck/rust-bir-place.h index 728728c08aa2..6a6519d6d7d1 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 {