summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bm.ctr
blob: 5579e2141051347f2a384f884ad9ed3524bd5e48 (plain)
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
%%	options

copyright owner	=	Dirk Krause
copyright year	=	2019-xxxx
SPDX-License-Identifier:	BSD-3-Clause


%%	header

/**	@file	dk4bm.h	Bit matrix.

Among other things a bit matrix can be used to represent a directed
graph. A 1-bit in row <i>i</i> column <i>j</i> indicates a directed
edge from <i>i</i> to <i>j</i> (row number represents source node,
column number represents destination node).

Alternatively you may choose reverse representation:
A 1-bit in row <i>i</i> column <i>j</i> indicates a directed edge from <i>j</i>
to <i>i</i> (row number represents destination node number, column number
represents source node number).<br>
In a dependency graph it means: <i>i</i> depends on <i>j</i>.

After expanding the matrix you can check whether or not there is a path
from any node to any other, either direct or
indirect (path consisting of a sequence of direct edges).
*/

#ifndef	DK4CONF_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4conf.h"
#else
#include <dktools-4/dk4conf.h>
#endif
#endif

#ifndef DK4ERROR_H_INCLUDED
#if	DK4_BUILDING_DKTOOLS4
#include "dk4error.h"
#else
#include <dktools-4/dk4error.h>
#endif
#endif


/**	Bit matrix structure.
*/
typedef struct {
	unsigned char	**data;				/**< Bits grouped in bytes. */
	size_t			  rows;				/**< Number of rows. */
	size_t			  columns;			/**< Number of columns. */
	size_t			  bytes_per_row;	/**< Bytes per row. */
} dk4_bit_matrix_t;



#ifdef	__cplusplus
extern "C" {
#endif


/**	Open bit matrix.

	CRT on Windows: Optional.
	@param	rows	Number of rows.
	@param	columns	Number of columns.
	@param	erp		Error report, may be NULL.
	@return	Valid pointer on success, NULL on error.
	Use dk4bm_close() to close the bit matrix when done with it.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if the number of rows or columns is 0,
	- DK4_E_MATH_OVERFLOW<br>
	  if a numeric overflow occurs in size calculation (number of rows and/or
	  columns too large),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if not enough memory available.
*/

dk4_bit_matrix_t *
dk4bm_open(
	size_t					 rows,
	size_t					 columns,
	dk4_er_t				*erp
);


/**	Create a full copy of a bit matrix as new bit matrix.

	CRT on Windows: Optional.
	@param	psrc	Source bit matrix.
	@param	erp		Error report, may be NULL.
	@return	Valid pointer on success, NULL on error.
	Use dk4bm_close() to close the bit matrix when done with it.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if a NULL pointer is passed as psrc or the row or column number
	  in psrc is 0,
	- DK4_E_MATH_OVERFLOW<br>
	  if a numeric overflow occurs in size calculation (number of rows and/or
	  columns too large),
	- DK4_E_MEMORY_ALLOCATION_FAILED<br>
	  if not enough memory available.
*/

dk4_bit_matrix_t *
dk4bm_open_copy(
	dk4_bit_matrix_t const	*psrc,
	dk4_er_t				*erp
);


/**	Set one bit in matrix.

	CRT on Windows: Optional.
	@param	pmb		Bit matrix to modify.
	@param	row		Row position to modify.
	@param	column	Column position to modify.
	@param	value	Value (non-zero to set bit, 0 to reset bit).
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if pmb is NULL or the row/column specification is out of range.
*/

int
dk4bm_set(
	dk4_bit_matrix_t		*pmb,
	size_t					 row,
	size_t					 column,
	int						 value,
	dk4_er_t				*erp
);


/**	Get bit value from matrix.

	CRT on Windows: Optional.
	@param	pdest	Address of destination variable, set to 1 or 0.
	@param	pmb		Bit matrix to retrieve bit from.
	@param	row		Row position of bit to find.
	@param	column	Column position of bit to find.
	@param	erp		Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if pdest or pmb is NULL or the row/column specification is out of range.
*/

int
dk4bm_get(
	int						*pdest,
	dk4_bit_matrix_t const	*pmb,
	size_t					 row,
	size_t					 column,
	dk4_er_t				*erp
);


/**	Expand bit matrix.

	CRT on Windows: Optional.
	@param	pmb	Bit matrix to expand.
	@param	erp	Error report, may be NULL.
	@return	1 on success, 0 on error.

	Error codes:
	- DK4_E_INVALID_ARGUMENTS<br>
	  if pmb is NULL or not a square matrix.
*/

int
dk4bm_expand(
	dk4_bit_matrix_t		*pmb,
	dk4_er_t				*erp
);


/**	Close bit matrix, release resources.

	CRT on Windows: Optional.
	@param	pbm	Bit matrix to close.
	@param	san	Flag: Sanitize memory.
*/

void
dk4bm_close(
	dk4_bit_matrix_t *pbm,
	int				  san
);


#ifdef	__cplusplus
}
#endif

%%	module

#include "dk4mem.h"
#include "dk4memrs.h"
#include "dk4bm.h"

$!trace-include


/**	One byte.
*/
typedef unsigned char  dk4bm_uc_t;


/**	Pointer to unsigned chars.
*/
typedef unsigned char *dk4bm_puc_t;


/**	Bits within a byte.
*/
static unsigned char bib[] = {
	0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01
};


/**	Constant value 0 for comparisons.
*/
#define	SZ0 ((size_t)0U)


/**	Constant value 8 for comparisons.
*/
#define	SZ8	((size_t)8U)


dk4_bit_matrix_t *
dk4bm_open(
	size_t					 rows,
	size_t					 columns,
	dk4_er_t				*erp
)
{
	dk4_bit_matrix_t	*back	= NULL;		/* Function result */
	dk4bm_puc_t			*ppuc;				/* Pointer: traverse back->data */
	size_t				 bpr;				/* Bytes per row */
	size_t				 i;					/* Index: traverse back->data */
	int					 ok;				/* Flag: All allocations ok */
	$? "+ dk4bm_open r=%lu c=%lu", (unsigned long)rows, (unsigned long)columns
	if ((SZ0 < rows) && (SZ0 < columns)) {
		bpr = columns / SZ8;
		if (SZ0 != (columns % SZ8)) bpr++;
		$? ". bpr=%lu", (unsigned long)bpr
		back = dk4mem_new(dk4_bit_matrix_t,1,erp);
		if (NULL != back) {
			back->rows			= rows;
			back->columns		= columns;
			back->bytes_per_row	= bpr;
			back->data			= dk4mem_new(dk4bm_puc_t,rows,erp);
			if (NULL != back->data) {
				/*
					Initialize all row pointers to NULL
				*/
				ppuc = back->data;
				for (i = 0; i < rows; i++) {
					*(ppuc++) = NULL;
				}
				/*
					Allocate the rows
				*/
				ppuc = back->data;
				ok = 1;
				for (i = 0; i < rows; i++) {
					*ppuc = dk4mem_new(dk4bm_uc_t,bpr,erp);
					if (NULL != *ppuc) {
						DK4_MEMRES(*ppuc,bpr);
					}
					else {			$? "! allocation (row)"
						ok = 0;
					}
					ppuc++;
				}
				/*
					Check allocations, delete object on error
				*/
				if (0 == ok) {
					dk4bm_close(back, 0);
					back = NULL;
				}
			}
			else {					$? "! allocation (pointers array)"
				dk4bm_close(back, 0);
				back = NULL;
			}
		}
#if	TRACE_DEBUG
		else {						$? "! allocation (matrix structure)"
		}
#endif
	}
	else {							$? "! arguments"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}	$? "- dk4bm_open %d", TR_IPTR(back)
	return back;
}


#define	USE_MEMCPY_FOR_ROWS	1

dk4_bit_matrix_t *
dk4bm_open_copy(
	dk4_bit_matrix_t const	*psrc,
	dk4_er_t				*erp
)
{
	dk4_bit_matrix_t	*back	= NULL;
#if	USE_MEMCPY_FOR_ROWS
	dk4bm_puc_t const	*ps;			/* Source data */
	dk4bm_puc_t			*pd;			/* Destination data */
#endif
	size_t				 i;				/* Current row number to copy */
#if	(!(USE_MEMCPY_FOR_ROWS))
	size_t				 j;
	int					 v;
#endif
	$? "+ dk4bm_open_copy"
	if (NULL != psrc) {
		back = dk4bm_open(psrc->rows, psrc->columns, erp);
		if (NULL != back) {
#if	USE_MEMCPY_FOR_ROWS
			ps = psrc->data;
			pd = back->data;
			for (i = SZ0; i < psrc->rows; i++) {
				DK4_MEMCPY(*pd,*ps,psrc->bytes_per_row);
				ps++; pd++;
			}
#endif
#if	(!(USE_MEMCPY_FOR_ROWS))
			for (i = SZ0; i < back->rows; i++) {
				for (j = SZ0; j < back->columns; j++) {
					v = 0;
					if (0 != dk4bm_get(&v, psrc, i, j, NULL)) {
						if (0 != v) {
							dk4bm_set(back, i, j, 1, NULL);
						}
					}
				}
			}
#endif
		}
	}
	else {
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}
	$? "- dk4bm_open_copy %d", TR_IPTR(back)
	return back;
}



int
dk4bm_set(
	dk4_bit_matrix_t		*pmb,
	size_t					 row,
	size_t					 column,
	int						 value,
	dk4_er_t				*erp
)
{
	size_t		 byteno;		/* Byte number within row */
	size_t		 bitno;			/* Bit number within byte */
	int			 back	= 0;	/* Function result */
	$? "+ dk4bm_set r=%lu c=%lu", (unsigned long)row, (unsigned long)column
	$? ". v=%d", value
	if ((NULL != pmb) && (NULL != pmb->data)) {
		if ((row < pmb->rows) && (column < pmb->columns)) {
			byteno	= column / SZ8;
			bitno   = column % SZ8;
			if (0 != value) {
				((pmb->data)[row])[byteno] |= bib[bitno];
			}
			else {
				((pmb->data)[row])[byteno] &= (~(bib[bitno]));
			}
			back = 1;
		}
		else {	$? "! arguments"
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {		$? "! arguments"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}	$? "- dk4bm_set %d", back
	return back;
}



int
dk4bm_get(
	int						*pdest,
	dk4_bit_matrix_t const	*pmb,
	size_t					 row,
	size_t					 column,
	dk4_er_t				*erp
)
{
	size_t	byteno;			/* Byte number within row */
	size_t	bitno;			/* Bit number within byte */
	int		back	= 0;	/* Function result */
	$? "+ dk4bm_get r=%lu c=%lu", (unsigned long)row, (unsigned long)column
	if ((NULL != pdest) && (NULL != pmb) && (NULL != pmb->data)) {
		if ((row < pmb->rows) && (column < pmb->columns)) {
			byteno	= column / SZ8;
			bitno	= column % SZ8;
			if (0x00 != ((((pmb->data)[row])[byteno]) & bib[bitno])) {
				*pdest = 1;
			}
			else {
				*pdest = 0;
			}
			back = 1;
		}
		else {	$? "! arguments"
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {		$? "! arguments"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}	$? "- dk4bm_get %d", back
	return back;
}



/*	2019-10-06
	----------
	Cache-friendly programming.
	----------
	To see whether there is an indirect path from node i to node j -- if
	not yet present -- we check a list of all nodes k whether there is a
	known path from i to k and a known path from k to j.
	A cache-friendly algorithm is used here.
	Instead of ijk-algorithm (also called "naive algorithm") the
	order of loops is changed here to ikj.
	Variable i is used as row index for both i->j and i->k, an array pointer
	is calculated from the row index i.
	Variable k is used as row index k->j but also as column index i->k.
	Variable j is only used as column index in both i->j and k->j, the
	column index results in an index within an array.
	So i is the variable for the outermost loop, k is the variable for the
	middle loop and j is the variable for the inner loop.
*/

int
dk4bm_expand(
	dk4_bit_matrix_t		*pmb,
	dk4_er_t				*erp
)
{
	dk4bm_uc_t		*ptri;			/* Pointer: Row i bytes */
	dk4bm_uc_t		*ptrk;			/* Pointer: Row k bytes */
	size_t			 i;				/* Destination row index */
	size_t			 j;				/* Destination column index */
	size_t			 k;				/* Intermediate point index */
	size_t			 i_byte;		/* Byte number of i within row */
	size_t			 i_bit;			/* Bit number of i within byte */
	size_t			 j_byte;		/* Byte number of j within row */
	size_t			 j_bit;			/* Bit number of j within byte */
	size_t			 k_byte;		/* Byte number of k within row */
	size_t			 k_bit;			/* Bit number of k within byte */
#if	TRACE_DEBUG
	unsigned long	 passno	= 0UL;	/* Current pass number */
#endif
	int				 changed;		/* Flag: Change occured in current pass */
	int		 		 back	= 0;	/* Function result */
	$? "+ dk4bm_expand"
	if ((NULL != pmb) && (NULL != pmb->data) && (pmb->rows == pmb->columns)) {
		if (0 < pmb->rows) {
			back = 1;
			changed = 1;
			while(0 != changed) {	$? ". begin pass %lu", passno++
				changed = 0;
				i_byte = 0;
				i_bit  = 0;
				for (i = 0; i < pmb->rows; i++) {
					$? ". i=%lu", (unsigned long)i
					ptri = (pmb->data)[i];
					k_byte = 0;
					k_bit  = 0;
					for (k = 0; k < pmb->rows; k++) {	
						$? ". i=%lu k=%lu", (unsigned long)i, (unsigned long)k
						ptrk = (pmb->data)[k];
						if (0x00 != (ptri[k_byte] & bib[k_bit])) {
							j_byte	= 0;
							j_bit	= 0;
							for (j = 0; j < pmb->rows; j++) {
								$? ". i=%lu k=%lu j=%lu", (unsigned long)i, (unsigned long)k, (unsigned long)j
								if (0x00 == (ptri[j_byte] & bib[j_bit])) {
									$? ". %lu -> %lu not yet set", (unsigned long)i, (unsigned long)j
									if (0x00 != (ptrk[j_byte] & bib[j_bit])) {
										$? ". set %lu -> %lu", (unsigned long)i, (unsigned long)j
										ptri[j_byte] |= bib[j_bit];
										changed = 1;
									}
								}
								j_bit++;
								if (8 <= j_bit) {
									j_bit = 0; j_byte++;
								}
							}
						}
						k_bit++;
						if (8 <= k_bit) {
							k_bit = 0; k_byte++;
						}
					}
					i_bit++;
					if (8 <= i_bit) {
						i_bit = 0; i_byte++;
					}
				}	$? ". end pass changed=%d", changed
			}
		}
		else {	$? "! arguments"
			dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
		}
	}
	else {		$? "! arguments"
		dk4error_set_simple_error_code(erp, DK4_E_INVALID_ARGUMENTS);
	}	$? "- dk4bm_expand %d", back
	return back;
}



void
dk4bm_close(
	dk4_bit_matrix_t *pbm,
	int				  san
)
{
	dk4bm_puc_t		*ppuc;		/* Pointer: Traverse pbm->data */
	size_t			 i;			/* Index: Traverse pbm->data */
	$? "+ dk4bm_close"
	if (NULL != pbm) {
		if (NULL != pbm->data) {
			ppuc = pbm->data;
			for (i = 0; i < pbm->rows; i++) {
				if (NULL != *ppuc) {
					if (0 != san) {
						dk4mem_reset_secure(*ppuc,pbm->bytes_per_row,NULL);
					}
					dk4mem_free(*ppuc);
				}
				ppuc++;
			}
			dk4mem_free(pbm->data);
		}
		if (0 != san) {
			dk4mem_reset_secure(pbm,sizeof(dk4_bit_matrix_t),NULL);
		}
		dk4mem_free(pbm);
	}	$? "- dk4bm_close"
}



/* vim: set ai sw=4 ts=4 : */