diff options
Diffstat (limited to 'examples')
-rw-r--r-- | examples/enough.c | 12 |
1 files changed, 8 insertions, 4 deletions
diff --git a/examples/enough.c b/examples/enough.c index 56ad63e..3f8c0d2 100644 --- a/examples/enough.c +++ b/examples/enough.c | |||
@@ -18,6 +18,7 @@ | |||
18 | Clean up comparisons of different types | 18 | Clean up comparisons of different types |
19 | Clean up code indentation | 19 | Clean up code indentation |
20 | 1.5 1 Aug 2018 Clean up code style and formatting | 20 | 1.5 1 Aug 2018 Clean up code style and formatting |
21 | Use inline function instead of macro for index | ||
21 | */ | 22 | */ |
22 | 23 | ||
23 | /* | 24 | /* |
@@ -179,7 +180,10 @@ struct { | |||
179 | } g; | 180 | } g; |
180 | 181 | ||
181 | // Index function for num[] and done[]. | 182 | // Index function for num[] and done[]. |
182 | #define INDEX(i,j,k) (((size_t)((i-1)>>1)*((i-2)>>1)+(j>>1)-1)*(g.max-1)+k-1) | 183 | local inline size_t map(int i, int j, int k) { |
184 | return k - 1 + ((size_t)((i - 1) >> 1) * ((i - 2) >> 1) + (j >> 1) - 1) * | ||
185 | (g.max - 1); | ||
186 | } | ||
183 | 187 | ||
184 | // Free allocated space. Uses globals code, num, and done. | 188 | // Free allocated space. Uses globals code, num, and done. |
185 | local void cleanup(void) { | 189 | local void cleanup(void) { |
@@ -218,7 +222,7 @@ local big_t count(int syms, int len, int left) { | |||
218 | assert(syms > left && left > 0 && len < g.max); | 222 | assert(syms > left && left > 0 && len < g.max); |
219 | 223 | ||
220 | // see if we've done this one already | 224 | // see if we've done this one already |
221 | index = INDEX(syms, left, len); | 225 | index = map(syms, left, len); |
222 | got = g.num[index]; | 226 | got = g.num[index]; |
223 | if (got) | 227 | if (got) |
224 | return got; // we have -- return the saved result | 228 | return got; // we have -- return the saved result |
@@ -264,7 +268,7 @@ local int beenhere(int syms, int len, int left, int mem, int rem) { | |||
264 | char *vector; // new or enlarged bit vector | 268 | char *vector; // new or enlarged bit vector |
265 | 269 | ||
266 | // point to vector for (syms,left,len), bit in vector for (mem,rem) | 270 | // point to vector for (syms,left,len), bit in vector for (mem,rem) |
267 | index = INDEX(syms, left, len); | 271 | index = map(syms, left, len); |
268 | mem -= 1 << g.root; | 272 | mem -= 1 << g.root; |
269 | offset = (mem >> 3) + rem; | 273 | offset = (mem >> 3) + rem; |
270 | offset = ((offset * (offset + 1)) >> 1) + rem; | 274 | offset = ((offset * (offset + 1)) >> 1) + rem; |
@@ -416,7 +420,7 @@ local void enough(int syms) { | |||
416 | for (left = 2; left < n; left += 2) { | 420 | for (left = 2; left < n; left += 2) { |
417 | // look at all reachable (root + 1) bit nodes, and the | 421 | // look at all reachable (root + 1) bit nodes, and the |
418 | // resulting codes (complete at root + 2 or more) | 422 | // resulting codes (complete at root + 2 or more) |
419 | index = INDEX(n, left, g.root + 1); | 423 | index = map(n, left, g.root + 1); |
420 | if (g.root + 1 < g.max && g.num[index]) // reachable node | 424 | if (g.root + 1 < g.max && g.num[index]) // reachable node |
421 | examine(n, g.root + 1, left, 1 << g.root, 0); | 425 | examine(n, g.root + 1, left, 1 << g.root, 0); |
422 | 426 | ||