在多维数组的数组偏移计算(列VS大排) [英] array offset calculations in multi dimensional array (column vs row major)
问题描述
一个教科书最近,我读讨论列重大和放大器;列为主阵列。这本书主要集中在1和2维数组,但并没有真正讨论3维数组。我在找一些很好的例子,以帮助巩固我使用行主要与放一个多维数组中的寻址元素的理解;列为主阵列。
+ - + - + - + |
/ / / / | |
+ - + - + - + | + --- + --- + --- + --- +
/ / / / | / | | / / / / / |
+ - + - + - + + | + --- + --- + --- + --- + +
/ / / / | / | / | | / / / / / | / |
+ - + - + - + + + + | + --- + --- + --- + --- + + +
/ / / / | / | / | / | | / / / / / | / | / |
+ - + - + - + + + + + | + --- + --- + --- + --- + + + +
/ / / / | / | / | / | / | | 000 | 001 | 002 | 003 | / | / | / |
+ - + - + - + + + + + | + --- + --- + --- + --- + + + +
| 00 | 01 | 02 | / | / | / | / | | 004 | 005 | 006 | 007 | / | / | / |
+ - + - + - + + + + | + --- + --- + --- + --- + + + +
| 03 | 04 | 05 | / | / | / | | 008 | 009 | 00A | 00B | / | / | /
+ - + - + - + + | + --- + --- + --- + --- + + +
| 06 | 07 | 08 | / | / | | 00C | 00D | 00E | 00F | / | /
+ - + - + - + | + --- + --- + --- + --- + +
| 09 | 0A | 0B | / | | 010 | 011 | 012 | 013 | /
+ - + - + - + | + --- + --- + --- + --- +
改编[5] [3] [4] |改编[3] [4] [5]
注意:的原题正确重新presented ARR [3] [4] [5]。我了解到,原来标重新presents深度。这些数据已得到纠正,以反映预期的阵列重新presentation。
十六进制数据示例
+ --- + --- + --- + --- + + --- + --- + --- + --- + + --- + --- + --- + --- +
| 000 | 001 | 002 | 003 | | 100 | 101 | 102 | 103 | | 200 | 201 | 202 | 203 |
+ --- + --- + --- + --- + + --- + --- + --- + --- + + --- + --- + --- + --- +
| 004 | 005 | 006 | 007 | | 104 | 105 | 106 | 107 | | 204 | 205 | 206 | 207 |
+ --- + --- + --- + --- + + --- + --- + --- + --- + + --- + --- + --- + --- +
| 008 | 009 | 00A | 00B | | 108 | 109 | 10A | 10B | | 208 | 209 | 20A | 20B |
+ --- + --- + --- + --- + + --- + --- + --- + --- + + --- + --- + --- + --- +
| 00C | 00D | 00E | 00F | | 10C | 10D | 10E | 10F | | 20 | 20D | 20E | 20F |
+ --- + --- + --- + --- + + --- + --- + --- + --- + + --- + --- + --- + --- +
| 010 | 011 | 012 | 013 | | 110 | 111 | 112 | 113 | | 210 | 211 | 212 | 213 |
+ --- + --- + --- + --- + + --- + --- + --- + --- + + --- + --- + --- + --- +
片0片1片2 短编曲[3] [4] [5]; //假设数组充满了十六进制的测试数据 改编[1] [2] [3] = 0x10B使用切片1,行2,列3
ARR [2] [3] [4] = 0x210使用切片2,行3,列4
解析为第4行,列0
行主要结果
{000,001,002,003,004,005,006,007,008,009,00A,00B,00C,00D,00E,00F,010011012013,
100,101,102,103,104,105,106,107,108,109,10A,10B,10C,10D,10E,10F,110111112113,
200,201,202,203,204,205,206,207,208,209,20A,20B,20C,20D,20E,20F,210211212213}
列为主
{000,004,008,00C,010,001,005,009,00D,011,002,006,00A,00E,012,003,007,00B,00F,013,
100,104,108,10C,110,101,105,109,10D,111,102,106,10A,10E,112,103,107,10B,10F,113,
200,204,208,20C,210,201,205,209,20D,211,202,206,20A,20E,212,203,207,20B,20F,213}
计算[1] [2] [3]使用行主要偏移ARR偏置?
计算ARR偏移[1] [2] [3]使用柱主要偏移?
当我问这个问题,我希望能找到一些不错的3维数组的例子。尤其是code的例子。因为我没有找到任何东西,是可以理解的,我决定创建一个小的C程序,以帮助显示的概念。它采用相同的测试数据在一个3x4x5阵列。它还包括一个5x5x5阵列试验数据。它创建从该行主要阵列列重大阵列使偏移计算可以得到验证。
搜索结果
数组偏移方法是:
字符* calc_RowMajor(字符*基地,诠释elemSz,诠释depth_idx,诠释row_idx,诠释col_idx)
搜索结果
我加了code意见适用于帮助澄清一下code在做什么。
//
// Arrays.cpp:
//目的:显示rowMajor&安培; colMajor数据和计算。
//
的#includestdafx.h中#定义_show_Arrays 1 // 1 =显示rowMajor&colMajor阵列
的#define _square_array 0 // 1 =使用改编[5] [5] [5],0 =使用改编[3] [4] [5]的#if(_square_array == 1)
const int的depthSz = 5;
const int的rowSz = 5;
const int的colSz = 5;
/ *
+ --- + --- + --- + --- + --- +
| X00 | X01 | X02 | X03 | X04 |
+ --- + --- + --- + --- + --- +
| X05 | X06 | X07 | X08 | X09 |
+ --- + --- + --- + --- + --- +
| X0A | X0B | X0C | X0D | x0E |
+ --- + --- + --- + --- + --- +
| X0F | X10 | X11 | X12 | X13 |
+ --- + --- + --- + --- + --- +
| X14 | X15 | X16 | X17 | X18 |
+ --- + --- + --- + --- + --- +
片X
* /
短row_arr [depthSz] [colSz] [rowSz] = {
{/ *片0 * /
{} 0x000,0x001,0x002,0x003,0x004,
{} 0x005,0x006,0x007,0x008,0x009,
{0x00A,0x00B,量0x00C,0x00D,0x00E},
{0x00F,0x010,0x011,0x012,0x013},
{0x014,0x015,0x016,0x017,0x018}},
{/ *片1 * /
{} 0x100,0x101,0x102,0x103,0x104,
{} 0x105,0x106,0x107,0x108,0x109,
{0x10A,0x10B,0x10C,0x10D,0x10E},
{0x10F,0x110,0x111,0x112,0x113},
{0x114,0x115,0x116,0x117,0x118}},
{/ *切片2 * /
{} 0x200,0x201,0x202,0x203,0x204,
{} 0x205,0x206,0x207,0x208,0x209,
{0x20A,0x20B,0x20C,0x20D,0x20E},
{0x20F,0x210,0x211,0x212,0x213},
{0x214,0x215,0x216,0x217,0x218}},
{/ *片3 * /
{} 0x300,0x301,0x302,0x303,0x304,
{} 0x305,0x306,0x307,0x308,0x309,
{0x30A,0x30B,0x30C,0x30D,0x30E},
{0x30F,0x310,0x311,0x312,0x313},
{0x314,0x315,0x316,0x317,0x318}},
{/ *片4 * /
{} 0x400,0x401,0x402,0x403,0x404,
{} 0x405,0x406,0x407,0x408,0x409,
{0x40A,0x40B,量0x40C,0x40D,0x40E},
{0x40F,0x410,0x411,0x412,0x413},
{0x414,0x415,0x416,0x417,0x418}}
};#其他
const int的depthSz = 3;
const int的rowSz = 4;
const int的colSz = 5;
/ *
+ --- + --- + --- + --- +
| 000 | 001 | 002 | 003 |
+ --- + --- + --- + --- +
| 004 | 005 | 006 | 007 |
+ --- + --- + --- + --- +
| 008 | 009 | 00A | 00B |
+ --- + --- + --- + --- +
| 00C | 00D | 00E | 00F |
+ --- + --- + --- + --- +
| 010 | 011 | 012 | 013 |
+ --- + --- + --- + --- +
片X
* /
短row_arr [depthSz] [colSz] [rowSz] = {
{/ *片0 * /
{} 0x000,0x001,0x002,0x003,
{} 0x004,0x005,0x006,0x007,
{0x008,0x009,0x00A,0x00B},
{量0x00C,0x00D,0x00E,0x00F},
{0x010,0x011,0x012,0x013}},
{/ *片1 * /
{} 0x100,0x101,0x102,0x103,
{} 0x104,0x105,0x106,0x107,
{0x108,0x109,0x10A,0x10B},
{0x10C,0x10D,0x10E,0x10F},
{0x110,0x111,0x112,0x113}},
{/ *切片2 * /
{} 0x200,0x201,0x202,0x203,
{} 0x204,0x205,0x206,0x207,
{0x208,0x209,0x20A,0x20B},
{0x20C,0x20D,0x20E,0x20F},
{0x210,0x211,0x212,0x213}}
};
#万一
短col_arr [depthSz * colSz * rowSz]。 //字符* calc_RowMajor(字符*基地,诠释elemSz,诠释depth_idx,诠释row_idx,诠释col_idx)
{//行主要部分是用行导航
字符*地址;
INT LBOUND = 0; //下限(0从零开始的数组)
地址=基/ *使用基地通过了* /
+((depth_idx-LBOUND)*(* colSz * rowSz elemSz))/ *选择切片* /
+((row_idx-LBOUND)* rowSz * elemSz)/ *选择行* /
+((col_idx-LBOUND)* elemSz); / *选择山坳* /
退货地址;
}
字符* calc_ColMajor(字符*基地,诠释elemSz,诠释depth_idx,诠释col_idx,诠释row_idx)
{//山坳主要部分是用导航栏
字符*地址;
INT LBOUND = 0; //下限(0从零开始的数组)
INT pageSz = colSz * rowSz * elemSz;
INT抵消; 偏移量=(col_idx-LBOUND)*(* colSz elemSz)/ *选择列* /
+(row_idx-LBOUND)*(elemSz); / *选择行* /
如果(偏移> = pageSz)
{//页面溢出,翻转
偏移 - =(pageSz-elemSz); / *做出调整偏移返回到页* /
}
地址=基/ *使用基地通过了* /
+((depth_idx-LBOUND)* pageSz)/ *选择切片* /
+偏移;
退货地址;
}无效disp_slice(字符* PSTR,短*帕尔,诠释片,诠释COLS,诠释行)
{
的printf(==%S切片%d个==%P \\ r \\ n,PSTR,切片,帕尔+(片*行* COLS));
为(中间体X = 0; X&下;行; X ++)
{
对于(INT Y = 0; Y< COLS; Y ++)
的printf(%03X,*(帕尔+(片*行* COLS)+(X * COLS)+ Y));
的printf(\\ r \\ n);
}
}INT _tmain(INT ARGC,_TCHAR *的argv [])
{
//使用基于排阵数据初始化基于阵列的山坳
{// row_arr转换成col_arr
短* PSRC = row_arr [0] [0] [0];
短* pDst = col_arr [0];
对于(INT D = 0; D< depthSz; d ++)
对于(INT R = 0;为r rowSz; R ++)
对于(INT C = 0;℃下colSz; C ++)
{
* pDst ++ = *(PSRC +((D * rowSz * colSz)+(C * rowSz)+ R));
}
} 的printf(使用数组[%D]。[%D]。[%D]。\\ r \\ n,depthSz,rowSz,colSz);的#if(_show_Arrays == 1)
{为(中间体X = 0; X&下; depthSz; X ++){disp_slice(rowMajor,&row_arr [0] [0] [0]中,x,rowSz,colSz);}}
{为(中间体X = 0; X&下; depthSz; X ++){disp_slice(colMajor,&col_arr [0]中,x,rowSz,colSz);}}
#万一 INT D = 2; //深度
INT R = 3; //行
INT C = 4; //列 为(D = 0; D< depthSz; d ++)
{
C = R = D组; //简单的访问测试图案改编[0] [0] [0],ARR [1] [1] [1],ARR [2] [2] [2],...
{//获取数组元素
的printf(row_arr [%D]。[%D]。[%d个] =%X \\ t的,D,R,C,row_arr [D]。[R] [C]);
的printf(&row_arr [%D]。[%D]。[%D]。=%P \\ r \\ n,D,R,C&row_arr [D]。[R] [C]);
}
{//获取RowMajor元
短* pRowMajor =(短*)calc_RowMajor((字符*)row_arr [0] [0] [0]的sizeof(短),D,R,C);
的printf(calc_RowMajor(%D,%D,%D)=%X \\ t \\ t的,D,R,C,* pRowMajor);
的printf(pRowMajor =%P \\ r \\ n,pRowMajor);
}
{//获取ColMajor元
短* pColMajor =(短*)calc_ColMajor((字符*)col_arr [0]的sizeof(短),D,C,R);
的printf(calc_ColMajor(%D,%D,%D)=%X \\ t \\ t的,D,R,C,* pColMajor);
的printf(pColMajor =%P \\ r \\ n,pColMajor);
}
} //对 的getchar(); //仅仅是保持控制台,而看的信息
返回0;
}
A textbook I recently read discussed row major & column major arrays. The book primarily focused on 1 and 2 dimensional arrays but didn't really discuss 3 dimensional arrays. I'm looking for some good examples to help solidify my understanding of addressing an element within a multi-dimensional array using row major & column major arrays.
+--+--+--+ | / / / /| | +--+--+--+ + | +---+---+---+---+ / / / /|/| | / / / / /| +--+--+--+ + + | +---+---+---+---+ + / / / /|/|/| | / / / / /|/| +--+--+--+ + + + | +---+---+---+---+ + + / / / /|/|/|/| | / / / / /|/|/| +--+--+--+ + + + + | +---+---+---+---+ + + + / / / /|/|/|/|/ | |000|001|002|003|/|/|/| +--+--+--+ + + + + | +---+---+---+---+ + + + |00|01|02|/|/|/|/ | |004|005|006|007|/|/|/| +--+--+--+ + + + | +---+---+---+---+ + + + |03|04|05|/|/|/ | |008|009|00A|00B|/|/|/ +--+--+--+ + + | +---+---+---+---+ + + |06|07|08|/|/ | |00C|00D|00E|00F|/|/ +--+--+--+ + | +---+---+---+---+ + |09|0A|0B|/ | |010|011|012|013|/ +--+--+--+ | +---+---+---+---+ arr[5][3][4] | arr[3][4][5]
NOTE: Original question incorrectly represented arr[3][4][5]. I have learned that the original subscript represents depth. The data has been corrected to reflect intended array representation.
Example hex data +---+---+---+---+ +---+---+---+---+ +---+---+---+---+ |000|001|002|003| |100|101|102|103| |200|201|202|203| +---+---+---+---+ +---+---+---+---+ +---+---+---+---+ |004|005|006|007| |104|105|106|107| |204|205|206|207| +---+---+---+---+ +---+---+---+---+ +---+---+---+---+ |008|009|00A|00B| |108|109|10A|10B| |208|209|20A|20B| +---+---+---+---+ +---+---+---+---+ +---+---+---+---+ |00C|00D|00E|00F| |10C|10D|10E|10F| |20C|20D|20E|20F| +---+---+---+---+ +---+---+---+---+ +---+---+---+---+ |010|011|012|013| |110|111|112|113| |210|211|212|213| +---+---+---+---+ +---+---+---+---+ +---+---+---+---+ slice 0 slice 1 slice 2 short Arr[3][4][5]; // assume array is filled with hex test data arr[1][2][3] = 0x10B use slice 1, row 2, col 3 arr[2][3][4] = 0x210 use slice 2, row 3, col 4 resolves to row 4, col 0
row major
{000,001,002,003,004,005,006,007,008,009,00A,00B,00C,00D,00E,00F,010,011,012,013,
100,101,102,103,104,105,106,107,108,109,10A,10B,10C,10D,10E,10F,110,111,112,113,
200,201,202,203,204,205,206,207,208,209,20A,20B,20C,20D,20E,20F,210,211,212,213}
column major {000,004,008,00C,010,001,005,009,00D,011,002,006,00A,00E,012,003,007,00B,00F,013, 100,104,108,10C,110,101,105,109,10D,111,102,106,10A,10E,112,103,107,10B,10F,113, 200,204,208,20C,210,201,205,209,20D,211,202,206,20A,20E,212,203,207,20B,20F,213}
Calculation offset for arr[1][2][3] using row major offset? Calculation offset for arr[1][2][3] using column major offset?
When I asked this question I was hoping to find some good 3 dimensional array examples. Especially code examples. Since I didn't find anything that was understandable, I decided to create a little C program to help display the concept. It uses the same test data in a 3x4x5 array. It also includes test data for a 5x5x5 array. It creates a column major array from the row major array so the offset calculations can be verified.
The array offset methods are:
I added comments in the code where applicable to help clarify what the code is doing.
//
// Arrays.cpp :
// Purpose: Display rowMajor & colMajor data and calculations.
//
#include "stdafx.h"
#define _show_Arrays 1 // 1=display rowMajor & colMajor arrays
#define _square_array 0 // 1=use arr[5][5][5], 0=use arr[3][4][5]
#if (_square_array == 1)
const int depthSz = 5;
const int rowSz = 5;
const int colSz = 5;
/*
+---+---+---+---+---+
|x00|x01|x02|x03|x04|
+---+---+---+---+---+
|x05|x06|x07|x08|x09|
+---+---+---+---+---+
|x0A|x0B|x0C|x0D|x0E|
+---+---+---+---+---+
|x0F|x10|x11|x12|x13|
+---+---+---+---+---+
|x14|x15|x16|x17|x18|
+---+---+---+---+---+
slice x
*/
short row_arr[depthSz][colSz][rowSz] = {
{ /* slice 0 */
{0x000,0x001,0x002,0x003,0x004},
{0x005,0x006,0x007,0x008,0x009},
{0x00A,0x00B,0x00C,0x00D,0x00E},
{0x00F,0x010,0x011,0x012,0x013},
{0x014,0x015,0x016,0x017,0x018}},
{ /* slice 1 */
{0x100,0x101,0x102,0x103,0x104},
{0x105,0x106,0x107,0x108,0x109},
{0x10A,0x10B,0x10C,0x10D,0x10E},
{0x10F,0x110,0x111,0x112,0x113},
{0x114,0x115,0x116,0x117,0x118}},
{ /* slice 2 */
{0x200,0x201,0x202,0x203,0x204},
{0x205,0x206,0x207,0x208,0x209},
{0x20A,0x20B,0x20C,0x20D,0x20E},
{0x20F,0x210,0x211,0x212,0x213},
{0x214,0x215,0x216,0x217,0x218}},
{ /* slice 3 */
{0x300,0x301,0x302,0x303,0x304},
{0x305,0x306,0x307,0x308,0x309},
{0x30A,0x30B,0x30C,0x30D,0x30E},
{0x30F,0x310,0x311,0x312,0x313},
{0x314,0x315,0x316,0x317,0x318}},
{ /* slice 4 */
{0x400,0x401,0x402,0x403,0x404},
{0x405,0x406,0x407,0x408,0x409},
{0x40A,0x40B,0x40C,0x40D,0x40E},
{0x40F,0x410,0x411,0x412,0x413},
{0x414,0x415,0x416,0x417,0x418}}
};
#else
const int depthSz = 3;
const int rowSz = 4;
const int colSz = 5;
/*
+---+---+---+---+
|000|001|002|003|
+---+---+---+---+
|004|005|006|007|
+---+---+---+---+
|008|009|00A|00B|
+---+---+---+---+
|00C|00D|00E|00F|
+---+---+---+---+
|010|011|012|013|
+---+---+---+---+
slice x
*/
short row_arr[depthSz][colSz][rowSz] = {
{ /* slice 0 */
{0x000,0x001,0x002,0x003},
{0x004,0x005,0x006,0x007},
{0x008,0x009,0x00A,0x00B},
{0x00C,0x00D,0x00E,0x00F},
{0x010,0x011,0x012,0x013}},
{ /* slice 1 */
{0x100,0x101,0x102,0x103},
{0x104,0x105,0x106,0x107},
{0x108,0x109,0x10A,0x10B},
{0x10C,0x10D,0x10E,0x10F},
{0x110,0x111,0x112,0x113}},
{ /* slice 2 */
{0x200,0x201,0x202,0x203},
{0x204,0x205,0x206,0x207},
{0x208,0x209,0x20A,0x20B},
{0x20C,0x20D,0x20E,0x20F},
{0x210,0x211,0x212,0x213}}
};
#endif
short col_arr[depthSz*colSz*rowSz]; //
char *calc_RowMajor(char *Base, int elemSz, int depth_idx, int row_idx, int col_idx)
{ // row major slice is navigated by rows
char *address;
int lbound = 0; // lower bound (0 for zero-based arrays)
address = Base /* use base passed */
+ ((depth_idx-lbound)*(colSz*rowSz*elemSz)) /* select slice */
+ ((row_idx-lbound)*rowSz*elemSz) /* select row */
+ ((col_idx-lbound)*elemSz); /* select col */
return address;
}
char *calc_ColMajor(char *Base, int elemSz, int depth_idx, int col_idx, int row_idx)
{ // col major slice is navigated by columns
char *address;
int lbound = 0; // lower bound (0 for zero-based arrays)
int pageSz = colSz*rowSz*elemSz;
int offset;
offset = (col_idx-lbound)*(colSz*elemSz) /* select column */
+ (row_idx-lbound)*(elemSz); /* select row */
if (offset >= pageSz)
{ // page overflow, rollover
offset -= (pageSz-elemSz); /* ajdust offset back onto page */
}
address = Base /* use base passed */
+ ((depth_idx-lbound)*pageSz) /* select slice */
+ offset;
return address;
}
void disp_slice(char *pStr, short *pArr,int slice,int cols, int rows)
{
printf("== %s slice %d == %p\r\n",pStr, slice,pArr+(slice*rows*cols));
for(int x=0;x<rows;x++)
{
for(int y=0;y<cols;y++)
printf("%03X ",*(pArr+(slice*rows*cols)+(x*cols)+y));
printf("\r\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
// initialize col based array using row based array data
{ // convert row_arr into col_arr
short *pSrc = &row_arr[0][0][0];
short *pDst = &col_arr[0];
for(int d=0;d<depthSz;d++)
for(int r=0;r<rowSz;r++)
for(int c=0;c<colSz;c++)
{
*pDst++ = *(pSrc+((d*rowSz*colSz)+(c*rowSz)+r));
}
}
printf("Using Array[%d][%d][%d]\r\n",depthSz,rowSz,colSz);
#if (_show_Arrays == 1)
{ for(int x=0;x<depthSz;x++) {disp_slice("rowMajor",&row_arr[0][0][0],x,rowSz,colSz);}}
{ for(int x=0;x<depthSz;x++) {disp_slice("colMajor",&col_arr[0],x,rowSz,colSz);}}
#endif
int d = 2; // depth
int r = 3; // row
int c = 4; // column
for(d=0;d<depthSz;d++)
{
c = r = d; // simple access test pattern arr[0][0][0],arr[1][1][1],arr[2][2][2],...
{ // retrieve Array element
printf(" row_arr[%d][%d][%d] = %x\t",d,r,c,row_arr[d][r][c]);
printf("&row_arr[%d][%d][%d] = %p\r\n",d,r,c,&row_arr[d][r][c]);
}
{ // retrieve RowMajor element
short *pRowMajor = (short*)calc_RowMajor((char*)&row_arr[0][0][0],sizeof(short),d,r,c);
printf("calc_RowMajor(%d,%d,%d) = %x\t\t",d,r,c,*pRowMajor);
printf("pRowMajor = %p\r\n",pRowMajor);
}
{ // retrieve ColMajor element
short *pColMajor = (short*)calc_ColMajor((char*)&col_arr[0],sizeof(short),d,c,r);
printf("calc_ColMajor(%d,%d,%d) = %x\t\t",d,r,c,*pColMajor);
printf("pColMajor = %p\r\n",pColMajor);
}
} // for
getchar(); // just to hold the console while looking at the information
return 0;
}
这篇关于在多维数组的数组偏移计算(列VS大排)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!