我尝试通过使用函数H5Tget_nmembers()
,H5Tget_member_name()
,H5Tget_member_offset()
,H5Tget_member_class()
和H5Tget_member_type()
来确定两种复合HDF5数据类型是否等效。
前四个函数按预期工作,但是H5Tget_member_type()
返回奇怪的值。
该代码片段
struct Agent0 {
int m_iInt1;
int m_iInt2;
} ta0;
hid_t hAgentDataType = H5Tcreate (H5T_COMPOUND, sizeof(ta0));
H5Tinsert(hAgentDataType, "my_int1", qoffsetof(ta0, m_iInt1), H5T_NATIVE_INT);
H5Tinsert(hAgentDataType, "my_int2", qoffsetof(ta0, m_iInt2), H5T_NATIVE_INT);
printf("type of member %d: %ld\n", 0, H5Tget_member_type(hAgentDataType, 0));
printf("type of member %d: %ld\n", 1, H5Tget_member_type(hAgentDataType, 1));
printf("H5T_NATIVE_INT: %ld\n", H5T_NATIVE_INT);
给出以下输出:
type of member 0: 216172782113784122
type of member 1: 216172782113784123
H5T_NATIVE_INT: 216172782113783820
因此,即使成员0和1都是整数,它们也具有不同的类型值。这两个值都与
H5T_NATIVE_INT
的值不同。我希望所有3个值都相等...那么
H5Tget_member_type()
真正返回了什么?我实际上如何比较复合数据类型的成员的数据类型?
编辑:什至更奇怪:如果我两次为同一成员调用H5Tget_meber_type(),我得到不同的值:
printf("call #1 for member %d: %016lx\n", 0, H5Tget_member_type(hAgentDataType, 0));
printf("call #2 for member %d: %016lx\n", 0, H5Tget_member_type(hAgentDataType, 0));
产量
call #1 for member 0: 0300000000000151
call #2 for member 0: 0300000000000152
最佳答案
如果您不受特定库的约束,请查看HDFql,因为它可以减轻处理HDF5复合数据集/属性的低级详细信息。在C++中使用HDFql,您的问题可以通过以下方式解决:
int equivalent(const std::string &compound1, const std::string &compound2)
{
// declare variables
HDFql::Cursor cursor;
char script[1024];
int i;
// get name of members of compound1
sprintf(script, "SHOW MEMBER %s", compound1);
HDFql::execute(script);
// get name of members of compound2
HDFql::cursorUse(&cursor);
sprintf(script, "SHOW MEMBER %s", compound2);
HDFql::execute(script);
if (HDFql::cursorGetCount() != HDFql::cursorGetCount(&cursor))
{
return -1; // number of members of compound1 differs from number of members of compound2
}
// compare name of members of compound1 with name of members of compound2
for(i = 0; i < HDFql::cursorGetCount(); i++)
{
HDFql::cursorNext();
HDFql::cursorNext(&cursor);
if (strcmp(HDFql::cursorGetChar(), HDFql::cursorGetChar(&cursor)) != 0)
{
return -1; // name of member of compound1 differs from name of member of compound2
}
}
// get data type of members of compound1
HDFql::cursorUseDefault();
sprintf(script, "SHOW DATA TYPE %s/", compound1);
HDFql::execute(script);
// get data type of members of compound2
HDFql::cursorUse(&cursor);
sprintf(script, "SHOW DATA TYPE %s/", compound2);
HDFql::execute(script);
// compare data type of members of compound1 with data type of members of compound2
for(i = 0; i < HDFql::cursorGetCount(); i++)
{
HDFql::cursorNext();
HDFql::cursorNext(&cursor);
if (*HDFql::cursorGetInt() != *HDFql::cursorGetInt(&cursor))
{
return -1; // data type of member of compound1 differs from data type of member of compound2
}
}
// get offset of members of compound1
HDFql::cursorUseDefault();
sprintf(script, "SHOW OFFSET %s", compound1);
HDFql::execute(script);
// get offset of members of compound2
HDFql::cursorUse(&cursor);
sprintf(script, "SHOW OFFSET %s", compound2);
HDFql::execute(script);
// compare offset of members of compound1 with offset of members of compound2
for(i = 0; i < HDFql::cursorGetCount(); i++)
{
HDFql::cursorNext();
HDFql::cursorNext(&cursor);
if (*HDFql::cursorGetInt() != *HDFql::cursorGetInt(&cursor))
{
return -1; // offset of member of compound1 differs from offset of member of compound2
}
}
return 0; // compound1 is equivalent to compound2
}
可以在reference manual中找到有关如何使用HDFql的其他详细信息和示例。