125 lines
2.8 KiB
C
125 lines
2.8 KiB
C
|
|
#ifndef CCArray_H
|
||
|
|
#define CCArray_H
|
||
|
|
#pragma once
|
||
|
|
|
||
|
|
#include "CC.h"
|
||
|
|
|
||
|
|
template<typename T>
|
||
|
|
class CCArray
|
||
|
|
{
|
||
|
|
private:
|
||
|
|
T* m_data; // 指针指向数组
|
||
|
|
size_t capacity; // 数组的最大容量
|
||
|
|
size_t count; // 当前元素数量
|
||
|
|
|
||
|
|
public:
|
||
|
|
|
||
|
|
|
||
|
|
private:
|
||
|
|
void resize() {
|
||
|
|
size_t new_capacity = capacity + sizeof(T) + sizeof(T);
|
||
|
|
m_data = (T*) realloc(m_data, new_capacity);
|
||
|
|
memset(m_data + capacity, 0, (new_capacity - capacity));
|
||
|
|
capacity = new_capacity - sizeof(T);
|
||
|
|
}
|
||
|
|
public:
|
||
|
|
CCArray() : m_data(nullptr), capacity(0), count(0) {
|
||
|
|
m_data = (T*) calloc(1,sizeof(T));
|
||
|
|
}
|
||
|
|
~CCArray(){
|
||
|
|
free(m_data);
|
||
|
|
}
|
||
|
|
// 添加元素到数组末尾
|
||
|
|
void add(const T& element){
|
||
|
|
if (count == capacity){
|
||
|
|
resize();
|
||
|
|
}
|
||
|
|
m_data[count++] = element;
|
||
|
|
}
|
||
|
|
void add(const T* element){
|
||
|
|
if (count == capacity){
|
||
|
|
resize();
|
||
|
|
}
|
||
|
|
m_data[count++] = element;
|
||
|
|
}
|
||
|
|
// 访问指定索引处的元素
|
||
|
|
T* getPtr(size_t index){
|
||
|
|
if (index >= count){
|
||
|
|
CC::ErrorPrintln("Index out of range");
|
||
|
|
}
|
||
|
|
return &m_data[index];
|
||
|
|
}
|
||
|
|
T& get(size_t index){
|
||
|
|
if (index >= count){
|
||
|
|
throw std::out_of_range("Index out of range");
|
||
|
|
}
|
||
|
|
return m_data[index];
|
||
|
|
}
|
||
|
|
[[nodiscard]] size_t size() const {
|
||
|
|
return count;
|
||
|
|
}
|
||
|
|
void remove(size_t index) {
|
||
|
|
if (index >= count){
|
||
|
|
CC::ErrorPrintln("Index out of range");
|
||
|
|
}
|
||
|
|
for (size_t i = index; i <= count - 1; ++i){
|
||
|
|
if(i < count - 1){
|
||
|
|
m_data[i] = m_data[i + 1];
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
m_data[i] = {};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
--count;
|
||
|
|
}
|
||
|
|
void clear() {
|
||
|
|
free(m_data);
|
||
|
|
m_data = nullptr;
|
||
|
|
capacity = 0;
|
||
|
|
count = 0;
|
||
|
|
}
|
||
|
|
T* data() {
|
||
|
|
return m_data;
|
||
|
|
}
|
||
|
|
size_t find(T element) {
|
||
|
|
for (size_t i = 0; i < count; ++i){
|
||
|
|
if (m_data[i] == element){
|
||
|
|
return i;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return -1;
|
||
|
|
}
|
||
|
|
bool revise(int index,T element) {
|
||
|
|
if (index >= count){
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
m_data[index] = element;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
void CopyAll(CCArray &array){
|
||
|
|
for (int i = 0; i < array.count; ++i) {
|
||
|
|
if(count == capacity){
|
||
|
|
add(array.get(i));
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
revise(i,array.get(i));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
void Copy(CCArray &array,int start,int end){
|
||
|
|
for (int i = start; i < end; ++i) {
|
||
|
|
if(count == capacity){
|
||
|
|
add(array.get(i));
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
revise(i,array.get(i));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
};
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
#endif
|