|
1 |
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
2 |
/* |
3 |
* Copyright (c) 2009 University of Applied Sciences Rapperswil, Switzerland (HSR) |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or modify |
6 |
* it under the terms of the GNU General Public License version 2 as |
7 |
* published by the Free Software Foundation; |
8 |
* |
9 |
* This program is distributed in the hope that it will be useful, |
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 |
* GNU General Public License for more details. |
13 |
* |
14 |
* You should have received a copy of the GNU General Public License |
15 |
* along with this program; if not, write to the Free Software |
16 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
17 |
* |
18 |
* Author: Fabian Mauchle <fabian.mauchle@hsr.ch> |
19 |
*/ |
20 |
|
21 |
#ifndef SEQUENCE_H |
22 |
#define SEQUENCE_H |
23 |
|
24 |
#include <stdint.h> |
25 |
|
26 |
/** |
27 |
* \breif generic sequence number |
28 |
* |
29 |
* LessOrEqualThan is defined as the compared number itself and the preceding |
30 |
* 2^(n-1) -1 numbers in the finite field of 2^n |
31 |
* |
32 |
* GreaterThan is defined as the proceding 2^(n-1) numbers in the finite field |
33 |
* of 2^n |
34 |
* |
35 |
*/ |
36 |
|
37 |
template <typename T> |
38 |
class Sequence |
39 |
{ |
40 |
public: |
41 |
Sequence () : seq(0) {} |
42 |
Sequence (const T s) : seq (s) { } |
43 |
operator T () const { return seq;} |
44 |
Sequence& operator= (const T s) { seq = s; return *this;} |
45 |
Sequence& operator+= (const T s) { seq += s; return *this;} |
46 |
Sequence operator++ () { seq++; return *this;} |
47 |
Sequence operator++ (int) { Sequence ss (seq); seq++; return ss;} |
48 |
Sequence& operator-= (const T s) { seq -= s; return *this;} |
49 |
Sequence operator-- () { seq--; return *this;} |
50 |
Sequence operator-- (int) { Sequence ss (seq); seq--; return ss;} |
51 |
|
52 |
T seq; |
53 |
}; |
54 |
|
55 |
template <typename T> |
56 |
bool operator<= (Sequence<T> a, Sequence<T> b) |
57 |
{ |
58 |
return (b - a) < (((Sequence<T>)(-1))>>1); |
59 |
} |
60 |
|
61 |
template <typename T> |
62 |
bool operator>= (Sequence<T> a, Sequence<T> b) |
63 |
{ |
64 |
return b <= a; |
65 |
} |
66 |
|
67 |
template <typename T> |
68 |
bool operator< (Sequence<T> a, Sequence<T> b) |
69 |
{ |
70 |
return ! (a >= b); |
71 |
} |
72 |
|
73 |
template <typename T> |
74 |
bool operator> (Sequence<T> a, Sequence<T> b) |
75 |
{ |
76 |
return b < a; |
77 |
} |
78 |
|
79 |
template <typename T> |
80 |
Sequence<T> operator+ (const Sequence<T> l, const Sequence<T> r) |
81 |
{ |
82 |
return Sequence<T> (l.seq + r.seq); |
83 |
} |
84 |
|
85 |
template <typename T> |
86 |
Sequence<T> operator+ (const Sequence<T> l, const T r) |
87 |
{ |
88 |
return Sequence<T> (l.seq + r); |
89 |
} |
90 |
|
91 |
template <typename T> |
92 |
Sequence<T> operator- (const Sequence<T> l, const Sequence<T> r) |
93 |
{ |
94 |
return Sequence<T> (l.seq - r.seq); |
95 |
} |
96 |
|
97 |
template <typename T> |
98 |
Sequence<T> operator- (const Sequence<T> l, const T r) |
99 |
{ |
100 |
return Sequence<T> (l.seq - r); |
101 |
} |
102 |
|
103 |
typedef Sequence<uint32_t> sequence32_t; |
104 |
typedef Sequence<uint16_t> sequence16_t; |
105 |
typedef Sequence<uint8_t> sequence8_t; |
106 |
|
107 |
#endif /* sequence_H_ */ |