added Read/Write/operator =/Find/Insert in DataArray<>

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-07-20 19:28:40 +08:00
parent befd853b83
commit 75c52f0b93
2 changed files with 111 additions and 37 deletions

View File

@ -151,4 +151,3 @@ namespace hgl
return arh.Rearrange(new_array,old_array,index);
}
}//namespace hgl

View File

@ -2,6 +2,7 @@
#include<hgl/type/DataType.h>
#include<hgl/type/ArrayRearrangeHelper.h>
#include<hgl/type/ArrayItemProcess.h>
namespace hgl
{
/**
@ -22,18 +23,16 @@ namespace hgl
const int GetBytes ()const{return count*sizeof(T);} ///<取得阵列已使用的字节数
const int GetAllocBytes ()const{return alloc_count*sizeof(T);} ///<取得阵列已分配空间字节数
const bool IsEmpty ()const{count==0;} ///<是否为空
const bool IsEmpty ()const{return(count==0);} ///<是否为空
const T * GetData()const{return items;}
T * data() {return items;}
T * GetData ()const{return items;}
T * data ()const{return items;}
public:
T * begin (){return items;} ///<取得阵列起始地址指针
T * end (){return items+count;} ///<取得阵列结束地址指针
const T * begin ()const{return items;} ///<取得阵列起始地址指针
const T * end ()const{return items+count;} ///<取得阵列结束地址指针
T * begin ()const{return items;} ///<取得阵列起始地址指针
T * end ()const{return items+count;} ///<取得阵列结束地址指针
T * last ()const{return items+(count-1);} ///<取得阵列最后一个数据地址指针
public:
@ -55,20 +54,25 @@ namespace hgl
/**
*
* @param size
* @return
*/
void SetCount(int size) ///<设置阵列长度(注:非字节数)
int SetCount(int size) ///<设置阵列长度(注:非字节数)
{
Alloc(size);
count=size;
return count;
}
/**
*
* @param size
* @return
*/
void AddCount(int size)
int AddCount(int size)
{
SetCount(count+size);
return SetCount(count+size);
}
public:
@ -153,6 +157,53 @@ namespace hgl
return items[n];
}
const bool Read(T *obj,int index)const
{
if(!obj||index<0||index>=count)return(false);
hgl_cpy(*obj,items[index]);
return(true);
}
const bool Write(const T *obj,int index)
{
if(!obj||index<0||index>=count)return(false);
hgl_cpy(items[index],*obj);
return(true);
}
const bool Write(const T *obj,int start,const int num=1)
{
if(!obj||start<0||start>=count||start+num>count)return(false);
hgl_cpy(items+start,*obj,num);
return(true);
}
/**
*
*/
void operator = (const DataArray<T> &da)
{
if(lt.count<=0)
{
count=0;
return;
}
SetCount(da.GetCount());
hgl_cpy<T>(items,da.items,count);
}
void operator = (const std::initializer_list<T> &l)
{
SetCount((int)l.size());
hgl_cpy<T>(items,l.begin(),count);
}
/**
* ()
* @param start
@ -248,6 +299,9 @@ namespace hgl
/**
*
* @param new_index
* @param old_index
* @param move_number
*/
bool Move(int new_index,int old_index,int move_number=1)
{
@ -280,12 +334,6 @@ namespace hgl
// [----mid----][old]
//int mid_start=move_number;
//int mid_count=count-mid_start;
//hgl_cpy(new_items, items+mid_start,mid_count ); //mid
//hgl_cpy(new_items+mid_count,items, move_number ); //old
result=ArrayRearrange( new_items,items,count,
{
move_number, //old
@ -305,16 +353,6 @@ namespace hgl
// [left][old][right]
//int left_start=move_number;
//int left_count=new_index-left_start;
//int right_start=new_index;
//int right_count=count-right_start;
//hgl_cpy(new_items, items+left_start, left_count); //left
//hgl_cpy(new_items+left_count, items, move_number); //old
//hgl_cpy(new_items+left_count+move_number, right_start, right_count); //right
result=ArrayRearrange( new_items,items,count,
{
move_number, //old
@ -339,9 +377,6 @@ namespace hgl
// [old][----left----]
//hgl_cpy(new_items, items+old_index,move_number); //old
//hgl_cpy(new_items+move_number, items, old_index); //left
result=ArrayRearrange( new_items,items,count,
{
old_index, //left
@ -361,12 +396,6 @@ namespace hgl
// [left][old][-mid-]
//int mid_count=old_index-new_index;
//hgl_cpy(new_items, items, new_index ); //left
//hgl_cpy(new_items+new_index, items+old_index,move_number ); //old
//hgl_cpy(new_items+new_index+move_number,items+new_index,mid_count ); //mid
result=ArrayRearrange( new_items,items,count,
{
new_index, //left
@ -491,5 +520,51 @@ namespace hgl
return(false);
}
}
/**
*
* @param data
* @return <0
*/
int Find(const T &data)
{
return FindDataAtArray<T>(items,count,data);
}
/**
*
*/
bool Insert(int pos,const T *data,const int data_number)
{
if(!data||data_number<=0)
return(false);
if(pos<0)pos=0;
if(pos>count)pos=count;
if(count+data_number>alloc_count)
{
int new_alloc_count=alloc_count+data_number;
T *new_items=hgl_align_malloc<T>(new_alloc_count);
hgl_cpy(new_items,items,pos);
hgl_cpy(new_items+pos,data,data_number);
hgl_cpy(new_items+pos+data_number,items+pos,(count-pos));
hgl_free(items);
items=new_items;
alloc_count=new_alloc_count;
}
else
{
hgl_move(items+pos+data_number,items+pos,count-pos);
hgl_cpy(items+pos,data,data_number);
}
count+=data_number;
return(true);
}
};//template<typename T> class DataArray
}//namespace hgl