renamed to DataArray instead of MemBlock
This commit is contained in:
parent
11ef085d97
commit
96c89b000f
@ -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);
|
||||||
|
|
@ -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
245
inc/hgl/type/DataArray.h
Normal 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
|
@ -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
|
|
@ -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}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user