int main() { int data[LOOP]; int i, res; int* p; struct slist_t* sl; sl = slist_init(); assert(sl); for (i = 0; i < LOOP; ++ i) { data[i] = rand() % LOOP; res = slist_push_front(sl, &data[i]); assert(0 == res); res = slist_push_back(sl, &data[i]); assert(0 == res); } printf("list count=%d\n", slist_count(sl)); for (i = LOOP-1; i >= 0; -- i) { res = slist_find(sl, &data[i]); assert(0 == res); p = slist_pop_front(sl); assert(p == &data[i]); p = slist_pop_back(sl); assert(p == &data[i]); res = slist_find(sl, &data[i]); assert(res < 0); } printf("list count=%d\n", slist_count(sl)); slist_release(sl); sl = 0; return 0; }
bool hash_table_insert(struct hash_table *m, void *key, void *value) { if (!m) { return false; } const fsize_t current_load = hash_table_get_current_load(m); if (current_load > m->load_factor) { hash_table_rehash(m); } struct slist *bucket = get_map_bucket(m, key, true); if (!bucket) { return false; } struct slist_node *found = slist_find(bucket, key); if (found) { return false; } struct gcl_pair *node = create_map_node(key, value); if (node) { slist_insert(bucket, node); ++m->count; } return node != NULL; }
int main(int argc, char **argv) { int num_size = sizeof(int); slnode_t node = NULL; slist_t numbers = slist_create(free_mydata); if (numbers == NULL) return errno; for (int i = 0; i < 10; ++i) { int *num = num = (int *) malloc(num_size); if (num == NULL) { errno = ENOMEM; break; } *num = i; node = slnode_create(num, num_size); if (node == NULL) { break; } slist_add(numbers, node); } if ((node = slist_find(numbers, my_compare, (void *) 8)) != NULL) { printf("number %d was found.\n", *((int *)node->data)); } slist_free(numbers); return 0; }
SList *queue_find (Queue *queue, const void *data) { /* Precondition */ assert(queue != NULL); /* Postcondicion */ assert((int) queue->length == slist_length(queue->head)); return slist_find(queue->head, data); }
int test_base_slist(const char* param) { struct slist_t* sl = slist_create(); if (!sl) { fprintf(stderr, "slist create fail\n"); return -1; } rand_seed(time(NULL)); int loop = param ? atoi(param) : 32; int data[loop]; for (int i = 0; i < loop; ++ i) { data[i] = (int)(rand_gen()) % loop; int res = slist_push_front(sl, &data[i]); CHECK(sl, res == 0, "slist push front fail"); res = slist_push_back(sl, &data[i]); CHECK(sl, res == 0, "slist push back fail"); } CHECK(sl, slist_size(sl) == 2 * loop, "slist size fail"); for (int i = loop - 1; i >= 0; -- i) { int res = slist_find(sl, &data[i]); CHECK(sl, res == 0, "slist find fail"); void* p = slist_pop_front(sl); CHECK(sl, p == &data[i], "slist pop front fail"); p = slist_pop_back(sl); CHECK(sl, p == &data[i], "slist pop back fail"); res = slist_find(sl, &data[i]); CHECK(sl, res < 0, "slist find fail"); } CHECK(sl, slist_size(sl) == 0, "slist size fail"); slist_release(sl); return 0; }
// return = 0, success & process // return < 0, fail // return > 0, noting to do int epoll_dispatch(struct reactor_t* reactor, int ms) { int res, i, type; struct handler_t* h; epoll_t* epoll; if (!reactor || !reactor->data) return -1; epoll = (epoll_t*)(reactor->data); res = epoll_wait(epoll->epoll_fd, epoll->events, EPOLL_SIZE, ms); if (res < 0) { if (EINTR != errno) return -errno; return 0; } // nothing to do if (0 == res) return 1; // get events for (i = 0; i < res; i++) { type = epoll->events[i].events; h = (struct handler_t*)epoll->events[i].data.ptr; // check if expired if (0 == slist_find(epoll->expired, h)) continue; // IO callback if ((EPOLLIN & type) || (EPOLLHUP & type)) { res = h->in_func(h); if (res < 0) { h->close_func(h); continue; } } if (EPOLLOUT & type) { res = h->out_func(h); if (res < 0) { h->close_func(h); continue; } } if (EPOLLERR & type) { h->close_func(h); } } // clean expired list slist_clean(epoll->expired); return 0; }
/* all jobs must have successors except the end job, and all jobs must have predeccessors except the start job */ int check_dependencies(struct rcps_problem *p) { int result = RCPS_CHECK_OK; int end_count = 0; int i, k; struct rcps_job *start_job; struct slist *visited; struct slist *has_predecessor = slist_new(job_compare); for (i = 0; i < p->job_count; ++i) { struct rcps_job *j = p->jobs[ i ]; //printf("check_dependencies: job %s successors: %i\n", j->name, j->successor_count); if (j->successor_count == 0) { ++end_count; } else { for (k = 0; k < j->successor_count; ++k) { //printf("check_dependencies: job %s successor[%i] = %s\n", j->name, k, j->successors[k]->name); slist_add(has_predecessor, slist_node_new(j->successors[k])); } } } if (end_count > 1) { result += RCPS_CHECK_MULTIPLE_END_JOBS; } else if (end_count == 0) { result += RCPS_CHECK_END_JOB_MISSING; } if (result == RCPS_CHECK_OK) { start_job = 0; for (i = 0; i < p->job_count; ++i) { if (!slist_find(has_predecessor, p->jobs[i])) { start_job = p->jobs[i]; } } if (start_job) { /* All other jobs should be successors of the start job */ //printf("check_dependencies: check circular\n"); visited = slist_new(job_compare); result += check_circulardependencies(start_job, visited); slist_free(visited, NULL); } else { result += RCPS_CHECK_START_JOB_MISSING; } } slist_free(has_predecessor, NULL); //printf("check_dependencies: result=%i\n", result); return result; }
int check_circulardependencies(struct rcps_job *job, struct slist *visited) { int result = RCPS_CHECK_OK; int i; struct slist_node *n; //printf("check_circulardependencies: %s visited: %i\n", job->name, slist_count(visited)); if ( slist_find(visited, job)) { result = RCPS_CHECK_CIRCULAR_DEPENDENCY; //printf("check_circulardependencies: %s already visited\n", job->name); } else { n = slist_node_new(job); slist_add(visited, n); for (i = 0; i < job->successor_count; ++i) { result = check_circulardependencies( job->successors[ i ], visited ); if ( result != RCPS_CHECK_OK ) { break; } } // remove this job from the visited to avoid false positive slist_unlink(visited, n); slist_node_free(n, NULL); } return result; }
struct gcl_pair *hash_table_find(struct hash_table *m, void *key) { const struct slist *bucket = get_map_bucket(m, key, false); if (!bucket) { return NULL; } return slist_find(bucket, key); }
gchar* snippets_get_value (GuSnippets* sc, const gchar* term) { gchar* key = g_strdup_printf ("%s,", term); slist* index = slist_find (sc->head, key, TRUE, FALSE); g_free (key); return (index)? index->second: NULL; }
const lt_dlvtable * lt_dlloader_find (const char *name) { return lt_dlloader_get (slist_find (loaders, loader_callback, (void *) name)); }