Magisk/native/jni/utils/include/array.h

179 lines
3.3 KiB
C
Raw Normal View History

2018-10-25 01:08:06 +00:00
#pragma once
#include <stdlib.h>
2018-11-07 07:10:38 +00:00
#include "cpputils.h"
2018-10-25 01:08:06 +00:00
template <class T>
class Array {
public:
Array() : _data(0), _size(0), _capacity(0) {}
~Array() { delete []_data; }
class iterator {
friend class Array;
public:
iterator(T* n= 0): _node(n) {}
iterator(const iterator& i): _node(i._node) {}
~iterator() {} // Should NOT delete _node
const T& operator * () const { return (*_node); }
T& operator * () { return (*_node); }
iterator& operator ++ () {
++_node;
return (*this);
}
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
iterator operator ++ (int) {
iterator temp = *this;
++_node;
return temp;
}
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
iterator& operator -- () {
--_node;
return (*this);
}
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
iterator operator -- (int) {
iterator temp = *this;
--_node;
return temp;
}
iterator operator + (int i) const {
iterator temp = *this;
temp += i;
return temp;
}
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
iterator& operator += (int i) {
_node += i;
return (*this);
}
iterator& operator = (const iterator& i) {
_node = i._node;
return (*this);
}
bool operator != (const iterator& i) const {
return _node != i._node;
}
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
bool operator == (const iterator& i) const { return !(*this != i); }
private:
T* _node;
};
2018-11-04 08:38:06 +00:00
T& operator = (const T& a) {
_size = a._size;
_capacity = a._capacity;
2018-11-05 19:37:47 +00:00
if (_capacity) {
_data = new T[_capacity];
for(int i = 0; i < _size; ++i)
2018-11-07 07:10:38 +00:00
_data[i] = utils::move(a[i]);
2018-11-05 19:37:47 +00:00
}
2018-11-04 08:38:06 +00:00
return *this;
}
2018-10-25 01:08:06 +00:00
iterator begin() const { return iterator(_data); }
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
iterator end() const { return iterator(_data + _size); }
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
bool empty() const { return !_size; }
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
size_t size() const { return _size; }
T& operator [] (size_t i) { return _data[i]; }
2018-11-03 03:56:15 +00:00
2018-10-25 01:08:06 +00:00
const T& operator [] (size_t i) const { return _data[i]; }
const T& back() const { return _data[_size - 1]; }
void push_back(const T& x) {
if(_size == _capacity)
expand();
_data[_size] = x;
++_size;
}
2018-11-03 03:56:15 +00:00
void push_back(T&& x) {
if(_size == _capacity)
expand();
2018-11-07 07:10:38 +00:00
_data[_size] = utils::move(x);
2018-11-03 03:56:15 +00:00
++_size;
}
2018-10-25 01:08:06 +00:00
void pop_front() { erase(begin()); }
void pop_back() { if(_size) --_size; }
bool erase(iterator pos) {
T* d = pos._node;
if (_size == 0 || d < _data || d >= _data + _size)
return false;
for (; d < _data + _size - 1; ++d)
2018-11-07 07:10:38 +00:00
*d = utils::move(*(d + 1));
2018-10-25 01:08:06 +00:00
--_size;
return true;
}
bool erase(const T& x) {
for (T* i = _data; i < _data + _size; ++i) {
if(*i == x) {
erase(iterator(i));
return true;
}
}
return false;
}
2018-11-07 07:10:38 +00:00
void clear(bool dealloc = false) {
_size = 0;
if (dealloc) {
_capacity = 0;
delete [] _data;
_data = nullptr;
}
}
2018-10-25 01:08:06 +00:00
2018-11-07 07:10:38 +00:00
void sort() {
2018-10-25 01:08:06 +00:00
qsort(_data, _size, sizeof(T), compare);
}
2018-11-03 08:03:11 +00:00
T* data() { return _data; }
const T* data() const { return _data; }
2018-10-25 01:08:06 +00:00
// void reserve(size_t n) { ... }
// void resize(size_t n) { ... }
private:
T* _data;
size_t _size; // number of valid elements
size_t _capacity; // max number of elements
static int(*_cmp)(T&, T&);
static int compare(const void *a, const void *b) {
return _cmp ? _cmp(*((T*) a), *((T*) b)) : 0;
}
void expand() {
if (_capacity == 0)
_capacity = 1;
else
_capacity *= 2;
T* temp = _data;
_data = new T[_capacity];
2018-11-04 08:38:06 +00:00
for(int i = 0; i < _size; ++i)
2018-11-07 07:10:38 +00:00
_data[i] = utils::move(temp[i]);
2018-10-25 01:08:06 +00:00
delete [] temp;
}
};
template<class T>
int(* Array<T>::_cmp)(T&, T&) = nullptr;