1/*
2* $Id: permute.c,v 1.3 2007/01/04 21:29:21 jms Exp $
3*
4* Revision History
5* ===================
6* $Log: permute.c,v $
7* Revision 1.3 2007/01/04 21:29:21 jms
8* Porting changes uncovered as part of move to VS2005. No impact on data set
9*
10* Revision 1.2 2005/01/03 20:08:59 jms
11* change line terminations
12*
13* Revision 1.1.1.1 2004/11/24 23:31:47 jms
14* re-establish external server
15*
16* Revision 1.1.1.1 2003/08/07 17:58:34 jms
17* recreation after CVS crash
18*
19* Revision 1.2 2003/08/07 17:58:34 jms
20* Convery RNG to 64bit space as preparation for new large scale RNG
21*
22* Revision 1.1.1.1 2003/04/03 18:54:21 jms
23* initial checkin
24*
25*
26*/
27/*
28* permute.c -- a permutation generator for the query
29* sequences in TPC-H and TPC-R
30*/
31
32#ifdef TEST
33#define DECLARER
34#endif
35#include "config.h"
36#include "dss.h"
37#ifdef TEST
38#include <stdlib.h>
39#if (defined(_POSIX_)||!defined(WIN32)) /* Change for Windows NT */
40#include <unistd.h>
41#include <sys/wait.h>
42#endif /* WIN32 */
43#include <stdio.h> /* */
44#include <limits.h>
45#include <math.h>
46#include <ctype.h>
47#include <signal.h>
48#include <string.h>
49#include <errno.h>
50#ifdef HP
51#include <strings.h>
52#endif
53#if (defined(WIN32)&&!defined(_POSIX_))
54#include <process.h>
55#pragma warning(disable:4201)
56#pragma warning(disable:4214)
57#pragma warning(disable:4514)
58#define WIN32_LEAN_AND_MEAN
59#define NOATOM
60#define NOGDICAPMASKS
61#define NOMETAFILE
62#define NOMINMAX
63#define NOMSG
64#define NOOPENFILE
65#define NORASTEROPS
66#define NOSCROLL
67#define NOSOUND
68#define NOSYSMETRICS
69#define NOTEXTMETRIC
70#define NOWH
71#define NOCOMM
72#define NOKANJI
73#define NOMCX
74#include <windows.h>
75#pragma warning(default:4201)
76#pragma warning(default:4214)
77#endif
78#endif
79
80DSS_HUGE NextRand(DSS_HUGE seed);
81void permute(long *set, int cnt, long stream);
82void permute_dist(distribution *d, long stream);
83long seed;
84char *eol[2] = {" ", "},"};
85extern seed_t Seed[];
86#ifdef TEST
87tdef tdefs = { NULL };
88#endif
89
90
91#define MAX_QUERY 22
92#define ITERATIONS 1000
93#define UNSET 0
94
95void permute(long *a, int c, long s)
96{
97 int i;
98 static DSS_HUGE source;
99 static long *set, temp;
100
101 if (a != (long *)NULL)
102 {
103 for (i=0; i < c; i++)
104 {
105 RANDOM(source, (long)i, (long)(c - 1), s);
106 temp = *(a + source);
107 *(a + source) = *(a + i) ;
108 *(a + i) = temp;
109 }
110 }
111
112 return;
113}
114
115void permute_dist(distribution *d, long stream)
116{
117 static distribution *dist = NULL;
118 int i;
119
120 if (d != NULL)
121 {
122 if (d->permute == (long *)NULL)
123 {
124 d->permute = (long *)malloc(sizeof(long) * DIST_SIZE(d));
125 MALLOC_CHECK(d->permute);
126 }
127 for (i=0; i < DIST_SIZE(d); i++)
128 *(d->permute + i) = i;
129 permute(d->permute, DIST_SIZE(d), stream);
130 }
131 else
132 INTERNAL_ERROR("Bad call to permute_dist");
133
134 return;
135}
136
137
138#ifdef TEST
139
140main(int ac, char *av[])
141 {
142 long *sequence,
143 i,
144 j,
145 streams = UNSET,
146 *a;
147 char sep;
148 int index = 0;
149
150 set_seeds = 0;
151 sequence = (long *)malloc(MAX_QUERY * sizeof(long));
152 a = sequence;
153 for (i=0; i < MAX_QUERY; i++)
154 *(sequence + i) = i;
155 if (ac < 3)
156 goto usage;
157 Seed[0].value = (long)atoi(av[1]);
158 streams = atoi(av[2]);
159 if (Seed[0].value == UNSET || streams == UNSET)
160 goto usage;
161
162 index = 0;
163 printf("long permutation[%d][%d] = {\n", streams, MAX_QUERY);
164 for (j=0; j < streams; j++)
165 {
166 sep = '{';
167 printf("%s\n", eol[index]);
168 for (i=0; i < MAX_QUERY; i++)
169 {
170 printf("%c%2d", sep, *permute(a, MAX_QUERY, 0) + 1);
171 a = (long *)NULL;
172 sep = ',';
173 }
174 a = sequence;
175 index=1;
176 }
177 printf("}\n};\n");
178 return(0);
179
180usage:
181 printf("Usage: %s <seed> <streams>\n",av[0]);
182 printf(" uses <seed> to start the generation of <streams> permutations of [1..%d]\n", MAX_QUERY);
183 return(-1);
184
185 }
186#endif /* TEST */
187