| 1 | /*
|
|---|
| 2 | * Range.hpp
|
|---|
| 3 | *
|
|---|
| 4 | * Created on: Jul 22, 2010
|
|---|
| 5 | * Author: crueger
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | #ifndef RANGE_HPP_
|
|---|
| 9 | #define RANGE_HPP_
|
|---|
| 10 |
|
|---|
| 11 | // include config.h
|
|---|
| 12 | #ifdef HAVE_CONFIG_H
|
|---|
| 13 | #include <config.h>
|
|---|
| 14 | #endif
|
|---|
| 15 |
|
|---|
| 16 | #include <iosfwd>
|
|---|
| 17 |
|
|---|
| 18 | #include "LinearAlgebra/Vector.hpp"
|
|---|
| 19 |
|
|---|
| 20 | template <class T>
|
|---|
| 21 | struct range {
|
|---|
| 22 | range(const T&,const T&);
|
|---|
| 23 | bool isInRange(const T&) const;
|
|---|
| 24 | bool isBefore(const T&) const;
|
|---|
| 25 | bool isBeyond(const T&) const;
|
|---|
| 26 |
|
|---|
| 27 | T first;
|
|---|
| 28 | T last;
|
|---|
| 29 | };
|
|---|
| 30 |
|
|---|
| 31 | template <class T>
|
|---|
| 32 | inline range<T>::range(const T &_first,const T &_last) :
|
|---|
| 33 | first(_first),last(_last)
|
|---|
| 34 | {}
|
|---|
| 35 |
|
|---|
| 36 | template <class T>
|
|---|
| 37 | inline bool range<T>::isInRange(const T &value) const{
|
|---|
| 38 | return first <= value && value < last;
|
|---|
| 39 | }
|
|---|
| 40 |
|
|---|
| 41 | template <class T>
|
|---|
| 42 | inline bool range<T>::isBefore(const T &value) const{
|
|---|
| 43 | return value < first;
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | template <class T>
|
|---|
| 47 | inline bool range<T>::isBeyond(const T &value) const{
|
|---|
| 48 | return last <= value;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | template <class T>
|
|---|
| 52 | inline range<T> makeRange(const T&first, const T&last){
|
|---|
| 53 | return range<T>(first,last);
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | template <class T>
|
|---|
| 57 | inline bool operator<(const range <T> &x, const range<T> &y){
|
|---|
| 58 | return (x.first!=y.first)?x.first<y.first:x.last<y.last;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | template <class T>
|
|---|
| 62 | inline bool operator==(const range<T> &x,const range<T> &y){
|
|---|
| 63 | return x.first==y.first && x.last==y.last;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | template <class T>
|
|---|
| 67 | inline bool operator!= (const range<T>& x, const range<T>& y) { return !(x==y); }
|
|---|
| 68 | template <class T>
|
|---|
| 69 | inline bool operator> (const range<T>& x, const range<T>& y) { return y<x; }
|
|---|
| 70 | template <class T>
|
|---|
| 71 | inline bool operator<=(const range<T>& x, const range<T>& y) { return !(y<x); }
|
|---|
| 72 | template <class T>
|
|---|
| 73 | inline bool operator>= (const range<T>& x, const range<T>& y) { return !(x<y); }
|
|---|
| 74 | template <class T>
|
|---|
| 75 | std::ostream &operator<< (std::ostream &ost, const range<T>& y)
|
|---|
| 76 | {
|
|---|
| 77 | ost << "[" << y.first << ";" << y.last << "]";
|
|---|
| 78 | return ost;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 | /******************************** Vector ********************************/
|
|---|
| 83 | /* template specialization for Vector */
|
|---|
| 84 | /************************************************************************/
|
|---|
| 85 |
|
|---|
| 86 | class Vector;
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 | template <>
|
|---|
| 90 | inline bool range<Vector>::isInRange(const Vector &value) const{
|
|---|
| 91 | return first[0] <= value[0] && value[0] < last[0] &&
|
|---|
| 92 | first[1] <= value[1] && value[1] < last[1] &&
|
|---|
| 93 | first[2] <= value[2] && value[2] < last[2];
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | template <>
|
|---|
| 97 | inline bool range<Vector>::isBefore(const Vector &value) const{
|
|---|
| 98 | return value[0] < first[0] || value[1] < first[1] || value[2] < first[2];
|
|---|
| 99 | }
|
|---|
| 100 |
|
|---|
| 101 | template <>
|
|---|
| 102 | inline bool range<Vector>::isBeyond(const Vector &value) const{
|
|---|
| 103 | return last[0] <= value[0] || last[1] <= value[1] || last[2] <= value[2];
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | #endif /* RANGE_HPP_ */
|
|---|