skiplist_node_t *pos_insert_skiplist(skiplist_t *sl, skiplist_node_t **ptr, skiplist_key_t *key, skiplist_data_t *data) { skiplist_node_t *sn2; int level, i; //** New node has to be inserted level = get_random_level(sl->max_levels, sl->p, sl->current_max); sn2 = create_skiplist_node(level); log_printf(15, "insert_skiplist: new node. level=%d\n", level); if (level > sl->current_max) { //** Need to adjust the head to handle the higher levels for (i=sl->current_max+1; i<=level; i++) { ptr[i] = sl->head; } sl->current_max = level; } for (i=0; i<=level; i++) { sn2->next[i] = ptr[i]->next[i]; ptr[i]->next[i] = sn2; } //** Finally store the data sn2->key = sl->dup(key); sn2->ele.data = data; return(sn2); }
bool skip_list<T>::insert_node(int key, T value) { //find in every level in the past skip list //which node is the last smaller than key skip_node<T> *last_smaller_in_level[MAX_LEVEL]; skip_node<T> *tmp = header; for(int i = 0; i <= level; i++) { while(tmp->next_in_level[i] != NULL) { if(tmp->next_in_level[i]->key == key) { printf("can't insert same key\n"); return false; } if(tmp->next_in_level[i]->key > key) break; else tmp = tmp->next_in_level[i]; } last_smaller_in_level[i] = tmp; } //random how much level to inserit //build a new skip list int insert_level = get_random_level(); printf("key %d insert_level %d\n", key, insert_level); skip_node<T> *p = create_skip_node(key, value); if(insert_level > level) { for(int i = 0; i <= level; i++) { skip_node<T> *tmp2 = last_smaller_in_level[i]->next_in_level[i]; last_smaller_in_level[i]->next_in_level[i] = p; p->next_in_level[i] = tmp2; } for(int i = level + 1; i <= insert_level; i++) { header->next_in_level[i] = p; } level = insert_level; } else { for(int i = 0; i <= insert_level; i++) { skip_node<T> *tmp2 = last_smaller_in_level[i]->next_in_level[i]; last_smaller_in_level[i]->next_in_level[i] = p; p->next_in_level[i] = tmp2; } } return true; }
static void insert_data_in_skiplist( skiplist_struct *skiplist, update_struct *update, float key, void *data_ptr ) { int i, new_level; skip_struct *x; #ifdef DEBUG int prev_size = -1; void test_skiplist_integrity(); #endif new_level = get_random_level(); if( new_level > skiplist->level ) { for( i = skiplist->level; i < new_level; ++i ) update->update[i] = skiplist->header; #ifdef DEBUG prev_size = skiplist->level; #endif skiplist->level = new_level; } ALLOC_SKIP_STRUCT( x, new_level ); x->data_ptr = data_ptr; x->key = key; for( i = 0; i < new_level; ++i ) { x->forward[i] = update->update[i]->forward[i]; update->update[i]->forward[i] = x; } #ifdef DEBUG test_skiplist_integrity( skiplist ); #endif }
static void insert_ptr_in_alloc_list( alloc_struct *alloc_list, update_struct *update, void *ptr, size_t n_bytes, VIO_STR source_file, int line_number, int sequence_number ) { int i, new_level; skip_entry *x; new_level = get_random_level(); if( new_level > alloc_list->level ) { for( i = alloc_list->level; i < new_level; ++i ) update->update[i] = alloc_list->header; alloc_list->level = new_level; } ALLOC_SKIP_STRUCT( x, new_level ); skip_alloc_size += sizeof(skip_entry)+((size_t)new_level-1) * sizeof(skip_entry *); x->ptr = ptr; x->n_bytes = n_bytes; x->source_file = source_file; x->line_number = line_number; x->sequence_number = sequence_number; update_total_memory( alloc_list, n_bytes ); for( i = 0; i < new_level; ++i ) { x->forward[i] = update->update[i]->forward[i]; update->update[i]->forward[i] = x; } }