void test_list(void) { int i, arr[MAX]; int k, s; list *l = EMPTY_LIST; generate(arr, MAX); for (i = 0; i < MAX; i++) { l = list_create(arr[i], 0); assert(l != EMPTY_LIST); assert(l->key == arr[i]); assert(list_count(l) == 1); assert(list_contains(l, arr[i])); l = list_destroy(l); assert(l == EMPTY_LIST); } for (i = 0; i < MAX; i++) { l = list_insert(l, arr[i], arr[i]); assert(list_contains(l, arr[i])); } assert(list_ordered(l)); for (i = 0; i < MAX; i++) { l = list_remove_first(l, &k, &s); assert(list_ordered(l)); } l = list_destroy(l); l = list_insert(l, 5, 0); l = list_insert(l, 4, 1); l = list_insert(l, 1, 2); l = list_insert(l, 2, 6); l = list_insert(l, 7, 4); l = list_insert(l, 0, 5); list_print(l); l = list_insert(l, 20, 10); l = list_insert(l, 25, -1); l = list_insert(l, 30, 3); list_print(l); l = list_remove_first(l, &k, &s); s = 15; l = list_insert(l, k, s); list_print(l); printf("count: %d\n", list_count(l)); printf("last: %d\n", list_last(l)->key); l = list_destroy(l); }
main() { DynamicList* l = list_inicialize(); /*Aluno* Al1 = (Aluno*)malloc(sizeof(Aluno)); Aluno* Al2 = (Aluno*)malloc(sizeof(Aluno)); Al1->RA = 1516; Al1->nome = "JOAO"; Al2->RA = 2324; Al2->nome = "SIBA"; */ printf("\nInseriu? %s", list_add_last(l, 'a')?"sim":"não"); printf("\nInseriu? %s", list_add_last(l, 'b')?"sim":"não"); printf("\nFirst %c\n", list_get_first(l)); printf("\nLast %c\n", list_get_last(l)); printf("\nInseriu? %s", list_add_last(l, 'c')?"sim":"não"); printf("\nInseriu? %s", list_add_last(l, 'd')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'e')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'f')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'g')?"sim":"não"); printf("\nFirst %c\n", list_get_first(l)); printf("\nLast %c\n", list_get_last(l)); list_print(l); printf("\nSize %d\n", list_size(l)); list_clear(l); printf("\nSize %d\n", list_size(l)); printf("\nInseriu? %s", list_add_last(l, 'c')?"sim":"não"); printf("\nInseriu? %s", list_add_last(l, 'd')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'e')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'f')?"sim":"não"); printf("\nInseriu? %s", list_add_first(l, 'g')?"sim":"não"); list_print(l); list_remove_first(l); list_remove_first(l); list_remove_first(l); list_print(l); }
t_list *list_remove_i(t_list **list, int n) { int i; t_list *current; t_list *temp_node; current = *list; temp_node = NULL; i = 0; if (n == 0) { free((*list)->content); *list = list_remove_first(list); return (*list); } while (i < n - 1) { if (!current->next) return (*list); current = current->next; i++; } temp_node = current->next; current->next = temp_node->next; free(temp_node->content); free(temp_node); return (*list); }
void sock_stop(void) { t_users *head; head = g_server->users; while ((head = list_remove_first(&head, sock_free_user))); if (close(g_server->sock) == -1) perror("close"); if (g_server->handlers->stop) g_server->handlers->stop(g_server->arg); free(g_server); }
Node *pool_append(PoolObject *pool, Data data){ if (pool == NULL) return NULL; if (list_is_empty(&pool->free_list)) if (append_array(pool) < 0) return NULL; Node *node = list_remove_first(&pool->free_list); node->value = data; hash_table_append(&pool->table, node); if (data.counter == 0){ ilist_append(&pool->list_zero, node); } return node; }
void hub_logout_log(struct hub_info* hub, struct hub_user* user) { struct hub_logout_info* loginfo = hub_malloc_zero(sizeof(struct hub_logout_info)); if (!loginfo) return; loginfo->time = time(NULL); memcpy(loginfo->cid, user->id.cid, sizeof(loginfo->cid)); memcpy(loginfo->nick, user->id.nick, sizeof(loginfo->nick)); memcpy(&loginfo->addr, &user->id.addr, sizeof(struct ip_addr_encap)); loginfo->reason = user->quit_reason; list_append(hub->logout_info, loginfo); while (list_size(hub->logout_info) > (size_t) hub->config->max_logout_log) { list_remove_first(hub->logout_info, hub_free); } }
void cons(i_list* l) { int i; // list size was increased: add nodes if (*l->size > l->prev_size) { for (i = 0; i < *l->size - l->prev_size; ++i) list_add_first(l->head, 0); } // list size was decreased: delete nodes else if (*l->size < l->prev_size) { for (i = 0; i < l->prev_size - *l->size; ++i) list_remove_first(l->head); } l->prev_size = *l->size; printf("list: "); list_print(*l->head); }
/** get and remove item from tail */ void* list_remove_last (linked_list* _this) { // list is empty if (_this->size == 0) { //NODE_DBG("LinkedList#_removeLast: The list is empty."); return NULL; } if (_this->size == 1) { // only head node return list_remove_first(_this); } else { node* tail = _this->tail; node* prev = tail->prev; void* item = tail->item; prev->next = NULL; os_free(tail); if (_this->size > 1) _this->tail = prev; _this->size--; if (_this->size <= 1) // empty or only head node _this->tail = NULL; return item; } }
/** get item and remove it from any position */ void* list_remove (linked_list* _this, int position) { // list is empty if (_this->size == 0) { //NODE_DBG("LinkedList#_remove: The list is empty."); return NULL; } else if (position >= _this->size) { // out of bound //NODE_DBG("LinkedList#_remove: Index out of bound"); return NULL; } // remove from head if (position == 0) { return list_remove_first(_this); } else if (position+1 == _this->size) { // remove from tail return list_remove_last(_this); } else { node* n = _this->head; node* prev; node* next; int i = 0; void* item; // loop until position while (i < position) { n = n->next; i++; } item = n->item; // remove node from list prev = n->prev; next = n->next; prev->next = next; next->prev = prev; os_free(n); _this->size--; return item ; } }
int main(int argc, char** argv) { init_config(); int execute_cmd(char* line, bool is_dump) { //if is_dump == true then file cmd // is_dump == false then stdin cmd if(is_dump == true) printf("%s\n", line); sendto(sock, line, strlen(line) + 1, 0,(struct sockaddr*)&lb_addr, sizeof(lb_addr)); cmd_exec(line); printf("> "); fflush(stdout); return 0; } List* fd_list = list_create(NULL); for(int i = 1; i < argc; i++) { int fd = open(argv[i], O_RDONLY); if(fd != -1) { list_add(fd_list, (void*)(int64_t)fd); } } list_add(fd_list, STDIN_FILENO); //fd of stdin void get_cmd_line(int fd) { static char line[MAX_LINE_SIZE] = {0, }; char* head; int seek = 0; static int eod = 0; //end of data while((eod += read(fd, &line[eod], MAX_LINE_SIZE - eod))) { head = line; for(; seek < eod; seek++) { if(line[seek] == '\n') { line[seek] = '\0'; int ret = execute_cmd(head, fd != STDIN_FILENO); if(fd != STDIN_FILENO && ret != 0 && ret != CMD_STATUS_ASYNC_CALL) {//parsing file and return error code printf("stop parsing file\n"); eod = 0; return; } head = &line[seek] + 1; } } if(head == line && eod == MAX_LINE_SIZE){ //not found '\n' and head == 0 printf("Command line is too long %d > %d\n", eod, MAX_LINE_SIZE); eod = 0; return; } else { //not found '\n' and seek != 0 memmove(line, head, eod - (head - line)); eod -= head - line; seek = eod; if(fd == STDIN_FILENO) { return; } else continue; } } if(eod != 0) { line[eod] = '\0'; execute_cmd(&line[0], fd != STDIN_FILENO); } eod = 0; return; } int fd = (int)(int64_t)list_remove_first(fd_list); int retval; char buf[CMD_RESULT_SIZE] = {0, }; fd_set in_put; fd_set temp_in; FD_ZERO(&in_put); FD_SET(sock, &in_put); FD_SET(fd, &in_put); printf(">"); fflush(stdout); while(is_continue) { temp_in = in_put; retval = select(fd > sock ? (fd + 1) : (sock + 1), &temp_in, 0, 0, NULL); if(retval == -1) { printf("selector error\n"); return -1; } else if(retval == 0) { printf("selector timeout\n"); continue; } else { if(FD_ISSET(sock, &temp_in) != 0) { struct sockaddr_in recv_addr; socklen_t recv_addr_size; recvfrom(sock, buf, CMD_RESULT_SIZE - 1, 0, (struct sockaddr*)&recv_addr, &recv_addr_size); printf("%s\n",buf); fflush(stdout); } if(FD_ISSET(fd, &temp_in) != 0) { get_cmd_line(fd); if(fd != STDIN_FILENO) { FD_CLR(fd, &in_put); close(fd); fd = (int)(int64_t)list_remove_first(fd_list); FD_SET(fd, &in_put); } } } } }
void test_list(void) { int val[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; list me = list_init(sizeof(int)); assert(me); assert(list_size(me) == 0); assert(list_is_empty(me)); for (int i = 0; i < 10; i++) { list_add_first(me, &val[i]); assert(list_size(me) == i + 1); int get = 0; list_get_first(&get, me); assert(get == val[i]); } assert(list_size(me) == 10); assert(!list_is_empty(me)); int get_arr[10] = {0}; list_copy_to_array(get_arr, me); for (int i = 0; i < 10; i++) { int get = 0; list_get_at(&get, me, i); assert(get == val[9 - i]); assert(get_arr[i] == val[9 - i]); } for (int i = 0; i < 7; i++) { list_remove_last(me); } int trimmed[5] = {0}; list_copy_to_array(trimmed, me); assert(list_size(me) == 3); for (int i = 0; i < 3; i++) { assert(10 - i == trimmed[i]); } int add = 3; list_add_last(me, &add); add = -1; list_add_at(me, 1, &add); add = -2; list_add_last(me, &add); assert(list_size(me) == 6); int get = 0xdeadbeef; list_get_first(&get, me); assert(get == 10); get = 0xdeadbeef; list_get_at(&get, me, 0); assert(get == 10); list_get_at(&get, me, 1); assert(get == -1); list_get_at(&get, me, 2); assert(get == 9); list_get_at(&get, me, 3); assert(get == 8); list_get_at(&get, me, 4); assert(get == 3); list_get_at(&get, me, 5); assert(get == -2); get = 0xdeadbeef; list_get_last(&get, me); assert(get == -2); list_remove_first(me); list_remove_at(me, 2); list_remove_last(me); assert(list_size(me) == 3); get = 345; list_get_first(&get, me); assert(get == -1); list_get_at(&get, me, 1); assert(get == 9); list_get_last(&get, me); assert(get == 3); int set = 12; list_set_first(me, &set); set = 13; list_set_at(me, 1, &set); set = 14; list_set_last(me, &set); int arr[3] = {0}; list_copy_to_array(arr, me); assert(arr[0] == 12); assert(arr[1] == 13); assert(arr[2] == 14); set = -5; list_set_at(me, 0, &set); set = -6; list_set_at(me, 1, &set); set = -7; list_set_at(me, 2, &set); list_copy_to_array(arr, me); assert(arr[0] == -5); assert(arr[1] == -6); assert(arr[2] == -7); assert(list_set_at(me, 4, &set) == -EINVAL); assert(list_get_at(&set, me, 4) == -EINVAL); assert(list_remove_at(me, 4) == -EINVAL); assert(list_add_at(me, 5, &set) == -EINVAL); assert(list_set_at(me, -1, &set) == -EINVAL); assert(list_get_at(&set, me, -1) == -EINVAL); assert(list_remove_at(me, -1) == -EINVAL); assert(list_add_at(me, -1, &set) == -EINVAL); list_clear(me); assert(list_size(me) == 0); assert(list_is_empty(me)); assert(list_remove_first(me) == -EINVAL); assert(list_remove_last(me) == -EINVAL); me = list_destroy(me); assert(!me); }