int list_find_idx(LIST *list, int start_idx, void *extra, BOOL(*cmp_fn)(void*, void*)){ int i; for(i = start_idx; i < list->used_len; i++) if(cmp_fn(list_get_elem_by_idx(list, i), extra)) return i; return -1; }
void shift(item** root_address, const char* direction, int (*cmp_fn)(item*, item*)){ item* replacement; item* move = *root_address; if(strcmp(direction, "left")==0) replacement = move->right; else replacement = move->left; /* cant move */ if(!replacement) return; item* parent = move->parent; replacement->parent = parent; if(strcmp(direction, "left")==0) move->right=NULL; else move->left=NULL; insert(replacement, move, cmp_fn); *root_address = replacement; if(!parent) return; int comp = cmp_fn(parent, replacement); if(comp > 0) parent->left = replacement; else parent->right = replacement; *root_address = replacement; return; }
/* * list_find * * This routine tries to lookup an element in the given key using the * supplied key and a compare function. If no compare function is * passed, the default compare function is used. */ void * list_find (list_t *list, void *key, int32_t (*cmp_fn)(void *key1, void *key2)) { list_elem_t *elem; void *elem_key; /* Sanity check */ if (!list || !key) { return NULL; } /* Use the default compare function ifnothing is passed */ if (!cmp_fn) { cmp_fn = list_default_cmp_fn; } elem = list->list_head; while (elem != NULL) { elem_key = (void *)((uint8_t *)elem - list->list_offset); /* Compare the elements */ if (cmp_fn(key, elem_key) == 0) { /* Match found */ return elem_key; } elem = elem->next; } return NULL; }
static int cmp_entries_key(const struct tracing_map_sort_entry **a, const struct tracing_map_sort_entry **b) { const struct tracing_map_elt *elt_a, *elt_b; struct tracing_map_sort_key *sort_key; struct tracing_map_field *field; tracing_map_cmp_fn_t cmp_fn; void *val_a, *val_b; int ret = 0; elt_a = (*a)->elt; elt_b = (*b)->elt; sort_key = &elt_a->map->sort_key; field = &elt_a->fields[sort_key->field_idx]; cmp_fn = field->cmp_fn; val_a = elt_a->key + field->offset; val_b = elt_b->key + field->offset; ret = cmp_fn(val_a, val_b); if (sort_key->descending) ret = -ret; return ret; }
void* list_add_unique_elem(LIST *list, void *data, BOOL(*cmp_fn)(void*, void*)){ int i; void *exist_data; for(i = 0; i < list->used_len; i++){ exist_data = list_get_elem_by_idx(list, i); if(cmp_fn(exist_data, data)) return NULL; } return list_add_elem(list, data); }
/* we sort data from max to min */ int list_sort_insert(list_t *lst, node_t *node, int (*cmp_fn)(node_t *, node_t *)) { node_t *iter; if (node == NULL) return -1; if (lst->node_cnt == 0) list_insert_tail(lst, node); else if (!cmp_fn(node, list_tail(lst))) list_insert_tail(lst, node); else { for_each_node(iter, lst) { if (cmp_fn(node, iter)) { list_insert_after(lst, iter, node); break; } } } return 0; }
void selection_sort(void* arr, int len, size_t elem_size, int(cmp_fn)(void*, void*)) { for (int i = 0; i < len; i++) { int flag = i; void* smallest_addr = (char*)arr + i * elem_size; for (int j = i; j < len; j++) { void* curr_elem_addr = (char*)arr + j * elem_size; if (cmp_fn(smallest_addr, curr_elem_addr) > 0) { smallest_addr = (char*)arr + j * elem_size; flag = j; } } if (flag != i) { void* first_addr = (char*)arr + i * elem_size; void* second_addr = (char*)arr + flag * elem_size; swap(first_addr, second_addr, elem_size); } } }
void* nshmhash_get(NShm *nshm, const nshmhash_t *hash, const void *ptr, nshmhash_hash_fn hash_fn, nshmhash_cmp_fn cmp_fn){ void *_nshm_base = NULL; void *val = NULL; int bucket_idx; int64_t cursor; nshmhash_bucket_node_t *bn = NULL; if( NULL == nshm || NULL == hash || NULL == ptr || NULL == cmp_fn ){ return(NULL); } SET_NSHMBASE(nshm); bucket_idx = (int)(hash_fn(ptr) % hash->count); cursor = hash->bucket[bucket_idx]; while(0 <= cursor){ bn = vos_ptr(nshmhash_bucket_node_t*, cursor); val = vos_ptr(void*, bn->offset); if(cmp_fn(ptr, val) == 0){ return(val); } cursor = bn->next; } return(NULL); }