renamed to DataArray instead of MemBlock

This commit is contained in:
hyzboy 2023-07-15 00:27:06 +08:00
parent 11ef085d97
commit 96c89b000f
No known key found for this signature in database
GPG Key ID: 067EE4525D4FB6D3
5 changed files with 264 additions and 199 deletions

View File

@ -1,13 +1,13 @@
#pragma once #pragma once
#include<hgl/type/MemBlock.h> #include<hgl/type/DataArray.h>
#include<hgl/io/FileInputStream.h> #include<hgl/io/FileInputStream.h>
namespace hgl namespace hgl
{ {
/** /**
* *
*/ */
template<typename T> MemBlock<T> *LoadFileToMemBlock(const OSString &filename) template<typename T> static DataArray<T> *LoadFileToDataArray(const OSString &filename)
{ {
io::FileInputStream fis; io::FileInputStream fis;
@ -16,7 +16,7 @@ namespace hgl
const size_t file_size =fis.GetSize(); const size_t file_size =fis.GetSize();
const size_t size =(file_size+sizeof(T)-1)/sizeof(T); const size_t size =(file_size+sizeof(T)-1)/sizeof(T);
MemBlock<T> *mb=new MemBlock<T>(size); DataArray<T> *mb=new DataArray<T>(size);
fis.Read(mb->data(),file_size); fis.Read(mb->data(),file_size);

View File

@ -1,13 +1,13 @@
#pragma once #pragma once
#include<hgl/type/MemBlock.h> #include<hgl/type/DataArray.h>
#include<hgl/filesystem/FileSystem.h> #include<hgl/filesystem/FileSystem.h>
namespace hgl namespace hgl
{ {
/** /**
* *
*/ */
template<typename T> bool SaveMemBlockToFile(const OSString &filename,const MemBlock<T> &mb) template<typename T> static bool SaveDataArrayToFile(const OSString &filename,const DataArray<T> &mb)
{ {
const size_t size=mb.bytes(); const size_t size=mb.bytes();

245
inc/hgl/type/DataArray.h Normal file
View File

@ -0,0 +1,245 @@
#pragma once
#include<hgl/type/DataType.h>
namespace hgl
{
/**
*
*/
template<typename T> class DataArray
{
protected:
T *items;
size_t count;
size_t alloc_count;
public:
size_t GetCount ()const{return count;} ///<取得数据数量(注:非字节数)
const size_t GetAllocCount()const{return alloc_count;} ///<取得已分配的阵列大小(注:非字节数)
const size_t GetBytes ()const{return count*sizeof(T);} ///<取得阵列已使用的字节数
const size_t GetAllocBytes()const{return alloc_count*sizeof(T);} ///<取得阵列已分配空间字节数
T * GetData()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;} ///<取得阵列结束地址指针
public:
/**
* 使
*/
void Alloc(size_t size)
{
if(size<=alloc_count)
return;
alloc_count=power_to_2(size);
if(!items)
items=(T *)hgl_malloc(alloc_count*sizeof(T));
else
items=(T *)hgl_realloc(items,alloc_count*sizeof(T));
}
/**
*
*/
void SetCount(size_t size) ///<设置阵列长度(注:非字节数)
{
Alloc(size);
count=size;
}
/**
*
*/
void AddCount(size_t size)
{
SetCount(count+size);
}
public:
DataArray()
{
items=0;
count=0;
alloc_count=0;
}
DataArray(size_t size)
{
if(size<=0)
items=0;
else
items=(T *)hgl_malloc(size*sizeof(T));
if(items)
{
count=size;
alloc_count=size;
}
else
{
count=0;
alloc_count=0;
}
}
virtual ~DataArray()
{
Clear();
}
void Clear()
{
if(items)
hgl_free(items);
count=0;
alloc_count=0;
}
void ClearData()
{
count=0;
}
void Zero()
{
if(items)
memset(items,0,alloc_count);
}
/**
* ,使hgl_malloc分配
*/
void SetData(T *d,int s)
{
Clear();
items=d;
alloc_count=s;
count=s;
}
/**
*
*/
void Unlink()
{
items=nullptr;
count=0;
alloc_count=0;
}
/**
*
* @param d
* @param s
*/
void CopyData(const T *d,int s)
{
SetCount(s);
memcpy(items,d,s*sizeof(T));
}
/**
* ,
* @param index
* @return
*/
bool Delete(int index)
{
if(count>0&&index>=0&&index<count)
{
--count;
if(index<count)
memcpy(items+index,items+count,sizeof(T)); //将最后一个数据移到当前位置
return(true);
}
else
return(false);
}
/**
*
* @param index
* @return
*/
bool DeleteMove(int index)
{
if(count>0&&index>=0&&index<count)
{
--count;
if(index<count)
memmove(items+index,items+index+1,(count-index)*sizeof(T));
return(true);
}
else
return(false);
}
/**
*
* @param start
* @param number
* @return
*/
bool Delete(int start,int number)
{
if(start>=count)return(false);
if(start<0)
{
number+=start;
start=0;
}
if(start+number>count)
number=count-start;
if(number<=0)return(false);
count-=number;
if(start<count)
memmove(items+start,items+start+number,(count-start)*sizeof(T));
return(true);
}
operator T *()const
{
return items;
}
T *operator ->()const
{
return items;
}
T &operator[](int n)
{
return items[n];
}
const T &operator[](int n)const
{
return items[n];
}
};//template<typename T> class DataArray
}//namespace hgl

View File

@ -1,180 +0,0 @@
#ifndef HGL_MEM_BLOCK_INCLUDE
#define HGL_MEM_BLOCK_INCLUDE
#include<hgl/type/DataType.h>
namespace hgl
{
/**
*
*/
template<typename T> class MemBlock
{
protected:
T *buf;
size_t cur_size;
size_t buf_size;
public:
size_t GetLength ()const{return cur_size;} ///<取得内存块长度(注:非字节数)
const size_t GetMaxLength ()const{return buf_size;} ///<取得内存块最大长度(注:非字节数)
const size_t GetBytes ()const{return cur_size*sizeof(T);} ///<取得内存块字节数
const size_t GetMaxBytes ()const{return buf_size*sizeof(T);} ///<取得内存块最大字节数
T * begin (){return buf;} ///<取得内存块起始指针
T * end (){return buf+cur_size;} ///<取得内存块结束地址指针
const T * begin ()const{return buf;} ///<取得内存块起始
const T * end ()const{return buf+cur_size;} ///<取得内存块结束地址指针
/**
* 使
*/
void Malloc(size_t size)
{
if(size<=buf_size)
return;
buf_size=power_to_2(size);
if(!buf)
buf=(T *)hgl_malloc(buf_size*sizeof(T));
else
buf=(T *)hgl_realloc(buf,buf_size*sizeof(T));
}
/**
*
*/
void SetLength(size_t size) ///<设置内存块长度(注:非字节数)
{
Malloc(size);
cur_size=size;
}
void AddLength(size_t size)
{
SetLength(cur_size+size);
}
public:
MemBlock()
{
buf=0;
cur_size=0;
buf_size=0;
}
MemBlock(size_t size)
{
if(size<=0)
buf=0;
else
buf=(T *)hgl_malloc(size*sizeof(T));
if(buf)
{
cur_size=size;
buf_size=size;
}
else
{
cur_size=0;
buf_size=0;
}
}
virtual ~MemBlock()
{
Clear();
}
void Clear()
{
if(buf)
hgl_free(buf);
cur_size=0;
buf_size=0;
}
void ClearData()
{
cur_size=0;
}
void Zero()
{
if(buf)
memset(buf,0,buf_size);
}
/**
* ,使hgl_malloc分配
*/
void SetData(T *d,int s)
{
Clear();
buf=d;
buf_size=s;
cur_size=s;
}
/**
*
*/
void Unlink()
{
buf=nullptr;
cur_size=0;
buf_size=0;
}
/**
*
* @param d
* @param s
*/
void CopyData(T *d,int s)
{
SetLength(s);
memcpy(buf,d,s*sizeof(T));
}
T *data()const{return buf;}
T *GetData()const{return buf;}
size_t length()const{return cur_size;}
size_t GetCount()const{return cur_size;}
size_t bytes()const
{
return cur_size*sizeof(T);
}
operator T *()const
{
return buf;
}
T *operator ->()const
{
return buf;
}
T &operator[](int n)
{
return buf[n];
}
const T &operator[](int n)const
{
return buf[n];
}
};//template<typename T> class MemBlock
}//namespace hgl
#endif//HGL_MEM_BLOCK_INCLUDE

View File

@ -105,8 +105,8 @@ SET(IO_DATA_FILES ${IO_INCLUDE_PATH}/DataInputStream.h
SET(IO_MEMORY_STREAM_FILES ${IO_INCLUDE_PATH}/MemoryInputStream.h SET(IO_MEMORY_STREAM_FILES ${IO_INCLUDE_PATH}/MemoryInputStream.h
${IO_INCLUDE_PATH}/MemoryOutputStream.h) ${IO_INCLUDE_PATH}/MemoryOutputStream.h)
SET(IO_MEM_BLOCK_FILES ${IO_INCLUDE_PATH}/LoadMemBlock.h SET(IO_DATA_ARRAY_FILES ${IO_INCLUDE_PATH}/LoadDataArray.h
${IO_INCLUDE_PATH}/SaveMemBlock.h) ${IO_INCLUDE_PATH}/SaveDataArray.h)
SET(IO_FILE_FILES ${IO_INCLUDE_PATH}/FileAccess.h SET(IO_FILE_FILES ${IO_INCLUDE_PATH}/FileAccess.h
${IO_INCLUDE_PATH}/FileInputStream.h ${IO_INCLUDE_PATH}/FileInputStream.h
@ -139,7 +139,7 @@ SOURCE_GROUP("IO\\Event" FILES ${INPUT_EVENT_FILES})
SOURCE_GROUP("IO\\Base" FILES ${IO_BASE_FILES}) SOURCE_GROUP("IO\\Base" FILES ${IO_BASE_FILES})
SOURCE_GROUP("IO\\DataIOStream" FILES ${IO_DATA_FILES}) SOURCE_GROUP("IO\\DataIOStream" FILES ${IO_DATA_FILES})
SOURCE_GROUP("IO\\MemBlock" FILES ${IO_MEM_BLOCK_FILES}) SOURCE_GROUP("IO\\DataArray" FILES ${IO_DATA_ARRAY_FILES})
SOURCE_GROUP("IO\\MemoryStream" FILES ${IO_MEMORY_STREAM_FILES}) SOURCE_GROUP("IO\\MemoryStream" FILES ${IO_MEMORY_STREAM_FILES})
SOURCE_GROUP("IO\\File" FILES ${IO_FILE_FILES}) SOURCE_GROUP("IO\\File" FILES ${IO_FILE_FILES})
SOURCE_GROUP("IO\\Jave" FILES ${IO_JAVA_FILES}) SOURCE_GROUP("IO\\Jave" FILES ${IO_JAVA_FILES})
@ -148,7 +148,7 @@ SOURCE_GROUP("ID\\StringList" FILES ${IO_STRING_LIST_FILES})
SET(IO_SOURCE_FILES ${IO_BASE_FILES} SET(IO_SOURCE_FILES ${IO_BASE_FILES}
${IO_DATA_FILES} ${IO_DATA_FILES}
${IO_MEM_BLOCK_FILES} ${IO_DATA_ARRAY_FILES}
${IO_MEMORY_STREAM_FILES} ${IO_MEMORY_STREAM_FILES}
${IO_FILE_FILES} ${IO_FILE_FILES}
${IO_JAVA_FILES} ${IO_JAVA_FILES}