Generic Library Dynamic Collection template class

Source: Internet
Author: User







/**/ /// General Library Dynamic Collection template class


/**/ /**
* General Library version 4.0 <br>
* This is a collection class where the elements of this class are stored in an ordered array. The element lookup method for this class is two-point lookup.
* This class provides all the functionality associated with a class. The method of the set has intersection *, and set +, difference set-, in addition to this, there are *=,+=,-= and other corresponding methods.
* Collection class checks whether the specified element is in the collection by contains
* @author Zdhsoft (Shundonghua)
* @version 4.0
* @date 2008-04-01
* @file xset.h
*/
#ifndef _x_set_h_
#define _x_set_h_
#include < Xarray.h >
namespace ZDH
... {
/**/ /// Array-based collection template class


Template<classT,classArray=Xarray<T> >
classXset
...{
Public:
typedef T ElementType;
typedef ElementType*Pelementtype;
Public:
/**/ /// default constructor


Xset ()
...{}
/**/ /// Default copy constructor


Xset (ConstXset<T,array> &aset)
: M_data (Aset.m_data)
...{}
/**/ /// functions for specifying minimum and maximum values


Xset (ConstT&Vmin,ConstT&Vmax,xint areparelength= 0);
/**/ /// The number of valid array elements


xint getlength ()Const
...{
return m_data.getlength ();
}
/**/ /// to determine if the collection is empty


  /**//**
@return return the results of the check
-True to indicate that the collection is empty
-False indicates that the collection is not empty
*/
BOOLIsEmpty ()Const
...{
return m_data.isempty ();
}
/**/ /// is the first subscript


  /**//**
@param aindex is checked for subscript
@return return the results of the check
-True indicates the first subscript
-False indicates that it is not the first subscript
*/
BOOLIsfirstindex (xint aindex)Const
...{
return m_data.isfirstindex (aindex)
}
/**/ /// is the last subscript


  /**//**
@param aindex is checked for subscript
@return return the results of the check
-True indicates the last subscript
-False indicates not the last subscript
*/
BOOLIslastindex (xint aindex)Const
...{
return m_data.islastindex (aindex);
}
/**/whether the /// is a valid subscript


  /**//**
@param aindex is checked for subscript
@return return the results of the check
-True indicates a valid subscript
-False indicates an invalid subscript
*/
BOOLIsvalidindex (xint aindex)Const
...{
return m_data.isvalidindex (aindex);
}
/**/ /// The capacity of the current collection


xint getcapacity ()Const
...{
return m_data.getcapacity ();
}
/**/ /// The collection element that specifies the subscript


  /**//**
The subscript specified @param [in] Aindex
@return returns a reference to the specified subscript
@exception throw a Xeoutofrange exception if there is a boundary crossing
*/
ConstT&getelement (xint aindex)Const
...{
return m_data.getelement (aindex);
}
/**/ /// confirm minimum capacity


  /**//**
Confirm minimum capacity, and expand capacity if capacity is insufficient
Minimum capacity confirmed @param [in] minimumcapacity
*/
voidensurecapacity (Xint minimuncapacity)
...{
M_data.ensurecapacity (minimuncapacity);
}
/**/ /// take the first subscript


  /**//**
@return returns the first subscript
-Array_invalid_index indicates invalid subscript
-0 indicates a valid first subscript
*/
Xint Getfirstindex ()Const
...{
return m_data.getfirstindex ();
}
/**/ /// take the last subscript


  /**//**
@return returns the last subscript
-Array_invalid_index indicates invalid subscript
->=0 represents a valid last subscript
*/
Xint Getlastindex ()Const
...{
return m_data.getlastindex ();
}
/**/ /// The maximum capacity of the collection


xint getmaxcapacity ()Const
...{
return m_data.getmaxcapacity ();
}
/**/ /// Purge collection


  voidClear ()
...{
M_data.clear ();
}
/**/ /// Adds an element to the collection


Xset<T,array> & operator << (ConstT&data)//Add an element
  ...{
ADD (data);
return * This ;
}
/**/ /// Deletes an element to the collection


Xset<T,array> & operator >> (ConstT&data)//Delete an element
  ...{
Remove (data);
return * This ;
}
/**/ /// the elements of a collection into a collection


Xset<T,array> & operator << (ConstXset<T,array> &RHS)//add an element to a collection
  ...{
ADD (RHS);
return * This ;
}
/**/ /// to the collection, delete the elements of a collection


Xset<T,array> & operator >> (ConstXset<T,array> &RHS)//deletes an element from a collection
  ...{
Remove (RHS);
return * This ;
}
/**/ /// Adds an element to the collection


  /**//* *
@param data is added to the element
*/
voidAdd (ConstT&data)//Add an element
  ...{
_insert (data);
}
/**/ /// to the collection, add the elements of a collection


  voidAdd (ConstXset<T,array> &RHS); //add an element to a collection
  /**/ /// Deletes a specified element


  voidRemove (ConstT&data)//Delete an element
  ...{
Xint iindex = _contains (data);
if(iindex != array_invalid_index) m_data.remove (iindex);
}
/**/ /// Delete some elements from another collection


  voidRemove (ConstXset<T,array> &RHS);//deletes an element from a collection
  /**/ /// Check if the element is in this collection


  BOOLContains (ConstT&data)Const
...{
return _contains (data) != array_invalid_index;
}
/**/ /// checks whether the elements of the specified collection are in this collection


  BOOLContains (ConstXset<T,array> &RHS)Const;
/**/ /// overload [] operator


  /**//**
overload [] operator, taking the collection element of the specified subscript
Subscript value @param Index
@return returns a constant reference to the element that specifies the subscript
*/
ConstT& operator[] (xint Index)Const
...{
return M_data[index];
};
/**/ /// default copy copy function


Xset<T,array> & operator = (ConstXset<T,array> &Rhs
...{
if ( this!= & RHS)
... {
m_data = rhs.m_data;
}
return * This ;
}
/**/ The operation of///two set difference sets


Xset<T,array> operator-(ConstXset<T,array> &RHS)Const; //Difference Set
  /**/ /// The operation of two set merging sets


  /**//* *
@param collection of RHS and sets
@return Returns the result of the set operation
*/
Xset<T,array> operator+(ConstXset<T,array> &RHS)Const //and set
  ...{
Xset<T,array> aset (*this);
Aset.add (RHS);
return aset;
}
/**/ /// Two sets of intersection operations


Xset<T,array> operator*(ConstXset<T,array> &RHS)Const; //intersection
  /**/ subset operations for /// sets


Xset<T,array>Subset (ConstT&Mindata,ConstT&maxdata)Const;
/**/ /// set difference set operation


Xset<T,array> & operator-=(ConstXset<T,array> &RHS); //Difference Set
  /**/ /// Set merging set operation


Xset<T,array> & operator+=(ConstXset<T,array> &RHS)//and set
  ...{
ADD (RHS);
return * This ;
}
/**/ /// set intersection operation


Xset<T,array> & operator*=(ConstXset<T,array> &RHS); //intersection
  /**/ /// The subscript of the specified element


  /**//**
@param the element specified by data
@return returns the subscript for the specified element
-Array_invalid_index said the element was not found
-Other value that represents the subscript for the element
*/
Xint GetIndex (ConstT&data)
...{
return _contains (data);
}
/**/ /// compare two sets to be identical


  BOOL operator == (ConstXset<T,array> &RHS)Const;
/**/ /// Compare two sets is not the same


  BOOL operator != (ConstXset<T,array> &RHS)Const;
Private:
/**/ /// Check whether this collection contains the specified element


xint _contains (ConstT&data)Const;//using binary search
  /**/ /// Inserts an element into the collection


  void_insert (ConstT&data); //Insert an element
 Private:
Array m_data;//An array that holds the collection data
 };
//----------------------------------------------------------------------------
 /**//**
To add an element of another collection to the collection
Collection of elements added @param RHS
*/
Template<classT,classArray>
voidXset<T,array>:: Add (ConstXset<T,array> &RHS)
...{
if ( this!= &RHS)
... {
for (xint i = rhs.getlength () - 1 ; I >= 0; I-- ) Add (Rhs[i]);
}
}
//----------------------------------------------------------------------------
 /**//**
@param RHS to delete the collection of elements
*/
Template<classT,classArray>
voidXset<T,array>:: Remove (ConstXset<T,array> &RHS)
...{
if ( this!= &RHS)
... {
For (xint i = rhs.getlength ()-1 ; i >=0; I--) Remove (Rhs[i]);
}
Else Clear ();
}
//----------------------------------------------------------------------------
 /**//**
Checks whether this collection contains the specified element. The lookup method used is a binary lookup
@param the elements of data being checked
@return the subscript of the element to be inspected
-Array_invalid_index said the element was not found
-Other >=0 values, indicating that a
*/
Template<classT,classArray>
Xint Xset<T,array>:: _contains (ConstT&data)Const
...{
Xint Low= 0, High=m_data.getlength ()- 1, Mid;
while( Low<=High )
... {
   mid  =   (Low   +  high)   / 2 ;
    const  t  /  Tmp  =  m_data[mid];
    if ( tmp  = =  data )   return  mid;
    Else   if   ( Tmp   data   high  =  Mid  - 1 ;
    Else  low  =  Mid  +   1 ;
  }
returnArray_invalid_index;
}
//----------------------------------------------------------------------------
 /**//**
Checks the current collection to see if the specified collection is included
@param RHS the collection to check
@return The results of the inspection
-True indicates that the specified collection is included
-False indicates that the specified collection is not included
*/
Template<classT,classArray>
BOOLXset<T,array>:: Contains (ConstXset<T,array> &RHS)Const
...{
if(  This == &RHS) return true;
if(Rhs.isempty ()) return true;
if(Rhs.getlength ()>getlength ()) return false;
for(Xint i=rhs.getlength ()-1; I>=0; I--)
...{
if( ! Contains (Rhs[i] ) return false;
}
return true;
}
//----------------------------------------------------------------------------
 /**//* *
Inserts an element into the collection. This method is a private method and is only for xset internal calls
@param the element that data is to be inserted
*/
Template<classT,classArray>
voidXset<T,array>:: _insert (ConstT&data)
...{
Xint Ilength=m_data.getlength ();
if(Ilength<= 0) M_data.append (Data);
Else
...{
//find where to insert
Xint Low= 0, High=Ilength-1, Mid;
while( Low<=High )
...{
Mid=( Low+High )/2;
ConstT&TMP=M_data[mid];
if(TMP==data) return; //if the element is already there, return the
    Else if(TMP>data) High=Mid-1;
Else Low=Mid+ 1;
}
if(M_data[mid]>data) M_data.insert (Mid,data);
ElseM_data.insert (Mid+1, data);
}
}
//----------------------------------------------------------------------------
 /**//**
Constructors that specify minimum and maximum values
@param vmin Minimum value
@param vMax Maximum Size
Size of the array prepared @param areparelength
*/
Template<classT,classArray>
Xset<T,array>:: Xset (ConstT&Vmin,ConstT&vmax,xint areparelength)
...{
Ensurecapacity (areparelength); //Determine capacity
  if(Vmin>VMax)
...{
for(T i = vMax i<=vmin; ++ i) m_data.append (i);
}
Else
...{
for(T i = vmin i<=vMax; ++ i) m_data.append (i);
}
}
//----------------------------------------------------------------------------
 /**//**
The set's differential operation, which returns a new set of elements in the collection that belong to two sets
element, but not a common element of two collections
@param RHS Another set of difference sets
@return Returns the result of a two set difference set
*/
Template<classT,classArray>
Xset<T,array>Xset<T,array>::operator - (ConstXset<T,array> &RHS)Const
...{
Xset<T,array>ret;
if(  This != &RHS)
...{
for(Xint i=m_data.getlength ()- 1; I>=0; I--)
...{
const T & data = m_data[i];
if( ! RHS. Contains (data)) Ret. ADD (data);
}
for(Xint i=rhs.getlength ()- 1; I>=0; I--)
...{
const T & data = m_data[i];
if( ! Contains (data)) Ret. ADD (data);
}
}
returnret;
}
//----------------------------------------------------------------------------
 /**//* *
The intersection operation of the collection, which returns a new set of elements in the collection that is a total of two sets
of elements
@param RHS Intersection Collection
@return Returns the result of an intersection operation
*/
Template<classT,classArray>
Xset<T,array>Xset<T,array>::operator*(ConstXset<T,array> &RHS)Const
...{
Xset<T,array>ret;
if(  This != &RHS)
...{

For (xint i = m_data.getlength () - 1; I >= 0; I-- )
... {
const T & data = m_data[i];
if(RHS. Contains (data)) Ret. ADD (data);
}
}
Elseret=Rhs
returnret;
}
//----------------------------------------------------------------------------
 /**//**
Compare whether two collections are the same
@param a collection of RHS comparisons
@return returns the results of a comparison
-True indicates that two collections are the same
-False indicates that two collections are different
*/
Template<classT,classArray>
BOOLXset<T,array>::operator == (ConstXset<T,array> &RHS)Const
...{
if(  This == &RHS) return true;
if(Rhs.getlength ()!=m_data.getlength ()) return false;

for(Xint i=m_data.getlength ()-1; I>= 0; I--)
...{
if(M_data[i] != rhs[i]) return false;
}
return true;
}
//----------------------------------------------------------------------------
 /**//* *
If two collections are different
@param a collection of RHS comparisons
@return returns the results of a comparison
-True indicates that two collections are different
-False indicates that two sets are the same
*/
Template<classT,classArray>
InlineBOOLXset<T,array>::operator != (ConstXset<T,array> &RHS)Const
...{
return! operator = = (RHS);
}
//----------------------------------------------------------------------------
 /**//* *
Two sets do the difference set and place the result in the current collection
@param RHS Set of difference sets
@return returns a reference to the current collection
*/
Template<classT,classArray>
Xset<T,array> &Xset<T,array>::operator-=(ConstXset<T,array> &RHS)
...{
if(  This == &RHS) Clear ();
Else
...{
if( !Rhs.isempty ())
...{
if(IsEmpty ())operator =(RHS);
Else
...{
Xset<T,array> tmp = * this ;
TMP *= RHS; // get the intersection of two sets
* this + + RHS; // get a set of two sets
Remove (TMP); // Delete intersection of two sets
    }
}
}
return * This;
}
//----------------------------------------------------------------------------
 /**//**
Two sets do the intersection and place the results in the current collection
@param RHS Intersection Collection
@return returns a reference to the current collection
*/
Template<classT,classArray>
Xset<T,array> &Xset<T,array>::operator*=(ConstXset<T,array> &RHS)
...{
if(  This != &RHS)
...{
if(Rhs.isempty ()) clear ();//If the RHS collection is empty, the intersection is empty
   Else //if the current collection is not empty
   ...{
if( !IsEmpty ())//if the two collections are not empty
    ...{

for (xint i = m_data.getlength () - 1; i >=0; I--)
... {
if( ! RHS. Contains (M_data[i])) m_data.remove (i); // If the specified element does not exist, delete the
}
}
}
}
return * This;
}
//----------------------------------------------------------------------------
 /**//**
From the current collection, take the element from the subset of the specified range and return it as a new collection, and the subset returned is [Mindata,maxdata]

@param mindata the smallest element
The largest element of @param maxdata
@return returns a subset of results
*/
Template<classT,classArray>
Xset<T,array>Xset<T,array>:: Subset (ConstT&Mindata,ConstT&maxdata)Const
...{
ConstT*Max=&Maxdata;
ConstT*min=&Mindata;
if(Mindata>maxdata)
...{
Max = &mindata;
Min = &maxdata;
}
Xset<T,array>Retset;
Xint Ilength=m_data.getlength ();
for(Xint i= 0; I<Ilength;i++)
...{
const T & data = m_data[i];
if(data >= *min) && (Data <= *max)) retSet.m_Data.Append (data);
}
returnRetset;
}
}
#endif

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.