This file is indexed.

/usr/include/polymake/RandomGenerators.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
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
/* 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_GENERATORS_H
#define POLYMAKE_RANDOM_GENERATORS_H

#include "polymake/Rational.h"
#include "polymake/Bitset.h"
#include "polymake/AccurateFloat.h"
#include "polymake/Vector.h"

#include <cstdlib>
#include <string.h>

namespace pm {

namespace perl { class Value; }

class RandomSeed {
public:
   RandomSeed() : data(64, Integer::Reserve()) { renew(); }
   RandomSeed(unsigned long x) : data(long(x)) {}
   RandomSeed(const Integer& x) : data(x) {}
   RandomSeed(perl::Value);

   const Integer& get() const { return data; }

   void renew();
private:
   static int rfd;
   Integer data;
};

class RandomState {
public:
   explicit RandomState(const RandomSeed& seed=RandomSeed())
   {
      gmp_randinit_default(state);
      gmp_randseed(state, seed.get().get_rep());
   }

   RandomState(const RandomState& s)
   {
      gmp_randinit_set(state, s.state);
   }

   RandomState& operator= (const RandomState&)
   {
      // do nothing
      return *this;
   }

   ~RandomState()
   {
      gmp_randclear(state);
   }

   gmp_randstate_t state;

#if GMP_LIMB_BITS==32
   void fix_for_mpfr();
   static void intercept_get_fn(gmp_randstate_t, mp_ptr, unsigned long int);
#endif
};

class SharedRandomState {
public:
   SharedRandomState() {}

   explicit SharedRandomState(const RandomSeed& seed)
      : s(seed) {}

   void reset(const RandomSeed& seed=RandomSeed())
   {
      gmp_randseed(s->state, seed.get().get_rep());
   }
protected:
   gmp_randstate_t& state() { return s->state; }

#if GMP_LIMB_BITS==32
   void fix_for_mpfr() { s->fix_for_mpfr(); }
#else
   static void fix_for_mpfr() {}
#endif

   shared_object<RandomState, CopyOnWriteTag<std::false_type>> s;
};

template <typename Generator, typename Eref>
class random_get_iterator {
protected:
   Generator* generator;
public:
   typedef input_iterator_tag iterator_category;
   typedef typename deref<Eref>::type value_type;
   typedef Eref reference;
   typedef void* pointer;
   typedef ptrdiff_t difference_type;

   random_get_iterator(const Generator* gen_arg=NULL) :
      generator(const_cast<Generator*>(gen_arg)) {}

   reference operator* () const { return generator->get(); }

   random_get_iterator& operator++() { return *this; }
   random_get_iterator& operator++(int) { return *this; }
   bool operator== (const random_get_iterator& it) const { return generator==it.generator; }
   bool operator!= (const random_get_iterator& it) const { return !operator==(it); }
};

/*
 *  The classes defined below comply with the STL container concept.
 *  To obtain the random numbers, you should either call the get() method,
 *  or create an iterator and dereference it.
 */

template <typename T> class UniformlyRandom;
template <typename T> class UniformlyRandomRanged;

template <typename Top, typename Eref=typename Top::reference>
class GenericRandomGenerator
   : public Generic<Top> {
public:
   typedef typename deref<Eref>::type value_type;
   typedef Eref reference;
   typedef reference const_reference;

   typedef random_get_iterator<Top, Eref> iterator;
   typedef iterator const_iterator;
   iterator begin() const { return iterator(static_cast<const Top*>(this)); }
   iterator end() const { return iterator(); }
};

template <>
class UniformlyRandom<long>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandom<long>, long> {
public:
   explicit UniformlyRandom(const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed) {}

   explicit UniformlyRandom(const SharedRandomState& s)
      : SharedRandomState(s) {}

   long get() { return gmp_urandomb_ui(state(), 32); }
};

template <>
class UniformlyRandomRanged<long>
   : public SharedRandomState
   , public GenericRandomGenerator< UniformlyRandomRanged<long>, long> {
public:
   explicit UniformlyRandomRanged(long max_arg, const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed)
      , max(max_arg) {}

   UniformlyRandomRanged(long max_arg, const SharedRandomState& s)
      : SharedRandomState(s)
      , max(max_arg) {}

   long get()
   {
      return gmp_urandomm_ui(state(), max);
   }

   long upper_limit() const { return max; }
   long& upper_limit() { return max; }
protected:
   long max;
};

template <>
class UniformlyRandom<Integer>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandom<Integer>, Integer> {
protected:
   static const unsigned long default_bits=48;
public:
   explicit UniformlyRandom(unsigned long bitlength_arg=default_bits, const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed)
      , bitlength(bitlength_arg) {}

   explicit UniformlyRandom(const RandomSeed& seed)
      : SharedRandomState(seed)
      , bitlength(default_bits) {}

   UniformlyRandom(unsigned long bitlength_arg, const SharedRandomState& s)
      : SharedRandomState(s)
      , bitlength(bitlength_arg) {}

   explicit UniformlyRandom(const SharedRandomState& s)
      : SharedRandomState(s)
      , bitlength(default_bits) {}

   Integer get() { return Integer(state(), bitlength); }

protected:
   const unsigned long bitlength;
};

template <>
class UniformlyRandomRanged<Integer>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandomRanged<Integer>, Integer> {
public:
   explicit UniformlyRandomRanged(const Integer& max_arg, const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed)
      , max(max_arg) {}

   UniformlyRandomRanged(const Integer& max_arg, const SharedRandomState& s)
      : SharedRandomState(s)
      , max(max_arg) {}

   Integer get()
   {
      return Integer(state(), max);
   }

   const Integer& upper_limit() const { return max; }
   Integer& upper_limit() { return max; }
protected:
   Integer max;
};

/** Generator of random rational numbers uniformly distributed in [0, 1).

    Only the numerators of the numbers are really random.
    The denominators are always some powers of 2.
*/
template <>
class UniformlyRandom<Rational>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandom<Rational>, Rational> {
protected:
   static const unsigned long default_bits=48;
public:
   explicit UniformlyRandom(unsigned long bitlength_arg=default_bits, const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed)
      , bitlength(bitlength_arg) {}

   explicit UniformlyRandom(const RandomSeed& seed)
      : SharedRandomState(seed)
      , bitlength(default_bits) {}

   UniformlyRandom(unsigned long bitlength_arg, const SharedRandomState& s)
      : SharedRandomState(s)
      , bitlength(bitlength_arg) {}

   explicit UniformlyRandom(const SharedRandomState& s)
      : SharedRandomState(s)
      , bitlength(default_bits) {}

   Rational get()
   {
      return Rational(state(), bitlength);
   }
protected:
   unsigned long bitlength;
};

/// Generator of random Bitset of a given maximal cardinality
template <>
class UniformlyRandom<Bitset>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandom<Bitset>, Bitset> {
public:
   explicit UniformlyRandom(int max_arg, const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed), max_elem(max_arg) {}

   UniformlyRandom(int max_arg, const SharedRandomState& s)
      : SharedRandomState(s), max_elem(max_arg) {}

   Bitset get()
   {
      return Bitset(state(), max_elem);
   }
protected:
   int max_elem;
};

/// Generator of random AccurateFloat numbers from [0, 1)
template <>
class UniformlyRandom<AccurateFloat>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandom<AccurateFloat>, AccurateFloat> {
public:
   explicit UniformlyRandom(const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed)
   {
      fix_for_mpfr();
   }

   explicit UniformlyRandom(const SharedRandomState& s)
      : SharedRandomState(s)
   {
      fix_for_mpfr();
   }

   AccurateFloat get()
   {
      return AccurateFloat(state());
   }
};

template <>
class UniformlyRandom<double>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandom<double>, double> {
public:
   explicit UniformlyRandom(const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed)
   {
      fix_for_mpfr();
   }

   explicit UniformlyRandom(const SharedRandomState& s)
      : SharedRandomState(s)
   {
      fix_for_mpfr();
   }

   double get()
   {
      x.set_random(state());
      return mpfr_get_d(x.get_rep(), MPFR_RNDZ);
   }
protected:
   AccurateFloat x;
};

template <>
class UniformlyRandomRanged<double>
   : public SharedRandomState
   , public GenericRandomGenerator<UniformlyRandomRanged<double>, double> {
public:
   explicit UniformlyRandomRanged(double max_arg, const RandomSeed& seed=RandomSeed())
      : SharedRandomState(seed)
      , max(max_arg)
   {
      fix_for_mpfr();
   }

   UniformlyRandomRanged(double max_arg, const SharedRandomState& s)
      : SharedRandomState(s)
      , max(max_arg)
   {
      fix_for_mpfr();
   }

   double get()
   {
      x.set_random(state());
      return max * mpfr_get_d(x.get_rep(), MPFR_RNDZ);
   }

   double upper_limit() const { return max; }
   double& upper_limit() { return max; }
protected:
   AccurateFloat x;
   double max;
};

class DiscreteRandom
   : public GenericRandomGenerator<DiscreteRandom, int> {
public:
   template <typename Container, typename enabled=typename std::enable_if<isomorphic_to_container_of<Container,double>::value>::type>
   DiscreteRandom(const Container& distrib_src, const RandomSeed& seed=RandomSeed())
      : rg(seed), distribution(distrib_src)
   {
      normalize();
   }

   template <typename Container, typename enabled=typename std::enable_if<isomorphic_to_container_of<Container,double>::value>::type>
   DiscreteRandom(const Container& distrib_src, const SharedRandomState& s)
      : rg(s), distribution(distrib_src)
   {
      normalize();
   }

   int get();
protected:
   void normalize();

   mutable UniformlyRandom<double> rg;
   Vector<double> distribution;
};


template <typename Generator, typename E>
struct check_iterator_feature<random_get_iterator<Generator, E>, unlimited> : std::true_type {};

template <typename Top, typename Eref>
struct spec_object_traits< GenericRandomGenerator<Top, Eref> > :
      spec_or_model_traits<Top,is_container> {};

} // end namespace pm

namespace polymake {

using pm::UniformlyRandom;
using pm::UniformlyRandomRanged;
using pm::DiscreteRandom;
using pm::RandomSeed;

}

#endif // POLYMAKE_RANDOM_GENERATORS_H

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: