| 27 | // Supports movable types. |
| 28 | template <typename T, typename Container, typename Comparator> |
| 29 | T ConsumeTop(std::priority_queue<T, Container, Comparator>* q) { |
| 30 | // std::priority_queue is required to implement pop() as if it |
| 31 | // called: |
| 32 | // std::pop_heap() |
| 33 | // c.pop_back() |
| 34 | // unfortunately, it does not provide access to the removed element. |
| 35 | // If the element is move only (such as a unique_ptr), there is no way to |
| 36 | // reclaim it in the standard API. std::priority_queue does, however, expose |
| 37 | // the underlying container as a protected member, so we use that access |
| 38 | // to extract the desired element between those two calls. |
| 39 | using Q = std::priority_queue<T, Container, Comparator>; |
| 40 | struct Expose : Q { |
| 41 | using Q::c; |
| 42 | using Q::comp; |
| 43 | }; |
| 44 | auto& c = q->*&Expose::c; |
| 45 | auto& comp = q->*&Expose::comp; |
| 46 | std::pop_heap(c.begin(), c.end(), comp); |
| 47 | auto r = std::move(c.back()); |
| 48 | c.pop_back(); |
| 49 | return r; |
| 50 | } |
| 51 | |
| 52 | } // namespace gtl |
| 53 | } // namespace tensorflow |
no test coverage detected