示例#1
0
int main(int argc, char *argv[])
{
  FILE *fp;
  char *line = NULL;
  size_t len = 0;
  int val, min;
  Heap* Hlow = new_heap();
  Heap* Hhigh = new_heap();
  int sum = 0;

  if(argv[1] == NULL) {
    printf("Please specify a source file\n");
    exit(1);
  }

  if((fp = fopen(argv[1], "rb")) == NULL) {
    printf("Couldn't open file. Whomp whomp.\n");
    exit(1);
  }

  while(getline(&line, &len, fp) != EOF) {
    sscanf(line, "%d", &val);

    if(Hlow->size > 0) {
      peek_min(Hlow, &min, &min);

      if(0-val < min)
        heap_insert(Hhigh, val, val);
      else
        heap_insert(Hlow, 0-val, 0-val);
    }
    else {
      heap_insert(Hlow, 0-val, 0-val);
    }

    /* Equalize size of heaps */
    if(Hhigh->size > Hlow->size) {
      extract_min(Hhigh, &val, &val);
      heap_insert(Hlow, 0-val, 0-val);
    }
    else if(Hlow->size > Hhigh->size+1) {
      extract_min(Hlow, &val, &val);
      heap_insert(Hhigh, 0-val, 0-val);
    }

    peek_min(Hlow, &min, &min);
    /* printf("Min: %d\n", min); */
    sum += (0-min);
  }

  printf("Res: %d\n", sum%10000);

  return 0;
}
示例#2
0
文件: arena.c 项目: Xilinx/eglibc
static mstate
_int_new_arena(size_t size)
{
  mstate a;
  heap_info *h;
  char *ptr;
  unsigned long misalign;

  h = new_heap(size + (sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT),
	       mp_.top_pad);
  if(!h) {
    /* Maybe size is too large to fit in a single heap.  So, just try
       to create a minimally-sized arena and let _int_malloc() attempt
       to deal with the large request via mmap_chunk().  */
    h = new_heap(sizeof(*h) + sizeof(*a) + MALLOC_ALIGNMENT, mp_.top_pad);
    if(!h)
      return 0;
  }
  a = h->ar_ptr = (mstate)(h+1);
  malloc_init_state(a);
  /*a->next = NULL;*/
  a->system_mem = a->max_system_mem = h->size;
  arena_mem += h->size;

  /* Set up the top chunk, with proper alignment. */
  ptr = (char *)(a + 1);
  misalign = (unsigned long)chunk2mem(ptr) & MALLOC_ALIGN_MASK;
  if (misalign > 0)
    ptr += MALLOC_ALIGNMENT - misalign;
  top(a) = (mchunkptr)ptr;
  set_head(top(a), (((char*)h + h->size) - ptr) | PREV_INUSE);

  tsd_setspecific(arena_key, (void *)a);
  mutex_init(&a->mutex);
  (void)mutex_lock(&a->mutex);

#ifdef PER_THREAD
  (void)mutex_lock(&list_lock);
#endif

  /* Add the new arena to the global list.  */
  a->next = main_arena.next;
  atomic_write_barrier ();
  main_arena.next = a;

#ifdef PER_THREAD
  (void)mutex_unlock(&list_lock);
#endif

  THREAD_STAT(++(a->stat_lock_loop));

  return a;
}
示例#3
0
 void extract_min()
 {
     T min = (*root_list.begin())->get_key();
     auto it_min = root_list.begin();
     for (auto it = root_list.begin(); it != root_list.end(); it++)
     {
         if ((*it)->get_key() < min)
         {
             min = (*it)->get_key();
             it_min = it;
         }
     }
     
     std::list<binomial_tree<T>*> copy = (*it_min)->get_child();
     root_list.erase(it_min);
     copy.reverse();
     binomial_heap<T> new_heap(copy);
     if (root_list.size() == 0)
     {
         *this = new_heap;
     }
     else
     {
         this->merge(new_heap);
     }
 }
示例#4
0
static void from_array(CHEAP *heap, GB_ARRAY array)
{
    int count = GB.Array.Count(array), i;

    new_heap(heap, count);
    for (i = 0; i < count; i++) {
        memcpy(&heap->h[i], GB.Array.Get(array, i),
               sizeof(*heap->h));
        if (TYPE_is_object(heap->h[i].type))
            GB.Ref(heap->h[i].value._object);
    }
    rebuild(heap);
}
示例#5
0
 void add(T a)
 {
     if (root_list.size() != 0)
     {
         binomial_heap<T> new_heap(a);
         this->merge(new_heap);
     }
     else
     {
         binomial_tree<T>* tree = new binomial_tree<T>(a);
         root_list.insert(root_list.begin(), tree);
     }
 }
示例#6
0
int main(void)
{
    printf("sizeof(struct block) = %d\n", (int)sizeof(struct block));
    heap_start = new_heap(malloc(HEAP_SIZE), HEAP_SIZE);
    printf("heap_start = %p\n", heap_start);
    
    assert(test_allocate(34) != NULL);
    assert(test_allocate(256) != NULL);
    assert(test_allocate(10) != NULL);
    assert(test_allocate(1) != NULL);
    assert(test_allocate(6) != NULL);
    assert(test_allocate(23) != NULL);
    assert(test_allocate(444) == NULL); /* should fail */

    return 0;
}
示例#7
0
RemovalPolicy *
createRemovalPolicy_heap(wordlist * args)
{
    RemovalPolicy *policy;
    HeapPolicyData *heap_data;
    const char *keytype;
    /* Allocate the needed structures */
    policy = cbdataAlloc(RemovalPolicy);
    heap_data = xcalloc(1, sizeof(*heap_data));
    /* Initialize the policy data */
    heap_data->policy = policy;
    if (args) {
	keytype = args->key;
	args = args->next;
    } else {
	debug(81, 1) ("createRemovalPolicy_heap: No key type specified. Using LRU\n");
	keytype = "LRU";
    }
    if (!strcmp(keytype, "GDSF"))
	heap_data->keyfunc = HeapKeyGen_StoreEntry_GDSF;
    else if (!strcmp(keytype, "LFUDA"))
	heap_data->keyfunc = HeapKeyGen_StoreEntry_LFUDA;
    else if (!strcmp(keytype, "LRU"))
	heap_data->keyfunc = HeapKeyGen_StoreEntry_LRU;
    else {
	debug(81, 0) ("createRemovalPolicy_heap: Unknown key type \"%s\". Using LRU\n",
	    keytype);
	heap_data->keyfunc = HeapKeyGen_StoreEntry_LRU;
    }
    /* No additional arguments expected */
    assert(!args);
    heap_data->heap = new_heap(1000, heap_data->keyfunc);
    heap_data->heap->age = 1.0;
    /* Populate the policy structure */
    policy->_type = "heap";
    policy->_data = heap_data;
    policy->Free = heap_free;
    policy->Add = heap_add;
    policy->Remove = heap_remove;
    policy->Referenced = NULL;
    policy->Dereferenced = heap_referenced;
    policy->WalkInit = heap_walkInit;
    policy->PurgeInit = heap_purgeInit;
    /* Increase policy usage count */
    nr_heap_policies += 0;
    return policy;
}
示例#8
0
文件: heap.c 项目: lmy375/Practices
int main()
{
	int option;
	setbuf(stdin, 0);
	setbuf(stdout,0);
	setbuf(stderr,0);
	while(1){
		puts("1 - new\n2 - edit\n3 - print\n4 - free\n5 - name\n6 - exit\ninput:");
		scanf("%d", &option);
		switch(option){
			case 1: new_heap(); break;
			case 2: edit_heap(); break;
			case 3: print_heap(); break;
			case 4: free_heap(); break;
			case 5: input_name(); break;
			case 6: exit(0); break;
			default: puts("error!\n");
		}
	}
}
示例#9
0
//initialize estimator with c samplers and k counters (used by Misra-Gries alg)
Naive_Estimator_type * Naive_Estimator_Init(int c, int k)
{
  Naive_Estimator_type* est = (Naive_Estimator_type*) safe_malloc(sizeof(Naive_Estimator_type));
  est->c=c;
  est->k=k;
  est->count = 0;
  est->prng=prng_Init(drand48(), 2); 
  // initialize the random number generator
  est->freq=Freq_Init((float)1.0/k);
	
  est->samplers = (Sample_type**) safe_malloc(sizeof(Sample_type*) * c);	
  for(int i = 0; i < c; i++)
  {
    est->samplers[i]=Naive_Sample_Init();
  }
  
  est->hashtable=new_naivesymtab(c);
  est->prim_heap = new_heap(prim_cmp, c); 
  return est;
}
示例#10
0
int main() {
  heap = new_heap(1, int_compare);
  insert_(5);
  print_heap(heap, print);
  insert_(3);
  print_heap(heap, print);
  print_extracted();
  insert_(3);
  print_heap(heap, print);
  insert_(2);
  print_heap(heap, print);
  insert_(4);
  print_heap(heap, print);
  insert_(5);
  insert_(5);
  insert_(5);
  insert_(35);
  insert_(1);
  insert_(5);
  print_heap(heap, print);
  insert_(5);
  print_heap(heap, print);
  insert_(8);
  print_heap(heap, print);
  insert_(9);
  print_heap(heap, print);
  insert_(4);
  print_heap(heap, print);
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  print_extracted();
  delete_heap(heap);
}
示例#11
0
//initialize estimator with c samplers and k counters (used by Misra-Gries alg)
Estimator_type * Estimator_Init(int c, int k)
{
  Estimator_type* est = (Estimator_type*) safe_malloc(sizeof(Estimator_type));
  est->c=c;
  est->k=k;
  est->count = 0;
  est->two_distinct_tokens=0;
  est->prng=prng_Init(drand48(), 2); 
  // initialize the random number generator
  est->freq=Freq_Init((float)1.0/k);
	
  est->samplers = (Sample_type**) safe_malloc(sizeof(Sample_type*) * c);	
  for(int i = 0; i < c; i++)
  {
    est->samplers[i]=Sample_Init();
  }
  
  est->hashtable=new_symtab(2*c);
  est->prim_heap = new_heap(prim_cmp, c);
  est->bheap = new_bheap(b_cmp, c);  
  return est;
}
示例#12
0
int main() 
{
      heap* h = new_heap(_TEST_SIZE, ORD_ASC, compare_integer);
      srand(time(NULL));
      int i;
      for (i=0;i<_TEST_SIZE;i++) 
      {
            int* data = malloc(sizeof(int));
            *data = rand()%1000;
            push_heap(h,data);
            printf("\n[%d]\t%d",i+1,*(int*)h->array[i+1]);
      }
      printf("\n\n\n");
           
      for (i=0;i<_TEST_SIZE+5;i++) 
      {
            int* x = pop_heap(h);
            if (x!=NULL) printf("\n[%d]\t%d",i+1,*x);
      }

      delete_heap(h);
      
      return 0;
}
示例#13
0
/* Allocate a code heap during startup */
void init_code_heap(CELL size)
{
	new_heap(&code_heap,size);
}
示例#14
0
文件: arena.c 项目: ddstreet/glibc
static mstate
_int_new_arena (size_t size)
{
  mstate a;
  heap_info *h;
  char *ptr;
  unsigned long misalign;

  h = new_heap (size + (sizeof (*h) + sizeof (*a) + MALLOC_ALIGNMENT),
                mp_.top_pad);
  if (!h)
    {
      /* Maybe size is too large to fit in a single heap.  So, just try
         to create a minimally-sized arena and let _int_malloc() attempt
         to deal with the large request via mmap_chunk().  */
      h = new_heap (sizeof (*h) + sizeof (*a) + MALLOC_ALIGNMENT, mp_.top_pad);
      if (!h)
        return 0;
    }
  a = h->ar_ptr = (mstate) (h + 1);
  malloc_init_state (a);
  a->attached_threads = 1;
  /*a->next = NULL;*/
  a->system_mem = a->max_system_mem = h->size;

  /* Set up the top chunk, with proper alignment. */
  ptr = (char *) (a + 1);
  misalign = (unsigned long) chunk2mem (ptr) & MALLOC_ALIGN_MASK;
  if (misalign > 0)
    ptr += MALLOC_ALIGNMENT - misalign;
  top (a) = (mchunkptr) ptr;
  set_head (top (a), (((char *) h + h->size) - ptr) | PREV_INUSE);

  LIBC_PROBE (memory_arena_new, 2, a, size);
  mstate replaced_arena = thread_arena;
  thread_arena = a;
  __libc_lock_init (a->mutex);

  __libc_lock_lock (list_lock);

  /* Add the new arena to the global list.  */
  a->next = main_arena.next;
  /* FIXME: The barrier is an attempt to synchronize with read access
     in reused_arena, which does not acquire list_lock while
     traversing the list.  */
  atomic_write_barrier ();
  main_arena.next = a;

  __libc_lock_unlock (list_lock);

  __libc_lock_lock (free_list_lock);
  detach_arena (replaced_arena);
  __libc_lock_unlock (free_list_lock);

  /* Lock this arena.  NB: Another thread may have been attached to
     this arena because the arena is now accessible from the
     main_arena.next list and could have been picked by reused_arena.
     This can only happen for the last arena created (before the arena
     limit is reached).  At this point, some arena has to be attached
     to two threads.  We could acquire the arena lock before list_lock
     to make it less likely that reused_arena picks this new arena,
     but this could result in a deadlock with
     __malloc_fork_lock_parent.  */

  __libc_lock_lock (a->mutex);

  return a;
}