Gudhi  1.2.0
 All Classes Functions Variables Typedefs Friends Groups Pages
Modifiable_priority_queue.h
1 // Copyright (c) 2006-2011 GeometryFactory (France). All rights reserved.
2 //
3 // This file is part of CGAL (www.cgal.org); you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public License as
5 // published by the Free Software Foundation; either version 3 of the License,
6 // or (at your option) any later version.
7 //
8 // Licensees holding a valid commercial license may use this file in
9 // accordance with the commercial license agreement provided with the software.
10 //
11 // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12 // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 //
14 // $URL$
15 // $Id$
16 //
17 // Author(s) : Fernando Cacciola <fernando.cacciola@geometryfactory.com>
18 //
19 #ifndef CONTRACTION_CGAL_QUEUE_MODIFIABLE_PRIORITY_QUEUE_H_
20 #define CONTRACTION_CGAL_QUEUE_MODIFIABLE_PRIORITY_QUEUE_H_
21 
22 #define CGAL_SURFACE_MESH_SIMPLIFICATION_USE_RELAXED_HEAP
23 
24 #include <boost/optional.hpp>
25 #include <boost/pending/relaxed_heap.hpp>
26 
27 #include <climits> // Neeeded by the following Boost header for CHAR_BIT.
28 #include <functional> // for less
29 
30 namespace CGAL {
31 
32 template <class IndexedType_, class Compare_ = std::less<IndexedType_>, class ID_ = boost::identity_property_map>
33 class Modifiable_priority_queue {
34  public:
35  typedef Modifiable_priority_queue Self;
36 
37  typedef IndexedType_ IndexedType;
38  typedef Compare_ Compare;
39  typedef ID_ ID;
40 
41  typedef boost::relaxed_heap<IndexedType, Compare, ID> Heap;
42  typedef typename Heap::value_type value_type;
43  typedef typename Heap::size_type size_type;
44 
45  typedef bool handle;
46 
47  public:
48  Modifiable_priority_queue(size_type largest_ID, Compare const& c, ID const& id) : mHeap(largest_ID, c, id) { }
49 
50  handle push(value_type const& v) {
51  mHeap.push(v);
52  return handle(true);
53  }
54 
55  handle update(value_type const& v, handle h) {
56  mHeap.update(v);
57  return h;
58  }
59 
60  handle erase(value_type const& v, handle) {
61  mHeap.remove(v);
62  return null_handle();
63  }
64 
65  value_type top() const {
66  return mHeap.top();
67  }
68 
69  void pop() {
70  mHeap.pop();
71  }
72 
73  bool empty() const {
74  return mHeap.empty();
75  }
76 
77  bool contains(value_type const& v) {
78  return mHeap.contains(v);
79  }
80 
81  boost::optional<value_type> extract_top() {
82  boost::optional<value_type> r;
83  if (!empty()) {
84  value_type v = top();
85  pop();
86  r = boost::optional<value_type>(v);
87  }
88  return r;
89  }
90 
91  static handle null_handle() {
92  return handle(false);
93  }
94 
95  private:
96  Heap mHeap;
97 };
98 
99 } // namespace CGAL
100 
101 #endif // CONTRACTION_CGAL_QUEUE_MODIFIABLE_PRIORITY_QUEUE_H_