Skip to content
Snippets Groups Projects
Select Git revision
  • e6dfee305cecd12972e8d11c9594bb60a74764f0
  • master default
  • benchmark-tools
  • postgres-lambda
  • REL9_4_25
  • REL9_5_20
  • REL9_6_16
  • REL_10_11
  • REL_11_6
  • REL_12_1
  • REL_12_0
  • REL_12_RC1
  • REL_12_BETA4
  • REL9_4_24
  • REL9_5_19
  • REL9_6_15
  • REL_10_10
  • REL_11_5
  • REL_12_BETA3
  • REL9_4_23
  • REL9_5_18
  • REL9_6_14
  • REL_10_9
  • REL_11_4
24 results

lispsort.c

Blame
  • user avatar
    Marc G. Fournier authored
    up, but one thing at a time :)
    e2329091
    History
    lispsort.c 1.64 KiB
    /*-------------------------------------------------------------------------
     *
     * lispsort.c--
     *
     * Copyright (c) 1994, Regents of the University of California
     *
     *
     * IDENTIFICATION
     *    $Header: /cvsroot/pgsql/src/backend/lib/Attic/lispsort.c,v 1.3 1996/11/06 08:27:14 scrappy Exp $
     *
     *-------------------------------------------------------------------------
     */
    
    #include <sys/types.h>
    
    #include <postgres.h>
    
    #include <nodes/pg_list.h>
    #include <nodes/primnodes.h>
    #include <nodes/plannodes.h>
    #include <nodes/relation.h>
    #include <lib/lispsort.h>
    #include <lib/qsort.h>
    
    /*
    ** lisp_qsort: Takes a lisp list as input, copies it into an array of lisp 
    **             nodes which it sorts via qsort() with the comparison function
    **             as passed into lisp_qsort(), and returns a new list with 
    **             the nodes sorted.  The old list is *not* freed or modified (?)
    */
    List *lisp_qsort(List *the_list,    /* the list to be sorted */
    		 int (*compare)())      /* function to compare two nodes */
    {
        int i;
        size_t num;
        List **nodearray;
        List *tmp, *output;
        
        /* find size of list */
        num = length(the_list);
        if (num < 2)
    	return(copyObject(the_list));
        
        /* copy elements of the list into an array */
        nodearray = (List **) palloc(num * sizeof(List *));
        
        for (tmp = the_list, i = 0; tmp != NIL; tmp = lnext(tmp), i++)
    	nodearray[i] = copyObject(lfirst(tmp));
        
        /* sort the array */
        pg_qsort(nodearray, num, sizeof(List *), compare);
        
        /* lcons together the array elements */
        output = NIL;
        for (i = num - 1; i >= 0; i--)
    	output = lcons(nodearray[i], output);
        
        return(output);
    }