Example #1
0
	static void remove_from_heap(const RandomAccessIterator &first,
		const RandomAccessIterator &item, const RandomAccessIterator &last,
		const LessComparer &less_comparer)
	{
		assert(last > first);
		assert(item >= first);
		assert(item < last);
		assert(is_heap(first, last, less_comparer));

		typedef typename std::iterator_traits<RandomAccessIterator>::value_type
			value_type;

		const size_t new_heap_size = last - first - 1;
		const size_t hole_index = item - first;
		if (hole_index < new_heap_size) {
			value_type tmp = std::move(first[new_heap_size]);
			_move(first[new_heap_size], *item);
			if (less_comparer(tmp, first[new_heap_size])) {
				_sift_down(first, less_comparer, new_heap_size, hole_index, tmp);
			}
			else {
				_sift_up(first, less_comparer, 0, hole_index, tmp);
			}
		}

		assert(is_heap(first, last - 1, less_comparer));
	}
Example #2
0
void TestHeapOperations (void)
{
    static const int c_Values [31] = {	// 31 values make a full 4-layer tree
	93, 92, 90, 86, 83, 86, 77, 40, 72, 36, 68, 82, 62, 67, 63, 15,
	26, 26, 49, 21, 11, 62, 67, 27, 29, 30, 35, 23, 59, 35, 29
    };
    vector<int> v;
    v.reserve (VectorSize(c_Values));
    for (uoff_t i = 0; i < VectorSize(c_Values); ++ i) {
	v.push_back (c_Values[i]);
	push_heap (v.begin(), v.end());
	cout << "------------------------------------------------\n";
	if (!is_heap (v.begin(), v.end()))
	    cout << "Is NOT a heap\n";
	PrintHeap (v);
    }
    cout << "------------------------------------------------\n";
    cout << "make_heap on the full range:\n";
    v.resize (VectorSize (c_Values));
    copy (VectorRange(c_Values), v.begin());
    make_heap (v.begin(), v.end());
    PrintHeap (v);
    if (!is_heap (v.begin(), v.end()))
	cout << "Is NOT a heap\n";
    cout << "------------------------------------------------\n";
    cout << "pop_heap:\n";
    pop_heap (v.begin(), v.end());
    v.pop_back();
    PrintHeap (v);
    if (!is_heap (v.begin(), v.end()))
	cout << "Is NOT a heap\n";

    cout << "------------------------------------------------\n";
    cout << "sort_heap:\n";
    v.resize (VectorSize (c_Values));
    copy (VectorRange(c_Values), v.begin());
    make_heap (v.begin(), v.end());
    sort_heap (v.begin(), v.end());
    foreach (vector<int>::const_iterator, i, v)
	cout << *i;
    cout << endl;

    cout << "------------------------------------------------\n";
    cout << "priority_queue push and pop:\n";
    priority_queue<int> q;
    for (uoff_t i = 0; i < VectorSize(c_Values); ++ i)
	q.push (c_Values[i]);
    while (!q.empty()) {
	cout << q.top();
	q.pop();
    }
    cout << endl;
}
Example #3
0
/*
 * heap_rem_elem - Removes an arbitrary element in the heap by finding
 *                 its index with linear search and then swapping
 *                 and deleting with the bottom-most, right-most element.
 */
void heap_rem_elem(heap H, elem x)
{
  REQUIRES(is_heap(H) && !heap_empty(H));
  int idx = find_elem(H, x);
  H->next--;

  if (H->next > 1) {
    H->data[idx] = H->data[H->next];
    sift_down(H, idx);
  }

  ENSURES(is_heap(H));
}
Example #4
0
	static void pop_heap(const RandomAccessIterator &first,
		const RandomAccessIterator &last, const LessComparer &less_comparer)
	{
		assert(last > first);
		assert(is_heap(first, last, less_comparer));

		const size_t heap_size = last - first;
		if (heap_size > 1) {
			_pop_max_item(first, less_comparer, heap_size - 1);
		}

		assert(is_heap(first, last - 1, less_comparer));
	}
Example #5
0
elem heap_rem(heap H)
{
  REQUIRES(is_heap(H) && !heap_empty(H));
  elem min = H->data[1];
  H->next--;

  if (H->next > 1) {
    H->data[1] = H->data[H->next];
    sift_down(H, 1);
  }

  ENSURES(is_heap(H));
  return min;
}
    void heapify_up(vector<T>& A, size_t i) {
        assert(is_heap(A, i));
        
        if(i == 0) 
            return;

        auto p = parent(i);
        if(A[p] < A[i])
        {
            swap(A[p], A[i]);
            heapify(A, p);
        }

        assert(is_heap(A, i+1));
    }
Example #7
0
void sift_down(heap H, int i)
{
  REQUIRES(is_safe_heap(H));
  REQUIRES(H->next > 1 && is_heap_except_down(H, i));

  while (2*i < H->next)
  {
    ASSERT(1 <= i && i < H->next);
    ASSERT(is_heap_except_down(H, i));
    ASSERT(grandparent_check(H, i));
    int left = 2*i;
    int right = left + 1;
    if (ok_above(H, i, left)
        && (right >= H->next || ok_above(H, i, right))) {
      return;
    }
    else if (right >= H->next || ok_above(H, left, right)) {
      swap_up(H, left);
      i = left;
    }
    else {
      ASSERT(right < H->next && ok_above(H, right, left));
      swap_up(H, right);
      i = right;
    }
  }
  ASSERT(i < H->next && 2*i >= H->next);
  ASSERT(is_heap_except_down(H, i));
  ENSURES(is_heap(H));
}
Example #8
0
/* Format access codes nicely and try to make an educated guess regarding memory-region */
static void access_output(int acc, long long from, long long to, void *stack_frame)
{
    if (acc == 1)      /*  read  only  */
    {
        if (is_code(from, to))
            printf("(CODE) ");
        else
            printf("(UNKN) ");

        printf("read access from \t \t 0x%08llx to 0x%08llx\n", from, to);

    }
    else if (acc == 2) /* read & write */
    {
        if (is_data(from, to))
            printf("(DATA) ");
        else if (is_heap(from, to))
            printf("(HEAP) ");
        else if (is_stck(from, to, stack_frame))
            printf("(STCK) ");
        else
            printf("(UNKN) ");

        printf("read and write access from \t 0x%08llx to 0x%08llx\n", from, to);
    }
    return;
}
Example #9
0
	static void restore_heap_after_item_increase(
		const RandomAccessIterator &first, const RandomAccessIterator &item,
		const LessComparer &less_comparer)
	{
		assert(item >= first);
		assert(is_heap(first, item, less_comparer));

		typedef typename std::iterator_traits<RandomAccessIterator>::value_type
			value_type;

		const size_t hole_index = item - first;
		if (hole_index > 0) {
			value_type tmp = std::move(*item);
			_sift_up(first, less_comparer, 0, hole_index, tmp);
		}

		assert(is_heap(first, item + 1, less_comparer));
	}
Example #10
0
	static void push_heap(const RandomAccessIterator &first,
		const RandomAccessIterator &last, const LessComparer &less_comparer)
	{
		assert(last > first);
		assert(is_heap(first, last - 1, less_comparer));

		typedef typename std::iterator_traits<RandomAccessIterator>::value_type
			value_type;

		const size_t heap_size = last - first;
		if (heap_size > 1) {
			const size_t u = heap_size - 1;
			value_type item = std::move(first[u]);
			_sift_up(first, less_comparer, 0, u, item);
		}

		assert(is_heap(first, last, less_comparer));
	}
Example #11
0
	static void restore_heap_after_item_decrease(
		const RandomAccessIterator &first, const RandomAccessIterator &item,
		const RandomAccessIterator &last, const LessComparer &less_comparer)
	{
		assert(last > first);
		assert(item >= first);
		assert(item < last);
		assert(is_heap(first, item, less_comparer));

		typedef typename std::iterator_traits<RandomAccessIterator>::value_type
			value_type;

		const size_t heap_size = last - first;
		const size_t hole_index = item - first;
		value_type tmp = std::move(*item);
		_sift_down(first, less_comparer, heap_size, hole_index, tmp);

		assert(is_heap(first, last, less_comparer));
	}
Example #12
0
	static void swap_max_item(const RandomAccessIterator &first,
		const RandomAccessIterator &last,
		typename std::iterator_traits<RandomAccessIterator>::value_type &item,
		const LessComparer &less_comparer)
	{
		assert(first < last);
		assert(is_heap(first, last, less_comparer));

		typedef typename std::iterator_traits<RandomAccessIterator>::value_type
			value_type;

		const size_t heap_size = last - first;

		value_type tmp = std::move(item);
		_move(item, first[0]);
		_sift_down(first, less_comparer, heap_size, 0, tmp);

		assert(is_heap(first, last, less_comparer));
	}
Example #13
0
heap heap_new(int capacity, higher_priority_fn *prior)
{
  REQUIRES(capacity > 0 && prior != NULL);
  heap H = malloc(sizeof(struct heap_header));
  H->next = 1;
  H->limit = capacity + 1;
  H->data = malloc(sizeof(void*) * H->limit);
  H->prior = prior;
  ENSURES(is_heap(H) && heap_empty(H));
  return H;
}
Example #14
0
void heap_add(heap H, elem x)
{
  REQUIRES(is_heap(H) && !heap_full(H));
  int i = H->next;
  H->data[H->next] = x;
  H->next++;

  // Sift up takes O(log n) time
  while (i > 1)
  {
    ASSERT(1 <= i && i < H->next);
    ASSERT(is_heap_except_up(H, i));
    ASSERT(grandparent_check(H, i));
    if (ok_above(H, i/2, i)) {
      return;
    }
    swap_up(H, i);
    i = i/2;
  }

  ENSURES(is_heap(H));
}
Example #15
0
/*
 * find_elem - Returns the index of the given element x in the array
 *             that represents the heap by doing linear search
 *             If e is not in the heap, returns -1.
 *             Assumes the elements in the heap are ints
 */
int find_elem(heap H, elem e)
{
  REQUIRES(is_heap(H));
  int x = *(int*)(e);

  for (int i = 1; i < H->limit; i++)
  {
    ASSERT(1 <= i && i <= H->limit);
    int curr = *(int*)(H->data[i]);
    if (x == curr) {
      return i;
    }
  }

  return -1;
}
Example #16
0
void retrieve_heaps(const jsa_counterexamplet &ce,
    __CPROVER_jsa_abstract_heapt *heaps)
{
  assert(std::is_sorted(ce.begin(), ce.end(), compare_assignment));
  size_t index=0;
  for (const jsa_counterexamplet::value_type &assignment : ce)
    if (is_heap(assignment))
    {
      const struct_exprt &value=to_struct_expr(assignment.second);
      __CPROVER_jsa_abstract_heapt &heap=heaps[index++];
      struct_exprt::operandst ops(value.operands());
      remove_padding(ops, value.type());
      assert(NUM_ABSTRACT_HEAP_MEMBERS == ops.size());
      read_array(heap.concrete_nodes, ops[CONCRETE_NODES_COMP_INDEX]);
      read_array(heap.abstract_nodes, ops[ABSTRACT_NODES_COMP_INDEX]);
      read_array(heap.abstract_ranges, ops[ABSTRACT_RANGES_COMP_INDEX]);
      read_array(heap.iterators, ops[ITERATORS_COMP_INDEX]);
      heap.iterator_count=to_integer(ops[ITERATOR_COUNT_COMP_INDEX]);
      read_array(heap.list_head_nodes, ops[LIST_HEAD_NODES_COMP_INDEX]);
      heap.list_count=to_integer(ops[LIST_COUNT_COMP_INDEX]);
    }
}
Example #17
0
	static void make_heap(const RandomAccessIterator &first,
		const RandomAccessIterator &last, const LessComparer &less_comparer)
	{
		assert(last >= first);

		typedef typename std::iterator_traits<RandomAccessIterator>::value_type
			value_type;

		const size_t heap_size = last - first;
		if (heap_size > 1) {
			// Skip leaf nodes without children. This is easy to do for non-paged
			// heap, i.e. when page_chunks = 1, but it is difficult for paged heaps.
			// So leaf nodes in paged heaps are visited anyway.
			size_t i = (PageChunks == 1) ? ((heap_size - 2) / Fanout) :
				(heap_size - 2);
			do {
				value_type item = std::move(first[i]);
				_sift_down(first, less_comparer, heap_size, i, item);
			} while (i-- > 0);
		}

		assert(is_heap(first, last, less_comparer));
	}
    
    wakeup = tc_frame_queue_put(Q, ptr[0]);
    TC_TEST_IS_TRUE(wakeup);
    TC_TEST_IS_TRUE(tc_frame_queue_size(Q) == 1);

    fp = tc_frame_queue_get(Q);
    TC_TEST_IS_TRUE(!TCFRAMEPTR_IS_NULL(fp));
    TC_TEST_IS_TRUE(tc_frame_queue_size(Q) == 0);
TC_TEST_END

/*************************************************************************/

TC_TEST_BEGIN(S_init_empty, QUEUESIZE, PRIORITY)
    TC_TEST_IS_TRUE(tc_frame_queue_empty(Q));
    TC_TEST_IS_TRUE(tc_frame_queue_size(Q) == 0);
    TC_TEST_IS_TRUE(is_heap(Q, 0));
TC_TEST_END

TC_TEST_BEGIN(S_get1, QUEUESIZE, PRIORITY)
    TCFramePtr fp = { .generic = NULL };

    TC_TEST_IS_TRUE(tc_frame_queue_empty(Q));
    TC_TEST_IS_TRUE(is_heap(Q, 0));

    fp = tc_frame_queue_get(Q);
    TC_TEST_IS_TRUE(TCFRAMEPTR_IS_NULL(fp));
    TC_TEST_IS_TRUE(tc_frame_queue_size(Q) == 0);

    TC_TEST_IS_TRUE(is_heap(Q, 0));
TC_TEST_END
Example #19
0
// Returns an instanceHandle of a MemoryPool object.
// It creates a MemoryPool instance when the first time
// this function is called.
instanceOop MemoryPool::get_memory_pool_instance(TRAPS) {
  // Must do an acquire so as to force ordering of subsequent
  // loads from anything _memory_pool_obj points to or implies.
  instanceOop pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);
  if (pool_obj == NULL) {
    // It's ok for more than one thread to execute the code up to the locked region.
    // Extra pool instances will just be gc'ed.
    klassOop k = Management::sun_management_ManagementFactory_klass(CHECK_NULL);
    instanceKlassHandle ik(THREAD, k);

    Handle pool_name = java_lang_String::create_from_str(_name, CHECK_NULL);
    jlong usage_threshold_value = (_usage_threshold->is_high_threshold_supported() ? 0 : -1L);
    jlong gc_usage_threshold_value = (_gc_usage_threshold->is_high_threshold_supported() ? 0 : -1L);

    JavaValue result(T_OBJECT);
    JavaCallArguments args;
    args.push_oop(pool_name);           // Argument 1
    args.push_int((int) is_heap());     // Argument 2

    symbolHandle method_name = vmSymbolHandles::createMemoryPool_name();
    symbolHandle signature = vmSymbolHandles::createMemoryPool_signature();

    args.push_long(usage_threshold_value);    // Argument 3
    args.push_long(gc_usage_threshold_value); // Argument 4

    JavaCalls::call_static(&result,
                           ik,
                           method_name,
                           signature,
                           &args,
                           CHECK_NULL);

    instanceOop p = (instanceOop) result.get_jobject();
    instanceHandle pool(THREAD, p);

    {
      // Get lock since another thread may have create the instance
      MutexLocker ml(Management_lock);

      // Check if another thread has created the pool.  We reload
      // _memory_pool_obj here because some other thread may have
      // initialized it while we were executing the code before the lock.
      //
      // The lock has done an acquire, so the load can't float above it,
      // but we need to do a load_acquire as above.
      pool_obj = (instanceOop)OrderAccess::load_ptr_acquire(&_memory_pool_obj);
      if (pool_obj != NULL) {
         return pool_obj;
      }

      // Get the address of the object we created via call_special.
      pool_obj = pool();

      // Use store barrier to make sure the memory accesses associated
      // with creating the pool are visible before publishing its address.
      // The unlock will publish the store to _memory_pool_obj because
      // it does a release first.
      OrderAccess::release_store_ptr(&_memory_pool_obj, pool_obj);
    }
  }

  return pool_obj;
}
Example #20
0
bool heap_full(heap H)
{
  REQUIRES(is_heap(H));
  return H->next == H->limit;
}
Example #21
0
bool heap_empty(heap H)
{
  REQUIRES(is_heap(H));
  return H->next == 1;
}
Example #22
0
	static bool is_heap(const RandomAccessIterator &first,
		const RandomAccessIterator &last)
	{
		return is_heap(first, last, _std_less_comparer<RandomAccessIterator>);
	}