Add Set function to List.

This commit is contained in:
Deukhoofd 2020-04-22 13:21:06 +02:00
parent 195043d4c5
commit 8306c14163
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 17 additions and 6 deletions

View File

@ -17,7 +17,7 @@ namespace Arbutils::Collections {
public: public:
List() : _vector() {} List() : _vector() {}
explicit List(size_t capacity) : _vector(capacity) {} explicit List(size_t capacity) : _vector(capacity) {}
List(const std::initializer_list<ValueT>& l) : _vector(l) {} explicit List(const std::initializer_list<ValueT>& l) : _vector(l) {}
List(const ValueT* begin, const ValueT* end) : _vector(begin, end) {} List(const ValueT* begin, const ValueT* end) : _vector(begin, end) {}
inline void Clear() { _vector.clear(); } inline void Clear() { _vector.clear(); }
@ -44,30 +44,41 @@ namespace Arbutils::Collections {
return _vector.at(index); return _vector.at(index);
} }
inline bool Contains(ValueT value) const { inline 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
_vector[index] = value;
}
inline bool Contains(const ValueT& value) const {
return std::find(_vector.begin(), _vector.end(), value) != _vector.end(); 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. /// 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. /// \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. /// \return The index of the first occurrence of the value in the list, or -1 if none is found.
inline size_t IndexOf(ValueT value) const { inline size_t IndexOf(const ValueT& value) const {
auto it = std::find(_vector.begin(), _vector.end(), value); auto it = std::find(_vector.begin(), _vector.end(), value);
if (it == _vector.end()) if (it == _vector.end())
return -1; return -1;
return std::distance(_vector.begin(), it); return std::distance(_vector.begin(), it);
} }
inline void Append(ValueT value) { _vector.push_back(value); } inline void Append(const ValueT& value) { _vector.push_back(value); }
inline void Insert(size_t index, ValueT value) { _vector.insert(index, value); } inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); }
inline reference operator[](size_t index) { return At(index); } inline reference operator[](size_t index) { return At(index); }
inline const_reference operator[](size_t index) const { return At(index); } inline const_reference operator[](size_t index) const { return At(index); }
inline size_t Count() const { return _vector.size(); } inline size_t Count() const { return _vector.size(); }
inline void Resize(size_t size) { _vector.resize(size); } inline void Resize(size_t size) { _vector.resize(size); }
inline void Resize(size_t size, ValueT defaultValue) { _vector.resize(size, defaultValue); } inline void Resize(size_t size, const ValueT& defaultValue) { _vector.resize(size, defaultValue); }
inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); } inline void Remove(size_t index) { _vector.erase(_vector.begin() + index); }