int main(int argc, char **argv) { //Array statistics double array[5] = {2.0, 3.89, -3.94, 10.1, 0.88}; print_array_stats(array, 5); //Creating liked list with 3 3s and 4 4s linked_list *ll3 = new_linked_list(3, 3); linked_list *ll4 = new_linked_list(4, 4); //Should print: "3 3 3" print_linked_list(ll3, 1, 1); //Inserting a 5 at the 1st position insert_linked_list(ll3, 1, 5); //Should print "3 5 3 3" print_linked_list(ll3, 1, 1); //Printing backwards, should print: "3 3 5 3" print_linked_list(ll3, 1, 0); //Merging the linked lists merge_linked_list(ll3, ll4); //Printing the result, should print: "3 4 5 4 3 4 3 4" print_linked_list(ll3, 1, 1); //Summing the elements, should be 30 printf("Sum: %d\n", sum_linked_list(ll3)); //Freeing the memory of ll3 destroy_linked_list(ll3); }
/* ========================== FUZZY ENGINE LOGIC ========================== */ fuzzy_engine* create_fuzzy_engine() { fuzzy_engine* engine = (fuzzy_engine*)os_malloc(sizeof(fuzzy_engine)); engine->ling_vars = new_linked_list(); engine->rules = new_linked_list(); engine->consequents = new_linked_list(); return engine; }
static char* test_create_list() { linked_list* list = new_linked_list(10); mu_assert("Couldn't create a list", list != NULL); list->delete_linked_list(&list); return 0; }
static char* test_add_after() { linked_list* list = new_linked_list(10); list->add_to_list(list, 1); mu_assert("Couldn't add to list", list->num_elements == 1); list->add_to_list(list, 5); mu_assert("Couldn't add to list", list->num_elements == 2); list->add_to_list(list, 9); mu_assert("Couldn't add to list", list->num_elements == 3); list->add_after(list, 5, 7); mu_assert("Couldn't add 7 after 5 1", list->num_elements == 4); node* element = list->find_element(list, 5); mu_assert("Coudln't add 7 after 5 2", element->next->data == 7); element = list->find_element(list, 7); mu_assert("Coudln't add 7 after 5 3", element->next->data == 9); list->add_after(list, 9, 11); mu_assert("Couldn't add 11 after 9 1", list->num_elements == 5); element = list->find_element(list, 9); mu_assert("Coudln't add 11 after 9 2", element->next->data == 11); element = list->find_element(list, 11); mu_assert("Coudln't add 11 after 9 3", element->next == NULL); return 0; }
// add item to linked list LinkedList add_linked_item( LinkedList list, void * item ) { LinkedList node = new_linked_list( item ); node->next = list; return node; }
linked_list* build_list_from_file(FILE* file, bool reverse) { linked_list* new_list = new_linked_list(); int ch; bool still_reading = true; bool first_val_read = false; int number = 0; while (!first_val_read && still_reading && (ch = getc(file)) != EOF) { switch(ch) { case SPACE: break; // ignore spaces case RETURN: case NEWLINE: still_reading = false; break; case COMMA: ll_init_add(new_list, number); number = 0; first_val_read = true; break; // NUMBER ENCOUNTERED default: number = number*10 + char_to_num(ch); } } while (still_reading && (ch = getc(file)) != EOF) { switch(ch) { case SPACE: break; // ignore spaces case RETURN: case NEWLINE: still_reading = false; break; case COMMA: if (reverse) { ll_add_to_head(new_list, number); } else { ll_add_to_tail(new_list, number); } number = 0; break; // NUMBER ENCOUNTERED default: number = number*10 + char_to_num(ch); } } if (reverse) { ll_add_to_head(new_list, number); } else { ll_add_to_tail(new_list, number); } return new_list; }
static char* test_add_to_beginning_of_list() { linked_list* list = new_linked_list(10); list->add_to_beginning(list, 1); mu_assert("List is null", list->root != NULL); mu_assert("Couldn't add to beginning of list", list->root->data == 1); list->add_to_beginning(list, 5); mu_assert("Couldn't add to beginning of list", list->root->data == 5); list->delete_linked_list(&list); return 0; }
static char* test_add_to_list() { linked_list* list = new_linked_list(10); list->add_to_list(list, 1); mu_assert("Couldn't add to list", list->num_elements == 1); list->add_to_list(list, 5); mu_assert("Couldn't add to list", list->num_elements == 2); list->add_to_list(list, 9); mu_assert("Couldn't add to list", list->num_elements == 3); list->delete_linked_list(&list); return 0; }
static char* test_find_element() { linked_list* list = new_linked_list(10); list->add_to_list(list, 1); mu_assert("Couldn't add to list", list->num_elements == 1); list->add_to_list(list, 5); mu_assert("Couldn't add to list", list->num_elements == 2); list->add_to_list(list, 9); mu_assert("Couldn't add to list", list->num_elements == 3); node* element = list->find_element(list, 5); mu_assert("Couldn't find element", element->data == 5); return 0; }
/* ====================== LINGUISTIC VARIABLE LOGIC ======================= */ ling_var* create_linguistic_variable(const char* name, int id, ling_var_type type) { uint8_t name_len = strlen(name); ling_var* var = (ling_var*)os_malloc(sizeof(ling_var)); os_memset(var, 0, sizeof(ling_var)); var->name = (char*)os_malloc(name_len + 1); strcpy(var->name, name); var->id = id; var->values = new_linked_list(); var->type = type; return var; }
static char* test_delete_from_list() { linked_list* list = new_linked_list(10); list->add_to_list(list, 1); mu_assert("Couldn't add to list", list->num_elements == 1); list->add_to_list(list, 5); mu_assert("Couldn't add to list", list->num_elements == 2); list->add_to_list(list, 9); mu_assert("Couldn't add to list", list->num_elements == 3); list->remove_from_list(list, 1); mu_assert("Couldn't remove 1 from list", list->num_elements == 2); mu_assert("Last element not correct", list->last->data != 1); list->remove_from_list(list, 5); mu_assert("Couldn't remove 5 from list", list->num_elements == 1); mu_assert("Last element not correct", list->last->data != 5); list->remove_from_list(list, 9); mu_assert("Couldn't remove 9 from list", list->num_elements == 0); list->delete_linked_list(&list); return 0; }
linked_list* get_polygon_points(point* points, uint8_t length) { uint8_t i, j, found; point* intersection_point, *tmp_point; linked_list* processed_points = new_linked_list(); for(i = 0; i < length; i++) { found = 0; for(j = i + 2; j < length - 1; j++) { intersection_point = get_intersection_point( points[i], points[i + 1], points[j], points[j + 1]); if(intersection_point != 0) { tmp_point = (point*)os_malloc(sizeof(point)); tmp_point->x = points[i].x; tmp_point->y = points[i].y; processed_points->add(processed_points, (void*)tmp_point); // the intersection point processed_points->add(processed_points, (void*)intersection_point); tmp_point = (point*)os_malloc(sizeof(point)); tmp_point->x = points[j + 1].x; tmp_point->y = points[j + 1].y; processed_points->add(processed_points, (void*)tmp_point); found = 1; break; } } if(found == 0) { tmp_point = (point*)os_malloc(sizeof(point)); tmp_point->x = points[i].x; tmp_point->y = points[i].y; processed_points->add(processed_points, (void*)tmp_point); } else { i = i + 3; } } return processed_points; }
/* * Creates a rule consequent if and only if it does not find * an identical one. If it does find an identical one, it will return it * Consequents will be singletones */ rule_consequent* create_rule_consequent(fuzzy_engine* engine, ling_var* variable, ling_val* value) { linked_list_node* node = engine->consequents->head; while(node != 0) { rule_consequent* tmp_consequent = (rule_consequent*)node->data; if(tmp_consequent->variable == variable && tmp_consequent->value == value) { return tmp_consequent; } node = node->next; } rule_consequent* consequent = (rule_consequent*)new_linked_list(); memset(consequent, 0, sizeof(rule_consequent)); consequent->variable = variable; consequent->value = value; // add the new consequent to the list engine->consequents->add(engine->consequents, consequent); return consequent; }
rule_antecedent* create_rule_antecedent() { rule_antecedent* antecedent = new_linked_list(); return antecedent; }
/** * create a new pointer list **/ pointer_list_t* new_pointer_list(const char *name) { return new_linked_list(name,sizeof(pointer_list_entry_t)); }