-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathshift.h
executable file
·64 lines (55 loc) · 1.01 KB
/
shift.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef UTILS_SHIFT_H
#define UTILS_SHIFT_H
#include <vector>
/** @file utils/shift.h
* @ingroup Utils
* @brief Cyclical shifts of array elements
*/
/** @addtogroup Utils */
/*@{*/
///Shift forward: set a'=c,b'=a,c'=b
template <class T>
void ShiftForward(T& a, T& b, T& c)
{
T temp=c;
c=b;
b=a;
a=temp;
}
///Shift backward: set a'=b,b'=c,c'=a
template <class T>
void ShiftBackward(T& a, T& b, T& c)
{
T temp=a;
a=b;
b=c;
c=temp;
}
///For all i, v'[i] = v[i-1], v'[0] = v[n-1]
template <class T>
void ShiftForward(std::vector<T>& v)
{
if(v.empty()) return;
T temp=v.back();
std::vector<int>::iterator i,prev;
for(i=--v.end();i!=v.begin();i--) {
prev=i; prev--;
*i = *prev;
}
v.front()=temp;
}
///For all i, v'[i] = v[i+1], v'[n-1] = v[0]
template <class T>
void ShiftBackward(std::vector<T>& v)
{
if(v.empty()) return;
T temp=v.front();
std::vector<int>::iterator i,next;
for(i=v.begin();next!=--v.end();i++) {
next=i; next++;
*i = *next;
}
v.back()=temp;
}
/*@}*/
#endif