// Copyright (c) 2006-2011 GeometryFactory (France). All rights reserved. // // This file is part of CGAL (www.cgal.org) // // $URL$ // $Id$ // SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial // // Author(s) : Fernando Cacciola // #ifndef CGAL_MODIFIABLE_PRIORITY_QUEUE_H #define CGAL_MODIFIABLE_PRIORITY_QUEUE_H #include // Needed by the following Boost header for CHAR_BIT. #include #include #include #include #include namespace CGAL { enum Heap_type { CGAL_BOOST_FIBONACCI_HEAP, CGAL_BOOST_PENDING_MUTABLE_QUEUE, CGAL_BOOST_PENDING_RELAXED_HEAP }; template ,class ID_ = boost::identity_property_map , Heap_type heap_type = CGAL_BOOST_PENDING_MUTABLE_QUEUE > class Modifiable_priority_queue { public: typedef Modifiable_priority_queue Self; typedef IndexedType_ IndexedType ; typedef Compare_ Compare; typedef ID_ ID ; typedef std::conditional_t,Compare,ID>, internal::boost_::relaxed_heap > Heap; typedef typename Heap::value_type value_type; typedef typename Heap::size_type size_type; public: Modifiable_priority_queue( size_type largest_ID, Compare const& c = Compare(), ID const& id = ID() ) : mHeap(largest_ID,c,id) {} void push ( value_type const& v ) { mHeap.push(v) ; } void update ( value_type const& v ) { mHeap.update(v); } void erase ( value_type const& v ) { mHeap.remove(v); } value_type top() const { return mHeap.top(); } void pop() { mHeap.pop(); } bool empty() const { return mHeap.empty() ; } bool contains ( value_type const& v ) { return mHeap.contains(v) ; } boost::optional extract_top() { boost::optional r ; if ( !empty() ) { value_type v = top(); pop(); r = boost::optional(v) ; } return r ; } value_type top_and_pop() { CGAL_precondition(!empty()); value_type v = top(); pop(); return v; } private: Heap mHeap ; } ; template class Modifiable_priority_queue { public: typedef Modifiable_priority_queue Self; typedef IndexedType_ IndexedType ; typedef Compare_ Compare; typedef ID_ ID ; struct Reverse_compare{ const Compare c; Reverse_compare(){} Reverse_compare(Compare const& c):c(c){} template bool operator() (T const& a, T const& b) const { return !c(a,b); } }; typedef boost::heap::fibonacci_heap > Heap; typedef typename Heap::value_type value_type; typedef typename Heap::size_type size_type; public: Modifiable_priority_queue( size_type largest_ID, Compare const& c = Compare(), ID const& id = ID() ) : mHeap(Reverse_compare(c)) , mID(id) , mHandles(largest_ID) {} void push ( value_type const& v ) { mHandles[get(mID, v)]=mHeap.push(v) ; } void update ( value_type const& v ) { mHeap.update(mHandles[get(mID, v)]); } void erase ( value_type const& v ) { auto vid = get(mID, v); mHeap.erase(mHandles[vid]); mHandles[vid]=typename Heap::handle_type(); } value_type top() const { return mHeap.top(); } void pop() { mHeap.pop(); } bool empty() const { return mHeap.empty() ; } bool contains ( value_type const& v ) { return mHandles[get(mID, v)] != typename Heap::handle_type(); } boost::optional extract_top() { boost::optional r ; if ( !empty() ) { value_type v = top(); pop(); r = boost::optional(v) ; } return r ; } value_type top_and_pop() { CGAL_precondition(!empty()); value_type v = top(); pop(); return v; } private: Heap mHeap ; ID mID; std::vector mHandles; } ; } //namespace CGAL #endif