FOSSology  4.4.0
Open Source License Compliance by Open Source Software
buildSquareVisitor.c
1 /*
2  Author: Daniele Fognini, Andreas Wuerl
3  SPDX-FileCopyrightText: © 2013-2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 #include <math.h>
9 #include <stdio.h>
10 #include <glib.h>
11 
12 #include "monk.h"
13 
14 #define SIZE MAX_ALLOWED_DIFF_LENGTH
15 
16 #ifndef M_PI_4
17 #define M_PI_2 1.57079632679489661923 /* pi/2 */
18 #define M_PI_4 0.78539816339744830962 /* pi/4 */
19 #endif
20 
21 typedef struct {
22  unsigned int x;
23  unsigned int y;
24  unsigned int time;
25 } point;
26 
27 gint pointSorter(gconstpointer a, gconstpointer b) {
28  unsigned int aTime = ((const point *) a)->time;
29  unsigned int bTime = ((const point *) b)->time;
30  if (aTime > bTime)
31  return 1;
32  if (aTime < bTime)
33  return -1;
34  return 0;
35 }
36 
37 void circleVisit(unsigned int timeOfVisit[SIZE][SIZE]) {
38  unsigned int time = 0;
39 
40  for (double r = 0; r < SIZE; r += 0.5)
41  for (double theta = 0; theta < M_PI_2; theta += M_PI_4 / (SIZE)) {
42  time += 1;
43  int x = floor(r * sin(theta));
44  int y = floor(r * cos(theta));
45  if ((x < SIZE) && (x >= 0) && (y < SIZE) && (y >= 0))
46  timeOfVisit[x][y] = time;
47  }
48 }
49 
50 GArray* generateTimeOrderedVisitor(unsigned int timeOfVisit[SIZE][SIZE]) {
51  GArray* visitor = g_array_new(TRUE, FALSE, sizeof(point));
52  for (unsigned int i = 0; i < SIZE; i++)
53  for (unsigned int j = 0; j < SIZE; j++) {
54  point p;
55  p.x = i;
56  p.y = j;
57  p.time = timeOfVisit[i][j];
58  if ((p.time > 0) && (i + j > 0))
59  g_array_append_val(visitor, p);
60  }
61 
62  g_array_sort(visitor, pointSorter);
63  return visitor;
64 }
65 
66 int writeVisitorToSourceFiles(GArray* visitor) {
67  FILE* fc = fopen("_squareVisitor.c", "w");
68 
69  if (!fc) {
70  return 2;
71  }
72 
73  fprintf(fc, "unsigned int squareVisitorX[] = ");
74  point p0 = g_array_index(visitor, point, 0);
75  fprintf(fc, "{%u", p0.x);
76  for (size_t i = 1; i < visitor->len; i++) {
77  point p = g_array_index(visitor, point, i);
78  fprintf(fc, ",\n\t%u", p.x);
79  }
80  fprintf(fc, "};\n");
81 
82  fprintf(fc, "unsigned int squareVisitorY[] = ");
83  fprintf(fc, "{%u", p0.y);
84  for (size_t i = 1; i < visitor->len; i++) {
85  point p = g_array_index(visitor, point, i);
86  fprintf(fc, ",\n\t%u", p.y);
87  }
88  fprintf(fc, "};\n");
89 
90  fclose(fc);
91 
92  FILE* fh = fopen("_squareVisitor.h.gen", "w");
93 
94  if (!fh) {
95  return 2;
96  }
97 
98  fprintf(fh, "#define SQUARE_VISITOR_LENGTH %u\n", visitor->len);
99 
100  fclose(fh);
101  return 0;
102 }
103 
104 int main() {
105  unsigned int timeOfVisit[SIZE][SIZE];
106 
107  for (int i = 0; i < SIZE; i++)
108  for (int j = 0; j < SIZE; j++)
109  timeOfVisit[i][j] = 0;
110 
111  circleVisit(timeOfVisit);
112 
113 #ifdef SQUARE_BUILDER_DEBUG
114  printf("time of visit:\n");
115  for (int i = 0; i < SIZE; i++) {
116  for (int j = 0; j < SIZE; j++)
117  printf("%u ", timeOfVisit[i][j]);
118  printf("\n");
119  }
120 
121  printf("visited:\n");
122  for (int i = 0; i < SIZE; i++) {
123  for (int j = 0; j < SIZE; j++)
124  if (timeOfVisit[i][j] > 0)
125  printf("+");
126  else
127  printf("-");
128  printf("\n");
129  }
130 #endif //SQUARE_BUILDER_DEBUG
131 
132  GArray* visitor = generateTimeOrderedVisitor(timeOfVisit);
133 
134  if (visitor->len == 0)
135  return 1;
136 
137 #ifdef SQUARE_BUILDER_DEBUG
138  printf("sorted visitor is:\n");
139  for (size_t i = 0; i < visitor->len; i++) {
140  point p = g_array_index(visitor, point, i);
141  printf("[%u]:{%u,%u}, ", p.time, p.x, p.y);
142  }
143  printf("\n");
144 #endif //SQUARE_BUILDER_DEBUG
145 
146  return writeVisitorToSourceFiles(visitor);
147 }