gdsl  1.8
examples/main_perm.c

This is an example of how to use gdsl_perm module.

/*
 * This file is part of the Generic Data Structures Library (GDSL).
 * Copyright (C) 1998-2018 Nicolas Darnis <ndarnis@free.fr>.
 *
 * GDSL is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * GDSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GDSL.  If not, see <http://www.gnu.org/licenses/>. 
 *
 *
 * GDSL - Generic Data Structures Library
 * $RCSfile: main_perm.c,v $
 * $Revision: 1.19 $
 * $Date: 2015/02/17 12:33:16 $
 */


#include <config.h>


#include <stdio.h>
#include <stdlib.h>


#include "gdsl_types.h"
#include "gdsl_perm.h"
#include "_integers.h"


static void 
usage (void)
{
    printf ("Usage: perm <n>\n");
}

static void
write (const gdsl_element_t e, FILE* file, gdsl_location_t pos, void* user_data)
{
    ulong n = * (ulong*) e;

    if (pos & GDSL_LOCATION_FIRST)
    {
        fprintf (file, "( ");

        if (pos & GDSL_LOCATION_LAST)
        {
            fprintf (file, "%ld )\n", n);
        }
    }

    if (pos & GDSL_LOCATION_LAST)
    {
        fprintf (file, "%ld )\n", n);
    }
    else
    {
        fprintf (file, "%ld, ", n);
    }
}

int main (int argc, char* argv [])
{
    ulong i, n;
    gdsl_perm_t l_alpha;
    gdsl_perm_t c_alpha;

    if (argc < 2)
    {
        usage ();
        return EXIT_FAILURE;
    }

    n = atoi (argv[1]);

    c_alpha = gdsl_perm_alloc ("c_alpha", n);

    l_alpha = gdsl_perm_alloc ("l_alpha", n);
    gdsl_perm_randomize (l_alpha);

    printf ("alpha         = ");
    gdsl_perm_write (l_alpha, write, stdout, NULL);
    printf ("                %ld cycles, %ld inversions\n\n",
        gdsl_perm_linear_cycles_count (l_alpha),
        gdsl_perm_linear_inversions_count (l_alpha));

    gdsl_perm_reverse (l_alpha);

    printf ("~alpha        = ");
    gdsl_perm_write (l_alpha, write, stdout, NULL);
    printf ("                %ld cycles, %ld inversions\n\n",
        gdsl_perm_linear_cycles_count (l_alpha),
        gdsl_perm_linear_inversions_count (l_alpha));

    gdsl_perm_reverse (l_alpha);
    gdsl_perm_inverse (l_alpha);

    printf ("alpha^-1      = ");
    gdsl_perm_write (l_alpha, write, stdout, NULL);
    printf ("                %ld cycles, %ld inversions\n\n",
        gdsl_perm_linear_cycles_count (l_alpha),
        gdsl_perm_linear_inversions_count (l_alpha));

    gdsl_perm_inverse (l_alpha);

    printf ("alpha         = ");
    gdsl_perm_write (l_alpha, write, stdout, NULL);
    printf ("                %ld cycles, %ld inversions\n\n",
        gdsl_perm_linear_cycles_count (l_alpha),
        gdsl_perm_linear_inversions_count (l_alpha));
  
    gdsl_perm_linear_to_canonical (c_alpha, l_alpha);
    printf ("cycles(alpha) = ");
    gdsl_perm_write (c_alpha, write, stdout, NULL);
    printf ("                %ld cycles\n\n",
        gdsl_perm_canonical_cycles_count (c_alpha));

    gdsl_perm_canonical_to_linear (l_alpha, c_alpha);

    printf ("alpha         = ");
    gdsl_perm_write (l_alpha, write, stdout, NULL);
    printf ("                %ld cycles, %ld inversions\n\n",
        gdsl_perm_linear_cycles_count (l_alpha),
        gdsl_perm_linear_inversions_count (l_alpha));

    gdsl_perm_free (l_alpha);
    gdsl_perm_free (c_alpha);

    {
    ulong v [] = {0, 2, 3, 1, 4, 5, 6, 7, 8};
    ulong n = sizeof (v) / sizeof (v [0]);
    gdsl_perm_t a = gdsl_perm_alloc ("a", n);

    printf ("initial array: ");
    for (i = 0; i < n; i++)
        {
        printf ("%ld ", v [i]);
        }
    printf ("\n");

    gdsl_perm_randomize (a);
    printf ("applying permutation: ");
    gdsl_perm_write (a, write, stdout, NULL);
    gdsl_perm_apply_on_array ((gdsl_element_t*) v, a);
    gdsl_perm_free (a);

    printf ("modified array: ");
    for (i = 0; i < n; i++)
        {
        printf ("%ld ", v [i]);
        }
    printf ("\n");
    }

    exit (EXIT_SUCCESS);
}