51 lines
1.3 KiB
C
51 lines
1.3 KiB
C
|
#ifndef ARRAY_H
|
||
|
#define ARRAY_H
|
||
|
|
||
|
#include "Exception.h"
|
||
|
#include "Object.h"
|
||
|
#include "RandomIterator.h"
|
||
|
|
||
|
namespace Kylin {
|
||
|
|
||
|
template <typename T>
|
||
|
class Array : public Object {
|
||
|
public:
|
||
|
using Iterator = RandomIterator<T>;
|
||
|
const T &at(size_t index) const {
|
||
|
if (index >= size()) {
|
||
|
THROW_EXCEPTION(IndexOutOfBoundsException, "Index out of bounds...");
|
||
|
}
|
||
|
return m_array[index];
|
||
|
}
|
||
|
|
||
|
size_t indexOf(const T &value) const {
|
||
|
auto size_ = size();
|
||
|
for (size_t i = 0; i < size_; i++) {
|
||
|
if (m_array[i] == value) return i;
|
||
|
}
|
||
|
return static_cast<size_t>(-1);
|
||
|
}
|
||
|
|
||
|
virtual size_t size() const noexcept = 0;
|
||
|
size_t length() const noexcept { return size(); }
|
||
|
|
||
|
T &operator[](size_t index) {
|
||
|
if (index >= size()) {
|
||
|
THROW_EXCEPTION(IndexOutOfBoundsException, "Index out of bounds...");
|
||
|
}
|
||
|
return m_array[index];
|
||
|
}
|
||
|
|
||
|
const T &operator[](size_t index) const { return const_cast<Array *>(this)->operator[](index); }
|
||
|
|
||
|
T *array() noexcept { return m_array; }
|
||
|
|
||
|
Iterator begin() { return Iterator(m_array); }
|
||
|
Iterator end() { return Iterator(m_array + size()); }
|
||
|
|
||
|
protected:
|
||
|
T *m_array = nullptr;
|
||
|
};
|
||
|
} // namespace Kylin
|
||
|
#endif // ARRAY_H
|