///수정 실행(삭제된 고유번호로 입력됨, 고유번호 변환 없음) unsigned int _tw2_word_update_run (BTREE* ws, BTREE* wi, char *in_word, unsigned int kno) { NODE5 *leaf_ps, *leaf_pi; char* pword; unsigned int *pno; //입력용 일련번호(고유숫자) int err=0; if (ws->kno != wi->kno) { //인덱스 고유번호 다름(이조건이 발생하면 않됨) printf ("## Failure to index serial number.(ws:%u, wi:%u)\n", ws->kno, wi->kno); return UIFAIL; } //삭제된 후 리프 다시 검색 if (bpt_find_leaf_key (ws, in_word, &leaf_ps) >= 0) { printf ("## Exist same word.\n"); return UIFAIL; } //리프에 동일한 키가 없다면 메모리할당후 삽입 pword = malloc (str_len(in_word) + 1); if (!pword) { printf ("## Failure to allocate memory.\n"); err++; //메모리 할당 실패 } str_copy (pword, in_word); pno = malloc (sizeof(unsigned int)); if (!pno) { printf ("## Failure to allocate memory.\n"); err++; //메모리 할당 실패 } *pno = kno; if (bpt_find_leaf_key (wi, pno, &leaf_pi) >= 0) { //고유번호 존재 (이조건이 발생하면 않됨) printf ("## Exist same number key.(s:%s, i:%u)\n", in_word, *pno); err++; } if (err) { free (pword); free (pno); return UIFAIL; } //문자키 입력 ws->root = bpt_insert (ws, leaf_ps, pword, pno, FLAG_UPDATE); //순자키 입력 wi->root = bpt_insert (wi, leaf_pi, pno, pword, FLAG_UPDATE); return *pno; }
void bpt_insert_check(bpt* b, long* xs, long n) { long i; void** ptr; for (i = 0; i < n; i++) { ptr = bpt_insert(b, (void*) xs[i], (void*) xs[i]); assert("insert failed:" && ptr && xs[i] == (long) *ptr); } }
int pdf_obj_insert(int n, int gen, void *d) { pdf_map *m = pdf_map_insert(n, gen); pdf_obj *o = NULL; if (o = pdf_obj_find(n, gen)) { char buf[128]; sprintf(buf, "Duplicated object (%d,%d) is found, old one is removed!\n", n, gen); DMSG(buf); pdf_obj_delete(o); pdf_free(o); } bpt_insert(m->head, n, d); return 0; }
//교정(revision) 단어 B+트리에 입력 bool tw2_rev_word_insert (BTREE* rs, char akey[][ASIZE]) { char *pkey1, *pkey2, *mkey1, *mkey2; NODE5 *leaf; pkey1 = str_lower (akey[0]); //소문자로 변환 pkey1 = str_trim (pkey1); //앞뒤 whitespace 잘라냄 if (! *pkey1) return false; pkey2 = str_lower (akey[1]); //소문자로 변환 pkey2 = str_trim (pkey2); //앞뒤 whitespace 잘라냄 if (! *pkey2) return false; if (bpt_find_leaf_key (rs, pkey1, &leaf) < 0) { //리프에 동일한 키가 없다면 메모리할당후 삽입 mkey1 = malloc (str_len (pkey1) + 1); if (!mkey1) { printf ("## Failure to allocate memory.\n"); return false; } str_copy (mkey1, pkey1); mkey2 = malloc (str_len (pkey2) + 1); if (!mkey2) { printf ("## Failure to allocate memory.\n"); return false; } str_copy (mkey2, pkey2); //B+트리에 입력 rs->root = bpt_insert (rs, leaf, mkey1, mkey2, FLAG_INSERT); //printf ("** Inserted.\n"); return true; } else { printf ("** Exist same key.\n"); return false; } }
//파일에서 사전정보를 읽어서 B+트리에 할당 (숫자키) unsigned int fio_read_from_file (char *fname, BTREE* btree) { FILE *fp; NODE5 *leaf, *leaf_left; register int i, j=0; int ch, ihalf, imod; unsigned int sno=0; unsigned int *pno, kno=0, *pnoa[ASIZE]; //키포인터 배열은 B_ORDER에 의해 결정되나, 단어 길이로 넉넉하게... char in_key[ASIZE], in_word[ASIZE]; //단어길이 char *pword, *pworda[ASIZE]; char prompt[] = {'+', '-', '*'}; printf ("Reading data from the %s...\n", fname); fp = fopen (fname, "r"); if (fp == NULL) { printf (", Not exist!\n"); return 0; } if (_fio_read_header (fp, fname, in_word) < 0) { printf (", Header error!\n"); return 0; } //리프노드 엔트리(배열)의 split 위치 인덱스 //B_ORDER가 짝수일때, 리프분할되는 개수가 일정해짐 ihalf = _bpt_half_order (btree->order); while ((ch = fgetc (fp)) != EOF) { //-1 i = 0; while (ch != '\0' && i < ASIZE-2) { in_key[i++] = ch; ch = fgetc (fp); } in_key[i] = '\0'; ch = fgetc (fp); i = 0; while (ch != '\0' && i < ASIZE-2) { in_word[i++] = ch; ch = fgetc (fp); } in_word[i] = '\0'; //문자열을 unsigned int로 변환 kno = str_to_uint (in_key); pno = malloc (sizeof(unsigned int)); if (!pno) { printf ("## Failure to allocate pno in fio_read_from_file().\n"); break; //메모리 할당 실패 } *pno = kno; //키고유번호 pword = malloc (str_len (in_word) + 1); if (!pword) { printf ("## Failure to allocate pword in fio_read_from_file().\n"); break; //메모리 할당 실패 } str_copy (pword, in_word); //printf ("%u:%s ", *pno, pword); //숫자키 순서대로 진행되기 때문에 입력속도 개선했지만, 노드의 50%만 채워지는 문제 있음 //leaf 노드가 오른쪽으로 연결된 순서대로 삽입되므로 다시 검색할 필요 없음(속도 개선) //리프노드 찾을 필요 없음 //leaf = bpt_find_leaf (btree, pno, btree->compare); //btree->root = bpt_insert (btree, leaf, pno, pword); //btree->root = bpt_insert_asc (btree, &leaf, pno, pword); //리프 노드의 엔트리를 100% 채우기 위해서 아래 코드로 수정 (단, B_ORDER가 짝수여야 함) imod = (int)(sno % (btree->order-1)); if (imod < ihalf) { //배열의 왼쪽 btree->root = bpt_insert_asc (btree, &leaf, pno, pword); if (sno == 0) leaf_left = btree->root; printf ("%c(%c)%u ", CR, prompt[sno%3], sno); //읽기표시 프롬프트 } else { //엔터리 오른쪽 보관 pnoa[j] = pno; pworda[j++] = pword; } if (imod == (ihalf-1) && j > 0) { //보관된 엔터리를 왼쪽 리프에 채움 (리프 노드가 100% 채워짐) for (i = 0; i < j; i++) btree->root = bpt_insert_asc (btree, &leaf_left, pnoa[i], pworda[i]); leaf_left = leaf_left->pointers [btree->order - 1]; j = 0; } sno++; //순번 } //while //보관된 엔터리가 있다면 맨오른쪽 리프에 삽입 for (i = 0; i < j; i++) { btree->root = bpt_insert (btree, leaf, pnoa[i], pworda[i], FLAG_INSERT); if (leaf->pointers [btree->order -1]) leaf = leaf->pointers [btree->order -1]; //리프분할이 다시 발생 했다면 } fclose (fp); printf ("%c(%c)%u ", CR, prompt[0], sno); //읽기표시 프롬프트 printf ("have read data: count (%u)\n", btree->kcnt); return sno; //읽은 키 개수 }
//HASHSIZE가 변경된 경우 해시값별로 B+트리에 저장(노드의 엔트리 100% 채워지지 않음) unsigned int fio_read_trans_hash (char *fname, BTREE** hb[], int sh) { FILE *fp; NODE5 *leaf; register int i; int ch; unsigned int sno=0, h; char in_key[SSIZE], in_word[SSIZE]; //숫자조합 인덱스 길이 char *pkey, *pword; char prompt[] = {'+', '-', '*'}; printf ("Reading data(hash) from the %s...\n", fname); fp = fopen (fname, "r"); if (fp == NULL) { printf (", Not exist!\n"); return 0; } if (_fio_read_header (fp, fname, in_word) < 0) { printf (", Header error!\n"); return 0; } while ((ch = fgetc (fp)) != EOF) { //-1 i = 0; while (ch != '\0' && i < SSIZE-2) { in_key[i++] = ch; ch = fgetc (fp); } in_key[i] = '\0'; ch = fgetc (fp); i = 0; while (ch != '\0' && i < SSIZE-2) { in_word[i++] = ch; ch = fgetc (fp); } in_word[i] = '\0'; pkey = malloc (str_len (in_key) + 1); if (!pkey) { printf ("## Failure to allocate pkey in fio_read_trans_asc().\n"); break; //메모리 할당 실패 } str_copy (pkey, in_key); pword = malloc (str_len (in_word) + 1); if (!pword) { printf ("## Failure to allocate pword in fio_read_trans_asc().\n"); break; //메모리 할당 실패 } str_copy (pword, in_word); //해시값에 따라 B+트리 A 리프찾은후 삽입 h = (sh==0) ? sh : hash_value (pkey); leaf = bpt_find_leaf (hb[0][h], pkey, hb[0][h]->compare); hb[0][h]->root = bpt_insert (hb[0][h], leaf, pkey, pword, FLAG_INSERT); //해시값에 따라 B+트리 B 리프찾은후 삽입 h = (sh==0) ? sh : hash_value (pword); leaf = bpt_find_leaf (hb[1][h], pword, hb[1][h]->compare); hb[1][h]->root = bpt_insert (hb[1][h], leaf, pword, pkey, FLAG_INSERT); printf ("%c(%c)%u ", CR, prompt[sno%3], sno); //읽기표시 프롬프트 sno++; //순번 } //while fclose (fp); printf ("%c(%c)%u ", CR, prompt[0], sno); //읽기표시 프롬프트 printf ("have read data(hash): count (%u)\n", sno); return sno; //읽은 키 개수 }