/** * perm.c * Definitions of functions useful for checking permutations. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include "perm.h" #include "bool.h" // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Check off one copy of val from vals, assuming that the elements of * checkmarks indicate whether or not we've checked off the corresponding * value in vals. */ static int check_off (int vals[], int checkmarks[], int size, int val) { int i; for (i = 0; i < size; i++) { if ((! checkmarks[i]) && (vals[i] == val)) { checkmarks[i] = 1; return 1; } } // for // If we've gotten this far, we didn't check anything off return 0; } // check_off // +--------------------+--------------------------------------------- // | Exported Functions | // +--------------------+ int ia_is_permutation (int a0[], int a1[], int size) { int checkmarks[size]; int i; // Initially, nothing is marked for (i = 0; i < size; i++) checkmarks[i] = 0; // For each element of a0 for (i = 0; i < size; i++) { // Try to check it off in a1 if (! check_off (a1, checkmarks, size, a0[i])) return 0; } // for // Yay! We checked off every element. We're done return 1; } // ia_is_permutation