Commit 464079a3 authored by thomas.forbriger's avatar thomas.forbriger Committed by thomas.forbriger
Browse files

new range

This is a legacy commit from before 2015-03-01.
It may be incomplete as well as inconsistent.
See COPYING.legacy and README.history for details.


SVN Path:     http://gpitrsvn.gpi.uni-karlsruhe.de/repos/TFSoftware/trunk
SVN Revision: 1107
SVN UUID:     67feda4a-a26e-11df-9d6e-31afc202ad0c
parent 50af355b
/*! \file range.h
* \brief deal with number ranges
*
* ----------------------------------------------------------------------------
*
* $Id: range.h,v 1.1 2002-11-27 14:58:56 forbrig Exp $
* \author Thomas Forbriger
* \date 09/06/2002
*
* deal with number ranges
*
* Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt)
*
* REVISIONS and CHANGES
* - 09/06/2002 V1.0 Thomas Forbriger
* - 27/11/2002 V1.1 copied and modified from libcontxx sources
*
* ============================================================================
*/
// include guard
#ifndef TFXX_RANGE_H_VERSION
#define TFXX_RANGE_H_VERSION \
"TFXX_RANGE_H V1.0 "
#define TFXX_RANGE_H_CVSID \
"$Id: range.h,v 1.1 2002-11-27 14:58:56 forbrig Exp $"
namespace tfxx {
/*! \brief A class to deal with numerical ranges
*
* \param T type of value (default=int)
*/
template<class T=int>
class Range {
public:
//! element type
typedef T Tvalue;
//! empty range
Range(): Mfirst(0), Mlast(0) { }
//! range containing exactly one element
Range(const Tvalue& index): Mfirst(index), Mlast(index) { }
//! set range
Range(const Tvalue& first, const Tvalue& last):
Mfirst(first<last ? first:last),
Mlast(last>first ? last:first) { }
//! access start of range
Tvalue& first() { return Mfirst; }
//! access end of range
Tvalue& last() { return Mlast; }
//! read start of range
const Tvalue& first() const { return Mfirst; }
//! read end of range
const Tvalue& last() const { return Mlast; }
//! is this range inside the other
bool isinside(const Range& other) const
{ return ((Mfirst >= other.first()) && (Mlast <= other.last())); }
//! does this range contain the other
bool contains(const Range& other) const
{ return ((Mfirst <= other.first()) && (Mlast >= other.last())); }
//! shrink to smaller of this and the other
Range& shrink(const Range& other)
{
Mfirst=Mfirst>other.first() ? Mfirst:other.first();
Mlast=Mlast<other.last() ? Mlast:other.last();
return *this;
}
//! expand to larger of this and the other
Range& expand(const Range& other)
{
Mfirst=Mfirst<other.first() ? Mfirst:other.first();
Mlast=Mlast>other.last() ? Mlast:other.last();
return *this;
}
//! shift by n
Range& shift(const Tvalue& n)
{ Mfirst+=n; Mlast+=n; return *this; }
//! shift by n
Range& operator+=(const Tvalue& n)
{ return this->shift(n); }
//! shift by -n
Range& operator-=(const Tvalue& n)
{ return this->shift(-n); }
private:
Tvalue Mfirst; //!< start of range.
Tvalue Mlast; //!< end of range.
}; // class Range
} // namespace tfxx
#endif // TFXX_RANGE_H_VERSION (includeguard)
/* ----- END OF range.h ----- */
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment