summaryrefslogtreecommitdiff
path: root/support/dktools/dk4bm.h
blob: cc9c07e8ae3779e34e6f66c7fa3cd11fc103f751 (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
/*
Copyright (C) 2019-2020, Dirk Krause
SPDX-License-Identifier: BSD-3-Clause
*/

/*
	WARNING: This file was generated by the dkct program (see
	http://dktools.sourceforge.net/ for details).
	Changes you make here will be lost if dkct is run again!
	You should modify the original source and run dkct on it.
	Original source: dk4bm.ctr
*/

#ifndef DK4BM_H_INCLUDED
/** Avoid multiple inclusions. */
#define DK4BM_H_INCLUDED 1


#line 9 "dk4bm.ctr"

/**	@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


#endif