samchon / tstl

TypeScript-STL (Standard Template Library, migrated from C++)
MIT License
603 stars 49 forks source link

Poplar heap is probably not what you want #23

Closed Morwenn closed 5 years ago

Morwenn commented 5 years ago

I noticed that you use my poplar heap project to implement the heap algorithms. I don't have a problem with that, but it is probably not what you want:

Well, now it's up to you what you want to do :p

samchon commented 5 years ago

Thanks for your advise. I should update those codes. \o/

samchon commented 5 years ago

The v2.1 update has adopted EASTL's implementation.

samchon commented 5 years ago

After adapting the EASTL's implementation, I tested the heap functions using example codes from the C++ guidances and the implementation worked exactly same with those examples. Thus, I consider that error has been fixed.

C++ Reference

function main(): void { let v: std.Vector = new std.Vector([10, 20, 30, 5, 15]);

std.make_heap(v.begin(), v.end());
console.log("initial max heap:", v.front());

std.pop_heap(v.begin(), v.end());
v.pop_back();
console.log("max heap after pop", v.front());

v.push_back(99);
std.push_heap(v.begin(), v.end());
console.log("max heap after push", v.front());

std.sort_heap(v.begin(), v.end());

console.log("final sorted range:", v.data());

} main();


## CPP Reference
  - https://en.cppreference.com/w/cpp/algorithm/make_heap

```typescript
import * as std from "tstl";

function main(): void
{
    let v: std.Vector<number> = new std.Vector();
    v.push(3, 1, 4, 1, 5, 9);

    console.log("initially, v:", v.data());

    std.make_heap(v.begin(), v.end());
    console.log("after make_heap:", v.data());

    std.pop_heap(v.begin(), v.end());
    console.log("largest element:", v.back());

    v.pop_back();
    console.log("after removing the largest element:", v.data());
}
main();