/usr/include/dballe/core/stlutils.h is in libdballe-dev 6.8-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 | /*
* core/stlutils - Useful functions to work with the STL
*
* Copyright (C) 2013 ARPA-SIM <urpsim@smr.arpa.emr.it>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Enrico Zini <enrico@enricozini.com>
*/
#ifndef DBA_CORE_STLUTILS_H
#define DBA_CORE_STLUTILS_H
#include <vector>
#include <set>
#include <memory>
namespace dballe {
namespace stl {
namespace stlutils {
template<typename T>
struct Sequence
{
virtual ~Sequence() {}
virtual bool valid() const = 0;
virtual const T& get() const = 0;
virtual void next() = 0;
};
} // back to dballe::stl
/// List of ranges
template<typename T>
struct Sequences : public std::vector<stlutils::Sequence<T>*>
{
typedef typename std::vector<stlutils::Sequence<T>*>::iterator iterator;
typedef typename std::vector<stlutils::Sequence<T>*>::const_iterator const_iterator;
Sequences() {}
~Sequences()
{
for (iterator i = this->begin(); i != this->end(); ++i)
delete *i;
}
/// Add a singleton set
void add_singleton(const T& val);
// Add a begin-end range
template<typename ITER>
void add(const ITER& begin, const ITER& end);
// Add a container's begin()-end() range
template<typename C>
void add(const C& container);
void add(std::auto_ptr< stlutils::Sequence<T> >& sequence);
// Add the union of the given sequences
void add_union(std::auto_ptr< Sequences<T> >& sequences);
// Add the union of the given sequences
void add_intersection(std::auto_ptr< Sequences<T> >& sequences);
private:
Sequences(const Sequences&);
Sequences& operator=(const Sequences&);
};
namespace stlutils {
template<typename T>
struct SequenceGenerator
{
Sequences<T>* sequences;
SequenceGenerator() : sequences(0) {}
SequenceGenerator(const SequenceGenerator<T>& sg) : sequences(sg.sequences)
{
// auto_ptr semantics
const_cast<SequenceGenerator<T>*>(&sg)->sequences = 0;
}
SequenceGenerator(std::auto_ptr< Sequences<T> >& sequences) : sequences(sequences.release()) {}
~SequenceGenerator() { if (sequences) delete sequences; }
SequenceGenerator<T>& operator=(const SequenceGenerator<T>& sg)
{
// auto_ptr semantics
if (&sg == this) return this;
if (sequences) delete sequences;
sequences = sg.sequences;
const_cast<SequenceGenerator<T>*>(&sg)->sequences = 0;
return *this;
}
void clear()
{
if (sequences) delete sequences;
sequences = 0;
}
bool has_items() const { return sequences != 0; }
bool operator==(const SequenceGenerator<T>& i) const
{
if (sequences == i.sequences) return true;
if (!sequences || !i.sequences) return false;
return *sequences == *i.sequences;
}
bool operator!=(const SequenceGenerator<T>& i) const
{
if (sequences == i.sequences) return false;
if (!sequences || !i.sequences) return true;
return *sequences != *i.sequences;
}
};
template<typename T>
struct Itersection : public SequenceGenerator<T>, public std::iterator<std::forward_iterator_tag, T>
{
Itersection() {}
Itersection(const Itersection<T>& sg) : SequenceGenerator<T>(sg) {}
Itersection(std::auto_ptr< Sequences<T> >& sequences)
: SequenceGenerator<T>(sequences)
{
sync_iters();
}
const T& get() const { return (*this->sequences)[0]->get(); }
void advance()
{
(*this->sequences)[0]->next();
sync_iters();
}
// Advance iterators so that they all point to items of the same value,
// or so that we become the end iterator
void sync_iters();
const T& operator*() const { return this->get(); }
Itersection<T>& operator++()
{
this->advance();
return *this;
}
};
template<typename T>
struct Iterunion : public SequenceGenerator<T>, public std::iterator<std::forward_iterator_tag, T>
{
const T* minval;
Iterunion() {}
Iterunion(const Iterunion<T>& sg)
: SequenceGenerator<T>(sg), minval(sg.minval)
{
// auto_ptr semantics
const_cast<Iterunion<T>*>(&sg)->minval = 0;
}
Iterunion(std::auto_ptr< Sequences<T> >& sequences)
: SequenceGenerator<T>(sequences), minval(0)
{
find_min();
}
Iterunion<T>& operator=(const Iterunion<T>& sg)
{
// auto_ptr semantics
SequenceGenerator<T>::operator=(sg);
minval = sg.minval;
const_cast<Iterunion<T>*>(&sg)->minval = 0;
return *this;
}
const T& get() const { return *minval; }
/**
* Find the next minimum value.
*
* If minval is 0, set it to the minimum value.
* If minval points to an element, advance all sequences that have that
* element as minimum value, and set minval to the next minimum value.
*
* Returns false when all sequences are exausted; true if a new minimum
* value was found.
*/
void find_min();
const T& operator*() const { return this->get(); }
Iterunion<T>& operator++()
{
this->find_min();
return *this;
}
};
}
/**
* Virtual container containing the intersection of an arbitrary number of
* sorted (begin, end) sequences.
*/
template<class T>
class Intersection
{
public:
typedef stlutils::Itersection<T> const_iterator;
const_iterator begin(std::auto_ptr< Sequences<T> >& sequences) const
{
return const_iterator(sequences);
}
const_iterator end() const
{
return const_iterator();
}
};
template<typename T>
class SetIntersection
{
protected:
std::vector<const std::set<T>*> sets;
Intersection<T> intersection;
public:
void add(const std::set<T>& set)
{
sets.push_back(&set);
}
typedef typename Intersection<T>::const_iterator const_iterator;
const_iterator begin()
{
std::auto_ptr< Sequences<T> > sequences(new Sequences<T>);
// Look for the highest first element in all sets
bool first = true;
T max_of_first;
for (typename std::vector<const std::set<T>*>::const_iterator i = sets.begin();
i != sets.end(); ++i)
{
const std::set<T>& s = **i;
// If one of the sets is empty, then the intersection is empty
if (s.begin() == s.end()) return end();
if (first)
{
max_of_first = *s.begin();
first = false;
} else {
if (max_of_first < *s.begin())
max_of_first = *s.begin();
}
}
// Populate intersection with all the ranges we intersect
for (typename std::vector<const std::set<T>*>::const_iterator i = sets.begin();
i != sets.end(); ++i)
{
const std::set<T>& s = **i;
sequences->add(s.lower_bound(max_of_first), s.end());
}
return intersection.begin(sequences);
}
const_iterator end()
{
return intersection.end();
}
};
/**
* Virtual container containing the union of an arbitrary number of
* sorted (begin, end) sequences.
*/
template<class T>
class Union
{
public:
typedef stlutils::Iterunion<T> const_iterator;
const_iterator begin(std::auto_ptr< Sequences<T> >& sequences) const
{
return const_iterator(sequences);
}
const_iterator end() const
{
return const_iterator();
}
};
/**
* Similar to std::inserter, but just calls target.insert() without requiring
* it to have iterators at all.
*/
template<typename T>
class TrivialInserter : public std::iterator<std::output_iterator_tag, void, void, void, void>
{
protected:
T* target;
public:
TrivialInserter(T& target) : target(&target) {}
template<typename V>
V operator=(V val) { target->insert(val); return val; }
TrivialInserter& operator*() { return *this; }
TrivialInserter& operator++() { return *this; }
TrivialInserter& operator++(int) { return *this; }
};
template<typename T>
TrivialInserter<T> trivial_inserter(T& target)
{
return TrivialInserter<T>(target);
}
/**
* Similar to std::inserter, but just calls target.insert() without requiring
* it to have iterators at all.
*/
template<typename T>
class Pusher : public std::iterator<std::output_iterator_tag, void, void, void, void>
{
protected:
T* target;
public:
Pusher(T& target) : target(&target) {}
template<typename V>
V operator=(V val) { target->push(val); return val; }
Pusher& operator*() { return *this; }
Pusher& operator++() { return *this; }
Pusher& operator++(int) { return *this; }
};
template<typename T>
Pusher<T> pusher(T& target)
{
return Pusher<T>(target);
}
/**
* Similar to std::inserter, but just calls target.insert() without requiring
* it to have iterators at all.
*/
template<typename T>
class Eraser : public std::iterator<std::output_iterator_tag, void, void, void, void>
{
protected:
T* target;
public:
Eraser(T& target) : target(&target) {}
template<typename V>
V operator=(V val) { target->erase(val); return val; }
Eraser& operator*() { return *this; }
Eraser& operator++() { return *this; }
Eraser& operator++(int) { return *this; }
};
template<typename T>
Eraser<T> eraser(T& target)
{
return Eraser<T>(target);
}
}
}
#endif
|