source: src/Helpers/Range.hpp@ 6b898c

Last change on this file since 6b898c was 6b898c, checked in by Frederik Heber <heber@…>, 15 years ago

Added operator<< to range<> and unit test.

  • Library version is now 6:0:1, API version is 1.0.13.
  • Property mode set to 100644
File size: 1.7 KB
Line 
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
16template <class T>
17struct range {
18 range(const T&,const T&);
19 bool isInRange(const T&) const;
20 bool isBefore(const T&) const;
21 bool isBeyond(const T&) const;
22
23 T first;
24 T last;
25};
26
27template <class T>
28inline range<T>::range(const T &_first,const T &_last) :
29first(_first),last(_last)
30{}
31
32template <class T>
33inline bool range<T>::isInRange(const T &value) const{
34 return first <= value && value < last;
35}
36
37template <class T>
38inline bool range<T>::isBefore(const T &value) const{
39 return value < first;
40}
41
42template <class T>
43inline bool range<T>::isBeyond(const T &value) const{
44 return last <= value;
45}
46
47template <class T>
48inline range<T> makeRange(const T&first, const T&last){
49 return range<T>(first,last);
50}
51
52template <class T>
53inline bool operator<(const range <T> &x, const range<T> &y){
54 return (x.first!=y.first)?x.first<y.first:x.last<y.last;
55}
56
57template <class T>
58inline bool operator==(const range<T> &x,const range<T> &y){
59 return x.first==y.first && x.last==y.last;
60}
61
62template <class T>
63inline bool operator!= (const range<T>& x, const range<T>& y) { return !(x==y); }
64template <class T>
65inline bool operator> (const range<T>& x, const range<T>& y) { return y<x; }
66template <class T>
67inline bool operator<=(const range<T>& x, const range<T>& y) { return !(y<x); }
68template <class T>
69inline bool operator>= (const range<T>& x, const range<T>& y) { return !(x<y); }
70template <class T>
71std::ostream &operator<< (std::ostream &ost, const range<T>& y)
72{
73 ost << "[" << y.first << ";" << y.last << "]";
74 return ost;
75}
76
77#endif /* RANGE_HPP_ */
Note: See TracBrowser for help on using the repository browser.