⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 basecollection.h

📁 线程安全的带索引功能的对象集合类
💻 H
字号:
/******************************************************************************
    CopyRight (c) 2000-2005 南京南自信息技术有限公司
    All rights reserved.

    文件名称:BaseCollection.h
    文件标识:对象集合基类
    文件摘要:对象集合基类实现文件

    作    者:王强
    建立日期:2005.11.09
    完成日期:
    当前版本:1.0

    修订版本:未修订
    作    者:
    完成日期:
******************************************************************************/
/******************************************************************************
    CopyRight (c) 2000-2005 南京南自信息技术有限公司
    All rights reserved.

    类    名:TBaseCollection
    父    类:无

    作    者:王强
    当前版本:1.1
    建立日期:2005.11.09
    完成日期:2006.06.14

    修订版本:1.0
    作    者:王强
    完成日期:2005.11.09

    目    的:实现通用的对象集合类功能,即添加、删除、查找等操作
    接    口:见文档
******************************************************************************/
#ifndef BaseCollectionH
#define BaseCollectionH

#include "bcb_comm.h"
#include <vector>
#include <map>
using namespace std;

//说明:
//1:K类型表示关键字索引类型,此类型需要重载<和==操作符
//2:T类型表示集合中元素的指针类型,此类型需要有拷贝构造函数
template<class K, class T> class TBaseCollection
{
	typedef map< K, T* >				CollectionType;
    typedef CollectionType::iterator	Position;
public:
    //构造
	TBaseCollection( void )
    {

    }

    //拷贝构造
    TBaseCollection(const TBaseCollection &rhs)
    {
        Position iteb(m_map.begin());
        Position itee(m_map.end());
        for(;iteb!=itee;++iteb)
        {
            T *pObj(new T(*iteb->second));
            m_map.insert(make_pair(iteb->first,pObj));
        }
    }

    //重载的=操作符
    TBaseCollection &operator=(const TBaseCollection &rhs)
    {
        if(this!=&rhs)
        {
            Clear();
            Position iteb(m_map.begin());
            Position itee(m_map.end());
            for(;iteb!=itee;++iteb)
            {
                T *pObj(new T(*iteb->second));
                m_map.insert(make_pair(iteb->first,pObj));
            }
        }
        return *this;
    }
    
    //析构
    virtual ~TBaseCollection( void )
    {
    	Clear();
    }

    //添加成员到集合中
    T* Add(const K &key, const T *object, const bool bUpdate =true)
    {
        if(object==NULL)
        {
            return NULL;
        }
        
    	//如果在集合中找到相同关键字的元素,则根据bUpdate参数的值来决定是否更新
        //集合中的元素,如果bUpdate为true,则表示要更新,否则不更新
    	Position itePos(m_map.find(key));
        //查找指定关键字的元素是否存在
        if(itePos==m_map.end())
        {
            //添加进集合中的元素需要有拷贝构造函数
            //这里集合中保存的是要添加元素的副本
        	T *newobj(new T(*object));
            m_map.insert(make_pair(key,newobj));
            return newobj;
        }
        else
        {
        	if(bUpdate)
            {
            	*(itePos->second) = *object;
            }
            return itePos->second;
        }
    }

    //从集合中删除元素,返回true表示指定元素被删除,返回false表示未找到指定元素
	bool Remove(const K &key)
    {
    	Position itePos(m_map.find(key));
        //查找指定关键字的元素是否存在
        if(itePos!=m_map.end())
        {
			T *obj(itePos->second);
            SAFE_DELETE(obj);
            m_map.erase(itePos);
            return true;
        }
        return false;
    }

    //从集合中查找元素
    T* Find(const K &key)
    {
    	Position itePos(m_map.find(key));
        //查找指定关键字的元素是否存在
        if(itePos!=m_map.end())
        {
            return itePos->second;
        }
        return NULL;
    }

    //返回集合中元素个数
    int Count(void)
    {
    	return m_map.size();
    }

    //根据索引返回指定位置的元素
    T* GetByIndex(int idx)
    {
		Position iteb(m_map.begin());
        Position itee(m_map.end());
        for(int i=0;iteb!=itee;++iteb,++i)
        {
        	T *obj(iteb->second);
            if(i==idx)
            {
            	return obj;
            }
        }
        return NULL;
    }

    //清空集合中元素,并释放元素占用的内容空间
    void Clear(void)
    {
    	//释放集合中元素对象占用的内存
		Position iteb(m_map.begin());
        Position itee(m_map.end());
        for(;iteb!=itee;++iteb)
        {
        	T *obj(iteb->second);
            SAFE_DELETE(obj);
        }
        m_map.clear();
    }

    //返回集合中所有对象
    int GetAllObjects(vector<T*> &vct,bool bClear=true)
    {
        if(bClear)
        {
            vct.clear();
        }
		Position iteb(m_map.begin());
        Position itee(m_map.end());
        for(int i=0;iteb!=itee;++iteb,++i)
        {
            vct.push_back(iteb->second);
        }
        return vct.size();
    }

    //返回某个对象的副本
    //2006.06.20    add by wq
    bool GetObjectCopy(const K &key,T *&pObject)
    {
        T *pFoo(Find(key));
        if(pFoo)
        {
            pObject = new T(*pFoo);
            return true;
        }
        return false;
    }

protected:
	CollectionType	m_map;
};

#endif	//BaseCollectionH

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -