/usr/include/CGAL/Tools/chained_map.h is in libcgal-dev 4.7-4.
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 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 | // Copyright (c) 1997-2000
// Utrecht University (The Netherlands),
// ETH Zurich (Switzerland),
// INRIA Sophia-Antipolis (France),
// Max-Planck-Institute Saarbruecken (Germany),
// and Tel-Aviv University (Israel). 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) : Courtesy of LEDA
#ifndef CGAL_CHAINED_MAP_H
#define CGAL_CHAINED_MAP_H
#include <CGAL/memory.h>
namespace CGAL {
namespace internal {
template <typename T, typename Allocator = CGAL_ALLOCATOR(T) > class chained_map;
template <typename T> class chained_map_elem;
template <typename T>
class chained_map_elem
{
template<typename T2, typename Alloc> friend class chained_map;
std::size_t k; T i;
chained_map_elem<T>* succ;
};
template <typename T, typename Allocator>
class chained_map
{
const std::size_t NULLKEY;
const std::size_t NONNULLKEY;
chained_map_elem<T> STOP;
chained_map_elem<T>* table;
chained_map_elem<T>* table_end;
chained_map_elem<T>* free;
std::size_t table_size;
std::size_t table_size_1;
chained_map_elem<T>* old_table;
chained_map_elem<T>* old_table_end;
chained_map_elem<T>* old_free;
std::size_t old_table_size;
std::size_t old_table_size_1;
std::size_t old_index;
typedef typename Allocator::template rebind<chained_map_elem<T> >::other allocator_type;
allocator_type alloc;
public:
T& xdef() { return STOP.i; }
const T& cxdef() const { return STOP.i; }
private:
void init_inf(T& x) const { x = STOP.i; }
chained_map_elem<T>* HASH(std::size_t x) const
{ return table + (x & table_size_1); }
void init_table(std::size_t t);
void rehash();
void del_old_table();
inline void insert(std::size_t x, T y);
public:
typedef chained_map_elem<T>* chained_map_item;
typedef chained_map_item item;
std::size_t index(chained_map_item it) const { return it->k; }
T& inf(chained_map_item it) const { return it->i; }
chained_map(std::size_t n = 1);
chained_map(const chained_map<T, Allocator>& D);
chained_map& operator=(const chained_map<T, Allocator>& D);
void clear_entries();
void clear();
~chained_map()
{
if (old_table)
{
for (chained_map_item item = old_table ; item != old_table_end ; ++item)
alloc.destroy(item);
alloc.deallocate(old_table, old_table_end - old_table);
}
for (chained_map_item item = table ; item != table_end ; ++item)
alloc.destroy(item);
alloc.deallocate(table, table_end - table);
}
T& access(chained_map_item p, std::size_t x);
T& access(std::size_t x);
chained_map_item lookup(std::size_t x) const;
chained_map_item first_item() const;
chained_map_item next_item(chained_map_item it) const;
void statistics() const;
};
template <typename T, typename Allocator>
inline T& chained_map<T, Allocator>::access(std::size_t x)
{ chained_map_item p = HASH(x);
if (old_table) del_old_table();
if ( p->k == x ) {
old_index = x;
return p->i;
}
else {
if ( p->k == NULLKEY ) {
p->k = x;
init_inf(p->i); // initializes p->i to xdef
old_index = x;
return p->i;
} else
return access(p,x);
}
}
template <typename T, typename Allocator>
void chained_map<T, Allocator>::init_table(std::size_t t)
{
table_size = t;
table_size_1 = t-1;
table = alloc.allocate(t + t/2);
for (std::size_t i = 0 ; i < t + t/2 ; ++i)
alloc.construct(table + i, chained_map_elem<T>());
free = table + t;
table_end = table + t + t/2;
for (chained_map_item p = table; p < free; p++)
{ p->succ = &STOP;
p->k = NULLKEY;
}
table->k = NONNULLKEY;
}
template <typename T, typename Allocator>
inline void chained_map<T, Allocator>::insert(std::size_t x, T y)
{ chained_map_item q = HASH(x);
if ( q->k == NULLKEY ) {
q->k = x;
q->i = y;
} else {
free->k = x;
free->i = y;
free->succ = q->succ;
q->succ = free++;
}
}
template <typename T, typename Allocator>
void chained_map<T, Allocator>::rehash()
{
old_table = table;
old_table_end = table_end;
old_table_size = table_size;
old_table_size_1 = table_size_1;
old_free = free;
chained_map_item old_table_mid = table + table_size;
init_table(2*table_size);
chained_map_item p;
for(p = old_table + 1; p < old_table_mid; p++)
{ std::size_t x = p->k;
if ( x != NULLKEY ) // list p is non-empty
{ chained_map_item q = HASH(x);
q->k = x;
q->i = p->i;
}
}
while (p < old_table_end)
{ std::size_t x = p->k;
insert(x,p->i);
p++;
}
}
template <typename T, typename Allocator>
void chained_map<T, Allocator>::del_old_table()
{
chained_map_item save_table = table;
chained_map_item save_table_end = table_end;
chained_map_item save_free = free;
std::size_t save_table_size = table_size;
std::size_t save_table_size_1 = table_size_1;
table = old_table;
table_end = old_table_end;
table_size = old_table_size;
table_size_1 = old_table_size_1;
free = old_free;
old_table = 0;
T p = access(old_index);
for (chained_map_item item = table ; item != table_end ; ++item)
alloc.destroy(item);
alloc.deallocate(table, table_end - table);
table = save_table;
table_end = save_table_end;
table_size = save_table_size;
table_size_1 = save_table_size_1;
free = save_free;
access(old_index) = p;
}
template <typename T, typename Allocator>
T& chained_map<T, Allocator>::access(chained_map_item p, std::size_t x)
{
STOP.k = x;
chained_map_item q = p->succ;
while (q->k != x) q = q->succ;
if (q != &STOP)
{ old_index = x;
return q->i;
}
// index x not present, insert it
if (free == table_end) // table full: rehash
{ rehash();
p = HASH(x);
}
if (p->k == NULLKEY)
{ p->k = x;
init_inf(p->i); // initializes p->i to xdef
return p->i;
}
q = free++;
q->k = x;
init_inf(q->i); // initializes q->i to xdef
q->succ = p->succ;
p->succ = q;
return q->i;
}
template <typename T, typename Allocator>
chained_map<T, Allocator>::chained_map(std::size_t n) :
NULLKEY(0), NONNULLKEY(1), old_table(0)
{
if (n < 512)
init_table(512);
else {
std::size_t ts = 1;
while (ts < n) ts <<= 1;
init_table(ts);
}
}
template <typename T, typename Allocator>
chained_map<T, Allocator>::chained_map(const chained_map<T, Allocator>& D) :
NULLKEY(0), NONNULLKEY(1), old_table(0)
{
init_table(D.table_size);
STOP.i = D.STOP.i; // xdef
for(chained_map_item p = D.table + 1; p < D.free; p++)
{ if (p->k != NULLKEY || p >= D.table + D.table_size)
{ insert(p->k,p->i);
//D.copy_inf(p->i); // see chapter Implementation
}
}
}
template <typename T, typename Allocator>
chained_map<T, Allocator>& chained_map<T, Allocator>::operator=(const chained_map<T, Allocator>& D)
{
clear_entries();
for (chained_map_item item = table ; item != table_end ; ++item)
alloc.destroy(item);
alloc.deallocate(table, table_end - table);
init_table(D.table_size);
STOP.i = D.STOP.i; // xdef
for(chained_map_item p = D.table + 1; p < D.free; p++)
{ if (p->k != NULLKEY || p >= D.table + D.table_size)
{ insert(p->k,p->i);
//copy_inf(p->i); // see chapter Implementation
}
}
return *this;
}
template <typename T, typename Allocator>
void chained_map<T, Allocator>::clear_entries()
{ for(chained_map_item p = table + 1; p < free; p++)
if (p->k != NULLKEY || p >= table + table_size)
p->i = T();
}
template <typename T, typename Allocator>
void chained_map<T, Allocator>::clear()
{
clear_entries();
for (chained_map_item item = table ; item != table_end ; ++item)
alloc.destroy(item);
alloc.deallocate(table, table_end - table);
init_table(512);
}
template <typename T, typename Allocator>
typename chained_map<T, Allocator>::chained_map_item
chained_map<T, Allocator>::lookup(std::size_t x) const
{ chained_map_item p = HASH(x);
((std::size_t &)STOP.k) = x; // cast away const
while (p->k != x)
{ p = p->succ; }
return (p == &STOP) ? 0 : p;
}
template <typename T, typename Allocator>
typename chained_map<T, Allocator>::chained_map_item
chained_map<T, Allocator>::first_item() const
{ return next_item(table); }
template <typename T, typename Allocator>
typename chained_map<T, Allocator>::chained_map_item
chained_map<T, Allocator>::next_item(chained_map_item it) const
{ if (it == 0) return 0;
do it++; while (it < table + table_size && it->k == NULLKEY);
return (it < free ? it : 0);
}
template <typename T, typename Allocator>
void chained_map<T, Allocator>::statistics() const
{ std::cout << "table_size: " << table_size <<"\n";
std::size_t n = 0;
for (chained_map_item p = table + 1; p < table + table_size; p++)
if (p ->k != NULLKEY) n++;
std::size_t used_in_overflow = free - (table + table_size );
n += used_in_overflow;
std::cout << "number of entries: " << n << "\n";
std::cout << "fraction of entries in first position: " <<
((double) (n - used_in_overflow))/n <<"\n";
std::cout << "fraction of empty lists: " <<
((double) (n - used_in_overflow))/table_size<<"\n";
}
} // namespace internal
} //namespace CGAL
#endif // CGAL_CHAINED_MAP_H
|