📄 ofoset.h
字号:
/* * * Copyright (C) 2002-2005, OFFIS * * This software and supporting documentation were developed by * * Kuratorium OFFIS e.V. * Healthcare Information and Communication Systems * Escherweg 2 * D-26121 Oldenburg, Germany * * THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND OFFIS MAKES NO WARRANTY * REGARDING THE SOFTWARE, ITS PERFORMANCE, ITS MERCHANTABILITY OR * FITNESS FOR ANY PARTICULAR USE, FREEDOM FROM ANY COMPUTER DISEASES OR * ITS CONFORMITY TO ANY SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND * PERFORMANCE OF THE SOFTWARE IS WITH THE USER. * * Module: ofstd * * Author: Thomas Wilkens * * Purpose: Template class for administrating an ordered set of elements * of an arbitrary type. * * Last Update: $Author: meichel $ * Update Date: $Date: 2005/12/08 16:06:00 $ * Source File: $Source: /share/dicom/cvs-depot/dcmtk/ofstd/include/dcmtk/ofstd/ofoset.h,v $ * CVS/RCS Revision: $Revision: 1.10 $ * Status: $State: Exp $ * * CVS/RCS Log at end of file * */#ifndef OFOrderedSet_h#define OFOrderedSet_h#include "dcmtk/config/osconfig.h"#include "dcmtk/ofstd/oftypes.h"#include "dcmtk/ofstd/ofset.h"/** This template class provides a data structure and operations for administrating an * ordered set of elements of an arbitrary type. Note the following properties of this * class: * - an element which is inserted into the set will be copied * - the datatype of the set's elements has to support operator== so that it is possible * to find a certain element * - it is allowed to insert identical elements into the set * - if a user requires to remove a certain element and if there are several elements * which are identical to this element, only one element will be removed from the set * - when removing an element, the indeces of the elements behind the removed element will * be reduced by one * - the set will be ordered according to the point in time at which an element is inserted * into the set; a new element will always be inserted at the end of the set */template <class T> class OFOrderedSet : public OFSet<T>{ protected: public: /** Default constructor. */ OFOrderedSet() : OFSet<T>() { } /** Copy constructor. * @param src Source object of which this will be a copy. */ OFOrderedSet( const OFOrderedSet<T> &src ) : OFSet<T>( src ) { } /** Destructor. */ virtual ~OFOrderedSet() { } /** operator=. * @param src Source object whose values will be assigned to this. * @return Reference to this. */ const OFOrderedSet<T> &operator=( const OFOrderedSet<T> &src ) { return( assign( src ) ); } /** This function is a workaround for avoiding a compiler warning on * Solaris 2.5.1 using compiler SC 2.0.1. */ const OFOrderedSet<T> &assign( const OFOrderedSet<T> &src ) { if( this != &src ) this->operator=( src ); return( *this ); } /** Determines if two sets are identical. Note that for ordered sets * not only their elements have to be identical, but also the order * of their elements has to be identical. * @param other Set which shall be compared with this. * @return OFTrue if sets are identical, OFFalse otherwise. */ virtual OFBool operator==( const OFOrderedSet<T> &other ) const { // check if both sets contain the same // amount of items; if not, return OFFalse if( this->num != other.num ) return( OFFalse ); // initialize result with OFTrue OFBool result = OFTrue; // as long as result is OFTrue go through all items in this for( unsigned int i=0 ; i < this->num && result == OFTrue ; i++ ) { // in case the current element does not equal the current // element in other, result shall be set to OFFalse if( *this->items[i] != *other.items[i] ) result = OFFalse; } // return result return( result ); } /** Determines if two sets are not identical. * @param other Set which shall be compared with this. * @return OFTrue if sets are not identical, OFFalse otherwise. */ virtual OFBool operator!=( const OFOrderedSet<T> &other ) const { return( !( *this == other ) ); } /** Inserts a new item into the set. * @param item Item which shall be inserted into the set. */ virtual void Insert( const T &item ) { // if size equals num, we need more space if( this->size == this->num ) Resize( this->size * 2 ); // copy item T *newItem = new T( item ); // insert copy into array this->items[this->num] = newItem; // increase counter this->num++; } /** Inserts all items of another set into this set. * @param other set whose items shall be inserted into the set. */ virtual void Insert( const OFOrderedSet<T> &other ) { // go through all items in other and insert each item into this for( unsigned int i=0 ; i<other.num ; i++ ) Insert( *other.items[i] ); } /** Inserts a new item at a certain position into the set. * @param item Item which shall be inserted into the set. * @param idx Index of the position at which the item shall be inserted. * The first position has index 0. Note that in case index * is greater than the index of the last item, the new item will * be inserted right behind the last item of the set. */ virtual void InsertAt( const T &item, unsigned int idx ) { unsigned int i; // in case index is greater than the index of the last item, // insert the new item right behind the last item of the set if( idx > this->num - 1 ) Insert( item ); else { // if size equals num, we need more space if( this->size == this->num ) Resize( this->size * 2 ); // copy item T *newItem = new T( item ); // create a new temporary array and assign all pointers correspondingly T **tmp = new T*[this->size]; for( i=0 ; i<idx ; i++ ) tmp[i] = this->items[i]; tmp[idx] = newItem; for( i=idx ; i < this->size - 1 ; i++ ) { if( i < this->num ) tmp[i+1] = this->items[i]; else tmp[i+1] = NULL; } // delete old array delete this->items; // assign new array to member variable this->items = tmp; // increase counter this->num++; } } /** Removes one item from the set. * @param item Item which shall be inserted into the set. */ virtual void Remove( const T &item ) { // so far, nothing was deleted OFBool itemDeleted = OFFalse; // go through all items for( unsigned int i=0 ; i < this->num && !itemDeleted ; i++ ) { // if current item is the one which shall be deleted if( *this->items[i] == item ) { // delete item delete this->items[i]; // and - so that there are no holes in the array - move all elements // behind the current element up one array field; only do so in case // we did _not_ delete the last item if( i != this->num - 1 ) { unsigned int j; for( j=i+1 ; j < this->num ; j++ ) { this->items[j-1] = this->items[j]; } this->items[j-1] = NULL; } else this->items[i] = NULL; // reduce counter this->num--; // remember that an item was deleted (so that always only one item will be deleted) itemDeleted = OFTrue; } } } /** Removes one item from the set. * @param idx Index of the item which shall be removed from the set.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -