|
[code:1]
#difine PTRSIZE sizeof(void*)
__inline__ void
insert_leaf_8(void *old_leaf, const __u32 old_kpn, const __u32 old_offset,
__u32 total_elem,
void *new_leaf, const __u32 new_kpn, const __u32 new_offset,
const __u8 ins_num, void *ins_pos[], const __u32 key[],
void *nextseg[])
{
/* size of one node in bytes */
const __u32 nodesize = old_kpn * (1 + PTRSIZE) + old_offset;
/* buckets left in current old_leaf */
__u32 old_free = old_kpn;
/* buckets left in current new_leaf */
__u32 new_free = new_kpn;
/* conditions to insert either 1. or 2. key */
__u8 insert_key1, insert_key2;
/* maintains wether or not key1/key2 has been inserted */
__u8 key_inserted[] = {0, 0};
/* buckets left in the leaf in which one key will be inserted */
__u32 ins_free = total_elem + 1;
/* ins_id = {0 - first key to be inserted,
1 - second key to be inserted} */
__u8 ins_id = 0;
__u32 len;
void **newn;
if (old_kpn == new_kpn && ins_pos[0] > old_leaf) {
/* first part of the leaf can be copied since nothing
changes here */
len = (__u8 *) ins_pos[0] - (__u8 *) old_leaf;
memcpy(new_leaf, old_leaf, len);
old_leaf = ins_pos[0];
new_leaf = (__u8 *) new_leaf + len;
total_elem -= old_kpn * len / nodesize;
}
while (total_elem > 0) {
insert_key1 = (old_leaf >= ins_pos[0] && !key_inserted[0]);
insert_key2 = (ins_num == 2 && old_leaf >= ins_pos[1] &&
key_inserted[0] && !key_inserted[1]);
if (insert_key1 || insert_key2) {
ins_id = insert_key1 ? 0 : 1;
for (ins_free = 0;
key[ins_id] > *((__u8 *) old_leaf + ins_free);
ins_free++);
}
/* copy elements until either old_leaf or new_leaf is full or
total_elem == 0 or insertion position is reached */
len = min_a_b(ins_free,
min_a_b(total_elem,
min_a_b(old_free, new_free)));
if (len > 0) {
memcpy(new_leaf, old_leaf, len);
memcpy((__u8 *) new_leaf + new_free +
(new_kpn - new_free) * PTRSIZE,
(__u8 *) old_leaf + old_free +
(old_kpn - old_free) * PTRSIZE, len * PTRSIZE);
}
old_free -= len;
new_free -= len;
total_elem -= len;
ins_free -= len;
old_leaf = (__u8 *) old_leaf + len;
new_leaf = (__u8 *) new_leaf + len;
if (old_free == 0) {
/* goto next leaf in old leaf level */
old_leaf = (__u8 *) old_leaf + old_kpn * PTRSIZE +
old_offset;
old_free = old_kpn;
}
if (new_free == 0) {
/* goto next leaf in new leaf level */
new_leaf = (__u8 *) new_leaf + new_kpn * PTRSIZE +
new_offset;
new_free = new_kpn;
}
if (ins_free == 0) {
/* insert new key in leaf */
*(__u8 *) new_leaf = key[ins_id];
newn = (void **) ((__u8 *) new_leaf + new_free +
(new_kpn - new_free) * PTRSIZE);
*newn = nextseg[ins_id];
new_leaf = (__u8 *) new_leaf + 1;
new_free--;
ins_free = total_elem + 1;
key_inserted[ins_id] = 1;
if (new_free == 0) {
/* goto next leaf in new leaf level */
new_leaf = (__u8 *) new_leaf +
new_kpn * PTRSIZE + new_offset;
new_free = new_kpn;
}
}
}
}
[/code:1]
虽然有些注释,但是还是看不明白,请各位帮忙,多谢!!! |
|