/usr/include/CGAL/Skiplist.h is in libcgal-dev 4.11-2build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | // Copyright (c) 2012 GeometryFactory (France). All rights reserved.
// All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 3 of the License,
// or (at your option) any later version.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL:
// $Id:
//
// Author(s) : Philipp Moeller
#ifndef CGAL_SKIPLIST_H
#define CGAL_SKIPLIST_H
#include <boost/intrusive/list.hpp>
#include <boost/range/iterator_range.hpp>
#include <boost/iterator/iterator_adaptor.hpp>
namespace CGAL {
/// The skiplist maintains two iterator ranges on the set of elements:
/// the all view and the skip view. The all view at all times contains
/// all elements in the Skiplist. The skip view can be modified and
/// items can be removed from it.
///
/// insert will add elements to the all_view.
///
/// @tparam T the value_type to store in the Skiplist
template<typename T>
class Skiplist
{
public:
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
private:
struct Node {
explicit Node(const T& t) : t_(t) {}
const T& get() const { return t_; }
T& get() { return t_; }
private:
value_type t_;
public:
boost::intrusive::list_member_hook<> skip_hook;
boost::intrusive::list_member_hook<> all_hook;
};
struct Node_disposer {
void operator()(Node* p) const { delete p; }
};
typedef boost::intrusive::member_hook<
Node,
boost::intrusive::list_member_hook<>,
&Node::skip_hook>
SkipOption;
typedef boost::intrusive::member_hook<
Node,
boost::intrusive::list_member_hook<>,
&Node::all_hook>
AllOption;
typedef boost::intrusive::list<Node, SkipOption> skip_list;
typedef boost::intrusive::list<Node, AllOption> all_list;
public:
// assume both lists have the same size_type/ptr_diff
typedef typename all_list::size_type size_type;
typedef typename all_list::difference_type difference_type;
struct all_iterator :
public boost::iterator_adaptor<
all_iterator
, typename all_list::iterator
, T
>
{
public:
all_iterator() {}
all_iterator(typename all_list::iterator it)
: all_iterator::iterator_adaptor_(it) {}
private:
friend class boost::iterator_core_access;
T& dereference() const { return this->base()->get(); }
};
struct skip_iterator :
public boost::iterator_adaptor<
skip_iterator
, typename skip_list::iterator
, T
>
{
public:
skip_iterator() {}
skip_iterator(typename skip_list::iterator it)
: skip_iterator::iterator_adaptor_(it) {}
operator all_iterator() const { return all_list::s_iterator_to(*this->base()); }
private:
friend class boost::iterator_core_access;
T& dereference() const { return this->base()->get(); }
};
typedef boost::iterator_range<all_iterator> all_range;
typedef boost::iterator_range<skip_iterator> skip_range;
Skiplist() {}
~Skiplist()
{
clear();
}
/// Construct a Skiplist from the range [begin,end)
/// @postcond Both views are equal to the range [begin,end)
template<typename InputIterator>
Skiplist(InputIterator begin, InputIterator end)
{
while(begin != end) {
push_back(*begin++);
}
}
/// The semantics of front and back try to be consistent with
/// push_back and push_front. Questionable.
/// Returns front of the skiplist
const_reference
front() const { return skip_.front().get(); }
/// Returns front of the skiplist
reference
front() { return skip_.front().get(); }
/// Returns back of the skiplist
const_reference
back() const { return skip_.back().get(); }
/// Returns back of the skiplist
reference
back() { return skip_.back().get(); }
all_iterator all_begin()
{
return all_.begin();
}
all_iterator all_end()
{
return all_.end();
}
skip_iterator skip_begin()
{
return skip_.begin();
}
skip_iterator skip_end()
{
return skip_.end();
}
all_range
all_elements()
{
return boost::make_iterator_range(all_begin(), all_end());
}
skip_range
skip_elements()
{
return boost::make_iterator_range(skip_begin(), skip_end());
}
/// The elements pointed to by it are no longer in the range
/// [skip_begin(), skip_end()).
void skip(skip_iterator it)
{
skip_.erase(it.base());
}
/// The elements pointed to by it are no longer in the range
/// [skip_begin(), skip_end()).
///
/// @precond it and to_skip(it) are valid iterators
///
void skip(all_iterator it)
{
skip_.erase(skip_.iterator_to((*it.base())));
}
/// The elements pointed to by [begin, end) are no longer in the
/// range [skip_begin(), skip_end()). If an element in [begin, end)
/// already is removed from the skip view, it will stay removed.
///
/// @precond [begin,end) is a slice of the range [all_begin(), all_end())
///
void skip(all_iterator begin, all_iterator end)
{
if(end == all_end()) {
skip_.erase(skip_.iterator_to(*begin.base()), skip_.end());
} else {
skip_.erase(skip_.iterator_to(*begin.base()), skip_.iterator_to(*end.base()));
}
}
void unskip(skip_iterator pos, all_iterator it)
{
skip_.insert(pos.base(), *(it.base()));
}
/// Check if an all_iterator has been skipped.
///
/// @param it a valid all_iterator
///
/// @return true if the element pointed to by it is not in the range [skip_begin(), skip_end())
bool is_skipped(all_iterator it) const
{
return !(it.base()->skip_hook.is_linked());
}
/// Adds an element to the end of both views in Skiplist.
void push_back(const value_type& t)
{
all_.push_back(*new Node(t));
skip_.push_back(all_.back());
}
/// Adds an element to the front of both views in Skiplist.
void push_front(const value_type& t)
{
all_.push_front(*new Node(t));
skip_.push_front(all_.front());
}
void pop_back()
{
all_.pop_back();
skip_.pop_back();
}
/// Insert \c t before \c pos in the all_view. \t will not be inserted into the skip view.
/// @returns an skip_iterator to the inserted element.
all_iterator insert(all_iterator pos, const value_type& t)
{
return all_.insert(pos.base(), *new Node(t));
}
/// Insert \c t before \c pos in the all_view. \t will be inserted into the skip view.
/// @returns an iterator to the inserted element.
skip_iterator insert(skip_iterator pos, const value_type& t)
{
all_iterator it = insert(static_cast<all_iterator>(pos), t);
return skip_.insert(pos.base(), *it.base());
}
/// Insert the range [begin,end) into the all view. If the container
/// is empty() the range will also be visible in the skip view.
template<typename InputIterator>
void insert(all_iterator pos, InputIterator begin, InputIterator end)
{
if(all_.empty()) {
while(begin != end) {
push_back(*begin++);
}
} else {
while(begin != end) {
pos = insert(pos, *begin++);
}
}
}
/// Drop the contents of iterator \c it from both views.
all_iterator erase(all_iterator it)
{
if(!is_skipped(it)) {
skip_.erase(skip_.iterator_to(*it.base()));
}
return all_.erase_and_dispose(it.base(), Node_disposer());
}
void splice(skip_iterator pos, Skiplist& other,
skip_iterator first, skip_iterator last)
{
all_iterator alllast = last == other.skip_end() ?
other.all_.end() : other.all_.iterator_to(*last.base());
all_iterator allpos = pos == skip_end() ?
all_end() : all_.iterator_to(*pos.base());
all_.splice(allpos.base(), other.all_,
other.all_.iterator_to(*first.base()), alllast.base());
skip_.splice(pos.base(), other.skip_, first.base(), last.base());
}
size_type
all_size() const { return all_.size(); }
size_type
skip_size() const { return skip_.size(); }
bool empty() const { return all_.empty(); }
void swap(Skiplist& other)
{
using std::swap;
this->all_.swap(other.all_);
this->skip_.swap(other.skip_);
}
/// Reset the container.
/// @postcond *this.empty() == true
void clear()
{
skip_.clear();
all_.clear_and_dispose(Node_disposer());
}
private:
all_list all_;
skip_list skip_;
};
template<typename T>
void swap(Skiplist<T>& a, Skiplist<T>& b)
{
a.swap(b);
}
} // CGAL
#endif /* CGAL_SKIPLIST_H */
|