代码改变世界

缩略图设计初探二

  curer  阅读(479)  评论(1编辑  收藏  举报

之前的问题还是很大的,参考了.net framework Dictionary的思路,保留MPQ的hash算法和判断冲突的思路。重新整理了一下。

第一部分:改进部分

1、处理冲突的方法由原来线性再散列,改为分离链表法。

2、修正了一些因为处理冲突而变化的部分。

3、增加了扩容的部分。

4、增加了CRC校验部分。

第二部分:疑问

1、如何保证数据的安全性?Delete操作只是将数据从hashTable中delete,但是文件中依然有图片存在。

要么将每个图片加密储存,但是手机上资源消耗太大。那么最有可能就是delete之后将文件数据格式中关键数据上写一些随机数,从而正常解码失败。

2、如果要统一管理图片,需要建立文件索引。

3、过多的seek会影响效率,我这里还是保存了文件的偏移量,在读取的时候也必然会seek,因为我觉得使用seek似乎没有在效率产生很大的损失(可能我测试的数据不具有普遍性吧),但是很多资料上都对寻道很讨厌。这里我又参考了一下空间问题,故而保存了文件偏移量。

第三部分:部分代码

插入

复制代码
代码
BOOL MPFile::InsertData( LPCVOID lpBuffer, TCHAR *lpszString, const UINT fileSize, const FILETIME LastModiTime,
LONG
&lindex, DWORD dwFlags )
{
ASSERT( lpBuffer
!= NULL );
ASSERT( lpszString
!= NULL );

if( m_pMPBlockTable == NULL ) Initialize(0);
const DWORD HASH_OFFSET = 0, HASH_A = 1, HASH_B = 2;
DWORD hashCode
= hash.HashString(lpszString, HASH_OFFSET);
DWORD nHashA
= hash.HashString(lpszString, HASH_A);
DWORD nHashB
= hash.HashString(lpszString, HASH_B);

DWORD targetBucket
= hashCode % (m_pMPFileHeader->nHashTableLength);
for ( LONG i = m_pMPBlucketTable[targetBucket].iBlockIndex; i >= 0; i = m_pMPBlockTable[i].MPEntry.iNext )
{
if( m_pMPBlockTable[i].MPEntry.dwHashCode == hashCode
    
&&m_pMPBlockTable[i].MPEntry.dwHashValueA == nHashA
    
&& m_pMPBlockTable[i].MPEntry.dwHashValueB == nHashB )
{
if( !(dwFlags & INSERT_REPLACE_EXISTING) ) {return FALSE;}
return SetDataToFile(lpBuffer,lpszString,i);
}
}
// add new
LONG index;
BOOL isNew
=TRUE;
if (m_pMPFileHeader->nFreeCount > 0)
{
index
= m_pMPFileHeader->iFreeList;
m_pMPFileHeader
->iFreeList = m_pMPBlockTable[index].MPEntry.iNext;
m_pMPFileHeader
->nFreeCount--;
isNew
= FALSE;
}
else
{
if (m_pMPFileHeader->nChildFileCount == m_pMPFileHeader->nHashTableLength)
{
ReSize();
targetBucket
= hashCode % (m_pMPFileHeader->nHashTableLength);
}
index
= m_pMPFileHeader->nChildFileCount;
m_pMPFileHeader
->nChildFileCount++;
}

m_pMPBlockTable[index].MPEntry.dwHashCode
= hashCode;
m_pMPBlockTable[index].MPEntry.iNext
= m_pMPBlucketTable[targetBucket].iBlockIndex;
m_pMPBlockTable[index].MPEntry.dwHashValueA
= nHashA;
m_pMPBlockTable[index].MPEntry.dwHashValueB
= nHashB;
m_pMPBlockTable[index].dwFlag
= dwFlags;
m_pMPBlockTable[index].nSize
= fileSize + sizeof(MINIPICDATAITEMHEADER)+sizeof(DWORD);//size add fileName and crc32
m_pMPBlucketTable[targetBucket].iBlockIndex = index;

lindex
= index;

//TODO:修改数据
if(isNew)
{
m_pMPBlockTable[index].FileStartAt
= m_endOfFileData;
if(SetDataToFile(lpBuffer,lpszString,index) )
{
m_endOfFileData
+=m_pMPBlockTable[index].nSize;//TODO:防止溢出
   return TRUE;
}
return FALSE;
}
else
{
return SetDataToFile( lpBuffer,lpszString, index );
}
}
复制代码

读取数据

复制代码
代码
LONG MPFile::FindEntry( TCHAR *lpszString )
{
ASSERT(lpszString
!=NULL);
if (m_pMPBlucketTable != NULL )
{
const DWORD HASH_OFFSET = 0, HASH_A = 1, HASH_B = 2;
DWORD hashCode
= hash.HashString(lpszString, HASH_OFFSET);
DWORD nHashA
= hash.HashString(lpszString, HASH_A);
DWORD nHashB
= hash.HashString(lpszString, HASH_B);
DWORD nHashStart
= hashCode % (m_pMPFileHeader->nHashTableLength);

for (LONG i = m_pMPBlucketTable[nHashStart].iBlockIndex; i >= 0; i = m_pMPBlockTable[i].MPEntry.iNext)
{
if (m_pMPBlockTable[i].MPEntry.dwHashCode == hashCode
&&m_pMPBlockTable[i].MPEntry.dwHashValueA == nHashA
&& m_pMPBlockTable[i].MPEntry.dwHashValueB == nHashB )
return i;
}
}
return -1;
}
复制代码

 

MP_v2.rar源码

 

 

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示