#ifndef ARBUTILS_LIST_HPP #define ARBUTILS_LIST_HPP #include #include #include #include namespace ArbUt { template class List { private: std::vector _vector; using reference = typename std::vector::reference; using const_reference = typename std::vector::const_reference; using iterator = typename std::vector::iterator; using const_iterator = typename std::vector::const_iterator; public: List() noexcept : _vector() {} explicit List(size_t capacity) : _vector() { _vector.reserve(capacity); } List(const std::initializer_list& l) noexcept : _vector(l) {} List(const ValueT* begin, const ValueT* end) noexcept : _vector(begin, end) {} inline void Clear() noexcept { _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 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 const const_reference& Set(size_t index, const ValueT& value) { #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 const auto& prev = _vector.at(index); _vector[index] = value; return prev; } 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. inline size_t IndexOf(const ValueT& value) const noexcept { const auto& it = std::find(_vector.begin(), _vector.end(), value); if (it == _vector.end()) return -1; return std::distance(_vector.begin(), it); } inline void Append(const ValueT& value) { _vector.push_back(value); } template inline void CreateBack(parameters... pars) { _vector.emplace_back(pars...); } inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); } inline reference operator[](size_t index) { return At(index); } inline const const_reference& operator[](size_t index) const { return At(index); } inline size_t Count() const noexcept { return _vector.size(); } inline void Reserve(size_t size) { _vector.reserve(size); } inline void Resize(size_t size) { _vector.resize(size); } inline void Resize(size_t size, const ValueT& defaultValue) { _vector.resize(size, defaultValue); } inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); } iterator begin() noexcept { return _vector.begin(); } const_iterator begin() const noexcept { return _vector.begin(); } iterator end() noexcept { return _vector.end(); } const_iterator end() const noexcept { return _vector.end(); } const ValueT* RawData() const noexcept { return _vector.data(); } const std::vector& GetStdList() const noexcept { return _vector; } std::vector& GetStdList() noexcept { return _vector; } }; } #endif // ARBUTILS_LIST_HPP