77 lines
2.6 KiB
C++
77 lines
2.6 KiB
C++
#ifndef ARBUTILS_LIST_HPP
|
|
#define ARBUTILS_LIST_HPP
|
|
#include <algorithm>
|
|
#include <sstream>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
|
|
namespace Arbutils::Collections {
|
|
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:
|
|
List() : _vector() {}
|
|
explicit List(size_t capacity) : _vector(capacity) {}
|
|
List(const std::initializer_list<ValueT>& l) : _vector(l) {}
|
|
List(const ValueT* begin, const ValueT* end) : _vector(begin, end) {}
|
|
|
|
inline void Clear() { _vector.clear(); }
|
|
|
|
inline reference At(size_t index) {
|
|
#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
|
|
return _vector.at(index);
|
|
}
|
|
|
|
inline const_reference At(size_t index) const {
|
|
#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
|
|
return _vector.at(index);
|
|
}
|
|
|
|
inline bool Contains(ValueT value) const {
|
|
return std::find(_vector.begin(), _vector.end(), value) != _vector.end();
|
|
}
|
|
|
|
inline void Append(ValueT value) { _vector.push_back(value); }
|
|
|
|
inline void Insert(size_t index, ValueT value) { _vector.insert(index, value); }
|
|
|
|
inline reference operator[](size_t index) { return At(index); }
|
|
inline const_reference operator[](size_t index) const { return At(index); }
|
|
|
|
inline size_t Count() const { return _vector.size(); }
|
|
inline void Resize(size_t size) { _vector.resize(size); }
|
|
inline void Resize(size_t size, ValueT defaultValue) { _vector.resize(size, defaultValue); }
|
|
|
|
inline void Remove(size_t index){
|
|
_vector.erase(_vector.begin() + index);
|
|
}
|
|
|
|
iterator begin() { return _vector.begin(); }
|
|
const_iterator begin() const { return _vector.begin(); }
|
|
|
|
iterator end() { return _vector.end(); }
|
|
const_iterator end() const { return _vector.end(); }
|
|
|
|
const ValueT* RawData() const { return _vector.data(); }
|
|
};
|
|
}
|
|
|
|
#endif // ARBUTILS_LIST_HPP
|