/usr/include/wibble/list.h is in libwibble-dev 1.1-1.
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 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 | // -*- C++ -*-
#include <memory>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstddef>
#ifndef WIBBLE_LIST_H
#define WIBBLE_LIST_H
namespace wibble {
namespace list {
template< typename List >
struct ListIterator
{
typedef std::forward_iterator_tag iterator_category;
typedef typename List::Type value_type;
typedef ptrdiff_t difference_type;
typedef value_type &pointer;
typedef value_type &reference;
List l;
ListIterator &operator++() {
l = l.tail();
return *this;
}
ListIterator operator++(int) {
ListIterator i = *this;
operator++();
return i;
}
typename List::Type operator*() {
return l.head();
}
bool operator==( const ListIterator &o ) const {
return l.empty() && o.l.empty();
}
bool operator!=( const ListIterator &o ) const {
return !(l.empty() && o.l.empty());
}
ListIterator( List _l = List() ) : l( _l )
{}
};
template< typename List >
struct Sorted
{
typedef std::vector< typename List::Type > Vec;
struct SharedVec {
int refs;
Vec vec;
SharedVec() : refs( 1 ) {}
void _ref() { ++refs; }
void _deref() { --refs; }
};
struct SharedPtr {
SharedVec *vec;
SharedPtr( bool a = false ) { vec = a ? new SharedVec : 0; }
SharedPtr( const SharedPtr &o ) {
vec = o.vec;
if ( vec )
vec->_ref();
}
SharedPtr &operator=( const SharedPtr &o ) {
vec = o.vec;
if ( vec )
vec->_ref();
return *this;
}
operator bool() { return vec; }
Vec &operator*() { return vec->vec; }
Vec *operator->() { return &(vec->vec); }
~SharedPtr() {
if ( vec ) {
vec->_deref();
if ( !vec->refs )
delete vec;
}
}
};
typedef typename List::Type Type;
List m_list;
mutable SharedPtr m_sorted;
size_t m_pos;
void sort() const {
if ( m_sorted )
return;
m_sorted = SharedPtr( true );
std::copy( ListIterator< List >( m_list ), ListIterator< List >(),
std::back_inserter( *m_sorted ) );
std::sort( m_sorted->begin(), m_sorted->end() );
}
Type head() const {
sort();
return (*m_sorted)[ m_pos ];
}
Sorted tail() const {
sort();
Sorted s = *this;
s.m_pos ++;
return s;
}
bool empty() const {
sort();
return m_pos == m_sorted->size();
}
Sorted( const Sorted &o ) : m_list( o.m_list ), m_sorted( o.m_sorted ),
m_pos( o.m_pos ) {}
Sorted &operator=( const Sorted &o ) {
m_sorted = o.m_sorted;
m_list = o.m_list;
m_pos = o.m_pos;
return *this;
}
Sorted( List l = List() ) : m_list( l ), m_sorted( 0 ), m_pos( 0 ) {}
};
template< typename List, typename Predicate >
struct Filtered
{
typedef typename List::Type Type;
mutable List m_list;
Predicate m_pred;
bool empty() const {
seek();
return m_list.empty();
}
Type head() const {
seek();
return m_list.head();
}
void seek() const
{
while ( !m_list.empty() && !m_pred( m_list.head() ) )
m_list = m_list.tail();
}
Filtered tail() const
{
Filtered r = *this;
r.seek();
r.m_list = r.m_list.tail();
return r;
}
Filtered( List l, Predicate p )
: m_list( l ), m_pred( p )
{
}
Filtered() {}
};
template< typename List >
struct Unique
{
typedef typename List::Type Type;
mutable List m_list;
bool empty() const {
seek();
return m_list.empty();
}
Type head() const {
seek();
return m_list.head();
}
void seek() const
{
if ( m_list.empty() )
return;
if ( m_list.tail().empty() )
return;
if ( m_list.head() == m_list.tail().head() ) {
m_list = m_list.tail();
return seek();
}
}
Unique tail() const
{
Unique r = *this;
r.seek();
r.m_list = r.m_list.tail();
return r;
}
Unique( List l = List() )
: m_list( l )
{
}
};
template< typename List >
struct Take {
List l;
int remaining;
typedef typename List::Type Type;
Type head() const {
return l.head();
}
bool empty() const {
return l.empty() || remaining == 0;
}
Take tail() const {
Take t;
t.remaining = remaining - 1;
t.l = l.tail();
return t;
}
Take( List _l, int m ) : l( _l ), remaining( m ) {}
Take() : remaining( 0 ) {}
};
template< typename List, typename F >
struct Map {
List l;
char f_space[ sizeof( F ) ];
F &f() {
return *static_cast<F *>(f_space);
}
const F &f() const {
return *static_cast<const F *>(f_space);
}
typedef typename F::result_type Type;
Type head() const {
return f()( l.head() );
}
Map tail() const {
Map m;
m.l = l.tail();
m.f() = f();
return m;
}
bool empty() const {
return l.empty();
}
Map() {}
Map( const List &_l, const F &_f )
: l( _l )
{
f() = _f;
}
};
template< typename T >
struct Empty {
typedef T Type;
T head() const { return T(); }
bool empty() const { return true; }
Empty tail() const { return Empty(); }
};
template< typename T >
struct Singular {
typedef T Type;
T m_value;
bool m_empty;
Singular() : m_empty( true ) {}
Singular( T i ) : m_value( i ), m_empty( false ) {}
T head() const { return m_value; }
bool empty() const { return m_empty; }
Singular tail() const { return Singular(); }
};
template< typename T1, typename T2 >
struct Append {
typedef typename T1::Type Type;
T1 m_1;
T2 m_2;
Append() {}
Append( T1 a, T2 b ) : m_1( a ), m_2( b ) {}
Type head() const {
if ( m_1.empty() )
return m_2.head();
return m_1.head();
}
bool empty() const { return m_1.empty() && m_2.empty(); }
Append tail() const {
Append t = *this;
if ( !t.m_1.empty() )
t.m_1 = t.m_1.tail();
else
t.m_2 = t.m_2.tail();
return t;
}
};
template< typename X >
Singular< X > singular( const X &x ) {
return Singular< X >( x );
}
template< typename X, typename Y >
Append< X, Y > append( const X &x, const Y &y ) {
return Append< X, Y >( x, y );
}
template< typename List >
size_t count( List l ) {
size_t count = 0;
while ( !l.empty() ) {
l = l.tail();
++ count;
}
return count;
}
#undef foreach // Work around Qt braindamage.
template< typename List, typename F >
void foreach( List l, F f ) {
while ( !l.empty() ) {
f( l.head() );
l = l.tail();
}
}
template< typename List, template< typename > class F >
void foreach( List l, F< typename List::Type > f ) {
while ( !l.empty() ) {
f( l.head() );
l = l.tail();
}
}
template< typename List, typename Pred >
Filtered< List, Pred > filter( List l, Pred p )
{
return Filtered< List, Pred >( l, p );
}
template< typename List, template< typename > class Pred >
Filtered< List, Pred< List > > filter( List l, Pred< List > p )
{
return Filtered< List, Pred< List > >( l, p );
}
template< typename List, typename F >
Map< List, F > map( const List &l, const F &f )
{
return Map< List, F >( l, f );
}
template< typename List >
Sorted< List > sort( List l )
{
return Sorted< List >( l );
}
template< typename List >
Unique< List > unique( List l )
{
return Unique< List >( l );
}
template< typename List >
Take< List > take( int t, List l )
{
return Take< List >( l, t );
}
template< typename List >
List drop( int t, List l )
{
while ( t > 0 ) {
l = l.tail();
-- t;
}
return l;
}
template< typename List, typename Out >
void output( List l, Out it ) {
std::copy( ListIterator< List >( l ), ListIterator< List >(), it );
}
template< typename List >
ListIterator< List > begin( List l ) {
return ListIterator< List >( l );
}
template< typename List >
ListIterator< List > end( List ) {
return ListIterator< List >();
}
}
}
#endif
|