File: Modifiable_priority_queue.h

package info (click to toggle)
cgal 6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 144,912 kB
  • sloc: cpp: 810,858; ansic: 208,477; sh: 493; python: 411; makefile: 286; javascript: 174
file content (275 lines) | stat: -rw-r--r-- 7,668 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2006-2011  GeometryFactory (France). All rights reserved.
//
// This file is part of CGAL (www.cgal.org)
//
// $URL: https://github.com/CGAL/cgal/blob/v6.1/STL_Extension/include/CGAL/Modifiable_priority_queue.h $
// $Id: include/CGAL/Modifiable_priority_queue.h b26b07a1242 $
// SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-Commercial
//
// Author(s)     : Fernando Cacciola <fernando.cacciola@geometryfactory.com>
//
#ifndef CGAL_MODIFIABLE_PRIORITY_QUEUE_H
#define CGAL_MODIFIABLE_PRIORITY_QUEUE_H

#include <climits> // Needed by the following Boost header for CHAR_BIT.
#include <optional>

#include <CGAL/STL_Extension/internal/boost/relaxed_heap.hpp>
#include <CGAL/STL_Extension/internal/boost/mutable_queue.hpp>

#include <boost/heap/pairing_heap.hpp>

#include <algorithm>
#include <type_traits>

namespace CGAL {

enum Heap_type { CGAL_BOOST_PAIRING_HEAP, CGAL_BOOST_PENDING_MUTABLE_QUEUE, CGAL_BOOST_PENDING_RELAXED_HEAP };

template <class IndexedType_
         ,class Compare_ = std::less<IndexedType_>
         ,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<heap_type == CGAL_BOOST_PENDING_MUTABLE_QUEUE,
                             internal::boost_::mutable_queue<IndexedType,std::vector<IndexedType>,Compare,ID>,
                             internal::boost_::relaxed_heap<IndexedType,Compare,ID> > 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); }

  const 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) ; }

  std::optional<value_type> extract_top()
  {
    std::optional<value_type> r ;
    if ( !empty() )
    {
      value_type v = top();
      pop();
      r = std::optional<value_type>(v) ;
    }
    return r ;
  }

  value_type top_and_pop()
  {
    CGAL_precondition(!empty());
    value_type v = top();
    pop();
    return v;
  }

private:

  Heap mHeap ;

} ;

template <class IndexedType_
         ,class Compare_
         ,class ID_>
struct Modifiable_priority_queue<IndexedType_, Compare_, ID_, CGAL_BOOST_PAIRING_HEAP>
{
  typedef Modifiable_priority_queue Self;

  typedef IndexedType_ IndexedType;
  typedef Compare_     Compare;
  typedef ID_          ID;

  struct Reverse_compare
  {
    Compare c;
    Reverse_compare(const Compare& c):c(c){}
    bool operator() (const IndexedType& a, const IndexedType& b) const
    {
      return !c(a,b);
    }
  };

  // reference time (SMS Iphigenia, keeping 0.05% of edges, all default parameters + Surface_mesh): 12045ms
  // --
  // boost::heap::priority_queue is ummutable and cannot be used
  // --
  // typedef boost::heap::d_ary_heap<IndexedType, boost::heap::arity<2>, boost::heap::compare<Reverse_compare>, boost::heap::mutable_<true> > Heap; //(15291ms)
  // typedef boost::heap::d_ary_heap<IndexedType, boost::heap::arity<3>, boost::heap::compare<Reverse_compare>, boost::heap::mutable_<true> > Heap; //(14351ms)
  // typedef boost::heap::d_ary_heap<IndexedType, boost::heap::arity<4>, boost::heap::compare<Reverse_compare>, boost::heap::mutable_<true> > Heap; //(13869ms)
  // typedef boost::heap::d_ary_heap<IndexedType, boost::heap::arity<5>, boost::heap::compare<Reverse_compare>, boost::heap::mutable_<true> > Heap; //(13879ms)
  // typedef boost::heap::d_ary_heap<IndexedType, boost::heap::arity<6>, boost::heap::compare<Reverse_compare>, boost::heap::mutable_<true> > Heap; //(13881ms)
  // --
  //typedef boost::heap::binomial_heap<IndexedType, boost::heap::compare<Reverse_compare>> Heap; //(16216ms)
  // --
  // typedef boost::heap::fibonacci_heap<IndexedType, boost::heap::compare<Reverse_compare>> Heap; // (13523ms)
  // --
  typedef boost::heap::pairing_heap<IndexedType, boost::heap::compare<Reverse_compare>> Heap; // (12174ms)
  // --
  // typedef boost::heap::skew_heap<IndexedType, boost::heap::compare<Reverse_compare>, boost::heap::mutable_<true>> Heap; //(17957ms)
  // --

  typedef typename Heap::value_type value_type;
  typedef typename Heap::size_type  size_type;
  typedef typename Heap::handle_type handle_type;

private:
  void reserve_impl(size_type r, std::true_type)
  {
    mHeap.reserve(r);
  }

  void reserve_impl(size_type, std::false_type)
  {}

public:
  Modifiable_priority_queue(size_type largest_ID,
                            const Compare& c = Compare(),
                            const ID& id = ID())
    : mHeap(Reverse_compare(c))
    , mID(id)
    , mHandles(largest_ID, handle_type())
  {
    reserve(largest_ID);
  }

  Modifiable_priority_queue(const Modifiable_priority_queue&) = delete;
  Modifiable_priority_queue& operator=(const Modifiable_priority_queue&) = delete;

public:
  handle_type push(const value_type& v)
  {
    CGAL_precondition(!contains(v));
    auto vid = get(mID, v);
    mHandles[vid] = mHeap.push(v);
    return mHandles[vid];
  }

  handle_type resize_and_push(const value_type& v)
  {
    auto vid = get(mID, v);
    CGAL_precondition(0 <= vid);

    if(vid >= mHandles.size())
    {
      mHandles.resize(vid + 1, handle_type());
//      std::cout << "resize() to " << mHandles.size() << " (capacity = " << mHandles.capacity() << ")" << std::endl;
    }

    CGAL_precondition(!contains(v));
    mHandles[vid] = mHeap.push(v);
    return mHandles[vid];
  }

  void update(const value_type& v)
  {
    CGAL_precondition(contains(v));
    auto vid = get(mID, v);
    mHeap.update(mHandles[vid]);
  }

  void erase(const value_type& v)
  {
    CGAL_precondition(contains(v));
    auto vid = get(mID, v);
    mHeap.erase(mHandles[vid]);
    mHandles[vid] = handle_type();
  }

  const value_type& top() const
  {
    return mHeap.top();
  }

  void pop()
  {
    auto vid = get(mID, top());
    mHeap.pop();
    mHandles[vid] = handle_type();
  }

  size_type size() const { return mHeap.size(); }

  bool empty() const { return mHeap.empty(); }

  void clear()
  {
    std::fill(std::begin(mHandles), std::end(mHandles), handle_type());
    mHeap.clear();
  }

  bool contains(const value_type& v)
  {
    auto vid = get(mID, v);
    CGAL_precondition(0 <= vid && vid < mHandles.size());
    return mHandles[vid] != handle_type();
  }

  bool contains_with_bounds_check(const value_type& v)
  {
    auto vid = get(mID, v);
    if(vid >= mHandles.size())
      return false;

    CGAL_precondition(0 <= vid && vid < mHandles.size());
    return (mHandles[vid] != handle_type());
  }

  std::optional<value_type> extract_top()
  {
    std::optional<value_type> r;
    if(!empty())
    {
      value_type v = top();
      pop();
      r = std::optional<value_type>(v);
    }

    return r;
  }

  value_type top_and_pop()
  {
    CGAL_precondition(!empty());
    value_type v = top();
    pop();
    return v;
  }

  void reserve(size_type r)
  {
    reserve_impl(r, std::integral_constant<bool, Heap::has_reserve>());
  }

private:
  Heap mHeap;
  ID mID;
  std::vector<handle_type> mHandles;
};

} // namespace CGAL

#endif // CGAL_MODIFIABLE_PRIORITY_QUEUE_H