view src/range.hpp @ 0:add35537fdbb tip

Initial import
author irh <ian.r.hobson@gmail.com>
date Thu, 25 Aug 2011 11:05:55 +0100
parents
children
line wrap: on
line source
//  Copyright 2011, Ian Hobson.
//
//  This file is part of gpsynth.
//
//  gpsynth is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  gpsynth is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with gpsynth in the file COPYING. 
//  If not, see http://www.gnu.org/licenses/.

#pragma once

#include <ostream>

namespace stdx {

// like std::pair, designed for minimum/maximum
template<typename T>
struct Range {
  T minimum_;
  T maximum_;

  Range()
  : minimum_(T()),
    maximum_(T())
  {}
  
  Range(T minimum, T maximum)
  : minimum_(minimum),
    maximum_(maximum)
  {}

  template<typename T2>
  Range(const Range<T2>& other)
  : minimum_(other.minimum_),
    maximum_(other.maximum_)
  {}

  // getters / setters
  T Minimum() const { return minimum_; }
  T Maximum() const { return maximum_; }

  void SetMinimum(const T& value) { minimum_ = value; }
  void SetMaximum(const T& value) { maximum_ = value; }

  void SetRange(const T& minimum, const T& maximum) {
    minimum_ = minimum;
    maximum_ = maximum;
  }

  T Size() const { return maximum_ - minimum_; }
  
  // Assignment operators
  template<typename T2>
  void operator=(const Range<T2>& other) {
    minimum_ = other.minumum_;
    maximum_ = other.maximum_;
  }

  template<typename T2>
  Range<T>& operator+=(const Range<T2>& other) {
    minimum_ += other.minimum_;
    maximum_ += other.maximum_;
    return *this;
  }
  template<typename T2>
  Range<T>& operator+=(const T2& value) {
    minimum_ += value;
    maximum_ += value;
    return *this;
  }

  template<typename T2>
  Range<T>& operator-=(const Range<T2>& other) {
    minimum_ -= other.minimum_;
    maximum_ -= other.maximum_;
    return *this;
  }
  template<typename T2>
  Range<T>& operator-=(const T2& value) {
    minimum_ -= value;
    maximum_ -= value;
    return *this;
  }

  template<typename T2>
  Range<T>& operator*=(const T2& value) {
    minimum_ *= value;
    maximum_ *= value;
    return *this;
  }
  template<typename T2>
  Range<T>& operator/=(const T2& value) {
    minimum_ /= value;
    maximum_ /= value;
    return *this;
  }


  // Arithmetic
  template<typename T2>
  Range<T> operator+(const T2& value) {
    return Range<T>(minimum_ + value, maximum_ + value);
  }
  template<typename T2>
  Range<T> operator+(const Range<T2>& other) {
    return Range<T>(minimum_ + other.minimum_, maximum_ + other.maximum_);
  }

  template<typename T2>
  Range<T> operator-(const T2& value) {
    return Range<T>(minimum_ - value, maximum_ - value);
  }
  template<typename T2>
  Range<T> operator-(const Range<T2>& other) {
    return Range<T>(minimum_ - other.minimum_, maximum_ - other.maximum_);
  }

  template<typename T2>
  Range<T> operator*(const T2& value) {
    return Range<T>(minimum_ * value, maximum_ * value);
  }
  template<typename T2>
  Range<T> operator/(const T2& value) {
    return Range<T>(minimum_ / value, maximum_ / value);
  }

  // Increments/Decrements
  Range<T>& operator++() {
    ++minimum_;
    ++maximum_;
    return *this;
  }
  Range<T>& operator--() {
    --minimum_;
    --maximum_;
    return *this;
  }

  Range<T> operator++(int) {
    Range<T> temp(*this);
    ++(*this);
    return temp;
  }
  Range<T> operator--(int) {
    Range<T> temp(*this);
    --(*this);
    return temp;
  }

};

// Comparison operators
template<typename T>
bool operator==(const Range<T>& x, const Range<T>& y) {
  return (x.minimum_ == y.minimum_) && (x.maximum_ == y.maximum_);
}

template<typename T>
bool operator<(const Range<T>& x, const Range<T>& y) {
  return (x.minimum_ < y.minimum_)
         || (!(y.minimum_ < x.minimum_) && (x.maximum_ < y.maximum_));
}

template<typename T>
bool operator!=(const Range<T>& x, const Range<T>& y) {
  return !(x == y);
}

template<typename T>
bool operator>(const Range<T>& x, const Range<T>& y) {
  return y < x;
}

template<typename T>
bool operator<=(const Range<T>& x, const Range<T>& y) {
  return !(y < x);
}

template<typename T>
bool operator>=(const Range<T>& x, const Range<T>& y) {
  return !(x < y);
}

// Output
template<typename T>
std::ostream& operator<<(std::ostream& out, const Range<T>& range) {
  out << '[' << range.minimum_ << ',' << range.maximum_ << ']';
  return out;
}

} // stdx namespace