简体   繁体   English

词典/ LUT在C中的实现

[英]Dictionary/LUT implementation in C

I have 500 frames for which I have stored the length of each frame in an array as the frames are in ascending order. 我有500个帧,因为帧是按升序排列的,所以我将每个帧的长度存储在数组中。

const char header_length = {23,34, 45, 12, 23,56,......,2,4};

Here frame 1 is of length 23 bytes, frame 2 is of length 34 bytes. 在这里,帧1的长度为23个字节,帧2的长度为34个字节。 Now when I am requested frame with header 4 I would have to reply with frame with header 7, frame with header 8 would require a reply frame with header 60. The co-relation is a constant, header 4 frame will always reply header 7 frame. 现在,当我被请求带有标题4的帧时,我将不得不以带有标题7的帧进行答复,带有标题8的帧将需要带有标题60的答复帧。该相互关系是一个常数,标题4的帧将始终答复标题7的帧。 。 So I need a look up table sort of implementation here. 因此,我需要在此处查找表排序的实现。 I plan to implement this using a multidimensional array. 我计划使用多维数组来实现这一点。 Although is there a better way to implement this? 尽管有更好的方法来实现这一点?

I am answering based on OP's comment above, otherwise OP's question just does not make any sense. 我是根据上述OP的评论回答的,否则OP的问题就没有任何意义。

Assuming that the maximum frame size is limited by a considerably small N , then it may be better to just keep another array as the following, 假设最大帧大小受相当小的N限制,那么最好保留另一个数组,如下所示,

const char header_length[] = {23,34, 45, 12, 23,56,......,2,4};
const int to_be_replied[N+1] = {...,0,...,2,...,0,...,4,...}; /* fill remaining */
/* here are their positions        @12   @23    @34  @45  */ 

and use the following during the reply. 并在回复时使用以下内容。

int frame_num, reply_frame_num;
frame_num = get_frame_number(); /* some function */
reply_frame_num = to_be_replied[header_length[frame_num]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM