这个主要是给我自己看的,记录一下语法的使用,也可以参照了解,我已经用C++和OC代码测试过这两个文件了,如果写的有什么不足,欢迎指出
文件Array.hpp
#ifndef Array_hpp
#define Array_hpp
#include <stdio.h>
#include <iostream>
using namespace std;
namespace GYJ{
template <class T>
class Array {
private:
static const int DEFAULT_CAPACITY = 10;
int m_size; //元素个数
T *arr; //数组
int m_capacity; //容量
inline void cheakIndexBeforeAdd(int index); //添加时的验证index
inline void cheakIndexBeforeSearch(int index); //查找时的验证index
void expandCapacity(int size); //扩容
void reduceCapacity(); //缩容
public:
Array(); //构造函数
Array(const int capacity); //带参构造函数
~Array(); //析构函数
int size(); //获取数组元素个数
void add(T item); //添加元素
T removeIndex(int index); //移除元素
T elmentAtIndex(int index);
void insert(int index,T item); //插入元素
T operator[](int index); //下标
};
template <class T>
Array<T>::Array() :Array(DEFAULT_CAPACITY){}
template <class T>
Array<T>::Array(const int capacity){
m_capacity = capacity < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : capacity;
m_size = 0;
arr = new T[m_capacity];
}
template <class T>
Array<T>::~Array(){
if (arr == NULL) return;
delete[] arr;
arr = NULL;
}
template <class T>
int Array<T>::size(){
return m_size;
}
template <class T>
void Array<T>::add(T item){
insert(m_size, item);
}
template <class T>
T Array<T>::removeIndex(int index){
cheakIndexBeforeSearch(index);
T tmp = arr[index];
for (int i = index + 1; i < m_size ; i++) {
arr[i-1] = arr[i];
}
m_size--;
reduceCapacity();
return tmp;
}
template <class T>
void Array<T>::insert(int index,T item){
cheakIndexBeforeAdd(index);
expandCapacity(m_size + 1);
for (int i = m_size - 1; i > index; i--) {
arr[i+1] = arr[i];
}
arr[index] = item;
m_size++;
}
template <class T>
T Array<T>::operator[](int index){
cheakIndexBeforeSearch(index);
return arr[index];
}
template <class T>
T Array<T>::elmentAtIndex(int index){
cheakIndexBeforeSearch(index);
return arr[index];
}
template <class T>
inline void Array<T>::cheakIndexBeforeAdd(int index){
if (index < 0 || index > m_size) {
throw "数组越界";
}
}
template <class T>
inline void Array<T>::cheakIndexBeforeSearch(int index){
if (index < 0 || index > m_size - 1) {
throw "数组越界";
}
}
template <class T>
void Array<T>::expandCapacity(int size){
if(size <= m_capacity) return;
m_capacity *= 1.8;
T *tempArr = new T[m_capacity];
for (int i = 0; i < m_size; i++) {
tempArr[i] = arr[i];
}
delete [] arr;
arr = tempArr;
}
template <class T>
void Array<T>::reduceCapacity(){
if (m_size > (m_capacity >> 1)) return;
m_capacity = (m_capacity>>1) < DEFAULT_CAPACITY ? DEFAULT_CAPACITY : m_capacity>>1;
T *tmpArr = new T[m_capacity];
for (int i = 0; i < m_size; i++) {
tmpArr[i] = arr[i];
}
delete [] arr;
arr = tmpArr;
}
template <class T>
ostream& operator<<(ostream &os,Array<T> &arr){
os << "[";
int size = arr.size();
for (int i = 0; i < size; i++) {
if(i != 0){
os << ",";
}
try{
os << arr[i];
}catch(...){
}
}
os << "]";
return os;
}
}
#endif
头文件GYJMutableArray.h,供OC测试和使用的
NS_ASSUME_NONNULL_BEGIN
@interface GYJMutableArray : NSObject
- (int)size;
- (GYJMutableArray *)initWithCapacity:(NSInteger)capacity;
- (void)addElement:(NSObject *)element;
- (void)insertElement:(NSObject *)element atIndex:(NSInteger)index;
- (NSObject *)elementAtIndex:(NSInteger)index;
- (NSObject *)removeAtIndex:(NSInteger)index;
@end
NS_ASSUME_NONNULL_END
实现文件GYJMutableArray.mm,后缀名为mm是可以兼容c++和OC代码的
#import "GYJMutableArray.h"
#include "Array.hpp"
using namespace GYJ;
@interface GYJMutableArray()
{
@private
Array<NSObject *> *_mArray;
}
@end
@implementation GYJMutableArray
- (instancetype)init {
if (self = [super init]) {
_mArray = new Array<NSObject *>();
}
return self;
}
- (GYJMutableArray *)initWithCapacity:(NSInteger)capacity {
if (self = [super init]) {
_mArray = new Array<NSObject *>((int)capacity);
}
return self;
}
- (void)addElement:(NSObject *)element {
_mArray->add(element);
}
- (void)insertElement:(NSObject *)element atIndex:(NSInteger)index {
_mArray->insert((int)index,element);
}
- (NSObject *)elementAtIndex:(NSInteger)index {
NSObject *obj = _mArray->elmentAtIndex((int)index);;
return obj;
}
- (NSObject *)removeAtIndex:(NSInteger)index {
NSObject *obj = _mArray->removeIndex((int)index);
return obj;
}
- (int)size {
return _mArray->size();
}
- (void)dealloc{
delete _mArray;
}
- (NSString *)description {
int count = _mArray->size();
NSMutableString *mStr = [[NSMutableString alloc]init];
[mStr appendString:@"[\n"];
for (int i = 0; i < count; i++) {
NSObject *element = _mArray->elmentAtIndex(i);
NSString *str = [element description];
[mStr appendString:str];
if (i != count - 1) {
[mStr appendString:@",\n"];
}
}
[mStr appendString:@"]\n"];
return mStr;
}
@end