2020-03-22 16:09:25 +00:00
|
|
|
#ifndef ARBUTILS_LIST_HPP
|
|
|
|
#define ARBUTILS_LIST_HPP
|
2020-03-22 16:32:31 +00:00
|
|
|
#include <algorithm>
|
2020-03-22 16:09:25 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
|
|
|
|
2020-05-26 15:36:39 +00:00
|
|
|
namespace ArbUt {
|
2020-03-22 16:09:25 +00:00
|
|
|
template <class ValueT> class List {
|
|
|
|
private:
|
|
|
|
std::vector<ValueT> _vector;
|
2020-03-22 17:44:50 +00:00
|
|
|
using reference = typename std::vector<ValueT>::reference;
|
|
|
|
using const_reference = typename std::vector<ValueT>::const_reference;
|
2020-03-22 16:09:25 +00:00
|
|
|
using iterator = typename std::vector<ValueT>::iterator;
|
2020-03-22 17:15:13 +00:00
|
|
|
using const_iterator = typename std::vector<ValueT>::const_iterator;
|
2020-03-22 16:09:25 +00:00
|
|
|
|
|
|
|
public:
|
2020-06-20 15:45:41 +00:00
|
|
|
List() noexcept : _vector() {}
|
2020-04-24 17:13:55 +00:00
|
|
|
explicit List(size_t capacity) : _vector() { _vector.reserve(capacity); }
|
2020-06-20 15:45:41 +00:00
|
|
|
List(const std::initializer_list<ValueT>& l) noexcept : _vector(l) {}
|
|
|
|
List(const ValueT* begin, const ValueT* end) noexcept : _vector(begin, end) {}
|
2020-03-22 16:09:25 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
inline void Clear() noexcept { _vector.clear(); }
|
2020-03-22 16:14:51 +00:00
|
|
|
|
2020-03-22 17:44:50 +00:00
|
|
|
inline reference At(size_t index) {
|
2020-03-22 16:09:25 +00:00
|
|
|
#ifndef NO_ASSERT
|
2020-03-22 17:21:29 +00:00
|
|
|
if (index >= _vector.size() || index < 0) {
|
2020-03-22 16:09:25 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Index " << index << " is out of bounds.";
|
|
|
|
throw std::logic_error(ss.str());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return _vector.at(index);
|
|
|
|
}
|
2020-03-22 17:34:05 +00:00
|
|
|
|
2020-04-24 15:47:49 +00:00
|
|
|
inline const const_reference& At(size_t index) const {
|
2020-03-22 16:09:25 +00:00
|
|
|
#ifndef NO_ASSERT
|
2020-03-22 17:21:29 +00:00
|
|
|
if (index >= _vector.size() || index < 0) {
|
2020-03-22 16:09:25 +00:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Index " << index << " is out of bounds.";
|
|
|
|
throw std::logic_error(ss.str());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return _vector.at(index);
|
|
|
|
}
|
|
|
|
|
2020-04-24 15:47:49 +00:00
|
|
|
inline const const_reference& Set(size_t index, const ValueT& value) {
|
2020-04-22 11:21:06 +00:00
|
|
|
#ifndef NO_ASSERT
|
|
|
|
if (index >= _vector.size() || index < 0) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Index " << index << " is out of bounds.";
|
|
|
|
throw std::logic_error(ss.str());
|
|
|
|
}
|
|
|
|
#endif
|
2020-04-24 15:47:49 +00:00
|
|
|
const auto& prev = _vector.at(index);
|
2020-04-22 11:21:06 +00:00
|
|
|
_vector[index] = value;
|
2020-04-22 11:22:20 +00:00
|
|
|
return prev;
|
2020-04-22 11:21:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline bool Contains(const ValueT& value) const {
|
2020-03-22 17:15:13 +00:00
|
|
|
return std::find(_vector.begin(), _vector.end(), value) != _vector.end();
|
|
|
|
}
|
2020-03-22 16:09:25 +00:00
|
|
|
|
2020-03-24 21:56:40 +00:00
|
|
|
/// 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.
|
2020-06-20 15:45:41 +00:00
|
|
|
inline size_t IndexOf(const ValueT& value) const noexcept {
|
2020-04-24 15:47:49 +00:00
|
|
|
const auto& it = std::find(_vector.begin(), _vector.end(), value);
|
2020-03-24 21:56:40 +00:00
|
|
|
if (it == _vector.end())
|
|
|
|
return -1;
|
|
|
|
return std::distance(_vector.begin(), it);
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:21:06 +00:00
|
|
|
inline void Append(const ValueT& value) { _vector.push_back(value); }
|
2020-08-09 08:47:41 +00:00
|
|
|
template <class... parameters> inline void CreateBack(parameters... pars) { _vector.emplace_back(pars...); }
|
2020-03-22 16:09:25 +00:00
|
|
|
|
2020-04-22 11:21:06 +00:00
|
|
|
inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); }
|
2020-03-22 16:09:25 +00:00
|
|
|
|
2020-03-22 17:44:50 +00:00
|
|
|
inline reference operator[](size_t index) { return At(index); }
|
2020-04-24 15:47:49 +00:00
|
|
|
inline const const_reference& operator[](size_t index) const { return At(index); }
|
2020-03-22 16:09:25 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
inline size_t Count() const noexcept { return _vector.size(); }
|
2020-04-24 16:47:17 +00:00
|
|
|
inline void Reserve(size_t size) { _vector.reserve(size); }
|
2020-03-22 17:01:39 +00:00
|
|
|
inline void Resize(size_t size) { _vector.resize(size); }
|
2020-04-22 11:21:06 +00:00
|
|
|
inline void Resize(size_t size, const ValueT& defaultValue) { _vector.resize(size, defaultValue); }
|
2020-03-22 16:09:25 +00:00
|
|
|
|
2020-03-24 21:56:40 +00:00
|
|
|
inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); }
|
2020-03-22 18:08:38 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
iterator begin() noexcept { return _vector.begin(); }
|
|
|
|
const_iterator begin() const noexcept { return _vector.begin(); }
|
2020-03-22 16:09:25 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
iterator end() noexcept { return _vector.end(); }
|
|
|
|
const_iterator end() const noexcept { return _vector.end(); }
|
2020-03-22 16:32:31 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
const ValueT* RawData() const noexcept { return _vector.data(); }
|
2020-03-29 16:49:10 +00:00
|
|
|
|
2020-06-20 15:45:41 +00:00
|
|
|
const std::vector<ValueT>& GetStdList() const noexcept { return _vector; }
|
|
|
|
std::vector<ValueT>& GetStdList() noexcept { return _vector; }
|
2020-03-22 16:09:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ARBUTILS_LIST_HPP
|