Lots of documentation.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-09-22 19:32:40 +02:00
parent 95f1e818e7
commit 31b63d56db
15 changed files with 2724 additions and 33 deletions

View File

@@ -5,6 +5,7 @@
#include "../Assert.hpp"
namespace ArbUt {
/// @brief Wrapper around unordered_map, allowing safer access and adding several helper methods.
template <class KeyT, class ValueT> class Dictionary {
private:
std::unordered_map<KeyT, ValueT> _map;
@@ -14,11 +15,15 @@ namespace ArbUt {
public:
Dictionary() : _map() {}
/// @brief Initialises a dictionary with a certain capacity.
explicit Dictionary(size_t capacity) : _map(capacity) {}
explicit Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l) {}
/// @brief Initialises a dictionary from an initializer_list.
Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l) {}
/// @brief Removes all items from the dictionary.
inline void Clear() noexcept { _map.clear(); }
/// @brief Inserts a new item in the dictionary. This will throw if the dictionary already contains the key.
inline void Insert(const KeyT& key, const ValueT& value) {
[[maybe_unused]] const auto& v = _map.insert({key, value});
#ifndef NO_ASSERT
@@ -27,8 +32,10 @@ namespace ArbUt {
#endif
}
/// @brief Sets a key in the dictionary to a specific value.
inline void Set(const KeyT& key, const ValueT& value) { _map[key] = value; }
/// @brief Gets a value from the dictionary.
[[nodiscard]] inline ValueT& Get(const KeyT& key) {
#ifndef NO_ASSERT
return _map.at(key);
@@ -36,9 +43,11 @@ namespace ArbUt {
return _map[key];
#endif
}
/// @brief Gets a value from the dictionary.
[[nodiscard]] inline const ValueT& Get(const KeyT& key) const { return _map.at(key); }
/// @brief Try to get an item from the dictionary using a key. Returns false if no item is found, and out will
/// not be touched in that case.
inline bool TryGet(const KeyT& key, ValueT& out) const noexcept {
const auto& find = _map.find(key);
if (find == _map.end()) {
@@ -48,22 +57,33 @@ namespace ArbUt {
return true;
}
/// @brief Removes an item with a certain key from the dictionary
inline void Remove(const KeyT& key) { _map.erase(key); }
/// @brief Returns the number of items in the dictionary.
[[nodiscard]] inline size_t Count() const noexcept { return _map.size(); }
/// @brief Checks whether the dictionary contains a specific key.
inline bool Has(const KeyT& key) const noexcept { return _map.find(key) != _map.end(); }
/// @brief Indexing operator to get a value from the dictionary using a key.
inline ValueT& operator[](const KeyT& key) { return Get(key); }
/// @brief Indexing operator to get a value from the dictionary using a key.
inline const ValueT& operator[](const KeyT& key) const { return Get(key); }
/// @brief returns an iterator to the beginning of the specified bucket
iterator begin() noexcept { return _map.begin(); }
/// @brief returns an iterator to the beginning of the specified bucket
const_iterator begin() const noexcept { return _map.begin(); }
/// @brief returns an iterator to the end of the specified bucket
iterator end() noexcept { return _map.end(); }
/// @brief returns an iterator to the end of the specified bucket
const_iterator end() const { return _map.end(); }
/// @brief returns the backing unordered_map.
const std::unordered_map<KeyT, ValueT>& GetStdMap() const noexcept { return _map; }
/// @brief returns the backing unordered_map.
std::unordered_map<KeyT, ValueT>& GetStdMap() noexcept { return _map; }
};
}

View File

@@ -7,6 +7,7 @@
#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;
@@ -17,12 +18,23 @@ namespace ArbUt {
public:
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* begin, const ValueT* 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()) {
@@ -34,6 +46,9 @@ namespace ArbUt {
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()) {
@@ -45,6 +60,10 @@ namespace ArbUt {
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()) {
@@ -58,13 +77,16 @@ namespace ArbUt {
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();
}
/// 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.
/// @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 size_t IndexOf(const ValueT& value) const noexcept {
const auto& it = std::find(_vector.begin(), _vector.end(), value);
if (it == _vector.end())
@@ -72,30 +94,66 @@ namespace ArbUt {
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* 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; }
};
}