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
/*
 * kol.c
 */
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include "message.h"
#include "kollib.h"

#define SIZE  12500003
int hashkeys[SIZE];
int hashvalues[SIZE];
#define LIMIT 10000000
//#define LIMIT 20000

int nn, myid, startnumber;

int receiveStudentNumber(int fromNode, int student) {

	//fprintf(stderr,"Sending Request to Node %i, about student %i\n",fromNode,student);
	PutInt(fromNode, student);
	Send(fromNode);

	//fprintf(stderr, "Receiving QueryResult from Node %i\n", fromNode);
	Receive(fromNode);
	//fprintf(stderr, "RECEIVED\n");
	int number = GetInt(fromNode);
	//fprintf(stderr,"%i\n",number);
	return number;
}

void initHash() {
	int i;
	for (i = 0; i < SIZE; i++)
		hashkeys[i] = -1;
	//memset(hashtmp,-1,SIZE);
}

int h1(int key) {
	return (key % SIZE);
}
int h2(int key) {
	return 1 + (key % (SIZE - 2));
}

int hfun(int key, int i) {
	return ((h1(key) + i * h2(key)) % SIZE);
}

void insert(int key, int attrib) {
	int i=0;
	do {
		int h = hfun(key, i);
		if (hashkeys[h] == -1) {
			hashkeys[h] = key;
			hashvalues[h] = attrib;
			return;
		}
		i++;
	} while (i < SIZE);
	assert(0);
}

int search(int key) {
	int i=0, h;
	do {
		h = hfun(key, i);
		if (hashkeys[h] == key)
			return hashvalues[h];
		i++;
	} while (hashkeys[h] != -1 && i < SIZE);
	return -1;
}

void hashSet(int student, int number) {
	insert(student, number);
	//fprintf(stderr, "id:%i hashSet: [%i]=%i\n", myid, student, number);
}

int hashRead(int student) {
	int n = search(student);
	//fprintf(stderr, "id:%i hashRead: [%i]=%i\n", myid, student, n);
	return n;

}

int Map(int student) {
	int n, sn;
	int m;
	m = hashRead(student);
	if (m != -1)
		return m;
	for (n = 1; n < nn; n++)
		if ((sn = receiveStudentNumber(n, student)) != -1)
			return sn;
	assert(0);
	return -1;
}

void fillHashInitNext(int prev, int cur, int startNumber, int limit) {
	int number = startNumber;
	//prev = startStudent; //receive
	//nsperthisnode = ns; //receive

	//next = FirstNeighbor(prev); //receive
	int next;

	/*	hashSet(cur, number++);
	 if ((next = FirstNeighbor(cur)) == prev)
	 next = SecondNeighbor(cur);
	 prev = cur;
	 */
	while (1) {
		if (cur == 1 || number - startNumber >= limit)
			break;
		hashSet(cur, number++);
		if ((next = FirstNeighbor(cur)) == prev)
			next = SecondNeighbor(cur);
		prev = cur;
		cur = next;
	}

	if (myid + 1 < nn) {
		PutInt(myid + 1, number);
		PutInt(myid + 1, prev);
		PutInt(myid + 1, cur);
		PutInt(myid + 1, limit);
		Send(myid + 1);
	} else {
		PutInt(0, cur);
		Send(0);
	}

}

int main() {
	int n, i, cs, number, from, to, length1, length2, prev;
	int nspernode;
	int startStudent;
	initHash();

	nn = NumberOfNodes();
	myid = MyNodeId();

	if (myid == 0) {

		startnumber = 1;
		hashSet(1, startnumber++);
		prev = 1;
		startStudent = FirstNeighbor(1);

		nspernode = LIMIT; //TODO
	} else {
		//fprintf(stderr, "Receiving INIT from Node %i\n", myid - 1);
		Receive(myid - 1);
		//fprintf(stderr, "RECEIVED\n");
		startnumber = GetInt(myid - 1);
		prev = GetInt(myid - 1);
		startStudent = GetInt(myid - 1);
		nspernode = GetInt(myid - 1); //TODO

	}

	fillHashInitNext(prev, startStudent, startnumber, nspernode);

	if (myid == 0) {
		//fprintf(stderr, "Receiving QUERY_START from Node %i\n", nn - 1);
		Receive(nn - 1);
		//fprintf(stderr, "RECEIVED\n");
		GetInt(nn - 1);

		int nq = NumberOfQueries();
		int ns = NumberOfStudents();
		for (i = 1; i <= nq; i++) {
			//fprintf(stderr, "Before Query\n");
			from = Map(QueryFrom(i));
			to = Map(QueryTo(i));
			//fprintf(stderr, "After Query\n");
			if (from > to)
				length1 = from - to;
			else
				length1 = to - from;
			length2 = ns - length1;
			if (length2 < length1)
				printf("%i\n", length2);
			else
				printf("%i\n", length1);
		}
		for (i = 1; i < nn; i++) {
			PutInt(i, -1);
			Send(i);
		}

	}

	if (myid != 0) {
		for (;;) {
			//fprintf(stderr, "Receiving Query from Node 0\n");
			Receive(0);
			//fprintf(stderr, "RECEIVED\n");
			int student = GetInt(0);
			if (student == -1)
				break;
			PutInt(0, hashRead(student));
			Send(0);
		}
	}
	return 0;
}