Kylin/DataStructure/List.h
2023-12-27 10:29:16 +08:00

43 lines
1.2 KiB
C++

#ifndef LIST_H
#define LIST_H
#include "Object.h"
namespace Kylin {
template <typename T>
class List : public Object {
public:
static constexpr size_t npos = static_cast<size_t>(-1);
virtual void append(const T &value) = 0;
const T &at(size_t index) const { return const_cast<List *>(this)->operator[](index); }
virtual inline void push_back(const T &value) { append(value); }
/**
* @brief Inserts value at index position i in the list.
* If i >= size(), the value is appended to the list.
*/
virtual void insert(size_t index, const T &value) = 0;
virtual void removeAt(size_t index) = 0;
virtual void clear() = 0;
virtual size_t size() const noexcept = 0;
size_t length() const noexcept { return size(); }
bool empty() const noexcept { return size() == 0; }
virtual size_t indexOf(const T &value, size_t from = 0) const = 0;
virtual T &last() = 0;
const T &last() const { return const_cast<List *>(this)->last(); }
virtual T &operator[](size_t index) = 0;
const T &operator[](size_t index) const { return const_cast<List *>(this)->operator[](index); }
};
} // namespace Kylin
#endif // LIST_H