aboutsummaryrefslogtreecommitdiff
path: root/src/utils/math.h
diff options
context:
space:
mode:
authorSebastiano Tronto <sebastiano@tronto.net>2025-07-29 10:00:56 +0200
committerSebastiano Tronto <sebastiano@tronto.net>2025-07-29 10:00:56 +0200
commit38014615238a51f1c0c1f2f96d37cbfbf765520a (patch)
tree24fff81dd4692ba91b96dd0ab099e043b7108824 /src/utils/math.h
parent1d93860c6f6d3fdbdddabdcd870ef0f8b6e47c81 (diff)
downloadnissy-core-38014615238a51f1c0c1f2f96d37cbfbf765520a.tar.gz
nissy-core-38014615238a51f1c0c1f2f96d37cbfbf765520a.zip
Optimized some coordinates
Diffstat (limited to 'src/utils/math.h')
-rw-r--r--src/utils/math.h22
1 files changed, 12 insertions, 10 deletions
diff --git a/src/utils/math.h b/src/utils/math.h
index 9936558..ebbd63e 100644
--- a/src/utils/math.h
+++ b/src/utils/math.h
@@ -49,7 +49,7 @@ permtoindex(size_t n, const uint8_t *a)
49 49
50 for (i = 0, ret = 0; i < n; i++) { 50 for (i = 0, ret = 0; i < n; i++) {
51 for (j = i+1, c = 0; j < n; j++) 51 for (j = i+1, c = 0; j < n; j++)
52 c += (a[i] > a[j]) ? 1 : 0; 52 c += a[i] > a[j];
53 ret += factorial[n-i-1] * c; 53 ret += factorial[n-i-1] * c;
54 } 54 }
55 55
@@ -59,21 +59,23 @@ permtoindex(size_t n, const uint8_t *a)
59STATIC void 59STATIC void
60indextoperm(int64_t p, size_t n, uint8_t *r) 60indextoperm(int64_t p, size_t n, uint8_t *r)
61{ 61{
62 int64_t c; 62 int64_t c, k;
63 size_t i, j; 63 size_t i, j, used;
64 uint8_t a[FACTORIAL_MAX+1];
65 64
66 DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute indextoperm() " 65 DBG_ASSERT(n <= FACTORIAL_MAX, "Error: cannot compute indextoperm() "
67 "for set of size %zu > %" PRId64 "\n", n, FACTORIAL_MAX); 66 "for set of size %zu > %" PRId64 "\n", n, FACTORIAL_MAX);
68 DBG_ASSERT(p >= 0 && p < factorial[n], "Error: invalid permutation " 67 DBG_ASSERT(p >= 0 && p < factorial[n], "Error: invalid permutation "
69 "index %" PRId64 " for set of size %zu\n", p, n); 68 "index %" PRId64 " for set of size %zu\n", p, n);
70 69
71 memset(a, 0, n); 70 for (i = 0, used = 0; i < n; i++) {
72 for (i = 0; i < n; i++) { 71 k = p / factorial[n-i-1];
73 for (j = 0, c = 0; c <= p / factorial[n-i-1]; j++) 72
74 c += a[j] ? 0 : 1; 73 /* Find k-th unused number */
74 for (j = 0, c = 0; c <= k; j++)
75 c += 1 - ((used & (1<<j)) >> j);
76
75 r[i] = j-1; 77 r[i] = j-1;
76 a[j-1] = 1; 78 used |= 1 << (j-1);
77 p %= factorial[n-i-1]; 79 p %= factorial[n-i-1];
78 } 80 }
79 81
@@ -87,7 +89,7 @@ permsign(size_t n, const uint8_t *a)
87 89
88 for (i = 0, ret = 0; i < n; i++) 90 for (i = 0, ret = 0; i < n; i++)
89 for (j = i+1; j < n; j++) 91 for (j = i+1; j < n; j++)
90 ret += a[i] > a[j] ? 1 : 0; 92 ret += a[i] > a[j];
91 93
92 return ret % 2; 94 return ret % 2;
93} 95}

Generated with cgit - Back to sebastiano.tronto.net