/usr/include/polymake/RandomSubset.h is in libpolymake-dev-common 3.2r2-3.
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-2018
Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
http://www.polymake.org
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, or (at your option) any
later version: http://www.gnu.org/licenses/gpl.txt.
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.
--------------------------------------------------------------------------------
*/
#ifndef POLYMAKE_RANDOM_SUBSET_H
#define POLYMAKE_RANDOM_SUBSET_H
#include "polymake/IndexedSubset.h"
#include "polymake/RandomGenerators.h"
#include "polymake/vector"
#include <stdexcept>
namespace pm {
template <typename Iterator>
class RandomSubset_iterator : public Iterator {
typedef Iterator base_t;
public:
typedef forward_iterator_tag iterator_category;
typedef RandomSubset_iterator<typename iterator_traits<Iterator>::iterator>
iterator;
typedef RandomSubset_iterator<typename iterator_traits<Iterator>::const_iterator>
const_iterator;
RandomSubset_iterator() {}
RandomSubset_iterator(const iterator& it)
: base_t(static_cast<const typename iterator::base_t&>(it))
, rg(it.rg)
, k(it.k) {}
template <typename SourceIterator, typename suitable=typename suitable_arg_for_iterator<SourceIterator, Iterator>::type>
RandomSubset_iterator(const SourceIterator& cur_arg, int n_arg, int k_arg, const SharedRandomState& random_arg)
: base_t(prepare_iterator_arg<Iterator>(cur_arg))
, rg(n_arg, random_arg)
, k(k_arg)
{
toss(typename iterator_traits<Iterator>::iterator_category(), 0);
}
template <typename SourceIterator, typename enable=typename std::enable_if<is_const_compatible_with<SourceIterator, Iterator>::value>::type>
RandomSubset_iterator(const SourceIterator& end_arg, const SharedRandomState& random_arg)
: base_t(end_arg)
, rg(0,random_arg)
, k(0) {}
RandomSubset_iterator& operator++()
{
--k; --(rg.upper_limit());
toss(typename iterator_traits<Iterator>::iterator_category(), 1);
return *this;
}
RandomSubset_iterator operator++ (int) { RandomSubset_iterator copy=*this; operator++(); return copy; }
bool at_end() const { return k==0; }
protected:
void toss(input_iterator_tag, int incr)
{
if (incr) base_t::operator++();
while (rg.upper_limit() > 0 && rg.get() >= k) {
--(rg.upper_limit());
base_t::operator++();
}
}
void toss(random_access_iterator_tag, int incr)
{
while (rg.upper_limit() > 0 && rg.get() >= k) {
--(rg.upper_limit()); ++incr;
}
base_t::operator+=(incr);
}
UniformlyRandomRanged<long> rg;
long k;
template <typename> friend class RandomSubset_iterator;
private:
// shadow possible base_t:: operators
void operator--();
void operator+=(int);
void operator-=(int);
void operator+(int);
void operator-(int);
void operator[](int);
};
template <typename Iterator>
struct check_iterator_feature<RandomSubset_iterator<Iterator>, end_sensitive> : std::true_type {};
template <typename ContainerRef>
class RandomSubset
: public generic_of_subset<RandomSubset<ContainerRef>, typename deref<ContainerRef>::type> {
public:
typedef typename deref<ContainerRef>::minus_ref base_type;
typedef typename container_traits<ContainerRef>::value_type value_type;
typedef typename container_traits<ContainerRef>::reference reference;
typedef typename container_traits<ContainerRef>::const_reference const_reference;
RandomSubset() {}
RandomSubset(typename alias<ContainerRef>::arg_type base_arg, long k_arg, const RandomSeed& seed=RandomSeed())
: base(base_arg), random_source(seed), k(k_arg)
{
if (POLYMAKE_DEBUG) {
if (k<0 || k>get_base().size())
throw std::runtime_error("RandomSubset constructor - invalid size");
}
}
RandomSubset(typename alias<ContainerRef>::arg_type base_arg, long k_arg, const SharedRandomState& s)
: base(base_arg), random_source(s), k(k_arg)
{
if (POLYMAKE_DEBUG) {
if (k<0 || k>get_base().size())
throw std::runtime_error("RandomSubset constructor - invalid size");
}
}
typedef RandomSubset_iterator<typename container_traits<ContainerRef>::iterator> iterator;
typedef RandomSubset_iterator<typename container_traits<ContainerRef>::const_iterator> const_iterator;
iterator begin()
{
return iterator(get_base().begin(), get_base().size(), k, random_source);
}
iterator end()
{
return iterator(get_base().end());
}
const_iterator begin() const
{
return const_iterator(get_base().begin(), get_base().size(), k, random_source);
}
const_iterator end() const
{
return const_iterator(get_base().end());
}
long size() const { return k; }
bool empty() const { return k==0; }
protected:
alias<ContainerRef> base;
UniformlyRandom<long> random_source;
long k;
typename alias<ContainerRef>::reference get_base() { return *base; }
typename alias<ContainerRef>::const_reference get_base() const { return *base; }
};
template <typename ContainerRef>
struct spec_object_traits< RandomSubset<ContainerRef> >
: spec_object_traits<is_container> {
static const bool is_temporary=true,
is_always_const=effectively_const<ContainerRef>::value;
};
class RandomPermutation_iterator {
public:
typedef forward_iterator_tag iterator_category;
typedef int value_type;
typedef const int& reference;
typedef const int* pointer;
typedef ptrdiff_t difference_type;
typedef RandomPermutation_iterator iterator;
typedef RandomPermutation_iterator const_iterator;
RandomPermutation_iterator()
: rg(0) {}
RandomPermutation_iterator(const sequence& start_arg, const SharedRandomState& random_arg)
: perm_index(start_arg.begin(), start_arg.end()), rg(start_arg.size(), random_arg)
{
if (!at_end()) toss();
}
explicit RandomPermutation_iterator(const SharedRandomState& random_arg)
: rg(0, random_arg) {}
reference operator* () const { return perm_index.back(); }
iterator& operator++()
{
perm_index.pop_back();
--(rg.upper_limit());
if (!at_end()) toss();
return *this;
}
const iterator operator++ (int) { iterator copy=*this; operator++(); return copy; }
bool at_end() const { return perm_index.empty(); }
bool operator== (const iterator& it) const
{
return perm_index.size() == it.perm_index.size() &&
std::equal(perm_index.begin(), perm_index.end(), it.perm_index.begin());
}
bool operator!= (const iterator& it) const { return !operator==(it); }
protected:
void toss()
{
const int i=rg.get();
std::swap(perm_index[i], perm_index.back());
}
std::vector<int> perm_index;
UniformlyRandomRanged<long> rg;
};
template <>
struct check_iterator_feature<RandomPermutation_iterator, end_sensitive> : std::true_type {};
template <typename ContainerRef=sequence,
bool _direct=identical_minus_const_ref<ContainerRef, sequence>::value>
class RandomPermutation {
public:
typedef int value_type;
typedef const int& reference;
typedef reference const_reference;
explicit RandomPermutation(const sequence& base_arg, const RandomSeed& seed=RandomSeed())
: base(base_arg), random_source(seed) {}
RandomPermutation(const sequence& base_arg, const SharedRandomState& s)
: base(base_arg), random_source(s) {}
explicit RandomPermutation(int n, const RandomSeed& seed=RandomSeed())
: base(0,n), random_source(seed) {}
RandomPermutation(int n, const SharedRandomState& random_arg)
: base(0,n), random_source(random_arg) {}
typedef RandomPermutation_iterator iterator;
typedef iterator const_iterator;
iterator begin() const
{
return iterator(base, random_source);
}
iterator end() const
{
return iterator(random_source);
}
int size() const { return base.size(); }
bool empty() const { return base.empty(); }
protected:
sequence base;
UniformlyRandom<long> random_source;
};
template <typename ContainerRef>
class RandomPermutation<ContainerRef, false>
: public indexed_subset_impl< RandomPermutation<ContainerRef, false>,
mlist< Container1Tag< ContainerRef >,
Container2Tag< RandomPermutation<> > > > {
typedef indexed_subset_impl<RandomPermutation> impl_t;
protected:
alias<ContainerRef> base;
RandomPermutation<> perm;
public:
explicit RandomPermutation(typename alias<ContainerRef>::arg_type base_arg, const RandomSeed& seed=RandomSeed())
: base(base_arg)
, perm(base->size(), seed) {}
RandomPermutation(typename alias<ContainerRef>::arg_type base_arg, const SharedRandomState& s)
: base(base_arg)
, perm(base->size(), s) {}
typename impl_t::container1& get_container1() { return *base; }
const typename impl_t::container1& get_container1() const { return *base; }
const RandomPermutation<>& get_container2() const { return perm; }
};
template <typename ContainerRef>
struct spec_object_traits< RandomPermutation<ContainerRef> >
: spec_object_traits<is_container> {
static const bool is_temporary=true;
};
template <typename Container> inline
RandomSubset<Container&>
select_random_subset(Container& c, int k, const RandomSeed& seed=RandomSeed())
{
return RandomSubset<Container&>(c,k,seed);
}
template <typename Container> inline
RandomSubset<Container&>
select_random_subset(Container& c, int k, const SharedRandomState& s)
{
return RandomSubset<Container&>(c,k,s);
}
template <typename Container> inline
RandomSubset<const Container&>
select_random_subset(const Container& c, int k, const RandomSeed& seed=RandomSeed())
{
return RandomSubset<const Container&>(c,k,seed);
}
template <typename Container> inline
RandomSubset<const Container&>
select_random_subset(const Container& c, int k, const SharedRandomState& s)
{
return RandomSubset<const Container&>(c,k,s);
}
template <typename Container> inline
RandomPermutation<Container&>
random_permutation(Container& c, const RandomSeed& seed=RandomSeed())
{
return RandomPermutation<Container&>(c,seed);
}
template <typename Container> inline
RandomPermutation<Container&>
random_permutation(Container& c, const SharedRandomState& s)
{
return RandomPermutation<Container&>(c,s);
}
template <typename Container> inline
RandomPermutation<const Container&>
random_permutation(const Container& c, const RandomSeed& seed=RandomSeed())
{
return RandomPermutation<const Container&>(c,seed);
}
template <typename Container> inline
RandomPermutation<const Container&>
random_permutation(const Container& c, const SharedRandomState& s)
{
return RandomPermutation<const Container&>(c,s);
}
inline
RandomPermutation<>
random_permutation(int n, const RandomSeed& seed=RandomSeed())
{
return RandomPermutation<>(n,seed);
}
inline
RandomPermutation<>
random_permutation(int n, const SharedRandomState& s)
{
return RandomPermutation<>(n,s);
}
} // end namespace pm
namespace polymake {
using pm::RandomSubset;
using pm::select_random_subset;
using pm::RandomPermutation;
using pm::random_permutation;
}
#endif // POLYMAKE_RANDOM_SUBSET_H
// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End:
|