/** * perm.c * Definitions of functions useful for checking permutations. */ // +---------+-------------------------------------------------------- // | Headers | // +---------+ #include "perm.h" #include "bool.h" // +---------+-------------------------------------------------------- // | Helpers | // +---------+ /** * Count how many times val appears in the first size elements of vals. */ static int ia_tally (int vals[], int size, int val) { int i; int tally = 0; for (i = 0; i < size; i++) if (vals[i] == val) ++tally; return tally; } // ia_tally // +--------------------+--------------------------------------------- // | Exported Functions | // +--------------------+ int ia_is_permutation (int a0[], int a1[], int size) { int i; // For each element in a0 for (i = 0; i < size; i++) { // Make sure that the element appears the same number of times in // a0 and a1. if (ia_tally (a0, size, a0[i]) != ia_tally (a1, size, a0[i])) return 0; } // for // If all tallies are equal, the two arrays are permutations return 1; } // ia_is_permutation