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>
|
|
|
|
|
|
|
|
namespace Arbutils::Collections {
|
|
|
|
template <class ValueT> class List {
|
|
|
|
private:
|
|
|
|
std::vector<ValueT> _vector;
|
|
|
|
using iterator = typename std::vector<ValueT>::iterator;
|
|
|
|
|
|
|
|
public:
|
|
|
|
List() : _vector() {}
|
|
|
|
explicit List(size_t capacity) : _vector(capacity) {}
|
|
|
|
List(const std::initializer_list<ValueT>& l) : _vector(l) {}
|
|
|
|
|
2020-03-22 16:32:31 +00:00
|
|
|
inline void Clear() { _vector.clear(); }
|
2020-03-22 16:14:51 +00:00
|
|
|
|
2020-03-22 16:09:25 +00:00
|
|
|
inline const ValueT& At(size_t index) const {
|
|
|
|
#ifndef NO_ASSERT
|
|
|
|
if (index >= _vector.size()) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Index " << index << " is out of bounds.";
|
|
|
|
throw std::logic_error(ss.str());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return _vector.at(index);
|
|
|
|
}
|
|
|
|
inline ValueT& At(size_t index) {
|
|
|
|
#ifndef NO_ASSERT
|
|
|
|
if (index >= _vector.size()) {
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << "Index " << index << " is out of bounds.";
|
|
|
|
throw std::logic_error(ss.str());
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return _vector.at(index);
|
|
|
|
}
|
|
|
|
|
2020-03-22 16:32:31 +00:00
|
|
|
inline bool Contains(ValueT value) { return std::find(_vector.begin(), _vector.end(), value) != _vector.end(); }
|
2020-03-22 16:09:25 +00:00
|
|
|
|
|
|
|
inline void Append(ValueT value) { _vector.push_back(value); }
|
|
|
|
|
|
|
|
inline void Insert(size_t index, ValueT value) { _vector.insert(index, value); }
|
|
|
|
|
|
|
|
inline ValueT& operator[](size_t index) { return At(index); }
|
|
|
|
inline const ValueT& operator[](size_t index) const { return At(index); }
|
|
|
|
|
2020-03-22 16:35:57 +00:00
|
|
|
inline size_t Count() const { return _vector.size(); }
|
2020-03-22 16:09:25 +00:00
|
|
|
|
|
|
|
iterator begin() { return _vector.begin(); }
|
|
|
|
iterator begin() const { return _vector.begin(); }
|
|
|
|
|
|
|
|
iterator end() { return _vector.end(); }
|
|
|
|
iterator end() const { return _vector.end(); }
|
2020-03-22 16:32:31 +00:00
|
|
|
|
|
|
|
const ValueT* RawData() const { return _vector.data(); }
|
2020-03-22 16:09:25 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // ARBUTILS_LIST_HPP
|