FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
buildSquareVisitor.c
1 /*
2 Author: Daniele Fognini, Andreas Wuerl
3 Copyright (C) 2013-2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 #include <math.h>
20 #include <stdio.h>
21 #include <glib.h>
22 
23 #include "monk.h"
24 
25 #define SIZE MAX_ALLOWED_DIFF_LENGTH
26 
27 #ifndef M_PI_4
28 #define M_PI_2 1.57079632679489661923 /* pi/2 */
29 #define M_PI_4 0.78539816339744830962 /* pi/4 */
30 #endif
31 
32 typedef struct {
33  unsigned int x;
34  unsigned int y;
35  unsigned int time;
36 } point;
37 
38 gint pointSorter(gconstpointer a, gconstpointer b) {
39  unsigned int aTime = ((const point *) a)->time;
40  unsigned int bTime = ((const point *) b)->time;
41  if (aTime > bTime)
42  return 1;
43  if (aTime < bTime)
44  return -1;
45  return 0;
46 }
47 
48 void circleVisit(unsigned int timeOfVisit[SIZE][SIZE]) {
49  unsigned int time = 0;
50 
51  for (double r = 0; r < SIZE; r += 0.5)
52  for (double theta = 0; theta < M_PI_2; theta += M_PI_4 / (SIZE)) {
53  time += 1;
54  int x = floor(r * sin(theta));
55  int y = floor(r * cos(theta));
56  if ((x < SIZE) && (x >= 0) && (y < SIZE) && (y >= 0))
57  timeOfVisit[x][y] = time;
58  }
59 }
60 
61 GArray* generateTimeOrderedVisitor(unsigned int timeOfVisit[SIZE][SIZE]) {
62  GArray* visitor = g_array_new(TRUE, FALSE, sizeof(point));
63  for (unsigned int i = 0; i < SIZE; i++)
64  for (unsigned int j = 0; j < SIZE; j++) {
65  point p;
66  p.x = i;
67  p.y = j;
68  p.time = timeOfVisit[i][j];
69  if ((p.time > 0) && (i + j > 0))
70  g_array_append_val(visitor, p);
71  }
72 
73  g_array_sort(visitor, pointSorter);
74  return visitor;
75 }
76 
77 int writeVisitorToSourceFiles(GArray* visitor) {
78  FILE* fc = fopen("_squareVisitor.c", "w");
79 
80  if (!fc) {
81  return 2;
82  }
83 
84  fprintf(fc, "unsigned int squareVisitorX[] = ");
85  point p0 = g_array_index(visitor, point, 0);
86  fprintf(fc, "{%u", p0.x);
87  for (size_t i = 1; i < visitor->len; i++) {
88  point p = g_array_index(visitor, point, i);
89  fprintf(fc, ",\n\t%u", p.x);
90  }
91  fprintf(fc, "};\n");
92 
93  fprintf(fc, "unsigned int squareVisitorY[] = ");
94  fprintf(fc, "{%u", p0.y);
95  for (size_t i = 1; i < visitor->len; i++) {
96  point p = g_array_index(visitor, point, i);
97  fprintf(fc, ",\n\t%u", p.y);
98  }
99  fprintf(fc, "};\n");
100 
101  fclose(fc);
102 
103  FILE* fh = fopen("_squareVisitor.h.gen", "w");
104 
105  if (!fh) {
106  return 2;
107  }
108 
109  fprintf(fh, "#define SQUARE_VISITOR_LENGTH %u\n", visitor->len);
110 
111  fclose(fh);
112  return 0;
113 }
114 
115 int main() {
116  unsigned int timeOfVisit[SIZE][SIZE];
117 
118  for (int i = 0; i < SIZE; i++)
119  for (int j = 0; j < SIZE; j++)
120  timeOfVisit[i][j] = 0;
121 
122  circleVisit(timeOfVisit);
123 
124 #ifdef SQUARE_BUILDER_DEBUG
125  printf("time of visit:\n");
126  for (int i = 0; i < SIZE; i++) {
127  for (int j = 0; j < SIZE; j++)
128  printf("%u ", timeOfVisit[i][j]);
129  printf("\n");
130  }
131 
132  printf("visited:\n");
133  for (int i = 0; i < SIZE; i++) {
134  for (int j = 0; j < SIZE; j++)
135  if (timeOfVisit[i][j] > 0)
136  printf("+");
137  else
138  printf("-");
139  printf("\n");
140  }
141 #endif //SQUARE_BUILDER_DEBUG
142 
143  GArray* visitor = generateTimeOrderedVisitor(timeOfVisit);
144 
145  if (visitor->len == 0)
146  return 1;
147 
148 #ifdef SQUARE_BUILDER_DEBUG
149  printf("sorted visitor is:\n");
150  for (size_t i = 0; i < visitor->len; i++) {
151  point p = g_array_index(visitor, point, i);
152  printf("[%u]:{%u,%u}, ", p.time, p.x, p.y);
153  }
154  printf("\n");
155 #endif //SQUARE_BUILDER_DEBUG
156 
157  return writeVisitorToSourceFiles(visitor);
158 }