C의 어소시에이션 어레이
데이터 세트를 프로그램 가능한 동글에 전송하는 방법을 구현하고 있습니다.동글은 스마트카드 기술을 기반으로 내부 임의의 코드를 실행할 수 있다.입력 및 출력 데이터는 입력 및 출력 포인터를 통해 액세스할 수 있는 이진 블록으로 전달됩니다.
데이터 처리 코드를 간단하게 하기 위해 어소시에이션 어레이를 사용하고 싶습니다.모든 것은 다음과 같이 동작합니다.
먼저 호스트 응용 프로그램:
// Host application in C++
in_data["method"] = "calc_r";
in_data["id"] = 12;
in_data["loc_a"] = 56.19;
in_data["loc_l"] = 44.02;
processor->send(in_data);
다음으로 동글 내부의 코드:
// Some dongle function in C
char* method_name = assoc_get_string(in_data, "method");
int id = assoc_get_int(in_data, "id");
float loc_a = assoc_get_float(in_data, "loc_a");
float loc_l = assoc_get_float(in_data, "loc_l");
그래서 동글 파트의 기능에 대한 질문입니다.위와 같은 어소시에이트 어레이 동작을 구현하기 위한 C 코드 또는 라이브러리가 있습니까?
Glib의 해시 테이블.는 맵 인터페이스 또는 (어소시에이션어레이)를 실장합니다.C에서 가장 많이 사용되는 해시 테이블 구현입니다.
GHashTable *table=g_hash_table_new(g_str_hash, g_str_equal);
/* put */
g_hash_table_insert(table,"SOME_KEY","SOME_VALUE");
/* get */
gchar *value = (gchar *) g_hash_table_lookup(table,"SOME_KEY");
내가 의심하는 것은 네가 직접 써야 한다는 것이다.설명하시는 아키텍처를 이해하면 데이터 청크 전체를 한 번에 전송해야 합니다.이 경우 대부분의 라이브러리는 여러 개의 메모리를 할당하기 때문에 이 기능을 하지 않습니다.이는 여러 개의 메모리 전송(및 구조에 대한 내부 이해)이 필요하기 때문입니다.를 에 만으로 소켓 상의 .send
★★★★★★ 。
단일 메모리 블록에 매우 단순한 연관 배열(또는 해시)을 관리하는 자체 유틸리티를 작성할 수 있습니다.데이터 양이 적은 경우 엔트리에 단순한 선형 검색을 사용할 수 있으며 상당히 콤팩트한 코드 비트가 됩니다.
C에서 해시 테이블을 구현하는 헤더 라이브러리인 uthash를 사용해 보십시오.작고 꽤 사용하기 편해요.
이것은 오래된 스레드입니다만, 실장을 찾고 있는 유저에게 있어서도 도움이 될 것이라고 생각했습니다.코드도 별로 필요 없고, 라이브러리도 필요 없고, 100줄까지 쓸 수 있습니다.python 데이터형과 유사하기 때문에 나는 그것을 사전이라고 불렀다.코드는 다음과 같습니다.
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
typedef struct hollow_list hollow_list;
struct hollow_list{
unsigned int size;
void *value;
bool *written;
hollow_list *children;
};
//Creates a hollow list and allocates all of the needed memory
hollow_list hollow_list_create(unsigned int size){
hollow_list output;
output = (hollow_list) {.size = size, .value = (void *) 0, .written = calloc(size, sizeof(bool)), .children = calloc(size, sizeof(hollow_list))};
return output;
}
//Frees all memory of associated with a hollow list and its children
void hollow_list_free(hollow_list *l, bool free_values){
int i;
for(i = 0; i < l->size; i++){
hollow_list_free(l->children + i, free_values);
}
if(free_values){
free(l->value);
}
free(l);
}
//Reads from the hollow list and returns a pointer to the item's data
void *hollow_list_read(hollow_list *l, unsigned int index){
if(index == 0){
return l->value;
}
unsigned int bit_checker;
bit_checker = 1<<(l->size - 1);
int i;
for(i = 0; i < l->size; i++){
if(bit_checker & index){
if(l->written[i] == true){
return hollow_list_read(l->children + i, bit_checker ^ index);
} else {
return (void *) 0;
}
}
bit_checker >>= 1;
}
}
//Writes to the hollow list, allocating memory only as it needs
void hollow_list_write(hollow_list *l, unsigned int index, void *value){
if(index == 0){
l->value = value;
} else {
unsigned int bit_checker;
bit_checker = 1<<(l->size - 1);
int i;
for(i = 0; i < l->size; i++){
if(bit_checker & index){
if(!l->written[i]){
l->children[i] = hollow_list_create(l->size - i - 1);
l->written[i] = true;
}
hollow_list_write(l->children + i, bit_checker ^ index, value);
break;
}
bit_checker >>= 1;
}
}
}
typedef struct dictionary dictionary;
struct dictionary{
void *value;
hollow_list *child;
};
dictionary dictionary_create(){
dictionary output;
output.child = malloc(sizeof(hollow_list));
*output.child = hollow_list_create(8);
output.value = (void *) 0;
return output;
}
void dictionary_write(dictionary *dict, char *index, unsigned int strlen, void *value){
void *hollow_list_value;
dictionary *new_dict;
int i;
for(i = 0; i < strlen; i++){
hollow_list_value = hollow_list_read(dict->child, (int) index[i]);
if(hollow_list_value == (void *) 0){
new_dict = malloc(sizeof(dictionary));
*new_dict = dictionary_create();
hollow_list_write(dict->child, (int) index[i], new_dict);
dict = new_dict;
} else {
dict = (dictionary *) hollow_list_value;
}
}
dict->value = value;
}
void *dictionary_read(dictionary *dict, char *index, unsigned int strlen){
void *hollow_list_value;
dictionary *new_dict;
int i;
for(i = 0; i < strlen; i++){
hollow_list_value = hollow_list_read(dict->child, (int) index[i]);
if(hollow_list_value == (void *) 0){
return hollow_list_value;
} else {
dict = (dictionary *) hollow_list_value;
}
}
return dict->value;
}
int main(){
char index0[] = "hello, this is a test";
char index1[] = "hello, this is also a test";
char index2[] = "hello world";
char index3[] = "hi there!";
char index4[] = "this is something";
char index5[] = "hi there";
int item0 = 0;
int item1 = 1;
int item2 = 2;
int item3 = 3;
int item4 = 4;
dictionary d;
d = dictionary_create();
dictionary_write(&d, index0, 21, &item0);
dictionary_write(&d, index1, 26, &item1);
dictionary_write(&d, index2, 11, &item2);
dictionary_write(&d, index3, 13, &item3);
dictionary_write(&d, index4, 17, &item4);
printf("%d\n", *((int *) dictionary_read(&d, index0, 21)));
printf("%d\n", *((int *) dictionary_read(&d, index1, 26)));
printf("%d\n", *((int *) dictionary_read(&d, index2, 11)));
printf("%d\n", *((int *) dictionary_read(&d, index3, 13)));
printf("%d\n", *((int *) dictionary_read(&d, index4, 17)));
printf("%d\n", ((int) dictionary_read(&d, index5, 8)));
}
유감스럽게도 목록[x] 구문은 복제할 수 없지만, 이것이 제가 생각해낸 가장 좋은 대안입니다.
예, 하지만 지정한 방식으로는 작동하지 않습니다.대신 RSA를 사용합니다.struct
해당 구조에서 작동하는 데이터 및 함수를 저장하여 원하는 결과를 얻을 수 있습니다.C의 Simple Associative Array Library를 참조하십시오.사용 예:
struct map_t *test;
test=map_create();
map_set(test,"One","Won");
map_set(test,"Two","Too");
map_set(test,"Four","Fore");
GLIB의 해시 테이블과 균형 잡힌 이진 트리가 여러분이 원하는 것일 수 있습니다.
마크 윌킨스가 정답을 알려줬어데이터를 단일 청크로 전송하는 경우 아키텍처에서 C++ 맵이 어떻게 표현되는지 이해하고 액세스 함수를 작성해야 합니다.
어쨌든, 만약 당신이 동글에 지도를 다시 만들기로 결정한다면, 나는 당신이 다음과 같은 생각을 쓸 수 있는 작은 C 라이브러리를 작성했다.
tbl_t in_data=NULL;
tblSetSS(in_data,"method","calc_r");
tblSetSN(in_data,"id",12);
tblSetSF(in_data,"loc_a",56.19);
tblSetSF(in_data,"loc_l",44.02);
그 후:
char *method_name = tblGetP(in_data, "method");
int id = tblGetN(in_data, "id");
float loc_a = tblGetF(in_data, "loc_a");
float loc_l = tblGetF(in_data, "loc_l");
해시 테이블은 Hopscotch 해시를 변형한 것으로, 평균적으로는 양호합니다.키와 데이터의 타입을 임의로 조합할 수 있습니다(즉, 테이블 전체를 키로 사용할 수 있습니다).
그 기능은 순수 속도보다는 프로그래밍을 쉽게 하는 데 초점이 맞춰져 있었고, 코드는 철저하게 테스트되지 않았지만, 만약 당신이 아이디어를 좋아하고 확장하기를 원한다면 구글 코드에서 코드를 볼 수 있습니다.
(변수 길이 문자열이나 고속 스트링 패턴 매칭 함수 등 다른 것도 있습니다만, 이 경우는 그다지 흥미가 없는 경우가 있습니다).
언급URL : https://stackoverflow.com/questions/4864453/associative-arrays-in-c
'programing' 카테고리의 다른 글
Vue - 문자열 외 요소 렌더링 (0) | 2022.07.21 |
---|---|
GCC가 구조를 최적화하지 않는 이유는 무엇입니까? (0) | 2022.07.21 |
vue tel 입력 국가 코드를 얻으려면 어떻게 해야 하나요? (0) | 2022.07.21 |
함수의 조기 복귀 효율성 (0) | 2022.07.21 |
MinGW란 무엇인가에 대한 간단한 설명 (0) | 2022.07.21 |