int main() { long i; o_t *o; printf("starting \n"); o = create_order(); for(i=100000; i>=0; i-- ) insert_bottom( o, p(i) ); for(i=100001; i< 300007; i+=2 ) { insert_after(o, p(i+1), p(i-1) ); insert_before( o, p(i), p(i+1) ); } printf("inserted 300000 elements. "); for(i = 250000; i < 300007; i++ ) delete_o( o, p(i) ); printf("deleted 50000 elements. "); insert_top( o, p(300006) ); for(i = 250000; i < 300006; i++ ) insert_before( o, p(i) , p(300006) ); printf("reinserted. now testing order\n"); for( i=0; i < 299000; i +=42 ) { if( compare( o, p(i), p(i+23) ) != 1 ) { printf(" found error (1) \n"); exit(0); } } for( i=300006; i >57; i -=119 ) { if( compare( o, p(i), p(i-57) ) != 0 ) { printf(" found error (0) \n"); exit(0); } } printf("finished. no problem found.\n"); }
void insert_bottom_test(Stats_Log *log, Buffer_Set *buffers, int test_repitions, float *advance_data, int edit_count, void *scratch, int scratch_size, Record_Statistics *stats_out){ Sample_Machine machine; machine = begin_machine(test_repitions, &scratch, &scratch_size); char word[] = "stuff"; int word_len = sizeof(word) - 1; int i, j; for (i = 0; i < test_repitions; ++i){ start(&machine); for (j = 0; j < edit_count; ++j){ insert_bottom(&buffers->buffer, word, word_len, advance_data, scratch, scratch_size); } machine.samples[i].buffer = stop(&machine); start(&machine); for (j = 0; j < edit_count; ++j){ insert_bottom(&buffers->gap_buffer, word, word_len, advance_data, scratch, scratch_size); } machine.samples[i].gap_buffer = stop(&machine); start(&machine); for (j = 0; j < edit_count; ++j){ insert_bottom(&buffers->multi_gap_buffer, word, word_len, advance_data, scratch, scratch_size); } machine.samples[i].multi_gap_buffer = stop(&machine); start(&machine); for (j = 0; j < edit_count; ++j){ insert_bottom(&buffers->rope_buffer, word, word_len, advance_data, scratch, scratch_size); } machine.samples[i].rope_buffer = stop(&machine); if (i == 0){ stream_check_test(buffers, scratch, scratch_size); } } end_machine(&machine, stats_out, __FUNCTION__); log_sample_set(log, litstr("insert-bottom"), stats_out, machine.samples, machine.count); }
/* inserts the key a immediately before key b in the ordered set */ void insert_before(o_t *ord, key_t a, key_t b){ if( ord == NULL ) return; if( ord->right == NULL && ord->height == 0 ){ /* single o_t node */ insert_bottom(ord,a); return; } int stack_p = 0; o_t *path_st_p[STACKSIZE]; for(int i=0; i <STACKSIZE; ++i ) path_st_p[i] = NULL; o_t *b_ot_leaf = ord; while( b_ot_leaf->right != NULL){ /* find o_t leaf */ path_st_p[stack_p++] = b_ot_leaf; if( b < b_ot_leaf->key ) b_ot_leaf = b_ot_leaf->left; else b_ot_leaf = b_ot_leaf->right; } if( b_ot_leaf->key != b ){ printf("ERROR: in insert_before(), key (%ld) not found in o_t \n", b); exit(1); } int is_bottom_need_update = ( ord->lot_root->bottom->key == b ) ? 1 : 0; /* begin insertion in l_o_t tree*/ l_o_t *b_lot_leaf = NULL; l_o_t *new_lot_leaf = NULL; l_o_t *new_lot_upper = NULL; l_o_t *lot_parent = NULL; b_lot_leaf = (l_o_t *) b_ot_leaf->left; lot_parent = b_lot_leaf->parent; /* new parent leaf */ new_lot_upper = _l_o_t_get_node(); new_lot_upper->top = NULL; new_lot_upper->bottom = NULL; new_lot_upper->parent = lot_parent; if( lot_parent != NULL ){ if( lot_parent->left != b_lot_leaf ) lot_parent->right = new_lot_upper; else lot_parent->left = new_lot_upper; } /* new leaf */ new_lot_leaf = _l_o_t_get_node(); new_lot_leaf->key = a; new_lot_leaf->height= 0; new_lot_leaf->right = NULL; key_t *obj = (key_t *) malloc( sizeof(key_t)); *obj = a; new_lot_leaf->left = (l_o_t *) obj; new_lot_leaf->top = NULL; new_lot_leaf->bottom = NULL; new_lot_leaf->parent = new_lot_upper; /* update- leaf's parent*/ b_lot_leaf->parent = new_lot_upper; if( a < b ) new_lot_upper->key = b; else new_lot_upper->key = a; /* a comes before b*/ new_lot_upper->left = new_lot_leaf; new_lot_upper->right = b_lot_leaf; if( is_bottom_need_update ) ord->lot_root->bottom = new_lot_leaf; _l_o_t_rebalance_tree(new_lot_leaf); stack_p = 0; for(int i=0; i <STACKSIZE; ++i ) path_st_p[i] = NULL; b_ot_leaf = ord; while( b_ot_leaf->right != NULL){ /* find o_t leaf */ path_st_p[stack_p++] = b_ot_leaf; if( a < b_ot_leaf->key ) b_ot_leaf = b_ot_leaf->left; else b_ot_leaf = b_ot_leaf->right; } /* insert new key in o_t tree*/ o_t *new_ot_leaf = NULL; o_t *other_ot_leaf = NULL; /* a new leaf to stand for the old one */ other_ot_leaf = _o_t_get_node(); other_ot_leaf->key = b_ot_leaf->key; other_ot_leaf->height = b_ot_leaf->height; other_ot_leaf->lot_root = NULL; other_ot_leaf->left = b_ot_leaf->left; other_ot_leaf->right = b_ot_leaf->right; new_ot_leaf = _o_t_get_node(); new_ot_leaf->key = a; new_ot_leaf->height = 0; new_ot_leaf->lot_root = NULL; new_ot_leaf->left = (o_t *) new_lot_leaf; new_ot_leaf->right = NULL; if( a < other_ot_leaf->key){ b_ot_leaf->left = new_ot_leaf; b_ot_leaf->right = other_ot_leaf; } else{ b_ot_leaf->key = new_ot_leaf->key; b_ot_leaf->left = other_ot_leaf; b_ot_leaf->right= new_ot_leaf; } path_st_p[stack_p++] = b_ot_leaf; _o_t_rebalance_tree(path_st_p, stack_p ); }