#pragma once
#pragma pack(1)
//注意此结构使用之前必须初始化为0
typedef struct _list_data {
char * sHeader; //必须保留的,这里是表的头指针,指向第一个字符串地址
char * sBacker;
char * sLowest;
char * szListStr; //必须保留的,这里是整个表内存,注意请勿再改变,初始化必须为0
unsigned int mSize; //最大字串大小,这里指增加的字符串,注意这里的值一但设置请勿再改变,如同连表固定的值大小,要改变请先关闭表,再重建表
unsigned int nSize; //必须保留的,这里保存实际有多少个字符串在整个表内
unsigned int tSize; //必须保留的,这里保存整个表内实际有多少个字节的数据
unsigned int rSize;
} _MemData,*_lpMemData;
#pragma pack()
void __cdecl _minit(_MemData& _mData,size_t _nLen,size_t _uMaxCou); //初始化结构
void __cdecl _mclist(_MemData& _mData,const char * _iString,size_t _uMaxCou); //创建与添加表或表数据
void __cdecl _mrlist(const _MemData& _mData,unsigned int _uIndex,char * _rString); //读取表数据
void __cdecl _mwlist(_MemData& _mData,const char * _rString,unsigned int _uIndex); //修改某项的数据
void __cdecl _mflist(const _MemData& _mData,const char * _fString,unsigned int _iIndex,unsigned int * _oIndex); //快速查找指定的数据
void __cdecl _mslist(_MemData& _mData,const char * _sString,const char * _dString,unsigned int _iIndex,unsigned int * _oIndex); //查找数据并替换
void __fastcall _mdlist(_MemData& _mData,unsigned int _uIndex); //删除指定的数据
void __fastcall _melist(_MemData& _mData); //关闭数据表
#include "stdafx.h"
#include "mlist.h"
#include "almem.h"
#include "memgr.h"
void __cdecl _minit(_MemData& _mData,size_t _nLen,size_t _uMaxCou)
{
if ( &_mData != NULL ) {
_mem_init(_nLen,_uMaxCou);
_memset(&_mData,0,sizeof(_MemData));
_mData.mSize = _nLen;
}
}
void __cdecl _mclist(_MemData& _mData,const char * _iString,size_t _uMaxCou)
{
if (&_mData != NULL && _iString != NULL)
{
if (NULL == _mData.sLowest)
{
_mData.rSize = _uMaxCou;
_mData.szListStr = (char *)_mem_alloc(_mData.mSize * _mData.rSize);
if (_mData.szListStr != NULL)
{
_mData.nSize++;
_mData.tSize += _mData.mSize;
_mData.sHeader = _mData.szListStr;
_mData.sLowest = _mData.sHeader + (_mData.rSize * _mData.mSize);
_memcpy(_mData.sHeader,_iString,_mData.mSize);
_mData.sBacker = _mData.sHeader + _mData.mSize;
}
}
else if (_mData.sLowest == _mData.sBacker)
{
_mData.rSize += _uMaxCou;
_mData.szListStr = (char *)_mem_alloc(_mData.mSize * _mData.rSize);
if (_mData.szListStr != NULL)
{
_memcpy(_mData.szListStr,_mData.sHeader,_mData.tSize);
_mData.sBacker = _mData.szListStr + _mData.tSize;
_memcpy(_mData.sBacker,_iString,_mData.mSize);
_mData.sBacker += _mData.mSize;
_mData.nSize++;
_mData.tSize += _mData.mSize;
_mem_free(_mData.sHeader);
_mData.sHeader = _mData.szListStr;
_mData.sLowest = _mData.sHeader + (_mData.rSize * _mData.mSize);
}
}
else
{
_mData.nSize++;
_mData.tSize += _mData.mSize;
_memcpy(_mData.sBacker,_iString,_mData.mSize);
_mData.sBacker += _mData.mSize;
}
}
}
void __cdecl _mrlist(const _MemData& _mData,unsigned int _uIndex,char * _rString)
{
if( &_mData != NULL && _mData.sHeader != NULL && _uIndex < _mData.nSize ) {
_memcpy(_rString,_mData.sHeader + (_mData.mSize * _uIndex),_mData.mSize);
}
}
void __cdecl _mwlist(_MemData& _mData,const char * _rString,unsigned int _uIndex)
{
if( &_mData != NULL && _mData.sHeader != NULL && _uIndex < _mData.nSize)
{
_mData.szListStr = _mData.sHeader + (_mData.mSize * _uIndex);
_memcpy(_mData.szListStr,_rString,_mData.mSize);
}
}
void __cdecl _mflist(const _MemData& _mData,const char * _fString,unsigned int _iIndex,unsigned int * _oIndex)
{
if ( &_mData != NULL && _mData.sHeader != NULL && _fString != NULL && _iIndex < _mData.nSize )
{
register unsigned int _z = _iIndex;
register unsigned int _x = _z + (((_mData.nSize - _z) / 3U) + ((_mData.nSize - _z) % 3U)) - 1U;
register unsigned int _y = _x * 2U;
for( ; _y < _mData.nSize; ++_z,++_x,++_y)
{
if( true == _memcmp(_fString,_mData.sHeader + (_mData.mSize * _z),_mData.mSize) )
{
*_oIndex = _z;
return;
}
if( true == _memcmp(_fString,_mData.sHeader + (_mData.mSize * _x),_mData.mSize) )
{
*_oIndex = _x;
return;
}
if( true == _memcmp(_fString,_mData.sHeader + (_mData.mSize * _y),_mData.mSize) )
{
*_oIndex = _y;
return;
}
}
}
*_oIndex = 0x0FFFFFFFF;
}
void __cdecl _mslist(_MemData& _mData,const char * _sString,const char * _dString,unsigned int _iIndex,unsigned int * _oIndex)
{
if( &_mData != NULL && _mData.sHeader != NULL && NULL == _dString )
{
register unsigned int _uIndex;
_mflist(_mData,_sString,_iIndex,&_uIndex);
if( _uIndex != 0x0FFFFFFFF )
{
*_oIndex = _uIndex;
return;
}
} else if ( &_mData != NULL && _mData.sHeader != NULL && _sString != NULL && _dString != NULL )
{
register unsigned int _uIndex;
_mflist(_mData,_sString,_iIndex,&_uIndex);
if( _uIndex != 0x0FFFFFFFF )
{
_mData.szListStr = (char *)_mData.sHeader + (_mData.mSize * _uIndex);
_memcpy(_mData.szListStr,_dString,_mData.mSize);
*_oIndex = _uIndex;
return;
}
}
*_oIndex = 0x0FFFFFFFF;
}
void __fastcall _mdlist(_MemData& _mData,unsigned int _uIndex)
{
if( &_mData != NULL && _mData.sHeader != NULL && _uIndex < _mData.nSize )
{
const unsigned int xSize = _mData.mSize * _uIndex;
_mData.nSize--;
_mData.tSize -= _mData.mSize;
_mData.sBacker -= _mData.mSize;
_mData.szListStr = _mData.sHeader;
_memcpy((_mData.szListStr + xSize),(_mData.sHeader + (xSize + _mData.mSize)),((_mData.nSize - _uIndex) * _mData.mSize));
_memset((_mData.sLowest - _mData.mSize),0,_mData.mSize);
}
}
void __fastcall _melist(_MemData& _mData)
{
if (&_mData != NULL)
_memset(&_mData,0,sizeof(_MemData));
return _mem_dele();
}
int _tmain(int argc, _TCHAR* argv[])
{
char rStr[16] = {0};
char strIn[] = "操作成功!";
char strFind[] = "查找成功!";
unsigned int uIndex = 0U;
_MemData _mdata = {0U};
vector<string> str_vector;
string str = "abcdefghijklmnopqrstuvwxyz0123456789";
int size = 102;
_minit(_mdata,16U,(size * 4U));
clock_t s = clock();
//生成长度大小为6-16之间的随机且不重复字串
for( int i = 0; i < size; i++ )
{
int x = rand() % 16U;
while (x < 6) {
x = rand() % 16U;
}
random_shuffle( str.begin(), str.end() );
str_vector.push_back( str.substr( 0,x) );
}
vector<string>::iterator it = unique( str_vector.begin(), str_vector.end() );
str_vector.resize( it - str_vector.begin() );
str_vector.resize( size );
clock_t d = clock();
printf("生成随机字符串 time: %d\r\n",d-s);
s = clock();
for ( register unsigned int i = 0U; i < str_vector.size(); i++ )
_mclist(_mdata,str_vector[ i ].c_str(),101U);
_mwlist(_mdata,strIn,81U); //修改某项数据
//for(register unsigned int i = 0U; i < _mdata.nSize; ++i)
//{
// _mrlist(_mdata,i,rStr);
// printf("%s\r\n",rStr);
//}
d = clock();
printf("插入字符串 time: %d\r\n",d-s);
system("pause.");
s = clock();
_mflist(_mdata,strIn,0U,&uIndex); //查找字符串 uIndex == 0xFFFFFFFF 表明没有找到
printf("%s->%u\r\n",strIn,uIndex);
system("pause.");
_mwlist(_mdata,"修改成功!",uIndex); //修改查找到的数据
for(register unsigned int i = 0U; i < _mdata.nSize; ++i)
{
_mrlist(_mdata,i,rStr);
printf("%s\r\n",rStr);
}
d = clock();
printf("time: %d\r\n",d-s);
system("pause.");
_mdlist(_mdata,uIndex); //删除指定的数据
for(register unsigned int i = 0; i < _mdata.nSize; ++i)
{
_mrlist(_mdata,i,rStr);
printf("%s\r\n",rStr);
}
system("pause.");
_mwlist(_mdata,strFind,92U);
_mslist(_mdata,strFind,NULL,0U,&uIndex); //fNum == 0xFFFFFFFF 表明没有找到
printf("%u\r\n",uIndex);
system("pause.");
for(register unsigned int i = 0U; i < _mdata.nSize; ++i) //查找指定的所有相同的数据,并全部替换
{
_mslist(_mdata,strFind,strIn,i,&uIndex);
if( uIndex == 0x0FFFFFFFF )
break;
}
for(register unsigned int i = 0; i < _mdata.nSize; ++i)
{
_mrlist(_mdata,i,rStr);
printf("%s\r\n",rStr);
}
for(register unsigned int i = 0U; i < _mdata.nSize; ++i)
{
_mrlist(_mdata,i,rStr);
if (i == uIndex)
printf("%s\r\n",rStr);
}
system("pause.");
printf("字节总大小:%d 字符串成员个数:%d 成员字符固定大小:%d\n",_mdata.tSize,_mdata.nSize,_mdata.mSize);
_melist(_mdata); //关闭数据表
system("pause.");
return 0;
}
简单之数组链
最新推荐文章于 2022-03-01 09:38:34 发布