smntc
an in-memory multimodal graph database
Loading...
Searching...
No Matches
Colors.c
Go to the documentation of this file.
1#include <stdlib.h>
2#include <limits.h>
3#include "errors.h"
4#include "Graph.h"
5#include "Binary.h"
6
7#include <stdio.h>
8
9#define COLOR_CLUSTER_LENGTH 2
10#define CHANNEL_SOURCES_LENGTH 3
11#define CHANNEL_CLUSTER_LENGTH 3
12
13struct Colors {
14 struct Binary *binary;
15 struct Graph *graph;
16 unsigned int *colorCache;
17 unsigned int colorType;
18 unsigned int colorDepth;
19};
20
21struct Colors *Colors_construct(unsigned int colorType, struct Binary *binary, struct Graph *graph, int *error)
22{
23 struct Colors *this = malloc(sizeof(struct Colors));
24
25 if (0 == this) {
26 *error = ERROR_NO_MEMORY;
27 return 0;
28 }
29
30 this->binary = binary;
31 this->graph = graph;
32 this->colorType = colorType;
33 this->colorDepth = UCHAR_MAX + 1;
34
35 this->colorCache = calloc(this->colorDepth * this->colorDepth * this->colorDepth, sizeof(unsigned int));
36
37 if (0 == this->colorCache) {
38 *error = ERROR_NO_MEMORY;
39 return 0;
40 }
41
42 return this;
43}
44
45struct Colors *Colors_destruct(struct Colors *this)
46{
47 if (0 != this) {
49 free(this->colorCache);
50 free(this);
51 }
52
53 return 0;
54}
55
56void Colors_initialize(struct Colors *this, int *error)
57{
58 unsigned int length = this->colorDepth * this->colorDepth * this->colorDepth;
59 unsigned int *colorTypes = calloc(length, sizeof(unsigned int));
60
61 Graph_readTargets(this->graph, this->colorType, length, colorTypes, error);
62
63 if (*error) {
64 return;
65 }
66 unsigned int colorCluster[COLOR_CLUSTER_LENGTH];
67 unsigned int colorSources[CHANNEL_SOURCES_LENGTH];
68 unsigned int redCluster[CHANNEL_CLUSTER_LENGTH];
69 unsigned int greenCluster[CHANNEL_CLUSTER_LENGTH];
70 unsigned int blueCluster[CHANNEL_CLUSTER_LENGTH];
71
72 for (unsigned int index = 0; index < length; index ++) {
73
74 unsigned int colorType = colorTypes[index];
75
76 if (0 == colorType) {
77 break;
78 }
79
80 Graph_readCluster(this->graph, colorType, COLOR_CLUSTER_LENGTH, colorCluster, error);
81
82 if (*error) {
83 return;
84 }
85
86 unsigned int color = colorCluster[1];
87
88 Graph_readSources(this->graph, color, CHANNEL_SOURCES_LENGTH, colorSources, error);
89
90 if (*error) {
91 return;
92 }
93
94 // red
95
96 Graph_readCluster(this->graph, colorSources[2], CHANNEL_CLUSTER_LENGTH, redCluster, error);
97
98 if (*error) {
99 return;
100 }
101
102 unsigned char redValue = Binary_readCode(this->binary, redCluster[0], error);
103
104 if (*error) {
105 return;
106 }
107
108 // green
109
110 Graph_readCluster(this->graph, colorSources[2], CHANNEL_CLUSTER_LENGTH, greenCluster, error);
111
112 if (*error) {
113 return;
114 }
115
116 unsigned char greenValue = Binary_readCode(this->binary, greenCluster[1], error);
117
118 if (*error) {
119 return;
120 }
121
122 // blue
123
124 Graph_readCluster(this->graph, colorSources[2], CHANNEL_CLUSTER_LENGTH, blueCluster, error);
125
126 if (*error) {
127 return;
128 }
129
130 unsigned char blueValue = Binary_readCode(this->binary, blueCluster[2], error);
131
132 if (*error) {
133 return;
134 }
135
136 // colorCache
137
138 unsigned int index = (redValue << 16) | (greenValue << 8) | blueValue;
139
140 this->colorCache[index] = color;
141 }
142
143 free(colorTypes);
144}
145
146unsigned int Colors_writeColor(struct Colors *this, unsigned char redValue, unsigned char greenValue, unsigned char blueValue, int *error)
147{
148 unsigned int index = (redValue << 16) | (greenValue << 8) | blueValue;
149
150 unsigned int redCluster[CHANNEL_CLUSTER_LENGTH];
151 unsigned int greenCluster[CHANNEL_CLUSTER_LENGTH];
152 unsigned int blueCluster[CHANNEL_CLUSTER_LENGTH];
153
154 if (0 == this->colorCache[index]) {
155
156 // red
157
158 unsigned int redCode = Binary_writeCode(this->binary, redValue, error);
159
160 if (*error) {
161 return 0;
162 }
163
164 unsigned int redClusterStart = Graph_readLastTarget(this->graph, redCode, error);
165
166 if (*error) {
167 return 0;
168 }
169
170 if (0 == redClusterStart) {
171
172 Graph_addCluster(this->graph, CHANNEL_CLUSTER_LENGTH, redCluster, error);
173
174 if (*error) {
175 return 0;
176 }
177
178 Graph_addEdge(this->graph, redCode, redCluster[0], error);
179
180 if (*error) {
181 return 0;
182 }
183
184 } else {
185
186 Graph_readCluster(this->graph, redClusterStart, CHANNEL_CLUSTER_LENGTH, redCluster, error);
187
188 if (*error) {
189 return 0;
190 }
191 }
192
193 unsigned int red = redCluster[0];
194
195 // green
196
197 unsigned int greenCode = Binary_writeCode(this->binary, greenValue, error);
198
199 if (*error) {
200 return 0;
201 }
202
203 unsigned int greenClusterStart = Graph_readLastTarget(this->graph, greenCode, error);
204
205 if (*error) {
206 return 0;
207 }
208
209 if (0 == greenClusterStart) {
210
211 Graph_addCluster(this->graph, CHANNEL_CLUSTER_LENGTH, greenCluster, error);
212
213 if (*error) {
214 return 0;
215 }
216
217 Graph_addEdge(this->graph, greenCode, greenCluster[0], error);
218
219 if (*error) {
220 return 0;
221 }
222
223 } else {
224
225 Graph_readCluster(this->graph, greenClusterStart, CHANNEL_CLUSTER_LENGTH, greenCluster, error);
226
227 if (*error) {
228 return 0;
229 }
230 }
231
232 unsigned int green = greenCluster[1];
233
234 // blue
235
236 unsigned int blueCode = Binary_writeCode(this->binary, blueValue, error);
237
238 if (*error) {
239 return 0;
240 }
241
242 unsigned int blueClusterStart = Graph_readLastTarget(this->graph, blueCode, error);
243
244 if (*error) {
245 return 0;
246 }
247
248 if (0 == blueClusterStart) {
249
250 Graph_addCluster(this->graph, CHANNEL_CLUSTER_LENGTH, blueCluster, error);
251
252 if (*error) {
253 return 0;
254 }
255
256 Graph_addEdge(this->graph, blueCode, blueCluster[0], error);
257
258 if (*error) {
259 return 0;
260 }
261
262 } else {
263
264 Graph_readCluster(this->graph, blueClusterStart, CHANNEL_CLUSTER_LENGTH, blueCluster, error);
265
266 if (*error) {
267 return 0;
268 }
269 }
270
271 unsigned int blue = blueCluster[2];
272
273 // color
274
275 unsigned int colorCluster[2];
276
277 Graph_addCluster(this->graph, 2, colorCluster, error);
278
279 if (*error) {
280 return 0;
281 }
282
283 unsigned int color = colorCluster[0];
284 unsigned int type = colorCluster[1];
285
286 Graph_addEdge(this->graph, this->colorType, type, error);
287
288 if (*error) {
289 return 0;
290 }
291
292 Graph_addEdge(this->graph, red, color, error);
293
294 if (*error) {
295 return 0;
296 }
297
298 Graph_addEdge(this->graph, green, color, error);
299
300 if (*error) {
301 return 0;
302 }
303
304 Graph_addEdge(this->graph, blue, color, error);
305
306 if (*error) {
307 return 0;
308 }
309
310 this->colorCache[index] = color;
311 }
312
313 return this->colorCache[index];
314
315 return 0;
316}
317
318void Colors_readColor(const struct Colors *this, unsigned int color, unsigned char *redValue, unsigned char *greenValue, unsigned char *blueValue, int *error)
319{
320 unsigned int colorCluster[COLOR_CLUSTER_LENGTH] = { 0, 0 };
321
322 Graph_readCluster(this->graph, color, COLOR_CLUSTER_LENGTH, colorCluster, error);
323
324 if (*error) {
325 return;
326 }
327
328 if (0 == colorCluster[0] && 0 == colorCluster[1]) {
329 *error = ERROR_NOT_A_COLOR;
330 return;
331 }
332
333 unsigned int colorType = Graph_readLastSource(this->graph, colorCluster[1], error);
334
335 if (colorType != this->colorType) {
336 *error = ERROR_NOT_A_COLOR;
337 return;
338 }
339
340 unsigned int colorSources[CHANNEL_SOURCES_LENGTH] = { 0, 0, 0 };
341
342 Graph_readSources(this->graph, color, CHANNEL_SOURCES_LENGTH, colorSources, error);
343
344 if (*error) {
345 return;
346 }
347
348 // red
349
350 unsigned int redCluster[CHANNEL_CLUSTER_LENGTH] = { 0, 0, 0};
351
352 Graph_readCluster(this->graph, colorSources[2], CHANNEL_CLUSTER_LENGTH, redCluster, error);
353
354 if (*error) {
355 return;
356 }
357
358 unsigned int redCode = Graph_readLastSource(this->graph, redCluster[0], error);
359
360 if (*error) {
361 return;
362 }
363
364 *redValue = Binary_readCode(this->binary, redCode, error);
365
366 if (*error) {
367 return;
368 }
369
370 // green
371
372 unsigned int greenCluster[CHANNEL_CLUSTER_LENGTH] = { 0, 0, 0};
373
374 Graph_readCluster(this->graph, colorSources[1], CHANNEL_CLUSTER_LENGTH, greenCluster, error);
375
376 if (*error) {
377 return;
378 }
379
380 unsigned int greenCode = Graph_readLastSource(this->graph, greenCluster[2], error);
381
382 if (*error) {
383 return;
384 }
385
386 *greenValue = Binary_readCode(this->binary, greenCode, error);
387
388 if (*error) {
389 return;
390 }
391
392 // blue
393
394 unsigned int blueCluster[CHANNEL_CLUSTER_LENGTH] = { 0, 0, 0};
395
396 Graph_readCluster(this->graph, colorSources[0], CHANNEL_CLUSTER_LENGTH, blueCluster, error);
397
398 if (*error) {
399 return;
400 }
401
402 unsigned int blueCode = Graph_readLastSource(this->graph, blueCluster[1], error);
403
404 if (*error) {
405 return;
406 }
407
408 *blueValue = Binary_readCode(this->binary, blueCode, error);
409}
unsigned int Binary_writeCode(struct Binary *this, unsigned int input, int *error)
It writes binary value from an unsigned integer into a graph.
Definition Binary.c:96
struct Binary * Binary_destruct(struct Binary *this)
Definition Binary.c:87
unsigned int Binary_readCode(struct Binary *this, unsigned int code, int *error)
It reads binary value into an unsigned integer when given a vertex from a binary cluster.
Definition Binary.c:211
#define CHANNEL_CLUSTER_LENGTH
Definition Colors.c:11
void Colors_initialize(struct Colors *this, int *error)
Definition Colors.c:56
unsigned int Colors_writeColor(struct Colors *this, unsigned char redValue, unsigned char greenValue, unsigned char blueValue, int *error)
Definition Colors.c:146
struct Colors * Colors_construct(unsigned int colorType, struct Binary *binary, struct Graph *graph, int *error)
Definition Colors.c:21
void Colors_readColor(const struct Colors *this, unsigned int color, unsigned char *redValue, unsigned char *greenValue, unsigned char *blueValue, int *error)
Definition Colors.c:318
struct Colors * Colors_destruct(struct Colors *this)
Definition Colors.c:45
#define COLOR_CLUSTER_LENGTH
Definition Colors.c:9
#define CHANNEL_SOURCES_LENGTH
Definition Colors.c:10
void Graph_readSources(const struct Graph *this, unsigned int target, unsigned int length, unsigned int *sources, int *error)
It reads all source vertices that have an edge leading to a particular target vertex.
Definition Graph.c:526
void Graph_addEdge(struct Graph *this, unsigned int source, unsigned int target, int *error)
It adds a directed edge from a source vertex to a target vertex.
Definition Graph.c:298
unsigned int Graph_readLastTarget(const struct Graph *this, unsigned int source, int *error)
It reads the last target vertex of a source vertex, if it exists.
Definition Graph.c:712
unsigned int Graph_readLastSource(const struct Graph *this, unsigned int target, int *error)
It reads the last source vertex of a target vertex, if it exists.
Definition Graph.c:680
void Graph_readCluster(const struct Graph *this, unsigned int predecessor, unsigned int length, unsigned int *vertices, int *error)
Definition Graph.c:776
void Graph_readTargets(const struct Graph *this, unsigned int source, unsigned int length, unsigned int *targets, int *error)
It reads all target vertices that have an edge leading from a particular source vertex.
Definition Graph.c:603
void Graph_addCluster(struct Graph *this, unsigned int length, unsigned int *vertices, int *error)
Definition Graph.c:744
#define ERROR_NOT_A_COLOR
Definition errors.h:11
#define ERROR_NO_MEMORY
Definition errors.h:5
unsigned int colorDepth
Definition Colors.c:18
unsigned int * colorCache
Definition Colors.c:16
unsigned int colorType
Definition Colors.c:17
struct Graph * graph
Definition Colors.c:15
struct Binary * binary
Definition Colors.c:14
Definition Graph.c:20