aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/Containers.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/Utilities/Containers.h')
-rw-r--r--src/common/Utilities/Containers.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/common/Utilities/Containers.h b/src/common/Utilities/Containers.h
index c34761941a6..dfd37f69d62 100644
--- a/src/common/Utilities/Containers.h
+++ b/src/common/Utilities/Containers.h
@@ -241,6 +241,37 @@ namespace Trinity
++itr;
}
}
+
+ /**
+ * Returns a mutable reference to element at index i
+ * Will resize vector if neccessary to ensure element at i can be safely written
+ *
+ * This exists as separate overload instead of one function with default argument to allow using
+ * with vectors of non-default-constructible classes
+ */
+ template<typename T>
+ inline decltype(auto) EnsureWritableVectorIndex(std::vector<T>& vec, typename std::vector<T>::size_type i)
+ {
+ if (i >= vec.size())
+ vec.resize(i + 1);
+
+ return vec[i];
+ }
+
+ /**
+ * Returns a mutable reference to element at index i
+ * Will resize vector if neccessary to ensure element at i can be safely written
+ *
+ * This overload allows specifying what value to pad vector with during .resize
+ */
+ template<typename T>
+ inline decltype(auto) EnsureWritableVectorIndex(std::vector<T>& vec, typename std::vector<T>::size_type i, T const& resizeDefault)
+ {
+ if (i >= vec.size())
+ vec.resize(i + 1, resizeDefault);
+
+ return vec[i];
+ }
}
//! namespace Containers
}