本文整理汇总了C++中Heap类的典型用法代码示例。如果您正苦于以下问题:C++ Heap类的具体用法?C++ Heap怎么用?C++ Heap使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Heap类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: main
int main()
{
string s;
char ch;
cin >> ch;
while (!cin.eof())
{
s += ch;
cin >> ch;
}
//cout << s;
//exit(0);
int count[27];
memset(count,0,sizeof(count));
for (int i = 0; i < s.length();i++)
{
cout << s[i] << " is " << (int)(s[i] - 'a') << "\n";
count[s[i] - 'a'] += 1;
}
#ifdef DEBUG
for (int i = 0; i < s.length(); i++)
{
cout << "The count of " << s[i] << " " << count[s[i] - 'a'] << "\n";
}
#endif
TreeNode <char,int>* t1;
Heap < TreeNode<char,int> > pq;
for (int i = 0; i < s.length();i++)
{
t1 = new TreeNode <char, int>(&s[i],&count[s[i] - 'a']);
pq.insert(t1);
}
return 0;
}
示例2: main
int main(int argc, char* argv[])
{
int dummy;
Heap<int> hp;
hp.Insert(10).Insert(6).Insert(20).Insert(30).Insert(25).Insert(3).Insert(8);
cout << "Heap elements: " << hp << endl;
hp.PrintTreeVertically(cout, 64);
cout << endl << "Heap elements on delete: ";
while (!hp.IsEmpty()) {
hp.DeleteMin(dummy);
cout << dummy << ", ";
}
cout << endl;
// Heap sort
// In this mechanism/implementation, note that:
// Descending sort by using Min-Heap, while ascending sort should use Max-Heap
int a[] = { 1, 3, 4, 2, 5, 9, 3, 6, 7, 9, 4, 8 };
#define ELEM_COUNT(a) (sizeof(a) / sizeof(a[0]))
Heap<int>::Sort(a, ELEM_COUNT(a));
cout << "Sort array descending: ";
for (int i = 0; i < ELEM_COUNT(a); i++) {
cout << a[i] << ", ";
}
cout << endl;
return 0;
}
示例3: trigger
void DefaultGCActivityCallbackPlatformData::trigger(CFRunLoopTimerRef timer, void *info)
{
Heap* heap = static_cast<Heap*>(info);
APIEntryShim shim(heap->globalData());
heap->collectAllGarbage();
CFRunLoopTimerSetNextFireDate(timer, CFAbsoluteTimeGetCurrent() + decade);
}
示例4: Timer
TestResults TestingSuite::runAppendTestsForHeap(int repeats)
{
Timer timer;
Heap * heap;
double avgTime = 0;
for (int i = 0; i < repeats; i++) {
timer = Timer();
heap = new Heap();
if (randomizeEachRepeat) prepareTestData();
timer.startTimer();
for (int q = 0; q < dataSize; q++) {
heap->insert(randomData[q]);
}
timer.endTimer();
avgTime += timer.getTimeInNanoseconds();
delete heap;
}
avgTime = avgTime / (double)repeats;
return TestResults("Heap Insert Test", dataSize, avgTime);
}
示例5: findKthLargest
int findKthLargest(vector<int>& nums, int k) {
Heap h = Heap(k);
for(int i = 0;i < nums.size();i++) {
h.insert(nums[i]);
}
return h.getMin();
}
示例6: doDijkstra
pair<int, int>* doDijkstra(Graph *graph, int src) {
int vNum = graph->vNum;
pair<int, int> *vertices = new pair<int, int>[vNum];
for (int i = 0; i < vNum; i++) {
vertices[i].first = i;
vertices[i].second = INT_MAX;
}
vertices[src].second = 0;
Heap *queue = new Heap(vNum * vNum);
queue->insert(vertices + src);
while (!queue->isEmpty()) {
pair<int, int> source = queue->extractMin();
for (Edge &edge : graph->graph[source.first]) {
pair<int, int> *target = vertices + edge.dest;
int weight = edge.weight;
if (source.second + weight < target->second) {
target->second = source.second + weight;
queue->insert(target);
}
}
}
delete queue;
return vertices;
}
示例7: main
int main()
{
//variable declaration
int* arr = new int[500]; //create a size of 500 array to store all the nums
int size, index, nums, input, choice = 0;
Heap* myheap;
//open the file and takes in numbers
ifstream file("data.txt");
while(true)
{
file>>nums;
if(file.eof()) break;
{
arr[index] = nums;
index++;
}
}
size = index;
//construct the heap
myheap = new Heap(arr, size);
while(choice != 5)
{
printList();
cin>>choice;
switch(choice)
{
case 1:
cout<<"Please insert the number that you want to be inserted in the heap"<<endl;
cin>>input;
//code here
myheap->insert(input);
break;
case 2:
//code here
myheap->deletemin();
break;
case 3:
//code here
myheap->deletemax();
break;
case 4:
//code here
myheap->levelorder();
break;
case 5:
return 0;
break;
default:
cout<<"Please input a valid number!!!"<<endl;
break;
}
}
}
示例8: FindBestCandidateAsy
void SearchEngineSCKMeans::FindBestCandidateAsy(double* p_query, Heap<PAIR<double> >& heap_knn)
{
SMatrix<double> matQueryCodewordInnerProduct;
ConstructQueryCodewordInnerProduct(p_query,
matQueryCodewordInnerProduct);
Vector<double*> vecTables(m_numTables);
for (int j = 0; j < m_nNumDataBasePoint; j++)
{
PAIR<double> node;
double s = ASymmetricDistance(p_query, j, matQueryCodewordInnerProduct);
node.distance = s;
if (heap_knn.size() < m_nNumCandidate)
{
node.index = j;
heap_knn.insert(node);
}
else
{
const PAIR<double>& top = heap_knn.Top();
if (top < node)
{
node.index = j;
heap_knn.popMin();
heap_knn.insert(node);
}
}
}
}
示例9: TEST_F
TEST_F(HeapTest, size) {
Heap heap;
EXPECT_EQ(0, heap.size());
EXPECT_EQ(1, oneItemHeap_.size());
EXPECT_EQ(2, twoItemHeap_.size());
EXPECT_EQ(5, heap_.size());
}
示例10: broadcast
void Transpose::scalar(const var& iVar, var& oVar) const
{
// Note that we need to transpose the view too. If it's allocated, it's
// transposed at allocation; if it's supplied it's assumed to be correct
// already. This leaves the in-place case; this happens after the
// operation.
// Transpose always broadcasts to array()
broadcast(iVar, oVar);
// Transpose view if necessary
if (iVar.is(oVar))
{
// Swap the trailing dimensions
int d = iVar.dim();
Heap* h = iVar.heap();
int& str1 = h->stride(d-1);
int& shp1 = h->shape(d-1);
int& str2 = h->stride(d-2);
int& shp2 = h->shape(d-2);
int tmp = shp1;
shp1 = shp2;
shp2 = tmp;
str2 = shp1 * str1;
}
}
示例11: sizeof
void MarkedArgumentBuffer::slowAppend(JSValue v)
{
int newCapacity = (Checked<int>(m_capacity) * 2).unsafeGet();
size_t size = (Checked<size_t>(newCapacity) * sizeof(EncodedJSValue)).unsafeGet();
EncodedJSValue* newBuffer = static_cast<EncodedJSValue*>(fastMalloc(size));
for (int i = 0; i < m_capacity; ++i)
newBuffer[i] = m_buffer[i];
if (EncodedJSValue* base = mallocBase())
fastFree(base);
m_buffer = newBuffer;
m_capacity = newCapacity;
slotFor(m_size) = JSValue::encode(v);
++m_size;
if (m_markSet)
return;
// As long as our size stays within our Vector's inline
// capacity, all our values are allocated on the stack, and
// therefore don't need explicit marking. Once our size exceeds
// our Vector's inline capacity, though, our values move to the
// heap, where they do need explicit marking.
for (int i = 0; i < m_size; ++i) {
Heap* heap = Heap::heap(JSValue::decode(slotFor(i)));
if (!heap)
continue;
m_markSet = &heap->markListSet();
m_markSet->add(this);
break;
}
}
示例12: Heap
Heap * Heap::Alloc(ID3D12Device * Device, int Type) {
Heap * heap = 0;
D3D12Render * render = D3D12Render::GetRender();
CommandQueue * Queue = render->GetQueue(D3D12_COMMAND_LIST_TYPE_DIRECT);
if (Free.Size()) {
heap = Free.PopBack();
}
else {
bool Found = 0;
for (auto Iter = Retired.Begin(); Iter != Retired.End(); Iter++) {
heap = *Iter;
UINT64 FenceValue = heap->FenceValue;
if (Queue->FenceComplete(FenceValue)) {
Retired.Remove(Iter);
Found = 1;
break;
}
}
if (!Found) {
heap = 0;
}
}
if (heap) {
return heap;
}
else {
// new context
heap = new Heap();
heap->Init(Device, MAX_CONSTANT_BUFFER_HEAP, Type);
}
return heap;
}
示例13: JSGarbageCollect
void JSGarbageCollect(JSContextRef ctx)
{
// Unlikely, but it is legal to call JSGarbageCollect(0) before actually doing anything that would implicitly call initializeThreading().
if (!ctx)
initializeThreading();
// It might seem that we have a context passed to this function, and can use toJS(ctx)->heap(), but the parameter is likely to be NULL,
// and it may actually be garbage for some clients (most likely, because of JSGarbageCollect being called after releasing the context).
JSLock lock;
// FIXME: It would be good to avoid creating a JSGlobalData instance if it didn't exist for this thread yet.
Heap* heap = JSGlobalData::threadInstance().heap;
if (!heap->isBusy())
heap->collect();
// FIXME: Similarly, we shouldn't create a shared instance here.
heap = JSGlobalData::sharedInstance().heap;
if (!heap->isBusy())
heap->collect();
// FIXME: Perhaps we should trigger a second mark and sweep
// once the garbage collector is done if this is called when
// the collector is busy.
}
示例14: generalizeTerm
CellRef generalizeTerm(Heap &heap, CellRef term, int p, bool lotsOfForward)
{
if (myRand(100) < p) {
return heap.newRef();
}
CellRef cellRef = heap.deref(term);
if (heap.getTag(cellRef) == Cell::STR) {
size_t arity = heap.getArity(cellRef);
std::vector<CellRef> args(arity);
CellRef newStr;
if (lotsOfForward) {
newStr = heap.newStr(heap.getFunctorConst(cellRef));
}
for (size_t i = 0; i < arity; i++) {
CellRef newArg = generalizeTerm(heap,heap.getArg(cellRef,i),
p,lotsOfForward);
args[i] = newArg;
}
if (!lotsOfForward) {
newStr = heap.newStr(heap.getFunctorConst(cellRef));
}
for (size_t i = 0; i < arity; i++) {
heap.setArg(newStr, i, args[i]);
}
return newStr;
} else {
return term;
}
}
示例15: onThrow
void Item::onThrow(int cell)
{
Heap* heap = Dungeon::level->drop(this, cell);
if (!heap->isEmpty()) {
heap->sprite->drop(cell);
}
}