1 
2 /* Web Polygraph       http://www.web-polygraph.org/
3  * Copyright 2003-2011 The Measurement Factory
4  * Licensed under the Apache License, Version 2.0 */
5 
6 #include "base/polygraph.h"
7 
8 #include <limits.h>
9 
10 #include "xstd/Ring.h"
11 #include "base/RndPermut.h"
12 #include "base/TwoWayPermutator.h"
13 
14 
configure(int mapSize,RndPermutator & rndPermutator,int seed)15 void TwoWayPermutator::configure(int mapSize, RndPermutator &rndPermutator, int seed) {
16 	Assert(mapSize < INT_MAX/2);
17 
18 	// direct
19 	{
20 		Ring<int> r(mapSize);
21 		while (r.count() < mapSize)
22 			r.enqueue(r.count());
23 
24 		RndGen rng(rndPermutator.permut(seed, rndTwoWayPermutator));
25 		r.randomize(rng);
26 
27 		theDirectMap.stretch(mapSize);
28 		while (theDirectMap.count() < mapSize)
29 			theDirectMap.append(r.dequeue());
30 	}
31 
32 	// reverse
33 	theReverseMap.resize(mapSize);
34 	for (int i = 0; i < theDirectMap.count(); ++i)
35 		theReverseMap[theDirectMap[i]] = i;
36 }
37 
38 // returns 0 <= m < mapSize
directLookup(int n) const39 int TwoWayPermutator::directLookup(int n) const {
40 	if (Should(0 <= n && n < theDirectMap.count()))
41 		return theDirectMap[n];
42 	else
43 		return 0;
44 }
45 
46 // returns 0 <= n < mapSize
reverseLookup(int m) const47 int TwoWayPermutator::reverseLookup(int m) const {
48 	if (Should(0 <= m && m < theReverseMap.count()))
49 		return theReverseMap[m];
50 	else
51 		return 0;
52 }
53 
54