bheap_t *add_elem(void *elem, bheap_t *heap) { if (heap->index >= heap->size) { bheap_t *old = heap; heap = create_heap(heap->comp_func, heap->size * 2); for (int i = 0; i < old->index; i++) heap->elems[i] = old->elems[i]; heap->index = old->index; free(old); } heap->elems[heap->index] = elem; /* While parent node is smaller, bubble elem up */ if (heap->index > 0) { size_t index = heap->index; size_t pindex = (heap->index - 1) / 2; void *parent = heap->elems[pindex]; while (index > 0 && heap->comp_func(parent,elem) < 0) { void *temp = parent; heap->elems[pindex] = elem; heap->elems[index] = temp; index = pindex; pindex = (pindex - 1) / 2; parent = heap->elems[pindex]; } } heap->index++; return heap; }
int main() { heap *h; int arr[]={1,5,1,2,1,2,1,3,1,8,7,1}; /*h=create_heap(); insert(h,31); insert(h,10); insert(h,16); insert(h,9); insert(h,8); insert(h,14); insert(h,12); insert(h,3); insert(h,1); insert(h,5); insert(h,7); print(h); printf("\n"); insert(h,34); print(h); printf("\n"); destroy(h);*/ h=create_heap(); build(h,arr,11); print(h); printf("deleted=%d\n",delete_node(h,1)); print(h); return 0; }
//프로세스 전용의 디폴트 힙을 생성한다 void CreateDefaultHeap() { EnterCriticalSection(); Process* pProcess = ProcessManager::GetInstance()->GetCurrentProcess(); Thread* pThread = pProcess->GetThread(0); //1메가 바이트의 힙을 생성 void* pHeapPhys = PhysicalMemoryManager::GetInstance()->AllocBlocks(DEFAULT_HEAP_PAGE_COUNT); u32int heapAddess = pThread->m_imageBase + pThread->m_imageSize + PAGE_SIZE + PAGE_SIZE * 2; //힙 주소를 4K에 맞춰 Align heapAddess -= (heapAddess % PAGE_SIZE); #ifdef _ORANGE_DEBUG console.Print("heap adress %x\n", heapAddess); #endif // _ORANGE_DEBUG for (int i = 0; i < DEFAULT_HEAP_PAGE_COUNT; i++) { VirtualMemoryManager::GetInstance()->MapPhysicalAddressToVirtualAddresss(pProcess->m_pPageDirectory, (uint32_t)heapAddess + i * PAGE_SIZE, (uint32_t)pHeapPhys + i * PAGE_SIZE, I86_PTE_PRESENT | I86_PTE_WRITABLE | I86_PTE_USER); } memset((void*)heapAddess, 0, DEFAULT_HEAP_PAGE_COUNT * PAGE_SIZE); pProcess->m_lpHeap = create_heap((u32int)heapAddess, (uint32_t)heapAddess + DEFAULT_HEAP_PAGE_COUNT * PAGE_SIZE, (uint32_t)heapAddess + DEFAULT_HEAP_PAGE_COUNT * PAGE_SIZE, 0, 0); LeaveCriticalSection(); }
int main() { n = sizeof(arr)/sizeof(arr[0]); create_heap(); print(); int x; x = eliminate(); printf("%d\n", x); print(); x = eliminate(); printf("%d\n", x); print(); add_to_heap(); print(); return(0); };
void test2() { heap_t *heap = create_heap( compare_heap_uint8, default_heap_size ); uint8_t val1 = 100; uint8_t val2 = 22; push_to_heap( heap, &val1 ); push_to_heap( heap, &val2 ); uint8_t *ptr = pop_from_heap( heap ); if ( *ptr != val2 ) { printf( "Failed. (%d != %d) \n", *ptr, val2 ); abort(); } ptr = pop_from_heap( heap ); if ( *ptr != val1 ) { printf( "Failed. (%d != %d) \n", *ptr, val1 ); abort(); } if ( check_heap( heap ) ) { printf( "%s : Success.\n", __func__ ); } else { printf( "%s : Failed.\n", __func__ ); abort(); } }
void dijkstra (graph_t *g, int a, int b) { int i, j; a = a - 'a'; b = b - 'a'; for (i = 0; i < g->vertices_len; i++) { vertex_t *v = g->vertices[i]; v->dist = INT_MAX; v->prev = 0; v->visited = 0; } vertex_t *v = g->vertices[a]; v->dist = 0; heap_t *h = create_heap(g->vertices_len); push_heap(h, a, v->dist); while (h->len) { i = pop_heap(h); if (i == b) break; v = g->vertices[i]; v->visited = 1; for (j = 0; j < v->edges_len; j++) { edge_t *e = v->edges[j]; vertex_t *u = g->vertices[e->vertex]; if (!u->visited && v->dist + e->weight <= u->dist) { u->prev = i; u->dist = v->dist + e->weight; push_heap(h, e->vertex, u->dist); } } } }
int main() { int i, it, len; freopen(FIN, "r", stdin); freopen(FOUT, "w", stdout); scanf("%d",&n); len = n; for(i = 1; i <= n; i++) scanf("%d", &arr[i]); create_heap(); for(it = 1; it <= len; it++ ) { printf("%d ", eliminate()); } print(); return(0); };
inline void* allocate(std::size_t sz) throw(std::bad_alloc) { sz = ALIGN_SIZE(sz, std::size_t, sizeof(aligner)); if (sz <= T_depth) { heap* h = m_head; while (h) { void* const ptr = h->allocate(sz); if (ptr) { if (h != m_head) { dlist_pop(this, h); dlist_push_head(this, h); DBG_PRINTF(("(%p/%d/%d) Dynamic Region Heap Adjust\n", (void*)h, m_depth, m_max_depth)); } return ptr; } h = h->m_next; } void* const ptr = create_heap()->allocate(sz); if (ptr) { return ptr; } } void* const ptr = std::malloc(sz); if(! ptr) { throw std::bad_alloc(); } return ptr; }
int main(void) { create_heap(); print(); sort(); print(); }
void CreateDefaultHeap() { Process* pProcess = ProcessManager::GetInstance()->g_pCurProcess; Thread* pThread = (Thread*)List_GetData(pProcess->pThreadQueue, "", 0); //Thread* pThread = ProcessManager::GetInstance()->g_pThread; void* pHeapaaPhys = (void*)pmmngr_alloc_blocks(300); u32int heapAddess = pThread->imageBase + pThread->imageSize + PAGE_SIZE + PAGE_SIZE * 2; heapAddess = heapAddess - (heapAddess % PAGE_SIZE); //u32int heapAddess = 0xB0000000; DebugPrintf("\nheap adress %x", heapAddess); for (int i = 0; i < 300; i++) { vmmngr_mapPhysicalAddress(pProcess->pPageDirectory, (uint32_t)heapAddess + i * PAGE_SIZE, (uint32_t)pHeapaaPhys + i * PAGE_SIZE, I86_PTE_PRESENT | I86_PTE_WRITABLE | I86_PTE_USER); } //pmmngr_load_PDBR((physical_addr)pProcezss->pPageDirectory); memset((void*)heapAddess, 0, 300 * PAGE_SIZE); DebugPrintf("\nimageSize %x", pThread->imageSize); pThread->lpHeap = create_heap((u32int)heapAddess, (uint32_t)heapAddess + 300 * 4096, (uint32_t)heapAddess + 300 * 4096, 0, 0); //DebugPrintf("\nThread Creation Success %x", pThread->lpHeap); //DebugPrintf("\ndfsdfds %x", pHeapPhys); //DebugPrintf("\nkkkkk"); }
/** * union_seed_packets * * Find the union of two seed_packet arrays. Return an array containing * the best seed_packets from this union. * * This function is used in reduce_across_heaps. * */ void union_seed_packets(void *f_data, void *f_result, int *f_length, MPI_Datatype *datatype) { int i; int num_seed_packets; SEED *bumped_seed; // create a heap to do the heap union HEAP *heap = create_heap( *f_length, (int (*) (void *, void*))compare_seed, (void *)copy_seed, (void (*)(void*))free_seed, (char* (*)(void*))get_str_seed, (void (*)(FILE *, void*))print_seed ); // get the number of seed_packets in f_data num_seed_packets = ((SEED_PACKET *)f_data + 0)->num_seed_packets; // unpack the seeds from f_data and add them to the heap for (i = 0; i < num_seed_packets; i++){ // get the data seed char *data_seed_str = ((SEED_PACKET *)f_data + i)->seed; double data_score = ((SEED_PACKET *)f_data + i)->score; SEED *data_seed = new_seed(data_seed_str, data_score); // add the seeds to the heap bumped_seed = (SEED *)(add_node_heap(heap, data_seed)); } // unpack the seeds from f_result and add them to the heap num_seed_packets = ((SEED_PACKET *)f_result + 0)->num_seed_packets; for (i = 0; i < num_seed_packets; i++){ // get the result seed char *result_seed_str = ((SEED_PACKET *)f_result + i)->seed; double result_score = ((SEED_PACKET *)f_result + i)->score; SEED *result_seed = new_seed(result_seed_str, result_score); // add the seeds to the heap bumped_seed = (SEED *)(add_node_heap(heap, result_seed)); } // pack the heap int num_seeds = get_num_nodes(heap); // set the number of filled packets (in case the heap is empty) ((SEED_PACKET *)f_result + 0)->num_seed_packets = num_seeds; for (i = 0; i < num_seeds; i++){ // set the number of seed_packets ((SEED_PACKET *)f_result + i)->num_seed_packets = num_seeds; // get the index for the seed in the heap // (populated heap nodes are at index 1 to num_seeds) int heap_idx = i + 1; // get the node SEED *curr_seed = get_node(heap, heap_idx); //double score = get_seed_score(curr_seed); ((SEED_PACKET *)f_result + i)->score = get_seed_score(curr_seed); char *seed_str = get_str_seed(curr_seed); strcpy(((SEED_PACKET *)f_result + i)->seed, seed_str); } } // union_seed_packets
void initialize_paging(uint32_t memsize) { nframes = memsize / 4; frames = (uint32_t *)kmalloc(INDEX_FROM_BIT(nframes)); uintptr_t pg; assert(frames != NULL); memset(frames, 0, INDEX_FROM_BIT(nframes)); uintptr_t physical; kernel_directory = (page_directory_t *)kmalloc_ap(sizeof(page_directory_t), &physical); memset(kernel_directory, 0, sizeof(page_directory_t)); current_directory = kernel_directory; #if 1 get_page(0,1,kernel_directory)->present = 0; set_frame(0); for(uintptr_t i = 0x1000; i < placement_address+0x3000; i += 0x1000) #else for(uintptr_t i = 0x0; i < placement_address+0x3000; i += 0x1000) #endif { direct_frame( get_page(i, 1, kernel_directory), 1, 0, i); } kernel_directory->physical_addr = (uintptr_t)kernel_directory->tables_physical; uintptr_t heap_start = KERNEL_HEAP_START; if(heap_start <= placement_address + 0x3000) { heap_start = placement_address + 0x100000; } for (uintptr_t i = placement_address + 0x3000; i < heap_start; i += 0x1000) { alloc_frame(get_page(i, 1, kernel_directory), 1, 0); } for(uintptr_t i = heap_start; i < heap_start + KERNEL_HEAP_INIT; i += 0x1000) { get_page(i, 1, kernel_directory); } for(uintptr_t i = heap_start; i < heap_start + KERNEL_HEAP_INIT; i += 0x1000) { alloc_frame(get_page(i, 1, kernel_directory), 0, 0); } register_isr_handler(13, general_protection_fault); register_isr_handler(14, page_fault); switch_page_directory(kernel_directory); kernel_heap = create_heap(heap_start, heap_start + KERNEL_HEAP_INIT, KERNEL_HEAP_END, 0, 0); //kernel_heap = create_heap(heap_start, KERNEL_HEAP_END, KERNEL_HEAP_END, 0, 0); }
region_allocator( std::size_t const prime, std::size_t const max_depth ) : m_depth(0), m_max_depth(max_depth) { dlist_init(this); for (std::size_t i = 0; i < prime && i < max_depth; ++i) { create_heap(); } }
void heap_sort(int a[], int length) { int i; create_heap(a, length); for(i = length - 1; i >= 1; i--) { swap(&a[i], &a[0]); heap_fixdown(a, 0, i); } }
void test1() { heap_t *heap = create_heap( compare_heap_node, default_heap_size ); if ( heap != NULL ) { return; } else { return; } }
static void create_heaps(void) { cfg_root(root); const char *d; d = cfg_get_str(root, CFG("projectdir"), NULL); if(d == NULL) { trace(LOG_ERR, "No 'projectdir' configured, giving up"); exit(1); } project_heap_mgr = create_heap(d); d = cfg_get_str(root, CFG("buildenvdir"), NULL); if(d == NULL) { trace(LOG_ERR, "No 'buildenvdir' configured, giving up"); exit(1); } buildenv_heap_mgr = create_heap(d); }
void heap_sort(int *a,int size) { struct maxheap* heapp=create_heap(a,size); while(heapp->size>1) { int temp=heapp->array[0]; heapp->array[0]=heapp->array[heapp->size-1]; heapp->array[heapp->size-1]=temp; heapp->size-=1; heapify(heapp,0); } }
struct PriQueue * create_priqueue(int capacity, compare_func compare) { struct PriQueue *priqueue = (struct PriQueue *)malloc(sizeof(struct PriQueue)); if(priqueue) { struct Heap *hp = create_heap(capacity, compare); if(hp == NULL) { free(priqueue); priqueue = NULL; } else priqueue->heap = hp; } return priqueue; }
/** * @brief Esta rutina se encarga de inicializar el area de memoria * para asignacion dinamica, y las estructuras de datos requeridas para su * gestion. * @param ptr Puntero al inicio de la memoria disponible para asignacion * dinámica * @param limit Tamaño de la memoria disponible * @return Apuntador al heap inicializado. */ heap_t * setup_heap(void * ptr, unsigned int limit) { int i; unsigned int base; heap_t * heap; base = (unsigned int)ptr; heap = create_heap(base, limit); return heap; }
HEAP *create_heap_from_sp_matrix ( SP_MATRIX *sp_mat // the matrix of s_points ) { int row_idx, col_idx, i; int num_seeds = 0; int num_rows = sp_get_num_rows(sp_mat); int num_cols = sp_get_num_cols(sp_mat); void *root, *temp; // iterate over the s_points in the sp_matrix to get the total number of seeds for (row_idx = 0; row_idx < num_rows; row_idx++) { for (col_idx = 0; col_idx < num_cols; col_idx++) { S_POINT *current_sp = get_spoint(sp_mat, row_idx, col_idx); HEAP *seed_heap = current_sp->seed_heap; num_seeds += get_num_nodes(seed_heap); } } // create the heap HEAP *mega_heap = create_heap( num_seeds, (int (*) (void *, void*))compare_seed, (void *)copy_seed, (void (*)(void*))free_seed, (char* (*)(void*))get_str_seed, (void (*)(FILE *, void*))print_seed ); // add the seeds to the heap for (row_idx = 0; row_idx < num_rows; row_idx++) { for (col_idx = 0; col_idx < num_cols; col_idx++) { S_POINT *current_sp = get_spoint(sp_mat, row_idx, col_idx); HEAP *current_heap = current_sp->seed_heap; HEAP *seed_heap = copy_heap(current_heap); // add copies of the seeds to the mega_heap int num_nodes = get_num_nodes(seed_heap); for (i=1; i<= num_nodes; i++){ root = pop_heap_root(seed_heap); temp = mega_heap->copy(root); temp = add_node_heap(mega_heap, temp); } } } // return the heap return mega_heap; } // create_heap_from_sp_matrix
void init_paging() { size_t sz; uint32_t i; uint32_t mem_end_page; DPRINTK("paging...\t\t"); mem_end_page = 0x1000000; nframes = mem_end_page / PAGE_SIZ; sz = INDEX_FROM_BIT(nframes); frames = (uint32_t *)kmalloc(sz); memset(frames, 0, sz); kernel_directory = (struct page_directory *) kmalloc_a(sizeof(struct page_directory)); memset(kernel_directory, 0, sizeof(struct page_directory)); // don't do this... current_directory = kernel_directory; // do this instead... kernel_directory->physical_addr = (uint32_t)kernel_directory->tables_physical; for (i = KHEAP_START; i < KHEAP_START + KHEAP_INITIAL_SIZE; i += PAGE_SIZ) get_page(i, 1, kernel_directory); i = 0; while (i < placement_addr + PAGE_SIZ) { alloc_frame(get_page(i, 1, kernel_directory), 0, 0); i += PAGE_SIZ; } for (i = KHEAP_START; i < KHEAP_START + KHEAP_INITIAL_SIZE; i += PAGE_SIZ) alloc_frame(get_page(i, 1, kernel_directory), 0, 0); // register_interrupt_handler(14, page_fault); switch_page_directory(kernel_directory); enable_paging(); kheap = create_heap(KHEAP_START, KHEAP_START + KHEAP_INITIAL_SIZE, 0xCFFFF000, 0, 0); current_directory = clone_directory(kernel_directory); switch_page_directory(current_directory); DPRINTK("done!\n"); }
void test_one_heap_entry() { printf("Testing one heap entry... "); create_heap(); add_to_heap(7); int max = peek_heap(); if (max != 7) { printf("ERROR: Expected 7, but got %d\n", max); } else { printf("Pass\n"); } destroy_heap(); }
void test_many_heap_entry() { printf("Testing many heap entry... "); create_heap(); for (int i = 100; i > 0; i -= 1) { add_to_heap(i); } print_heap(); int max = peek_heap(); if (max != 100) { printf("ERROR: Expected 100, but got %d\n", max); } else { printf("Pass\n"); } destroy_heap(); }
int main(int argc, char const *argv[]) { int n, i, temp; char c = '\0'; create_heap(516); scanf("%d", &n); for(i = 0; i < n; i++) { scanf("%d", &temp); insert_item(temp); } while (c != 'f') { scanf("%c", &c); switch(c) { case 'i': scanf("%d", &temp); insert_item(temp); break; case 'm': printf("%d\n", get_min()); //print_heap(); break; case 'M': printf("%d\n", get_max()); //print_heap(); break; } } printf("\n"); printf("Min-heap: "); print_heap_min(); printf("Max-heap: "); print_heap_max(); return 0; }
void main() { int i; clrscr(); printf ("\nEnter number of elements :"); scanf ("%d",&n); for(i=0;i<n;i++) { printf ("\nEnter element %d :",i+1); scanf ("%d",&a[i]); } printf ("\nEntered list is :\n"); display(); create_heap(); printf ("\nHeap is :\n"); display(); heap_sort(); printf ("\nSorted list is :\n"); display(); getch(); }
void main(){ int i; int a[]={2,8}; int n=sizeof(a)/sizeof(a[0]); h=create_heap(); fill_heap(a,n); display_heap(); printf("Top element is %d\n",top()); insert(77); display_heap(); printf("Top element is %d",top()); pop(); display_heap(); printf("Top element is %d",top()); }
main() { int i; printf("Enter number of elements : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("Enter element %d : ",i+1); scanf("%d",&arr[i]); } printf("Entered list is :\n"); display(); create_heap(); printf("Heap is :\n"); display(); heap_sort(); printf("Sorted list is :\n"); display(); }/*End of main()*/
void test4() { heap_t *heap = create_heap( compare_heap_node, default_heap_size ); srandom(100); for ( int i = 0; i < 1000; i++ ) { node_t *node = ( node_t * )malloc( sizeof( node_t ) ); node->datapath_id = ( uint64_t )random(); push_to_heap( heap, node ); } #if 0 if ( check_heap( heap ) ) { printf( "Success.\n" ); } else { printf( "Failed.\n" ); abort(); } #endif uint64_t prev = 0; for ( int i = 0; i < 1000; i++ ) { node_t *node = pop_from_heap( heap ); printf( "%ld\n", node->datapath_id ); if ( prev > node->datapath_id ) { printf( "Failed.\n" ); abort(); } prev = node->datapath_id; free( node ); } return; }
void test3() { heap_t *heap = create_heap( compare_heap_uint8, default_heap_size ); srandom(100); for ( unsigned int i = 0; i < default_heap_size; i++ ) { uint8_t *val = ( uint8_t * )malloc( sizeof( uint8_t ) ); *val = ( uint8_t )( random() % UCHAR_MAX ); push_to_heap( heap, val ); } #if 0 if ( check_heap( heap ) ) { printf( "%s : Success.\n", __func__ ); } else { printf( "%s : Failed.\n", __func__ ); abort(); } #endif uint8_t prev = 0; for ( unsigned int i = 0; i < default_heap_size; i++ ) { uint8_t *val = pop_from_heap( heap ); printf( "%d\n", *val ); if ( prev > *val ) { printf( "Failed.\n" ); abort(); } prev = *val; free( val ); } return; }
/** * create_spoint_row * * Creates an array of s_points with nsites0 values from the minimum up * to the maximum values specified, inclusive. * * \return The newly created row, which can then be added to a matrix. */ S_POINT *create_spoint_row ( int width, ///< The width of all s_points in the current row int *central_ws, ///< A sorted list of central widths values for the matrix int n_ws, ///< The number of central width values int *central_ns, ///< A sorted list of central nsites values for the matrix int n_ns, ///< The number of central nsites values DATASET *dataset ///< Contains information on heap size and branching factor ) { // Allocate memory for the row: int min_nsites = central_ns[0]; int max_nsites = central_ns[n_ns - 1]; int n_nsites0 = max_nsites - min_nsites + 1; // Size of row S_POINT *s_point_row = NULL; Resize(s_point_row, n_nsites0, S_POINT); /* initialize the starting points, making sure everything is initalized so that MPI won't barf. */ // Create each s_point: double curr_nsites;// Current number of sites int col_idx; // Current index in this row for (col_idx=0, curr_nsites=min_nsites; curr_nsites <= max_nsites; col_idx++, curr_nsites+=1) { s_point_row[col_idx].score = LITTLE; s_point_row[col_idx].iseq = 0; s_point_row[col_idx].ioff = 0; s_point_row[col_idx].w0 = width; s_point_row[col_idx].nsites0 = curr_nsites<max_nsites ? curr_nsites : max_nsites; s_point_row[col_idx].wgt_nsites = 0; s_point_row[col_idx].e_cons0 = NULL; s_point_row[col_idx].cons0 = NULL; s_point_row[col_idx].sig = BIG; // Calculate the manhattan distance from the current (n,w) position to // the closest "central" (n,w) position... // Find the distances of the central w and n closest to the current w and // n: int w_idx; int curr_central_w; double lowest_w_dist = BIG; for (w_idx = 0; w_idx < n_ws; w_idx++) { curr_central_w = central_ws[w_idx]; int curr_dist = (int)abs(curr_central_w - width); if (curr_dist < lowest_w_dist) { lowest_w_dist = curr_dist; } } int n_idx; int curr_central_n; double lowest_n_dist = BIG; for (n_idx = 0; n_idx < n_ns; n_idx++) { curr_central_n = central_ns[n_idx]; int curr_dist = (int)abs(curr_central_n - curr_nsites); if (curr_dist < lowest_n_dist) { lowest_n_dist = curr_dist; } } double manhattan_dist = lowest_w_dist + lowest_n_dist; // If branch_W search is to occur, then evaluation should occur at // every s_point. Otherwise it should only occur at central s_points: if (dataset->branch_params->w_branch && (lowest_n_dist == 0)) { s_point_row[col_idx].evaluate = TRUE; } else { // Initially only evaluate seeds at this s_point if the s_point is // central: s_point_row[col_idx].evaluate = (manhattan_dist == 0); } // Calculate the heap size for the current S_POINT: int max_hs = dataset->main_hs; double factor = dataset->hs_decrease; int heap_size = MAX((int)max_hs/(pow(factor, manhattan_dist)), 1); info_cons0_alloc++; Resize(s_point_row[col_idx].cons0, width+1, char); s_point_row[col_idx].cons0[0] = '\0'; s_point_row[col_idx].seed_heap = create_heap( heap_size, (int (*) (void *, void*))compare_seed, (void *)copy_seed, (void (*)(void*))free_seed, (char* (*)(void*))get_str_seed, (void (*)(FILE *, void*))print_seed ); } // col_idx return s_point_row; } // create_spoint_row