diff options
Diffstat (limited to 'src/utils.h')
| -rw-r--r-- | src/utils.h | 91 |
1 files changed, 37 insertions, 54 deletions
diff --git a/src/utils.h b/src/utils.h index 45a6302..80c33ae 100644 --- a/src/utils.h +++ b/src/utils.h | |||
| @@ -1,58 +1,41 @@ | |||
| 1 | #define min(a,b) (((a) < (b)) ? (a) : (b)) | 1 | #ifndef UTILS_H |
| 2 | #define max(a,b) (((a) > (b)) ? (a) : (b)) | 2 | #define UTILS_H |
| 3 | #define abs(a) (((a) > 0) ? (a) : (-(a))) | ||
| 4 | 3 | ||
| 5 | /* Some useful constants */ | 4 | #include <stdbool.h> |
| 6 | #define pow2to11 2048 | 5 | #include <stdlib.h> |
| 7 | #define pow2to12 4096 | 6 | #include <string.h> |
| 8 | #define pow3to7 2187 | ||
| 9 | #define pow3to8 6561 | ||
| 10 | #define pow12to4 20736 | ||
| 11 | #define factorial4 24 | ||
| 12 | #define factorial6 720 | ||
| 13 | #define factorial8 40320 | ||
| 14 | #define factorial12 479001600 | ||
| 15 | #define binom12on4 495 | ||
| 16 | #define binom8on4 70 | ||
| 17 | 7 | ||
| 18 | void swap(int *a, int *b); | 8 | #define POW2TO6 64ULL |
| 9 | #define POW2TO11 2048ULL | ||
| 10 | #define POW2TO12 4096ULL | ||
| 11 | #define POW3TO7 2187ULL | ||
| 12 | #define POW3TO8 6561ULL | ||
| 13 | #define FACTORIAL4 24ULL | ||
| 14 | #define FACTORIAL6 720ULL | ||
| 15 | #define FACTORIAL7 5040ULL | ||
| 16 | #define FACTORIAL8 40320ULL | ||
| 17 | #define FACTORIAL12 479001600ULL | ||
| 18 | #define BINOM12ON4 495ULL | ||
| 19 | #define BINOM8ON4 70ULL | ||
| 20 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) | ||
| 21 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) | ||
| 19 | 22 | ||
| 20 | /* Hardcoded factorial of small numbers (n<=12). */ | 23 | void apply_permutation(int *perm, int *set, int n); |
| 21 | extern int factorial[13]; | 24 | int binomial(int n, int k); |
| 22 | 25 | int digit_array_to_int(int *a, int n, int b); | |
| 23 | /* Converts the integer a to its representation in base b (first n digits | 26 | int factorial(int n); |
| 24 | * only) and saves the result in r. */ | 27 | void index_to_perm(int p, int n, int *r); |
| 25 | void int_to_digit_array(int a, int b, int n, int *r); | 28 | void index_to_subset(int s, int n, int k, int *r); |
| 26 | 29 | void int_to_digit_array(int a, int b, int n, int *r); | |
| 27 | /* Converts the array of n digits a to a integer using base b. */ | 30 | void int_to_sum_zero_array(int x, int b, int n, int *a); |
| 28 | int digit_array_to_int(int *a, int n, int b); | 31 | int invert_digits(int a, int b, int n); |
| 29 | 32 | bool is_perm(int *a, int n); | |
| 30 | /* Converts a permutation on [0..(n-1)] into the integer i which is the index | 33 | bool is_subset(int *a, int n, int k); |
| 31 | * of the permutation in the sorted list of all n! such permutations. | 34 | int perm_sign(int *a, int n); |
| 32 | * Only works for n<=12. */ | 35 | int perm_to_index(int *a, int n); |
| 33 | int perm_to_index(int *a, int n); | 36 | int powint(int a, int b); |
| 34 | 37 | int subset_to_index(int *a, int n, int k); | |
| 35 | /* Converts a permutation index to the actual permutation as an array | 38 | void sum_arrays_mod(int *src, int *dst, int n, int m); |
| 36 | * (see perm_to_index) and saves the result to r. */ | 39 | void swap(int *a, int *b); |
| 37 | void index_to_perm(int p, int n, int *r); | ||
| 38 | |||
| 39 | /* Determine the sign of a permutation, either in integer or array format. */ | ||
| 40 | int perm_sign_array(int a[], int n); | ||
| 41 | int perm_sign_int(int p, int n); | ||
| 42 | |||
| 43 | /* Converts a k-element subset of a set with an element from an array of n | ||
| 44 | * elements, of which k are 1 and n-k are 0, to its index in the sorted list | ||
| 45 | * of all such subsets. | ||
| 46 | * Works only for n <= 12. */ | ||
| 47 | int subset_to_index(int *a, int n, int k); | ||
| 48 | |||
| 49 | /* Inverse of the above */ | ||
| 50 | void index_to_subset(int s, int n, int k, int *r); | ||
| 51 | |||
| 52 | /* Converts the first n-1 digits of a number to an array a of digits in base b; | ||
| 53 | * then adds one element to the array, so that the sum of the elements of a is | ||
| 54 | * zero modulo b. | ||
| 55 | * This is used for determing the edge orientation from an 11-bits integer or | ||
| 56 | * the corner orientation from a 7-trits integer. */ | ||
| 57 | void int_to_sum_zero_array(int x, int b, int n, int *a); | ||
| 58 | 40 | ||
| 41 | #endif | ||
