//=========================================================================== // $Name: arts++-1-1-a12 $ // $Id: ArtsSelection.hh,v 1.2 2004/04/21 23:51:29 kkeys Exp $ //=========================================================================== // Copyright Notice // // By accessing this software, arts++, you are duly informed // of and agree to be bound by the conditions described below in this // notice: // // This software product, arts++, is developed by Daniel W. McRobb, and // copyrighted(C) 1998 by the University of California, San Diego // (UCSD), with all rights reserved. UCSD administers the CAIDA grant, // NCR-9711092, under which part of this code was developed. // // There is no charge for arts++ software. You can redistribute it // and/or modify it under the terms of the GNU Lesser General Public // License, Version 2.1, February 1999, which is incorporated by // reference herein. // // arts++ is distributed WITHOUT ANY WARRANTY, IMPLIED OR EXPRESS, OF // MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE or that the use // of it will not infringe on any third party's intellectual // property rights. // // You should have received a copy of the GNU Lesser General Public // License along with arts++. Copies can also be obtained from: // // http://www.gnu.org/copyleft/lesser.html // // or by writing to: // // Free Software Foundation, Inc. // 59 Temple Place, Suite 330 // Boston, MA 02111-1307 // USA // // Or contact: // // info@caida.org //=========================================================================== #ifndef _ARTSSELECTION_HH_ #define _ARTSSELECTION_HH_ #include //---------------------------------------------------------------------------- // template // class ArtsSelection : std::pair //---------------------------------------------------------------------------- //! This template just inherits from a pair and adds some //! specific behavior. An ArtsSelection may be used to store a single //! value or a range. //------------------------------------------------------------------------- /*! We use a single bit in \c _flags to indicate whether or not the value * is a range or a single item. * * This isn't really relevant to the class user, since \c _flags is * private. But I document the use anyway. */ //---------------------------------------------------------------------------- template class ArtsSelection : std::pair { public: //------------------------------------------------------------------------- //! If the value is a range, (_flags & k_isRangeMask) == 1 //------------------------------------------------------------------------- static const uint8_t k_isRangeMask = 0x01; //------------------------------------------------------------------------- // ArtsSelection() //......................................................................... //! constructor //------------------------------------------------------------------------- ArtsSelection() { this->_flags = 0; } //-------------------------------------------------------------------------- // ArtsSelection(const ArtsSelection & selection) //.......................................................................... //! copy constructor //-------------------------------------------------------------------------- ArtsSelection(const ArtsSelection & selection) { this->first = selection.first; this->second = selection.second; this->_flags = selection.Flags(); } //------------------------------------------------------------------------- // ArtsSelection(Type value) //......................................................................... //! Constructor for a single value. //------------------------------------------------------------------------- ArtsSelection(Type value) { this->first = value; this->_flags = 0; } //------------------------------------------------------------------------- // ArtsSelection(Type firstValue, Type lastValue) //......................................................................... //! Constructor for a value range selection. firstValue must be less //! than or equal to lastValue. The range is inclusive. //------------------------------------------------------------------------- ArtsSelection(Type firstValue, Type lastValue) { this->first = firstValue; this->second = lastValue; this->_flags = k_isRangeMask; } //------------------------------------------------------------------------- // ~ArtsSelection() //......................................................................... //! destructor //------------------------------------------------------------------------- ~ArtsSelection() { } //------------------------------------------------------------------------- // bool IsRange() const //......................................................................... //! Returns true if the selection is a range, else returns false. //------------------------------------------------------------------------- inline bool IsRange() const { if (this->_flags & k_isRangeMask) return(true); return(false); } //------------------------------------------------------------------------- // inline bool IsRange(bool isRange) //......................................................................... //! If isRange is true, sets the selection to a range, else sets //! the selection to a single value. //------------------------------------------------------------------------- inline bool IsRange(bool isRange) { if (isRange) this->_flags |= k_isRangeMask; else this->_flags &= (~ k_isRangeMask); return(isRange); } //-------------------------------------------------------------------------- // uint8_t Flags() const //.......................................................................... //! Returns the flags. //-------------------------------------------------------------------------- uint8_t Flags() const { return(this->_flags); } //------------------------------------------------------------------------- // void Value(const Type & value) //......................................................................... //! Sets the selection to a single value. //------------------------------------------------------------------------- void Value(const Type & value) { this->first = value; this->_flags = 0; } //------------------------------------------------------------------------- // void Value(const Type & firstValue, const Type & lastValue) //......................................................................... //! Sets the selection to a range [firstValue,lastValue], inclusive. //------------------------------------------------------------------------- void Value(const Type & firstValue, const Type & lastValue) { this->first = firstValue; this->second = lastValue; this->_flags = k_isRangeMask; } //------------------------------------------------------------------------- // bool Matches(const Type & value) const //......................................................................... //! Returns true if value matches our internal value (is equal to our //! first value if we're a single value, or is within the range //! specified by our [first,second] pair if we're a range). Returns //! false otherwise. //------------------------------------------------------------------------- bool Matches(const Type & value) const { if (this->_flags & k_isRangeMask) { return(value >= this->first && value <= this->second); } return(value == this->first); } //------------------------------------------------------------------------- // bool operator == (const ArtsSelection & selection) const //......................................................................... //! Overloaded '==' operator. //------------------------------------------------------------------------- bool operator == (const ArtsSelection & selection) const { if (this->_flags & k_isRangeMask) { return(this->first == selection.first && this->second == selection.second); } return(this->first == selection.first); } //------------------------------------------------------------------------- // bool operator < (const ArtsSelection & selection) const //......................................................................... //! Overloaded '<' operator, used for sorting a vector of //! ArtsSelection objects so that 2 vectors of ArtsSelection //! objects may be compared. //------------------------------------------------------------------------- bool operator < (const ArtsSelection & selection) const { if (this->first < selection.first) { return(true); } if (this->IsRange()) { if (this->second < selection.second) { return(true); } } return(false); } private: uint8_t _flags; }; #endif // _ARTSSELECTION_HH_