172 lines
8.2 KiB
C++
172 lines
8.2 KiB
C++
#ifndef ARBUTILS_LIST_HPP
|
|
#define ARBUTILS_LIST_HPP
|
|
#include <optional>
|
|
#include <sstream>
|
|
#include <vector>
|
|
#include "../Defines.hpp"
|
|
#include "../Exception.hpp"
|
|
|
|
namespace ArbUt {
|
|
/// @brief Wrapper around vector, allowing safer access, with several helper methods inserted.
|
|
template <class ValueT> class List {
|
|
private:
|
|
std::vector<ValueT> _vector;
|
|
using reference = typename std::vector<ValueT>::reference;
|
|
using const_reference = typename std::vector<ValueT>::const_reference;
|
|
using iterator = typename std::vector<ValueT>::iterator;
|
|
using const_iterator = typename std::vector<ValueT>::const_iterator;
|
|
|
|
public:
|
|
/// @brief The type of the value stored in the list.
|
|
typedef ValueT type;
|
|
|
|
List() noexcept : _vector() {}
|
|
/// @brief Creates a list with a reserved capacity.
|
|
/// @param capacity The number of spaces the list should reserve.
|
|
explicit List(size_t capacity) : _vector() { _vector.reserve(capacity); }
|
|
/// @brief Initialise the list from an initializer_list.
|
|
/// @param l The initializer_list to initialise from.
|
|
List(const std::initializer_list<ValueT>& l) noexcept : _vector(l) {}
|
|
/// @brief Initialise a list from a memory range.
|
|
/// @param begin A pointer to the beginning of the memory range.
|
|
/// @param end A pointer to the end of the memory range.
|
|
List(const ValueT* non_null begin, const ValueT* non_null end) noexcept : _vector(begin, end) {}
|
|
|
|
/// @brief Removes all items from the list.
|
|
inline void Clear() noexcept { _vector.clear(); }
|
|
|
|
/// @brief Gets an item at a position. Asserts whether the index is within bounds.
|
|
/// @param index The index requested.
|
|
/// @return A reference to the item at the given index.
|
|
inline reference At(size_t index) {
|
|
#ifndef NO_ASSERT
|
|
if (index >= _vector.size()) {
|
|
std::stringstream ss;
|
|
ss << "Index " << index << " is out of bounds for length: " << _vector.size();
|
|
throw ArbUt::Exception(ss.str());
|
|
}
|
|
#endif
|
|
return _vector.at(index);
|
|
}
|
|
|
|
/// @brief Gets an item at a position. Asserts whether the index is within bounds.
|
|
/// @param index The index requested.
|
|
/// @return A reference to the item at the given index.
|
|
inline const const_reference& At(size_t index) const {
|
|
#ifndef NO_ASSERT
|
|
if (index >= _vector.size()) {
|
|
std::stringstream ss;
|
|
ss << "Index " << index << " is out of bounds for length: " << _vector.size();
|
|
throw ArbUt::Exception(ss.str());
|
|
}
|
|
#endif
|
|
return _vector.at(index);
|
|
}
|
|
|
|
/// @brief Sets an item to a given position. Asserts whether the index is within bounds
|
|
/// @param index The index requested.
|
|
/// @param value The value of the item.
|
|
/// @return The previous item at the given position.
|
|
inline const const_reference& Set(size_t index, const ValueT& value) {
|
|
#ifndef NO_ASSERT
|
|
if (index >= _vector.size()) {
|
|
std::stringstream ss;
|
|
ss << "Index " << index << " is out of bounds for length: " << _vector.size();
|
|
throw ArbUt::Exception(ss.str());
|
|
}
|
|
#endif
|
|
const auto& prev = _vector.at(index);
|
|
_vector[index] = value;
|
|
return prev;
|
|
}
|
|
|
|
/// @brief Check whether an item exist in the list.
|
|
/// @param value A reference to the item we're checking for.
|
|
/// @return Whether the item exists in the list.
|
|
inline bool Contains(const ValueT& value) const {
|
|
return std::find(_vector.begin(), _vector.end(), value) != _vector.end();
|
|
}
|
|
|
|
/// @brief Find the index of the first occurrence of a value in the list, return -1 if none is found.
|
|
/// @param value The value we want the index for.
|
|
/// @return The index of the first occurrence of the value in the list, or -1 if none is found.
|
|
inline std::optional<size_t> IndexOf(const ValueT& value) const noexcept {
|
|
const auto& it = std::find(_vector.begin(), _vector.end(), value);
|
|
if (it == _vector.end())
|
|
return {};
|
|
return std::distance(_vector.begin(), it);
|
|
}
|
|
|
|
/// @brief Adds an item to the end of the list.
|
|
/// @param value The item that needs to be added.
|
|
inline void Append(const ValueT& value) { _vector.push_back(value); }
|
|
|
|
/// @brief Instantiate an item at the end of the list.
|
|
/// @tparam parameters Type argument of constructor args.
|
|
/// @param pars The arguments of the constructor for the item.
|
|
template <class... parameters> inline void CreateBack(parameters... pars) { _vector.emplace_back(pars...); }
|
|
|
|
/// @brief Inserts an item into a position in the list.
|
|
/// @param index The index the item should be.
|
|
/// @param value The item that should be in the position.
|
|
inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); }
|
|
|
|
/// @brief Returns the item at an index.
|
|
/// @param index The index we're looking for.
|
|
/// @return The item at the given index.
|
|
inline reference operator[](size_t index) { return At(index); }
|
|
/// @brief Returns the item at an index.
|
|
/// @param index The index we're looking for.
|
|
/// @return The item at the given index.
|
|
inline const const_reference& operator[](size_t index) const { return At(index); }
|
|
|
|
/// @brief Returns the number of items in the list.
|
|
/// @return The number of items in the list.
|
|
inline size_t Count() const noexcept { return _vector.size(); }
|
|
/// @brief Reserve a number of spaces for the list to use.
|
|
/// @param size A value that is greater or equal to the current size of the list.
|
|
inline void Reserve(size_t size) { _vector.reserve(size); }
|
|
/// @brief Resize the list to a certain size.
|
|
/// @param size The new size the list should be.
|
|
inline void Resize(size_t size) { _vector.resize(size); }
|
|
/// @brief Resize the list to a certain size.
|
|
/// @param size The new size the list should be.
|
|
/// @param defaultValue The default value new items should be instantiated with.
|
|
inline void Resize(size_t size, const ValueT& defaultValue) { _vector.resize(size, defaultValue); }
|
|
|
|
/// @brief Remove an item at an index.
|
|
/// @param index The index to remove the item from.
|
|
inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); }
|
|
|
|
/// @brief returns an iterator to the beginning of the specified bucket
|
|
iterator begin() noexcept { return _vector.begin(); }
|
|
/// @brief returns an iterator to the beginning of the specified bucket
|
|
const_iterator begin() const noexcept { return _vector.begin(); }
|
|
|
|
/// @brief returns an iterator to the end of the specified bucket
|
|
iterator end() noexcept { return _vector.end(); }
|
|
/// @brief returns an iterator to the end of the specified bucket
|
|
const_iterator end() const noexcept { return _vector.end(); }
|
|
|
|
/// @brief Return a raw pointer to the beginning of the list.
|
|
/// @return A raw array pointer to the beginning of the list.
|
|
const ValueT* nullable RawData() const noexcept { return _vector.data(); }
|
|
|
|
/// @brief Returns a std::vector representation of the current list.
|
|
/// @return A std::vector representation of the current list.
|
|
const std::vector<ValueT>& GetStdList() const noexcept { return _vector; }
|
|
/// @brief Returns a std::vector representation of the current list.
|
|
/// @return A std::vector representation of the current list.
|
|
std::vector<ValueT>& GetStdList() noexcept { return _vector; }
|
|
|
|
/// @brief Returns a raw pointer to the current list.
|
|
/// @return A raw pointer to the current list.
|
|
inline const List<ValueT>* non_null GetListPointer() const { return this; }
|
|
/// @brief Returns a raw pointer to the current list.
|
|
/// @return A raw pointer to the current list.
|
|
inline List<ValueT>* non_null GetListPointer() { return this; }
|
|
};
|
|
}
|
|
|
|
#endif // ARBUTILS_LIST_HPP
|