/* For those not familiar with binary search: n_lbound is the leftmost item that it could be, n_rbound the rightmost item that it could be. We examine the item halfway between n_lbound and n_rbound, and that tells us either that we can increase n_lbound, or decrease n_rbound, or that we have found it, or if n_lbound <= n_rbound that there are no possible items, and we have not found it. With each examination we cut the number of possible items it could be by one more than half rounded down, or we find it. */ inline int bin_search ( void * p_v_key, /* Key to search for. */ void * p_v_base, /* First item in the array. */ int p_n_num, /* Number of items in the array. */ int p_n_width, /* Item size in the array. searched. Lest the reader be confused, note that this is crafted as a general function, and when it is applied specifically to the array of item headers in a node, p_n_width is actually the item header size not the item size. */ int * p_n_pos /* Number of the searched for element. */ ) { int n_rbound, n_lbound, n_j; for ( n_j = ((n_rbound = p_n_num - 1) + (n_lbound = 0))/2; n_lbound <= n_rbound; n_j = (n_rbound + n_lbound)/2 ) switch( COMP_KEYS((struct key *)((char * )p_v_base + n_j * p_n_width), p_v_key) ) { case -1: n_lbound = n_j + 1; continue; case 1: n_rbound = n_j - 1; continue; case 0: *p_n_pos = n_j; return ITEM_FOUND; /* Key found in the array. */ } /* bin_search did not find given key, it returns position of key, that is minimal and greater than the given one. */ *p_n_pos = n_lbound; return ITEM_NOT_FOUND; }
/* * Check whether a key is contained in the tree rooted from a buffer at * a path. This works by looking at the left and right delimiting keys * for the buffer in the last path_element in the path. These delimiting * keys are stored at least one level above that buffer in the tree. * If the buffer is the first or last node in the tree order then one * of the delimiting keys may be absent, and in this case get_lkey and * get_rkey return a special key which is MIN_KEY or MAX_KEY. */ static inline int key_in_buffer( struct path *p_s_chk_path, /* Path which should be checked. */ const struct cpu_key *p_s_key, /* Key which should be checked. */ struct reiserfs_sb_info *p_s_sbi) /* Super block pointer. */ { if (COMP_KEYS(get_lkey(p_s_chk_path, p_s_sbi), p_s_key) == 1) /* left delimiting key is bigger, that the key we look for */ return (0); if (COMP_KEYS(get_rkey(p_s_chk_path, p_s_sbi), p_s_key) != 1) /* p_s_key must be less than right delimitiing key */ return (0); return (1); }
/* Check whether a key is contained in the tree rooted from a buffer at a path. This works by looking at the left and right delimiting keys for the buffer in the last path_element in the path. These delimiting keys are stored at least one level above that buffer in the tree. If the buffer is the first or last node in the tree order then one of the delimiting keys may be absent, and in this case get_lkey and get_rkey return a special key which is MIN_KEY or MAX_KEY. */ static inline int key_in_buffer ( struct path * p_s_chk_path, /* Path which should be checked. */ struct key * p_s_key, /* Key which should be checked. */ struct super_block * p_s_sb /* Super block pointer. */ ) { #ifdef CONFIG_REISERFS_CHECK if ( ! p_s_key || p_s_chk_path->path_length < FIRST_PATH_ELEMENT_OFFSET || p_s_chk_path->path_length > MAX_HEIGHT ) reiserfs_panic(p_s_sb, "PAP-5050: key_in_buffer: pointer to the key(%p) is NULL or illegal path length(%d)", p_s_key, p_s_chk_path->path_length); if ( PATH_PLAST_BUFFER(p_s_chk_path)->b_dev == NODEV ) reiserfs_panic(p_s_sb, "PAP-5060: key_in_buffer: device must not be NODEV"); #endif if ( COMP_KEYS(get_lkey(p_s_chk_path, p_s_sb), p_s_key) == 1 ) return 0; if ( COMP_KEYS(p_s_key, get_rkey(p_s_chk_path, p_s_sb)) != -1 ) return 0; return 1; }