void* BTMalloc( size_t s ) { void *p; p = malloc( s ); memset( p, 0, s ); BTInsert( &BTreeMem, p ); return( p ); }
int test_BTInsert (tBTNodePtr* TempTree, int Content) { solved=TRUE; BTInsert(TempTree, Content); if (!solved) { printf("Operace BTInsert() nebyla implementovana \n"); return(FALSE); } else { return(TRUE); } }
E_CODE symbolTableInsertFunction(tSymbolTable* symbolTable, tString functionName){ tFunction *func=mmuMalloc(sizeof(tFunction)); strCopyString(&functionName,&(func->name)); // jmeno fce btInit(&(func->symbols)); // symboly initList(&(func->instructions)); // instrukce initList(&(func->constants)); func->called=1; E_CODE err=BTInsert(&(symbolTable->functions),&(func->name),func); if (err!=ERROR_OK){strFree(&(func->name));mmuFree(func);} return err; }
int main() { tBTree *T; tBTNode N; mmuInit(); T=mmuMalloc(sizeof(tBTree)); btInit(T); tString str,str1,str2,str3; void *nic=NULL; str=strCreate("ahoj"); str1=strCreate("nazdar"); str2=strCreate("valhala"); str3=strCreate("abcd"); if(strCmp(&str1,&str1)!=0)printf("chyba strCmp"); //printf("%s\n",str.data); BTInsert(T,&str,nic); BTInsert(T,&str1,nic); BTInsert(T,&str2,nic); BTInsert(T,&str3,nic); printf("%d\n",T->root->height); printf("%s\n",T->root->left->key->data); printf("%s\n",T->root->right->key->data); printf("%s\n",T->root->right->right->key->data); printf("%s\n",T->root->key->data); N=btFind(T,&str1); //BTInsert(T,&str2,nic); if(N!=NULL)printf("%s\n",N->key->data); else printf("nic se nenaslo\n"); if(T->root->left==NULL && T->root->right==NULL) printf("chyba\n"); int cmp=strCmp(&str1,&str); printf("%d\n",cmp); //btFree(T); mmuGlobalFree(); return 0; }
int main( int ac, char* pArgs[] ) { BTree_t BTree; BTCursor_t Cursor; int Ret; NHeapPoolInit( &Pool, 1024*20, 5, malloc, free, 0/*HEAP_DYNAMIC*/ ); BTreeInit( &BTreeMem, 5, NULL, NULL, BTMemAlloc, BTMemFree, NULL ); BTreeInit( &BTree, 2, NULL, NULL, BTMalloc, BTFree, NULL ); BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, NULL ); int i; for( i = 0; i < 100; i++ ) { BTInsert( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); for( i = 0; i < 100; i++ ) { BTDelete( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); for( i = 0; i < 100; i++ ) { BTInsert( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); BTCursorOpen( &Cursor, &BTree ); for( Ret = BTCHead( &Cursor ); Ret == 0; Ret = BTCNext( &Cursor ) ) { printf("DELETE(%d)\n", i = PNT_INT32(BTCGetKey( void*, &Cursor )) ); if( i != 10 ) { Ret = BTCDelete( &Cursor ); } } BTreePrint( &BTree ); //#define RAND_ARRAY 151 #define RAND_ARRAY 10000 int aRand[RAND_ARRAY]; int j; for( j = 0; j < 100; j++ ) { for( i = 0; i < RAND_ARRAY; i++ ) { aRand[i] = rand(); } for( i = 0; i < RAND_ARRAY; i++ ) { BTInsert( &BTreeMem, INT32_PNT(aRand[i]) ); } BTreePrint( &BTreeMem ); // BTInsert( &BTreeMem, INT32_PNT(rand()) ); // BTreePrint( &BTreeMem ); BTCursorOpen( &Cursor, &BTreeMem ); for( Ret = BTCHead( &Cursor ); Ret == 0; Ret = BTCNext( &Cursor ) ) { Ret = BTCDelete( &Cursor ); } BTreePrint( &BTreeMem ); NHeapPoolDump( &Pool ); } for( j = 0; j < 100; j++ ) { for( i = 0; i < RAND_ARRAY; i++ ) { aRand[i] = rand(); } for( i = 0; i < RAND_ARRAY; i++ ) { BTInsert( &BTreeMem, INT32_PNT(aRand[i]) ); } BTreePrint( &BTreeMem ); // BTInsert( &BTreeMem, INT32_PNT(rand()) ); // BTreePrint( &BTreeMem ); for( i = 0; i < RAND_ARRAY; i++ ) { BTDelete( &BTreeMem, INT32_PNT(aRand[i]) ); } BTreePrint( &BTreeMem ); NHeapPoolDump( &Pool ); } for( i = 0; i < 100; i++ ) { BTInsert( &BTreeMem, INT32_PNT(i) ); } BTDelete( &BTreeMem, INT32_PNT(99) ); BTreePrint( &BTreeMem ); return( 0 ); }
int EXPORT(BTIns)(GBTree const btree, GBTObject const obj, GBTObject *ngb) { BTKey kpromoted; BTPage *newroot; BTPage *ppromoted; int i; int retval; if (btree == NULL) { // No tree return(BT_INVALID); } if (btree->root == NULL) { // Empty tree newroot = (BTPage *) EXPORT(AllocPage)(btree->pool); if (newroot == NULL) { return(BT_ALLOC_fail); } for(i = 0; i < BT_MAXKEY; i++) { newroot->child[i] = NULL; newroot->key[i] = 0; } // Numbers of children is key+1, so ensure the last child is initialised newroot->child[i] = NULL; newroot->count = 0; newroot->isleaf = 1; newroot->child[0] = (BTPage *) obj; btree->root = newroot; btree->depth = 1; (*ngb) = NULL; return(BT_OK); } if ((retval = BTInsert(btree->pool, btree->root, obj, &ppromoted, &kpromoted, ngb)) == BT_PROMOTION) { newroot = (BTPage *) EXPORT(AllocPage)(btree->pool); if (newroot == NULL) { return(BT_ALLOC_fail); } for(i = 0; i < BT_MAXKEY; i++) { newroot->child[i] = NULL; newroot->key[i] = 0; } newroot->child[i] = NULL; newroot->count = 1; newroot->isleaf = 0; newroot->child[0] = btree->root; newroot->child[1] = ppromoted; newroot->key[0] = kpromoted; btree->root = newroot; btree->depth++; retval = BT_OK; } return(0); }
static int BTInsert(PagePool *pool, BTPage * const current, GBTObject const obj, BTPage ** const ppromoted, BTKey * const kpromoted, GBTObject *ngb) { BTKey key; BTKey kmed; BTKeyCount hi; BTKeyCount i; BTKeyCount j; BTKeyCount lo; BTKeyCount mid; BTPage *newpage; int in_old; int retval; if (current == NULL) { (*kpromoted) = BTGetObjKey(obj); (*ppromoted) = NULL; return(BT_PROMOTION); } key = BTGetObjKey(obj); lo = 0; hi = current->count; while(lo < hi) { mid = (lo + hi) / 2; if (BTKeyEQ(current->key[mid], key)) { return(BT_DUPLICATE); } else if (BTKeyGT(current->key[mid], key)) { hi = mid; } else { lo = mid + 1; } } # ifdef BT_HAVE_INTERVALS if ((hi < current->count) && (BTObjMatch(obj,current->key[hi]))) { return(BT_OVERLAP); } # endif if (current->isleaf) { # ifdef BT_HAVE_INTERVALS if (BTObjMatch((GBTObject)(current->child[hi]), key) || (!hi && BTOverlaps(obj,(GBTObject)(current->child[0])))) { return(BT_OVERLAP); } # endif (*ppromoted) = (BTPage *) obj; (*kpromoted) = BTGetObjKey(obj); retval = BT_PROMOTION; (*ngb) = (GBTObject)(current->child[hi]); if (!hi && BTKeyLT(key,BTGetObjKey((GBTObject)(current->child[0])))) { hi = -1; } } else { retval = BTInsert(pool, current->child[hi], obj, ppromoted, kpromoted, ngb); } if (retval == BT_PROMOTION) { if (current->count < BT_MAXKEY) { return(BTInsertPage(current, hi, *kpromoted, *ppromoted)); } // split the page BT_MINKEY keys on left (org) and // BT_MAXKEY-BT_MINKEY on right if ((newpage = (BTPage *) EXPORT(AllocPage)(pool)) == NULL) { return(BT_ALLOC_fail); } newpage->isleaf = current->isleaf; newpage->count = BT_MAXKEY - BT_MINKEY; current->count = BT_MINKEY; if ((in_old = (hi < current->count))) { // insert promoted key in the left (original) page current->count--; kmed = current->key[current->count]; current->key[current->count] = 0; } else { // insert promoted key in the right (new) page newpage->count--; if (hi == current->count) { kmed = *kpromoted; } else { kmed = current->key[current->count]; current->key[current->count] = 0; } } newpage->child[0] = current->child[current->count+1]; current->child[current->count+1] = NULL; for(j = current->count + 1, i = 0; i < newpage->count; i++, j++) { newpage->key[i] = current->key[j]; newpage->child[i + 1] = current->child[j + 1]; current->key[j] = 0; current->child[j + 1] = NULL; } for (; i < BT_MAXKEY; i++) { newpage->key[i] = 0; newpage->child[i + 1] = NULL; } if (in_old) { retval = BTInsertPage(current, hi, *kpromoted, *ppromoted); } else { hi -= current->count + 1; retval = BTInsertPage(newpage, hi, *kpromoted, *ppromoted); } (*kpromoted) = kmed; (*ppromoted) = newpage; return(BT_PROMOTION); } return(retval); }
int main( int ac, char* pArgs[] ) { BTree_t BTree; BTCursor_t Cursor; BTCursor_t Cursor1; BTCursor_t Cursor2; NHeapPoolInit( &Pool, 1024*10, 3, malloc, free, 0/*HEAP_DYNAMIC*/ ); BTreeInit( &BTreeMem, 2, NULL, PrintMem, BTMemAlloc, BTMemFree, NULL ); BTreeInit( &BTree, 2, NULL, NULL, BTMalloc, BTFree, NULL ); BTCursorOpen( &Cursor, &BTree ); BTCursorOpen( &Cursor1, &BTree ); BTCursorOpen( &Cursor2, &BTree ); printf("\nINSERT\n"); BTreePrint(&BTree);printf("Insert 20\n"); BTInsert( &BTree, (void*)20 ); // BTCFind( &Cursor1, (void*)20 ); BTreePrint(&BTree);printf("Insert 40\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)40 ); BTreePrint(&BTree);printf("Insert 10\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)10 ); BTreePrint(&BTree);printf("Insert 30\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)30 ); BTreePrint(&BTree);printf("Insert 15\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)15 ); BTreePrint(&BTree);printf("Insert 35\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)35 ); BTreePrint(&BTree);printf("Insert 7\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)7 ); BTreePrint(&BTree);printf("Insert 26\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)26 ); BTreePrint(&BTree);printf("Insert 18\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)18 ); BTreePrint(&BTree);printf("Insert 22\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)22 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)5 ); printf("\nINSERT 5\n"); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)42 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)13 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)46 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)27 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)8 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)32 ); printf("\nINSERT 32\n"); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTreePrint(&BTree);printf("Insert 38\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)38 ); BTreePrint(&BTree);printf("Insert 24\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)24 ); BTreePrint(&BTree);printf("Insert 45\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)45 ); BTreePrint(&BTree);printf("Insert 25\n");BTreePrint(&BTreeMem); BTInsert( &BTree, (void*)25 ); // Insert( &BTree, (void*)15 ); printf("\nINSERT 25\n"); BTreePrint( &BTree );BTreePrint(&BTreeMem); // BTCFind( &Cursor2, (void*)25 ); BTCHead( &Cursor ); PrintCursor( &Cursor ); while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor ); BTreePrint( &BTree );BTreePrint(&BTreeMem); printf("\nDELETE 25\n"); if( !BTCFind( &Cursor, (void*)25 ) ) BTCDelete( &Cursor ); BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem); printf("\nDELETE 45\n"); if( !BTCFind( &Cursor, (void*)45 ) ) BTCDelete( &Cursor ); BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem); printf("\nDELETE 24\n"); if( !BTCFind( &Cursor, (void*)24 ) ) BTCDelete( &Cursor ); BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem); printf("\nDELETE 38\n"); if( !BTCFind( &Cursor, (void*)38 ) ) BTCDelete( &Cursor ); BTreePrint( &BTree ); PrintCursor( &Cursor );BTreePrint(&BTreeMem); BTDelete( &BTree, (void*)25 ); BTDelete( &BTree, (void*)45 ); BTDelete( &BTree, (void*)24 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); BTCHead( &Cursor ); PrintCursor( &Cursor ); while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor ); printf("\nFIND 8\n"); if( !BTCFind( &Cursor, (void*)8 ) ) PrintCursor( &Cursor); printf("\nFIND 20<=\n"); if( !BTCFindLower( &Cursor, (void*)20 ) ) PrintCursor( &Cursor ); while( !BTCNext( &Cursor ) ) PrintCursor( &Cursor ); BTDelete( &BTree, (void*)38 ); BTDelete( &BTree, (void*)32 ); BTDelete( &BTree, (void*)8 ); BTDelete( &BTree, (void*)27 ); BTDelete( &BTree, (void*)46 ); BTDelete( &BTree, (void*)13 ); BTDelete( &BTree, (void*)42 ); BTDelete( &BTree, (void*)5 ); BTDelete( &BTree, (void*)22 ); BTDelete( &BTree, (void*)18 ); BTDelete( &BTree, (void*)26 ); BTDelete( &BTree, (void*)7 ); BTDelete( &BTree, (void*)35 ); BTDelete( &BTree, (void*)15 ); BTreePrint( &BTree );BTreePrint(&BTreeMem); // BTDelete( &BTree, (void*)20 ); // BTDelete( &BTree, (void*)40 ); // BTDelete( &BTree, (void*)10 ); // BTDelete( &BTree, (void*)30 ); /*** printf("END\n"); BTreePrint( &BTree ); printf("HEAD\n"); BTCHead( &Cursor );PrintCursor( &Cursor ); printf("NEXT\n"); printf("%d\n",BTCDeleteAndNext( &Cursor ));PrintCursor( &Cursor ); printf("Key=%d\n", BTCGetKey( int, &Cursor ) ); ***/ BTreePrint( &BTree ); printf("=== MEMORY LEAK ===\n"); BTreePrint( &BTreeMem ); NHeapPoolDump( &Pool ); BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, NULL ); int i; for( i = 0; i < 100; i++ ) { BTInsert( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); for( i = 0; i < 100; i++ ) { BTDelete( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); int Ret; for( i = 0; i < 10; i++ ) { BTInsert( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); BTCursorOpen( &Cursor, &BTree ); while( BTCHead( &Cursor ) == 0 ) { printf("DELETE(%p)\n", BTCGetKey( void*, &Cursor ) ); Ret = BTCDelete( &Cursor ); } BTCursorClose( &Cursor ); BTreePrint( &BTree ); BTreeInit( &BTreeMem, 3, NULL, PrintMem, BTMemAlloc, BTMemFree, NULL ); BTreeInit( &BTree, 5, NULL, NULL, BTMalloc, BTFree, NULL ); for( i = 206; i < 285; i++ ) { BTInsert( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); BTCursorOpen( &Cursor, &BTree ); while( BTCHead( &Cursor ) == 0 ) { printf("DELETE(%d)\n", PNT_INT32(BTCGetKey( void*, &Cursor )) ); Ret = BTCDelete( &Cursor ); BTreePrint( &BTree ); } BTCursorClose( &Cursor ); BTreePrint( &BTree ); printf("=== MEMORY LEAK ===\n"); BTreePrint( &BTreeMem ); NHeapPoolDestroy( &Pool ); NHeapPoolInit( &Pool, 1024*10, 10, malloc, free, 0/*HEAP_DYNAMIC*/ ); BTreeInit( &BTreeMem, 5, NULL, NULL, BTMemAlloc, BTMemFree, NULL ); for( i = 206; i < 285; i++ ) { BTInsert( &BTreeMem, INT32_PNT(i) ); } BTreePrint( &BTreeMem ); BTCursorOpen( &Cursor, &BTreeMem ); while( BTCHead( &Cursor ) == 0 ) { printf("DELETE(%d)\n", PNT_INT32(BTCGetKey( void*, &Cursor )) ); Ret = BTCDelete( &Cursor ); BTreePrint( &BTreeMem ); } BTCursorClose( &Cursor ); BTreePrint( &BTreeMem ); // BTreeDestroy( &BTreeMem ); NHeapPoolDump( &Pool ); //#define RAND_ARRAY 151 #define RAND_ARRAY 10000 int aRand[RAND_ARRAY]; for( i = 0; i < RAND_ARRAY; i++ ) { aRand[i] = rand(); } for( i = 0; i < RAND_ARRAY; i++ ) { BTInsert( &BTreeMem, INT32_PNT(aRand[i]) ); } BTreePrint( &BTreeMem ); // BTInsert( &BTreeMem, INT32_PNT(rand()) ); // BTreePrint( &BTreeMem ); for( i = 0; i < RAND_ARRAY; i++ ) { Ret = BTDelete( &BTreeMem, INT32_PNT(aRand[i]) ); ASSERT( !Ret ); } BTreePrint( &BTreeMem ); NHeapPoolDump( &Pool ); for( i = 0; i < RAND_ARRAY; i++ ) { aRand[i] = rand(); } for( i = 0; i < RAND_ARRAY; i++ ) { BTInsert( &BTreeMem, INT32_PNT(aRand[i]) ); } for( i = 0; i < RAND_ARRAY; i++ ) { Ret = BTDelete( &BTreeMem, INT32_PNT(aRand[i]) ); ASSERT( !Ret ); } BTreePrint( &BTreeMem ); NHeapPoolDump( &Pool ); for( i = 0; i < 100; i++ ) { BTInsert( &BTreeMem, INT32_PNT(i) ); } BTCursorOpen( &Cursor, &BTreeMem ); for( Ret = BTCHead( &Cursor ); Ret == 0; Ret = BTCNext( &Cursor ) ) { i = PNT_INT32(BTCGetKey( void*, &Cursor )); printf("%d\n", i ); BTCDelete( &Cursor ); } // Destroy BTreeInit( &BTree, 1, NULL, NULL, BMalloc, BFree, BDestroy ); for( i = 0; i < 20; i++ ) { BTInsert( &BTree, INT32_PNT(i) ); } BTreePrint( &BTree ); BTreeDestroy( &BTree ); BTreePrint( &BTree ); return( 0 ); }