File: match.c

package info (click to toggle)
mtdev 1.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 1,528 kB
  • ctags: 394
  • sloc: sh: 11,301; ansic: 1,444; python: 85; makefile: 60
file content (390 lines) | stat: -rw-r--r-- 10,223 bytes parent folder | download | duplicates (3)
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
/*****************************************************************************
 *
 * mtdev - Multitouch Protocol Translation Library (MIT license)
 *
 * Copyright (C) 2010 Henrik Rydberg <rydberg@euromail.se>
 * Copyright (C) 2010 Canonical Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 ****************************************************************************/

#include "match.h"
#include <string.h>
#include <stdio.h>

/**
 * Bitmap implementation of the hungarian algorithm (MIT license)
 *
 * Copyright (C) 2008 Henrik Rydberg <rydberg@euromail.se>
 *
 * Based on code released by Markus Buehren (2004) (BSD license)
 *
 * Copyright (C) 2004, Markus Buehren. All rights reserved.
 */

typedef unsigned col_t[1];
typedef unsigned mat_t[DIM_FINGER];

#define GET1(m, x) ((m[0] >> (x)) & 1U)
#define SET1(m, x) (m[0] |= (1U << (x)))
#define CLEAR1(m, x) (m[0] &= ~(1U << (x)))

#define GET2(m, row, col) ((m[col] >> (row)) & 1U)
#define SET2(m, row, col) (m[col] |= (1U << (row)))
#define CLEAR2(m, row, col) (m[col] &= ~(1U << (row)))

/********************************************************/

static void buildixvector(int *ix, mat_t mstar, int nrows, int ncols)
{
	int row, col;
	for (row = 0; row < nrows; row++) {
		for (col = 0; col < ncols; col++) {
			if (GET2(mstar, row, col)) {
				ix[row] = col;
				break;
			}
		}
	}
}


/********************************************************/

static void step2a(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		   mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		   int dmin);
static void step2b(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		   mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		   int dmin);
static void step3(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin);
static void step4(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin, int row, int col);
static void step5(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin);

static void ixoptimal(int *ix, int *mdist, int nrows, int ncols)
{
	int *mdistTemp, *mdistEnd, *columnEnd, value, minValue;
	int dmin, row, col;
	col_t ccol, crow;
	mat_t mstar, mprime, nmstar;

	memset(ccol, 0, sizeof(col_t));
	memset(crow, 0, sizeof(col_t));
	memset(mstar, 0, sizeof(mat_t));
	memset(mprime, 0, sizeof(mat_t));
	memset(nmstar, 0, sizeof(mat_t));

	/* initialization */
	for (row = 0; row < nrows; row++)
		ix[row] = -1;

	mdistEnd = mdist + nrows * ncols;

	/* preliminary steps */
	if (nrows <= ncols) {
		dmin = nrows;

		for (row = 0; row < nrows; row++) {
			/* find the smallest element in the row */
			mdistTemp = mdist + row;
			minValue = *mdistTemp;
			mdistTemp += nrows;
			while (mdistTemp < mdistEnd) {
				value = *mdistTemp;
				if (value < minValue)
					minValue = value;
				mdistTemp += nrows;
			}

			/* subtract the smallest element from each element
			   of the row */
			mdistTemp = mdist + row;
			while (mdistTemp < mdistEnd) {
				*mdistTemp -= minValue;
				mdistTemp += nrows;
			}
		}

		/* Steps 1 and 2a */
		for (row = 0; row < nrows; row++) {
			for (col = 0; col < ncols; col++) {
				if (mdist[row + nrows * col] != 0)
					continue;
				if (GET1(ccol, col))
					continue;
				SET2(mstar, row, col);
				SET1(ccol, col);
				break;
			}
		}
	} else {
		dmin = ncols;

		for (col = 0; col < ncols; col++) {
			/* find the smallest element in the column */
			mdistTemp = mdist + nrows*col;
			columnEnd = mdistTemp + nrows;

			minValue = *mdistTemp++;
			while (mdistTemp < columnEnd) {
				value = *mdistTemp++;
				if (value < minValue)
					minValue = value;
			}

			/* subtract the smallest element from each element
			   of the column */
			mdistTemp = mdist + nrows*col;
			while (mdistTemp < columnEnd)
				*mdistTemp++ -= minValue;
		}

		/* Steps 1 and 2a */
		for (col = 0; col < ncols; col++) {
			for (row = 0; row < nrows; row++) {
				if (mdist[row + nrows * col] != 0)
					continue;
				if (GET1(crow, row))
					continue;
				SET2(mstar, row, col);
				SET1(ccol, col);
				SET1(crow, row);
				break;
			}
		}
		memset(crow, 0, sizeof(col_t));
	}

	/* move to step 2b */
	step2b(ix, mdist, mstar, nmstar,
	       mprime, ccol, crow, nrows, ncols,
	       dmin);
}

/********************************************************/
static void step2a(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		   mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		   int dmin)
{
	int col, row;

	/* cover every column containing a starred zero */
	for (col = 0; col < ncols; col++) {
		for (row = 0; row < nrows; row++) {
			if (!GET2(mstar, row, col))
				continue;
			SET1(ccol, col);
			break;
		}
	}

	/* move to step 3 */
	step2b(ix, mdist, mstar, nmstar,
	       mprime, ccol, crow, nrows, ncols,
	       dmin);
}

/********************************************************/
static void step2b(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		   mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		   int dmin)
{
	int col, ncc;

	/* count covered columns */
	ncc = 0;
	for (col = 0; col < ncols; col++)
		if (GET1(ccol, col))
			ncc++;

	if (ncc == dmin) {
		/* algorithm finished */
		buildixvector(ix, mstar, nrows, ncols);
	} else {
		/* move to step 3 */
		step3(ix, mdist, mstar, nmstar,
		      mprime, ccol, crow, nrows, ncols,
		      dmin);
	}

}

/********************************************************/
static void step3(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin)
{
	int zerosFound;
	int row, col, cstar;

	zerosFound = 1;
	while (zerosFound) {
		zerosFound = 0;
		for (col = 0; col < ncols; col++) {
			if (GET1(ccol, col))
				continue;
			for (row = 0; row < nrows; row++) {
				if (mdist[row + nrows * col] != 0)
					continue;
				if (GET1(crow, row))
					continue;

				/* prime zero */
				SET2(mprime, row, col);

				/* find starred zero in current row */
				for (cstar = 0; cstar < ncols; cstar++)
					if (GET2(mstar, row, cstar))
						break;

				if (cstar == ncols) { /* no starred zero */
					/* move to step 4 */
					step4(ix, mdist, mstar, nmstar,
					      mprime, ccol, crow, nrows, ncols,
					      dmin, row, col);
					return;
				} else {
					SET1(crow, row);
					CLEAR1(ccol, cstar);
					zerosFound = 1;
					break;
				}
			}
		}
	}

	/* move to step 5 */
	step5(ix, mdist, mstar, nmstar,
	      mprime, ccol, crow, nrows, ncols,
	      dmin);
}

/********************************************************/
static void step4(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin, int row, int col)
{
	int rstar, cstar, primeRow, primeCol;

	/* generate temporary copy of mstar */
	memcpy(nmstar, mstar, sizeof(mat_t));

	/* star current zero */
	SET2(nmstar, row, col);

	/* find starred zero in current column */
	cstar = col;
	for (rstar = 0; rstar < nrows; rstar++)
		if (GET2(mstar, rstar, cstar))
			break;

	while (rstar < nrows) {
		/* unstar the starred zero */
		CLEAR2(nmstar, rstar, cstar);

		/* find primed zero in current row */
		primeRow = rstar;
		for (primeCol = 0; primeCol < ncols; primeCol++)
			if (GET2(mprime, primeRow, primeCol))
				break;

		/* star the primed zero */
		SET2(nmstar, primeRow, primeCol);

		/* find starred zero in current column */
		cstar = primeCol;
		for (rstar = 0; rstar < nrows; rstar++)
			if (GET2(mstar, rstar, cstar))
				break;
	}

	/* use temporary copy as new mstar */
	/* delete all primes, uncover all rows */
	memcpy(mstar, nmstar, sizeof(mat_t));
	memset(mprime, 0, sizeof(mat_t));
	memset(crow, 0, sizeof(col_t));

	/* move to step 2a */
	step2a(ix, mdist, mstar, nmstar,
	       mprime, ccol, crow, nrows, ncols,
	       dmin);
}

/********************************************************/
static void step5(int *ix, int *mdist, mat_t mstar, mat_t nmstar,
		  mat_t mprime, col_t ccol, col_t crow, int nrows, int ncols,
		  int dmin)
{
	int h = 0, value;
	int row, col, found = 0;

	/* find smallest uncovered element h */
	for (row = 0; row < nrows; row++) {
		if (GET1(crow, row))
			continue;
		for (col = 0; col < ncols; col++) {
			if (GET1(ccol, col))
				continue;
			value = mdist[row + nrows * col];
			if (!found || value < h) {
				h = value;
				found = 1;
			}
		}
	}

	/* where to go if nothing uncovered? */
	if (!found)
		return;

	/* add h to each covered row */
	for (row = 0; row < nrows; row++) {
		if (!GET1(crow, row))
			continue;
		for (col = 0; col < ncols; col++)
			mdist[row + nrows * col] += h;
	}

	/* subtract h from each uncovered column */
	for (col = 0; col < ncols; col++) {
		if (GET1(ccol, col))
			continue;
		for (row = 0; row < nrows; row++)
			mdist[row + nrows * col] -= h;
	}

	/* move to step 3 */
	step3(ix, mdist, mstar, nmstar,
	      mprime, ccol, crow, nrows, ncols,
	      dmin);
}

void mtdev_match(int ix[DIM_FINGER], int A[DIM2_FINGER], int nrow, int ncol)
{
	ixoptimal(ix, A, nrow, ncol);
}