Advertisement
Guest User

Untitled

a guest
Jul 24th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import std.stdio, std.string;
  2. import std.container.binaryheap;
  3.  
  4. struct XD
  5. {
  6. int f;
  7. int s;
  8.  
  9. this(int f, int s)
  10. {
  11. this.f = f;
  12. this.s = s;
  13. }
  14.  
  15. int opCmp(const XD xd)
  16. {
  17. if (f == xd.f && s == xd.s) return 0;
  18. if (f < xd.f) return -1;
  19. if (f == xd.f && s < xd.s) return -1;
  20. return 1;
  21. }
  22. }
  23.  
  24. int main(string[] args)
  25. {
  26. auto maxHeap = heapify([4, 7, 3, 1, 5]);
  27. while (!maxHeap.empty)
  28. {
  29. writeln(maxHeap.front);
  30. maxHeap.removeFront;
  31. }
  32.  
  33. auto minHeap = heapify!"a > b"([4, 7, 3, 1, 5]);
  34. writeln(minHeap.front);
  35.  
  36. int[] b;
  37. BinaryHeap!(int[]) bh = BinaryHeap!(int[])(b, 0);
  38. foreach (elem; minHeap)
  39. {
  40. bh.insert(elem);
  41. }
  42. writeln(bh.length);
  43. writeln(minHeap.length);
  44.  
  45. XD[] a;
  46. a ~= XD(2, 3);
  47. a ~= XD(1, 4);
  48.  
  49. //auto heap = heapify!"a.f > b.f || a.f == b.f && a.s > b.s"(a);
  50. //auto heap = heapify!((a, b) => a.f > b.f || a.f == b.f && a.s > b.s)(a);
  51. //auto heap = heapify(a);
  52. auto heap = a.heapify;
  53. writeln(heap.front);
  54.  
  55. return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement