void insert_after(struct llist *target, int pos, struct llist *insert) { struct list_elem *elem; struct list_elem *after_insert; if (-1 == pos) { /* special case: prepend */ prepend_list(target, insert); return ; } else if ((pos >= 0) && (pos <= target->count-2)) { /* general case */ for (elem = target->head; pos > 0; pos--) elem = elem->next; after_insert = elem->next; elem->next = insert->head; insert->tail->next = after_insert; target->count += insert->count; return ; } else if (pos == target->count -1) { /* special case: append */ append_list(target, insert); return ; } else { /* unexpected index - this is not too bad, but it does indicate * wrong parameters */ /* We do nothing in this case - caller should check arguments. */ return ; } }
void AST_t::prepend(AST_t t) { if (t._ast == NULL) { // Do nothing return; } if (ASTType(t._ast) != AST_NODE_LIST) { std::cerr << "The prepended tree is not a list. No prepend performed" << std::endl; return; } AST prepended_list = t._ast; AST enclosing_list = get_enclosing_list(this->_ast); if (enclosing_list == NULL) { std::cerr << "Cannot found a suitable list to prepend" << std::endl; } prepend_list(enclosing_list, prepended_list); }
void AST_t::prepend_to_translation_unit(AST_t tree) { if (tree._ast == NULL) { return; } AST this_translation_unit = get_translation_unit(this->_ast); AST this_declaration_seq = ASTSon0(this_translation_unit); // Go to the very first one! while (ASTSon0(this_declaration_seq) != NULL) { this_declaration_seq = ASTSon0(this_declaration_seq); } AST tree_declaration_seq = tree._ast; if (this_declaration_seq == NULL) { ast_set_child(this_translation_unit, 0, tree_declaration_seq); } else { prepend_list(this_declaration_seq, tree_declaration_seq); } }
void AST_t::prepend_sibling_global(AST_t t) { if (t._ast == NULL) { return; } AST_t enclosing_global_tree = get_enclosing_global_tree_(this->_ast); AST list = get_enclosing_list(enclosing_global_tree._ast); AST prepended_list = get_list_of_extensible_block(t._ast); prepend_list(list, prepended_list); }
void AST_t::prepend_sibling_function(AST_t t) { // Do nothing if (t._ast == NULL) { return; } AST_t enclosing_function = this-> get_enclosing_function_definition(/*jump_templates*/true, /*jump_external_decl*/true); AST list = ASTParent(enclosing_function._ast); AST prepended_list = get_list_of_extensible_block(t._ast); prepend_list(list, prepended_list); }
int test_prepend_list_empty() { const char *test_name = "test_prepend_list_empty"; struct llist *list1; struct llist *list2; struct list_elem *el; /* Case 1: list 2 is empty */ list1 = create_llist(); append_element(list1, "yksi"); list2 = create_llist(); prepend_list(list1, list2); if (1 != list1->count) { printf ("%s: expected count of 1, got %d.\n", test_name, list1->count); return 1; } if (strcmp("yksi", (char *) list1->head->data) != 0) { printf ("%s: expected 'yksi', got '%s'.\n", test_name, (char *) list1->head->data); return 1; } if (strcmp("yksi", (char *) list1->tail->data) != 0) { printf ("%s: expexted list tail to be 'yksi', got '%s'.\n", test_name, (char *) list1->tail->data); return 1; } if (NULL != list1->tail->next) { printf ("%s: list1 is not terminated.\n", test_name); return 1; } /* Case 2: list 1 is empty */ list1 = create_llist(); list2 = create_llist(); append_element(list2, "yksi"); prepend_list(list1, list2); if (1 != list1->count) { printf ("%s: expected count of 1, got %d.\n", test_name, list1->count); return 1; } el = list1->head; if (strcmp("yksi", (char *) el->data) != 0) { printf ("%s: expected 'yksi', got '%s'.\n", test_name, (char *) el->data); return 1; } if (strcmp("yksi", (char *) list1->tail->data) != 0) { printf ("%s: expexted list tail to be 'yksi', got '%s'.\n", test_name, (char *) list1->tail->data); return 1; } if (NULL != list1->tail->next) { printf ("%s: list1 is not terminated.\n", test_name); return 1; } /* Case 3: both lists are empty */ list1 = create_llist(); list2 = create_llist(); prepend_list(list1, list2); if (0 != list1->count) { printf ("%s: expected count of 0, got %d.\n", test_name, list1->count); return 1; } if (NULL != list1->head) { printf ("%s: expected NULL head.\n", test_name); return 1; } if (NULL != list1->tail) { printf ("%s: expected NULL tail.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }
int test_prepend_list() { const char *test_name = "test_prepend_list"; struct llist *list1; struct llist *list2; list1 = create_llist(); append_element(list1, "yksi"); append_element(list1, "kaksi"); append_element(list1, "kolme"); append_element(list1, "neljä"); append_element(list1, "viisi"); list2 = create_llist(); append_element(list2, "kuusi"); append_element(list2, "seitsemän"); append_element(list2, "kahdeksan"); append_element(list2, "yhdeksän"); append_element(list2, "kymmenen"); prepend_list(list2, list1); struct list_elem *el; if (10 != list2->count) { printf ("%s: expected count of 10, got %d.\n", test_name, list2->count); return 1; } el = list2->head; if (strcmp("yksi", (char *) el->data) != 0) { printf ("%s: expected 'yksi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kaksi", (char *) el->data) != 0) { printf ("%s: expected 'kaksi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kolme", (char *) el->data) != 0) { printf ("%s: expected 'kolme', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("neljä", (char *) el->data) != 0) { printf ("%s: expected 'neljä', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("viisi", (char *) el->data) != 0) { printf ("%s: expected 'viisi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kuusi", (char *) el->data) != 0) { printf ("%s: expected 'kuusi', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("seitsemän", (char *) el->data) != 0) { printf ("%s: expected 'seitsemän', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kahdeksan", (char *) el->data) != 0) { printf ("%s: expected 'kahdeksan', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("yhdeksän", (char *) el->data) != 0) { printf ("%s: expected 'yhdeksän', got '%s'.\n", test_name, (char *) el->data); return 1; } el = el->next; if (strcmp("kymmenen", (char *) el->data) != 0) { printf ("%s: expected 'kymmenen', got '%s'.\n", test_name, (char *) el->data); return 1; } if (strcmp("kymmenen", (char *) list2->tail->data) != 0) { printf ("%s: expexted list tail to be 'kymmenen', got '%s'.\n", test_name, (char *) list2->tail->data); return 1; } if (NULL != list2->tail->next) { printf ("%s: list2 is not terminated.\n", test_name); return 1; } printf("%s ok.\n", test_name); return 0; }