-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathReorderX2Y.hpp
56 lines (47 loc) · 1.36 KB
/
ReorderX2Y.hpp
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
#ifndef ASU_REORDERX2Y
#define ASU_REORDERX2Y
#include<vector>
/*************************************************
* This C++ template reorder an input 2D array.
*
* X direction is the horizontal direction (the second
* index, increasing along the right hand side);
* Y direction is the vertical direction (the first
* index, increasing along downward);
*
* X2Y here means: input is first counted along the
* X direction, then return an array which is in the
* same dimension but counted along the Y direction.
*
* e.g.
*
* Input Output
*
* 1 2 3 1 3 5
* 4 5 6 --> 1 2 3 4 5 6 --> 2 4 6
*
* input(s):
* const vector<vector<T>> &p ---- Input 2D array.
*
* return(s):
* vector<vector<T>> ans ---- Reordered 2D array.
*
* Shule Yu
* Jan 21 2018
*
* Key words: reorder, x2y
*************************************************/
template <typename T>
std::vector<std::vector<T>> ReorderX2Y(const std::vector<std::vector<T>> &p){
std::vector<std::vector<T>> ans;
// Check array size.
if (p.empty() || p[0].empty()) return ans;
// fill in the output array.
int m=p.size(),n=p[0].size();
ans.resize(m,std::vector<T> (n,0));
for (int i=0;i<m;++i)
for (int j=0;j<n;++j)
ans[(i*n+j)%m][(i*n+j)/m]=p[i][j];
return ans;
}
#endif