सी

2011-02-01 2 views
35

में सहयोगी सरणी मैं डेटा के एक सेट को प्रोग्राम करने योग्य डोंगल में स्थानांतरित करने का एक तरीका कार्यान्वित कर रहा हूं। डोंगल एक स्मार्ट कार्ड प्रौद्योगिकी पर आधारित है और अंदर एक मनमाना कोड निष्पादित कर सकते हैं। इनपुट और आउटपुट डेटा को बाइनरी ब्लॉक के रूप में पास किया जाता है जिसे इनपुट और आउटपुट पॉइंटर्स के माध्यम से एक्सेस किया जा सकता है।सी

मैं डेटा प्रोसेसिंग कोड को सरल बनाने के लिए एक सहयोगी सरणी का उपयोग करना चाहता हूं। सब कुछ इस तरह से काम करना चाहिए:

पहले मेजबान आवेदन:

// 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"); 

तो मेरे सवाल का डोंगल हिस्सा कार्यक्षमता के बारे में है। क्या उपरोक्त की तरह एक सहयोगी सरणी व्यवहार को लागू करने के लिए सी कोड या लाइब्रेरी है?

उत्तर

7

मेरे संदेह आप अपने खुद के बारे में करने के लिए होता है। यदि मैं उस आर्किटेक्चर को समझता हूं जिसे आप वर्णन कर रहे हैं, तो आपको डेटा के पूरे हिस्से को एक टुकड़े में भेजना होगा। यदि ऐसा है, तो अधिकांश पुस्तकालय इसके लिए काम नहीं करेंगे क्योंकि वे अधिकतर स्मृति के कई टुकड़े आवंटित करेंगे, जिसके लिए एकाधिक स्थानान्तरण (और संरचना की आंतरिक समझ) की आवश्यकता होगी। यह लाइब्रेरी हैश फ़ंक्शन का उपयोग करने की कोशिश करने के समान होगा और फिर रूट पॉइंटर को send फ़ंक्शन पर पास करके बस सॉकेट पर नेटवर्क पर अपनी सामग्री भेजना होगा।

अपने आप की कुछ उपयोगिताएं लिखना संभव होगा जो स्मृति के एक ब्लॉक में एक बहुत ही सरल सहयोगी सरणी (या हैश) का प्रबंधन करें। यदि डेटा की मात्रा छोटी है, तो यह प्रविष्टियों के लिए एक सरल रैखिक खोज का उपयोग कर सकती है और कोड का काफी कॉम्पैक्ट बिट होगा।

+0

हाँ, आप सही हैं। डोंगल में डेटा प्रोसेसिंग फ़ंक्शन डेटा के एक टुकड़े से संबंधित है। मुझे सच में लगता है कि मुझे 8 अक्षर लंबाई सूचकांक और एक रैखिक अनुक्रमणिका एल्गोरिदम खोजने के साथ एक सरल सहयोगी सरणी को लागू करने की आवश्यकता है! मैंने सोचा कि पहिया को फिर से शुरू न करें और पूछें कि क्या किसी ने इसे पहले से लागू किया है। – ezpresso

+0

मैं निश्चित रूप से पहिया को पुनर्निर्मित करने के साथ सहमत हूं। और यह निश्चित रूप से ऐसा लगता है कि किसी ने पहले से ही यह किया है ... लेकिन यह खोजना मुश्किल साबित हो सकता है क्योंकि यह विशेष है। –

18

Glib's hash table. नक्शा इंटरफ़ेस या (सहयोगी सरणी) लागू करता है। और यह सबसे अधिक संभावना सी

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"); 
2

के लिए सबसे अधिक इस्तेमाल किया हैश तालिका कार्यान्वयन है GLib के Hash Tables और Balanced Binary Trees हो सकता है कि तुम क्या करने के बाद कर रहे हैं।

3

हां, लेकिन यह आपके द्वारा निर्दिष्ट तरीके से काम नहीं करेगा। इसके बजाय वह उस परिणाम को संचालित करने वाले डेटा और फ़ंक्शंस को संग्रहीत करने के लिए struct का उपयोग करेगा, जिससे आप इच्छित परिणाम दे सकते हैं। A Simple Associative Array Library In C देखें। उपयोग का उदाहरण:

struct map_t *test; 

test=map_create(); 
map_set(test,"One","Won"); 
map_set(test,"Two","Too"); 
map_set(test,"Four","Fore"); 
9

uthash, सी में एक हैश तालिका को लागू करने वाली हेडर लाइब्रेरी आज़माएं। यह छोटा और उपयोग करने में काफी आसान है।

2

मार्क विल्किन्स ने आपको सही उत्तर दिया। यदि आप डेटा को एक ही हिस्से के रूप में भेजना चाहते हैं, तो आपको यह समझने की आवश्यकता है कि आपके आर्किटेक्चर में सी ++ मानचित्रों का प्रतिनिधित्व कैसे किया जाता है और पहुंच कार्यों को लिखें।

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"); 

एक छोटी सी सी पुस्तकालय

वैसे भी, अगर आप डोंगल नक्शे पर बनाने का निर्णय लेते हैं, मैं लिखा है जहाँ आप लिख सकता है की तरह सोचता है हैशटेबल होप्सकॉच हैश का एक भिन्नता है, जो औसतन अच्छा है, और आपके पास कुंजी और डेटा के लिए प्रकार का कोई मिश्रण हो सकता है (यानी आप एक संपूर्ण तालिका को एक कुंजी के रूप में उपयोग कर सकते हैं)।

उन कार्यों के लिए फ़ोकस शुद्ध गति की बजाय प्रोग्रामिंग को आसान बनाना था और कोड का पूरी तरह से परीक्षण नहीं किया गया था, लेकिन यदि आपको विचार पसंद है और आप इसे विस्तारित करना चाहते हैं, तो आप googlecode पर कोड देख सकते हैं।

(वैरिएबल लम्बाई स्ट्रिंग्स और एक तेज़ स्ट्रिंग पैटर्न मिलान फ़ंक्शन जैसी अन्य चीजें हैं लेकिन शायद इस मामले में रुचि नहीं हो सकती है)।

1

Binn क्रमबद्धता प्रारूप सी

binn वस्तु (JSON वस्तु की तरह) में डेटा ट्रांसफर के लिए एक अच्छा समाधान है एक साहचर्य सरणी है।

binn *obj; 

    // create a new object 
    obj = binn_object(); 

    // add values to it 
    binn_object_set_str(obj, "method", "calc_r"); 
    binn_object_set_int32(obj, "id", 12); 
    binn_object_set_float(obj, "loc_a", 56.19); 
    binn_object_set_float(obj, "loc_l", 44.02); 

    // send over the network 
    send(sock, binn_ptr(obj), binn_size(obj)); 

    // release the buffer 
    binn_free(obj); 

और पढ़ने के लिए:

char* method_name = binn_object_str(obj, "method"); 
    int id = binn_object_int32(obj, "id"); 
    float loc_a = binn_object_float(obj, "loc_a"); 
    float loc_l = binn_object_float(obj, "loc_l"); 

यह सिर्फ 2 फ़ाइलें (binn.c और binn.h) है, इसलिए यह प्रयोग किया जा रहा है के बजाय परियोजना के भीतर संकलित किया जा सकता यहाँ एक के उपयोग का उदाहरण है एक साझा पुस्तकालय के रूप में।

लेकिन मुझे नहीं पता कि यह एक डोंगल में फिट हो सकता है या नहीं।

2

यह एक पुराना धागा है, लेकिन मैंने सोचा कि यह अभी भी कार्यान्वयन की तलाश में किसी के लिए उपयोगी हो सकता है। यह बहुत अधिक कोड नहीं लेता है; मैंने बिना किसी अतिरिक्त लाइब्रेरी के ~ 100 लाइनों में मेरा किया। मैंने इसे एक शब्दकोश कहा क्योंकि यह पाइथन डेटाटाइप के समानांतर (प्रकार) है। यहाँ मेरी कोड है:

#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] वाक्य रचना को दोहराने नहीं कर सकता है, लेकिन यह सबसे अच्छा विकल्प मैं के साथ आए हैं है।