Structure
This is the main structure I need to build, creating a type tParent
(pseudocode, where ()
is an array declaration):
这是我需要构建的主要结构,创建一个类型tParent(伪代码,其中()是一个数组声明):
type Ne {
single D()
dword DC
single B
single V
single D
}
type L {
Ne Ns()
dword NC
}
type tParent {
L La()
dword LC
single LR
}
tParent nTest
// would be accessed like nTest.La(0).Ns(0).B = 42 ...
However, the proprietary language that has to be used for this does not feature "arrays of user defined types". Nesting is supported, so apart from the ()
array declarations in the types above, the code is valid.
但是,必须使用的专有语言不具有“用户定义类型的数组”。支持嵌套,因此除了上面类型中的()数组声明之外,代码也是有效的。
Question
What is the most efficient way - algorithmically speaking - to implement this structure. I thought about expanding the subtypes into arrays, and accessing them using a second array that holds offsets, but I doubt this would be efficient in any way.
从算法上讲,实现这种结构的最有效方法是什么。我考虑过将子类型扩展为数组,并使用第二个保存偏移的数组来访问它们,但我怀疑这在任何方面都是有效的。
3 个解决方案
#1
1
All I can suggest is create array of byte and work with it like you work with memory in assembly languages. You can hide complexity of memory access behind accessor methods like
我所能建议的是创建字节数组并使用它,就像在汇编语言中使用内存一样。您可以隐藏访问器方法背后的内存访问复杂性
single typeName_fieldName_get(byte arr(), int offset) {
return convertFomByteToSingle(
arr(offset+OFFSET_OF_FIELD_NAME + 0),
arr(offset+OFFSET_OF_FIELD_NAME + 1),
arr(offset+OFFSET_OF_FIELD_NAME + 2),
....
);
}
#2
0
Seems like it should be possible to write an function that looks like
似乎应该可以编写一个看起来像的函数
function("nTest", ["La","Ns"], ["0","0"]).b = 42
It acts much like a nested dictionary, in that the first element in the dictionary. The second element is the list of subdictionaries, and the 3rd element is the list of keys for the sub dictionaries. An alternative form that groups subdictionaries and their keys could be:
它的行为很像嵌套字典,它是字典中的第一个元素。第二个元素是子类列表,第三个元素是子词典的键列表。组合子项及其键的替代形式可以是:
function("nTest", [["La","0"],["Ns","0"]]).b = 42
Although, the need to avoid using multiple types in an array would like require using something like below, but it may be tricky to handle if the nested structure does not have a predefined height:
虽然,避免在数组中使用多个类型的需要需要使用类似下面的内容,但如果嵌套结构没有预定义的高度,则处理起来可能很棘手:
function("nTest","La","0","Ns","0").b = 42
As for the contents of the function. It would basically just do this (but via a loop or recursion):
至于功能的内容。它基本上只是这样做(但通过循环或递归):
temp = nTest
temp = temp.La
temp = temp(0)
temp = temp.Ns
temp = temp(0)
return temp
This is close to how the functionality would be added to the language if I'm not mistaken after parsing. Though there may be a need to convert everything into string before putting into the function, then convert it back afterwords.
如果我在解析后没有弄错的话,这接近于如何将功能添加到语言中。虽然可能需要在放入函数之前将所有内容转换为字符串,然后将其转换回字。
Although, since it's strictly typed, youll probably have to include the types, resulting in something like this:
虽然,因为它是严格类型的,你可能必须包含类型,导致这样的事情:
function(["tParent","nTest"],["L","La"],["int","0"],["Ne","Ns"],["int","0"],["single","D"]) = 42
#3
0
The only solution I can think of since you can't use pointers is to use a linked list. Moving forward during iteration will be O(1) but lookups will of course be O(n) so depending on your use case it may not be the most efficient solution but it seems to me that it's the only one available without being able to store memory addresses in a primitive array to use as pointers.
由于你不能使用指针,我能想到的唯一解决方案是使用链表。在迭代期间向前移动将是O(1)但是查找当然将是O(n),因此根据您的使用情况,它可能不是最有效的解决方案,但在我看来,它是唯一一个无法存储的可用解决方案用作指针的基本数组中的内存地址。
type tParent {
ListL La
dword LC
single LR
}
type ListL {
NodeL First
}
type NodeL {
NodeL Next
L Value
}
#1
1
All I can suggest is create array of byte and work with it like you work with memory in assembly languages. You can hide complexity of memory access behind accessor methods like
我所能建议的是创建字节数组并使用它,就像在汇编语言中使用内存一样。您可以隐藏访问器方法背后的内存访问复杂性
single typeName_fieldName_get(byte arr(), int offset) {
return convertFomByteToSingle(
arr(offset+OFFSET_OF_FIELD_NAME + 0),
arr(offset+OFFSET_OF_FIELD_NAME + 1),
arr(offset+OFFSET_OF_FIELD_NAME + 2),
....
);
}
#2
0
Seems like it should be possible to write an function that looks like
似乎应该可以编写一个看起来像的函数
function("nTest", ["La","Ns"], ["0","0"]).b = 42
It acts much like a nested dictionary, in that the first element in the dictionary. The second element is the list of subdictionaries, and the 3rd element is the list of keys for the sub dictionaries. An alternative form that groups subdictionaries and their keys could be:
它的行为很像嵌套字典,它是字典中的第一个元素。第二个元素是子类列表,第三个元素是子词典的键列表。组合子项及其键的替代形式可以是:
function("nTest", [["La","0"],["Ns","0"]]).b = 42
Although, the need to avoid using multiple types in an array would like require using something like below, but it may be tricky to handle if the nested structure does not have a predefined height:
虽然,避免在数组中使用多个类型的需要需要使用类似下面的内容,但如果嵌套结构没有预定义的高度,则处理起来可能很棘手:
function("nTest","La","0","Ns","0").b = 42
As for the contents of the function. It would basically just do this (but via a loop or recursion):
至于功能的内容。它基本上只是这样做(但通过循环或递归):
temp = nTest
temp = temp.La
temp = temp(0)
temp = temp.Ns
temp = temp(0)
return temp
This is close to how the functionality would be added to the language if I'm not mistaken after parsing. Though there may be a need to convert everything into string before putting into the function, then convert it back afterwords.
如果我在解析后没有弄错的话,这接近于如何将功能添加到语言中。虽然可能需要在放入函数之前将所有内容转换为字符串,然后将其转换回字。
Although, since it's strictly typed, youll probably have to include the types, resulting in something like this:
虽然,因为它是严格类型的,你可能必须包含类型,导致这样的事情:
function(["tParent","nTest"],["L","La"],["int","0"],["Ne","Ns"],["int","0"],["single","D"]) = 42
#3
0
The only solution I can think of since you can't use pointers is to use a linked list. Moving forward during iteration will be O(1) but lookups will of course be O(n) so depending on your use case it may not be the most efficient solution but it seems to me that it's the only one available without being able to store memory addresses in a primitive array to use as pointers.
由于你不能使用指针,我能想到的唯一解决方案是使用链表。在迭代期间向前移动将是O(1)但是查找当然将是O(n),因此根据您的使用情况,它可能不是最有效的解决方案,但在我看来,它是唯一一个无法存储的可用解决方案用作指针的基本数组中的内存地址。
type tParent {
ListL La
dword LC
single LR
}
type ListL {
NodeL First
}
type NodeL {
NodeL Next
L Value
}