/* * Inserts a new pair n into a pair tree p */ static void insert_pair(ini_pair *p, ini_pair *n) { if(my_stricmp(p->param, n->param) < 0) { if(!p->left) p->left = n; else insert_pair(p->left, n); } else { if(!p->right) p->right = n; else insert_pair(p->right, n); } }
/* * Sets a parameter 'par' in section 'sec's value to 'val', replacing the * current value if it already exists, or creates the section if it does not * exist */ int ini_put(struct ini_file *ini, const char *sec, const char *par, const char *val) { ini_section *s; ini_pair *p, **pp; if(!ini || !val) return 0; p = find_param(ini, sec, par); if(p) { /* Replace the existing value */ char *t = p->value; if(!(p->value = my_strdup(val))) { p->value = t; return 0; } free(t); return 1; } if(sec) { s = find_section(ini->sections, sec); if(!s) { /* Create a new section */ if(!(s = malloc(sizeof *s))) return 0; if(!(s->name = my_strdup(sec))) { free(s); return 0; } s->fields = NULL; s->left = s->right = NULL; if(ini->sections) insert_section(ini->sections, s); else ini->sections = s; } pp = &s->fields; } else pp = &ini->globals; if(!(p = malloc(sizeof *p))) return 0; if(!(p->param = my_strdup(par)) || !(p->value = my_strdup(val))) { free(p); return 0; } p->left = p->right = NULL; if(!*pp) *pp = p; else insert_pair(*pp, p); return 1; }
//Creates table for the conditions. hashtable_t *create_condition_table(void) { hashtable_t *condition_table = create_hashtable(NUMBER_OF_CONDITIONS); insert_pair(condition_table, "eq", EQ); insert_pair(condition_table, "ne", NQ); insert_pair(condition_table, "ge", GE); insert_pair(condition_table, "lt", LT); insert_pair(condition_table, "gt", GT); insert_pair(condition_table, "le", LE); insert_pair(condition_table, "al", AL); return condition_table; }
/* * Adds a parameter-value pair to section s */ static ini_pair *add_pair(ini_section *s, char *p, char *v) { ini_pair *n; assert(s); n = malloc(sizeof *n); if(!n) return NULL; n->param = p; n->value = v; n->left = n->right = NULL; if(!s->fields) s->fields = n; else insert_pair(s->fields, n); return n; }
/* * Receive an opened source file. */ hashtable_t *create_instruction_symbol_table(FILE *source_file, uint32_t *number_of_instructions) { hashtable_t *hashtable = create_hashtable(MACHINE_MEMORY_CAPACITY); /*Allocate memory for line with the added NULL character. */ char *line = (char*) malloc((MAX_LENGTH_OF_LINE + 1) * sizeof(char)); int counter_address = 0; *number_of_instructions = 0; //Reads one line at a time until it reaches the end of file. while(fgets(line, MAX_LENGTH_OF_LINE, source_file) != NULL) { //If it encounters an empty line it just moves to the next line. if(isEmpty(line) || (line[0] == '/' && line[1] == '/')) { continue; } //Replaces the new line character with a null character. char *new_line_char; if((new_line_char = strchr(line, '\n')) != NULL) { *new_line_char = '\0'; } //If the line read represents a label then it is inserted into the symbol hash table. if(is_label(line)) { char *label = line; *(label + (strlen(label) - 1)) = '\0'; insert_pair(hashtable, label, counter_address); continue; } counter_address += BYTES_PER_INSTRUCTION; (*number_of_instructions)++; } free(line); return hashtable; }
void binomial_s_pair_set::insert(binomial_s_pair s) { int deg = R->degree(s.lcm); // Statistics control if (deg > _max_degree) { // Extend _npairs: for (int i=2*_max_degree+2; i<2*deg+2; i++) _npairs.append(0); _max_degree = deg; } _npairs[2*deg]++; _npairs[2*deg+1]++; s_pair_degree_list *q = _pairs; while (true) { if (q->next == NULL || q->next->deg > deg) { // Insert new degree node s_pair_degree_list *q1 = new s_pair_degree_list; q1->next = q->next; q1->deg = deg; q1->pairs = NULL; q->next = q1; break; } if (q->next->deg == deg) break; q = q->next; } q = q->next; insert_pair(q, s); _n_elems++; q->n_elems++; }
int main( int argc, char **argv ) { int i; int N=NUMBER_KEYS; // The number of keys to insert in the hash_table char* keys[NUMBER_KEYS]={KEYS}; char* values[NUMBER_KEYS]={VALUES}; hashtable *hash_table = new_hashtable(); //The definition of the zize intialized to 8 srand(time(NULL)); // here we start by genarting a random keys and values printf("############ Initialisation of keys #################################################################\n\n"); for(i=0;i<N;i++) { printf("keys[%d]==\"%s\" with hash %d\n",i,keys[i],hash(keys[i])%hash_table->size); } printf("\n############ Initialisation of values #############################################################\n\n"); for(i=0;i<N;i++) { printf("values[%d]==\"%s\"\n",i,values[i]); } // insertion of key-value pairs in the hash_table printf("\n############ Insertion of (key,value) pairs ########################################################\n\n"); for(i=0;i<N;i++) { insert_resize_pair(&hash_table,keys[i],values[i]); printf("Insertion of (\"%s\",\"%s\") succed , SIZE = %d \n",keys [i],values[i],hash_table->size); } printf("\n Our hash table = "); print_hashtable(hash_table); // reinsertion of some pairs in the hash_table printf("\n############ Reinsertion of some pairs #############################################################\n\n"); insert_pair( hash_table, keys[1], "19" ); printf("Reinsertion of (\"%s\",\"%s\") succed \n",keys [1],"19"); insert_pair( hash_table, keys[2], "13" ); printf("Reinsertion of (\"%s\",\"%s\") succed \n",keys [2],"13"); printf("the hash table = "); print_hashtable(hash_table); // getting the values of a given key printf("\n############ Getting the value for a given key #######################################################\n\n"); for(i=0;i<N;i++) printf("hash_table of \"%s\" gives \"%s\"\n",keys[i], get_value( hash_table, keys[i])); // removing the some keys from the hash table printf("\n############ Removing some keys from hash table ######################################################\n\n"); printf("Initialy we have\n"); print_hashtable(hash_table); for(i=0;2*i<N;i++) { delate_key( hash_table, keys[2*i]); printf("we delate the key \"%s\"\n",keys[2*i]); print_hashtable(hash_table); } printf("\n#### After free we print the hash_table to verify #################################################\n\n"); free_hashtable(hash_table); print_hashtable(hash_table); return 0; }
struct ini_file *ini_parse(const char *text, int *err, int *line) { jmp_buf on_error; int e_code; struct ini_file *ini = NULL; ini_section *cur_sec = NULL; const char *tstart, *tend; int t; if(err) *err = SUCCESS; if(line) *line = 1; ini = make_ini(); if((e_code = setjmp(on_error)) != 0) { if(err) *err = e_code; ini_free(ini); return NULL; } while((t = get_token(&text, &tstart, &tend, line, on_error)) != T_END) { if(t == '[') { char *section_name; if(get_token(&text, &tstart, &tend, line, on_error) != T_VALUE) { longjmp(on_error, EMPTY_SECTION); } section_name = get_string(tstart, tend, on_error); cur_sec = add_section(&ini->sections, section_name); if(!cur_sec) longjmp(on_error, OUT_OF_MEMORY); if(get_token(&text, &tstart, &tend, line, on_error) != ']') { longjmp(on_error, MISSING_END_BRACE); } } else if (t == T_VALUE ) { char *par, *val; par = get_string(tstart, tend, on_error); t = get_token(&text, &tstart, &tend, line, on_error); if(t != '=' && t != ':') { longjmp(on_error, EXPECTED_EQUALS); } if(get_token(&text, &tstart, &tend, line, on_error) != T_VALUE) { longjmp(on_error, EXPECTED_VALUE); } val = get_string(tstart, tend, on_error); if(cur_sec) add_pair(cur_sec, par, val); else { /* Add the parameter and value to the INI file's globals */ ini_pair *pair; if(!(pair = malloc(sizeof *pair))) longjmp(on_error, OUT_OF_MEMORY); pair->param = par; pair->value = val; pair->left = pair->right = NULL; if(!ini->globals) ini->globals = pair; else insert_pair(ini->globals, pair); } } else longjmp(on_error, EXPECTED_PARAMETER); } return ini; }