【www.gdgbn.com--linux】

 



/**////通用库动态集合模板类
/**//**
 * 通用库4.0版

 * 这是一个集合类,这个类的元素存放是一个有序的数组。这个类的元素查找方法为二分查找。
 * 这个类提供了类相关的所有功能.集合的方法有交集*,并集+,差集-,除此之后,还有*=,+=,-=等对应方法.
 * 集合类通过Contains,检查指定元素是否在集合中
 * @author zdhsoft(祝冬华)
 * @version 4.0
 * @date 2008-04-01
 * @file xset.h
 */
#ifndef _X_SET_H_
#define _X_SET_H_
#include 
namespace zdh
...{
 /**////基于数组的集合模板类
 template >
 class XSet
 ...{
 public:
  typedef T ElementType;
  typedef ElementType * PElementType;
 public:
  /**////缺省构造函数
  XSet()
  ...{}
  /**////缺省拷贝构造函数
  XSet(const XSet & aSet)
   :m_Data(aSet.m_Data)
  ...{}
  /**////指定最小值和最大值的构函造函数
  XSet(const T & vMin,const T & vMAX,XInt aRepareLength = 0);
  /**////取有效的数组元素个数
  XInt getLength() const
  ...{
   return  m_Data.getLength();
  }
  /**////判断集合是否为空
  /**//**
      @return 返回检查的结果
        - true 表示集合为空
        - false 表示集合不为空
  */
  bool isEmpty() const 
  ...{
   return m_Data.isEmpty();
  }
  /**////是否为第一个下标
  /**//**
      @param aIndex 被检查的下标
      @return 返回检查的结果 
        - true 表示是第一个下标
        - false 表示不是第一个下标
  */
  bool isFirstIndex(XInt aIndex) const
  ...{
   return m_Data.isFirstIndex(aIndex)
  }
  /**////是否为最后一个下标
  /**//**
      @param aIndex 被检查的下标
      @return 返回检查的结果 
        - true 表示是最后一个下标
        - false 表示不是最后一个下标
  */
  bool isLastIndex(XInt aIndex) const
  ...{
   return m_Data.isLastIndex(aIndex);
  }
  /**////是否为有效的下标
  /**//**
      @param aIndex 被检查的下标
      @return 返回检查的结果 
        - true 表示有效下标
        - false 表示无效下标
  */
  bool isValidIndex(XInt aIndex) const
  ...{
   return m_Data.isValidIndex(aIndex);
  }
  /**////取当前集合的容量
  XInt getCapacity() const
  ...{
   return m_Data.getCapacity();
  }
  /**////取指定下标的集合元素
  /**//**
      @param [in] aIndex 指定的下标
      @return 返回指定下标的引用
      @exception 如果发生越界,则抛出XEOutOfRange异常
  */
  const T & getElement(XInt aIndex) const
  ...{
   return m_Data.getElement(aIndex);
  }
  /**////确认最小容量
  /**//**
      确认最小容量,如果容量不够,则扩展容量
      @param [in] minimumCapacity 确认的最小容量
  */
  void ensureCapacity(XInt minimunCapacity)
  ...{
   m_Data.ensureCapacity(minimunCapacity);
  }
  /**////取第一个下标
  /**//**
      @return 返回第一个下标
        - ARRAY_INVALID_INDEX 表示无效下标
        - 0 表示有效第一个下标
  */
  XInt getFirstIndex() const
  ...{
   return m_Data.getFirstIndex();
  }
  /**////取最后一个下标
  /**//**
      @return 返回最后一个下标
        - ARRAY_INVALID_INDEX 表示无效下标
        - >=0 表示有效最后一个下标
  */
  XInt getLastIndex() const 
  ...{
   return m_Data.getLastIndex();
  }
  /**////取集合的最大容量
  XInt getMaxCapacity() const
  ...{
   return m_Data.getMaxCapacity();
  }
  /**////清除集合
  void Clear()
  ...{
   m_Data.Clear();
  }
  /**////向集合中加入一个元素
  XSet & operator << (const T & data) //加入一个元素
  ...{
   Add(data);
   return *this;
  }
  /**////向集合中删除一个元素
  XSet & operator >> (const T & data) //删除一个元素
  ...{
   Remove(data);
   return *this;
  }
  /**////向集合加入一个集合的元素
  XSet & operator << (const XSet & rhs) //加入一个集合中的有元素
  ...{
   Add(rhs);
   return *this;
  }
  /**////向集合中,删除一个集合的元素
  XSet & operator >> (const XSet & rhs) //删除一个集合中的有元素
  ...{
   Remove(rhs);
   return *this;
  }
  /**////向集合中加入一个元素
  /**//**
      @param data 被加入的元素
  */
  void Add(const T & data)     //加入一个元素
  ...{
   _Insert(data);
  }
  /**////向集合中,加入一个集合的元素
  void Add(const XSet & rhs); //加入一个集合中的有元素
  /**////删除一个指定的元素
  void Remove(const T & data)  //删除一个元素
  ...{
   XInt iIndex = _Contains(data);
   if( iIndex != ARRAY_INVALID_INDEX ) m_Data.Remove(iIndex);
  }
  /**////删除另一个集合中有的元素
  void Remove(const XSet & rhs);//删除一个集合中的有元素
  /**////检查元素是否在这个集合中
  bool Contains(const T & data) const
  ...{
   return _Contains(data) != ARRAY_INVALID_INDEX;
  }
  /**////检查指定集合的元素是否在这个集合中
  bool Contains(const XSet & rhs) const;
  /**////重载[]运算符
  /**//**
      重载[]运算符,取指定下标的集合元素
      @param Index 下标值
      @return 返回指定下标的元素的常量引用
  */
  const T & operator[](XInt Index) const
  ...{
   return m_Data[Index];
  };
  /**////缺省拷贝复制函数
  XSet & operator = (const XSet & rhs)
  ...{
   if( this != & rhs ) 
   ...{
    m_Data = rhs.m_Data;
   }
   return *this;
  }
  /**////两个集合差集的运算
  XSet operator-(const XSet & rhs) const; //差集
  /**////两个集合并集的运算
  /**//**
      @param rhs 并集集合
      @return 返回并集运算结果
  */
  XSet operator+(const XSet & rhs) const //并集
  ...{
   XSet aSet(*this);
   aSet.Add(rhs);
   return aSet;
  }
  /**////两个集合交集的运算
  XSet operator*(const XSet & rhs) const; //交集
  /**////集合的子集运算
  XSet SubSet(const T & MinData,const T & MaxData) const;
  /**////集合差集运算
  XSet & operator-=(const XSet & rhs); //差集
  /**////集合并集运算
  XSet & operator+=(const XSet & rhs) //并集
  ...{
   Add(rhs);
   return *this;
  }
  /**////集合交集运算
  XSet & operator*=(const XSet & rhs); //交集
  /**////取指定元素的下标
  /**//**
      @param data 指定的元素
      @return 返回指定元素的下标
        - ARRAY_INVALID_INDEX 表示未找到这个元素
        - 其它值,表示该元素的下标
  */
  XInt getIndex(const T & data) 
  ...{ 
   return _Contains(data); 
  }
  /**////比较两个集合是否相同
  bool operator == (const XSet & rhs) const;
  /**////比较两个集合是否不相同
  bool operator != (const XSet & rhs) const;
 private:
  /**////检查这个集合是否包含指定元素
  XInt _Contains(const T & data) const;//采用二分查找
  /**////向集合中插入一个元素
  void _Insert(const T & data); //插入一个元素
 private:
  Array m_Data;//存放集合数据的数组
 };
 //----------------------------------------------------------------------------
 /**//**
     向集合中加入另一个集合的元素
     @param rhs 加入的元素

本文来源:http://www.gdgbn.com/caozuoxitong/13152/