简单介绍一下,这个类实现的是一个双索引的MAP方式
///////////////////////////////////////////////H///////////////////////////////////////////
#include <string>
#include <map>
#include "Lg_SyncLock.h"
#include "lg_guid.h"
using namespace std;
namespace lglib
{
enum GetFileResult
{
AllHaveGet = 0,
GetOk,
ListEmpty,
CanNotFindById
};
class FileCell
{
public:
FileCell(string fn,lg_AtomicCounter count)
{
m_filename = fn;
m_id = count;
nFlag = 0;
}
~FileCell(){}
GetFileResult GetFileName(TransType type,string &fn)
{
nFlag |= type;
fn = m_filename;
if(nFlag & (TP_TxtInfo|TP_PlateInfo|TP_ImageInfo))
{
return AllHaveGet;
}
return GetOk;
}
void replace(string fn)
{
m_filename = fn;
}
int32 GetFlag()
{
return nFlag;
}
int32 GetId()
{
return m_id;
}
private:
string m_filename;
int32 m_id;
int32 nFlag;
};
class lg_FileList
{
public:
lg_FileList();
virtual ~lg_FileList();
BOOL push(string fn);
BOOL replace(string newone,string oldone);
BOOL DeleteCell(string fn);
void ClearAllCell();
GetFileResult GetString(TransType type,string & fn);
private:
uint32 dwPos[TP_EndPos];
map<string,FileCell*> m_StringMap;
typedef map<string,FileCell*>::iterator SMapIt;
map<long,FileCell*> m_LongMap;
typedef map<int32,FileCell*>::iterator LMapIt;
lg_Mutex m_Mutex;
lg_AtomicCounter m_Counter;
};
}
////////////////////////////////////////////////////////CPP///////////////////////////////////////////////////////
namespace lglib
{
lg_FileList::lg_FileList()
{
memset(dwPos,0,sizeof(dwPos)*TP_EndPos);
}
lg_FileList::~lg_FileList()
{
//ClearAllCell();
}
BOOL lg_FileList::push(string fn)
{
m_Mutex.lock();
SMapIt mapit = m_StringMap.find(fn);
if(mapit == m_StringMap.end())
{
FileCell* p = new FileCell(fn,m_Counter);
++m_Counter;
m_StringMap[fn] = p;
m_LongMap[p->GetId()] = p;
m_Mutex.unlock();
return TRUE;
}
m_Mutex.unlock();
return FALSE;
}
BOOL lg_FileList::replace(string newone,string oldone)
{
m_Mutex.lock();
SMapIt mapit = m_StringMap.find(oldone);
if(mapit != m_StringMap.end())
{
FileCell* p = (*mapit).second;
//必须保证 p有效
ASSERT(p != NULL);
m_StringMap.erase(mapit);
p->replace(newone);
m_StringMap[newone] = p;
m_Mutex.unlock();
return TRUE;
}
m_Mutex.unlock();
return FALSE;
}
BOOL lg_FileList::DeleteCell(string fn)
{
m_Mutex.lock();
SMapIt it = m_StringMap.find(fn);
if(it != m_StringMap.end())
{
FileCell* p = (*it).second;
//必须保证 p有效
ASSERT(p != NULL);
m_StringMap.erase(it);
LMapIt lMapit = m_LongMap.find(p->GetId());
if(lMapit != m_LongMap.end())
{
m_LongMap.erase(lMapit);
}
delete p;
m_Mutex.unlock();
return TRUE;
}
m_Mutex.unlock();
return FALSE;
}
void lg_FileList::ClearAllCell()
{
m_Mutex.lock();
if(m_StringMap.size() > 0){
for(SMapIt mapit = m_StringMap.begin();mapit != m_StringMap.end();mapit++){
delete ((*mapit).second);
}
m_StringMap.clear();
}
if(m_LongMap.size()>0)
m_LongMap.clear();
m_Mutex.unlock();
}
GetFileResult lg_FileList::GetString(TransType type,string & fn)
{
if(m_StringMap.size() <= 0||m_LongMap.size()<=0)
return ListEmpty;
LMapIt it = m_LongMap.find(dwPos[type]);
dwPos[type]++;
if(it != m_LongMap.end())
{
FileCell* p = (*it).second;
//必须保证 p有效
ASSERT(p != NULL);
return p->GetFileName(type,fn);
}
return CanNotFindById;
}
}